def write_handle(path, mode=None): path = scope_url(path) if _supports_make_dirs(path): gfile.MakeDirs(os.path.dirname(path)) if mode is None: if _supports_binary_writing(path): mode = "wb" else: mode = "w" handle = gfile.Open(path, mode) yield handle handle.close()
def read_handle(url, cache=None, mode="rb"): """Read from any URL with a file handle. Use this to get a handle to a file rather than eagerly load the data: ``` with read_handle(url) as handle: result = something.load(handle) result.do_something() ``` When program execution leaves this `with` block, the handle will be closed automatically. Args: url: a URL including scheme or a local path Returns: A file handle to the specified resource if it could be reached. The handle will be closed automatically once execution leaves this context. """ url = scope_url(url) scheme = urlparse(url).scheme if cache == "purge": _purge_cached(url) cache = None if _is_remote(scheme) and cache is None: cache = True log.debug("Cache not specified, enabling because resource is remote.") if cache: handle = _read_and_cache(url, mode=mode) else: if scheme in ("http", "https"): handle = _handle_web_url(url, mode=mode) elif scheme in ("gs"): handle = _handle_gfile(url, mode=mode) else: handle = open(url, mode=mode) yield handle handle.close()
def test_empty_io_scope(): path = "./some/file.ext" scoped = scope_url(path) assert scoped == path