GithubHelp home page GithubHelp logo

plaflamme / rust-cached-path Goto Github PK

View Code? Open in Web Editor NEW

This project forked from epwalsh/rust-cached-path

0.0 1.0 0.0 182 KB

๐Ÿฆ€ Rust utility for accessing both local and remote files through a unified interface

License: Apache License 2.0

Rust 99.25% Makefile 0.75%

rust-cached-path's Introduction

rust-cached-path

crates.io Documentation MIT/Apache-2 licensed CI

The idea behind cached-path is to provide a unified, simple interface for accessing both local and remote files. This can be used behind other APIs that need to access files agnostic to where they are located.

This is based on allennlp/common/file_utils.py and transformers/file_utils.py.

Installation

cached-path can be used as both a library and a command-line tool. To install cached-path as a command-line tool, run

cargo install --features build-binary cached-path

Usage

For remote resources, cached-path downloads and caches the resource, using the ETAG to know when to update the cache. The path returned is the local path to the latest cached version:

use cached_path::cached_path;

let path = cached_path(
    "https://github.com/epwalsh/rust-cached-path/blob/master/README.md"
).unwrap();
assert!(path.is_file());
# From the command line:
$ cached-path https://github.com/epwalsh/rust-cached-path/blob/master/README.md
/tmp/cache/055968a99316f3a42e7bcff61d3f590227dd7b03d17e09c41282def7c622ba0f.efa33e7f611ef2d163fea874ce614bb6fa5ab2a9d39d5047425e39ebe59fe782

For local files, the path returned is just the original path supplied:

use cached_path::cached_path;

let path = cached_path("README.md").unwrap();
assert_eq!(path.to_str().unwrap(), "README.md");
# From the command line:
$ cached-path README.md
README.md

For resources that are archives, like *.tar.gz files, cached-path can also automatically extract the files:

use cached_path::{cached_path_with_options, Options};

let path = cached_path_with_options(
    "https://raw.githubusercontent.com/epwalsh/rust-cached-path/master/test_fixtures/utf-8_sample/archives/utf-8.tar.gz",
    &Options::default().extract(),
).unwrap();
assert!(path.is_dir());
# From the command line:
$ cached-path --extract https://raw.githubusercontent.com/epwalsh/rust-cached-path/master/test_fixtures/utf-8_sample/archives/utf-8.tar.gz
README.md

It's also easy to customize the cache location, the HTTP client, and other options using a CacheBuilder to construct a custom Cache object. This is the recommended thing to do if your application makes multiple calls to cached_path, since it avoids the overhead of creating a new HTTP client on each call:

use cached_path::Cache;

let cache = Cache::builder()
    .dir(std::env::temp_dir().join("my-cache/"))
    .connect_timeout(std::time::Duration::from_secs(3))
    .build().unwrap();
let path = cache.cached_path("README.md").unwrap();
# From the command line:
$ cached-path --dir /tmp/my-cache/ --connect-timeout 3 README.md
README.md

rust-cached-path's People

Contributors

epwalsh avatar dependabot[bot] avatar korrat avatar danshapero avatar dependabot-preview[bot] avatar guillaume-be avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.