Beispiel #1
0
    def open(self, path, remote=None, mode="r", encoding=None):
        """Opens a specified resource as a file descriptor"""
        out, = self.find_outs_by_path(path)
        if out.isdir():
            raise ValueError("Can't open a dir")

        cache_file = self.cache.local.checksum_to_path_info(out.checksum)
        cache_file = fspath_py35(cache_file)

        if os.path.exists(cache_file):
            return _open(cache_file, mode=mode, encoding=encoding)

        try:
            remote_obj = self.cloud.get_remote(remote)
            remote_info = remote_obj.checksum_to_path_info(out.checksum)
            return remote_obj.open(remote_info, mode=mode, encoding=encoding)
        except RemoteActionNotImplemented:
            with self.state:
                cache_info = out.get_used_cache(remote=remote)
                self.cloud.pull(cache_info, remote=remote)

            # Since pull may just skip with a warning, we need to check it here
            if not os.path.exists(cache_file):
                raise OutputFileMissingError(relpath(path, self.root_dir))

            return _open(cache_file, mode=mode, encoding=encoding)
Beispiel #2
0
 def open(self, path, remote=None, mode="r", encoding=None):
     """Opens a specified resource as a file descriptor"""
     try:
         with self._open(path, remote, mode, encoding) as fd:
             yield fd
     except FileNotFoundError:
         raise OutputFileMissingError(relpath(path, self.root_dir))