8from collections
import OrderedDict
20__all__ = [
"PathManager",
"get_cache_dir"]
25 Returns a default directory to cache static files
26 (usually downloaded from Internet), if None is provided.
28 cache_dir (None or str): if not None, will be returned as is.
29 If None, returns the default cache directory as:
30 1) $FVCORE_CACHE, if set
31 2) otherwise ~/.torch/fvcore_cache
34 cache_dir = os.path.expanduser(
35 os.getenv(
"FVCORE_CACHE",
"~/.torch/fvcore_cache")
42 PathHandler is a base class that defines common I/O functionality for a URI
43 protocol. It routes I/O for a generic URI which may look like "protocol://*"
44 or a canonical filepath "/foo/bar/baz".
47 _strict_kwargs_check =
True
51 Checks if the given arguments are empty. Throws a ValueError if strict
52 kwargs checking is enabled and args are non-empty. If strict kwargs
53 checking is disabled, only a warning is logged.
55 kwargs (Dict[str, Any])
59 raise ValueError(
"Unused arguments: {}".format(kwargs))
61 logger = logging.getLogger(__name__)
62 for k, v
in kwargs.items():
64 "[PathManager] {}={} argument ignored".format(k, v)
70 List[str]: the list of URI prefixes this PathHandler can support
72 raise NotImplementedError()
76 Get a filepath which is compatible with native Python I/O such as `open`
78 If URI points to a remote resource, this function may download and cache
79 the resource to local disk. In this case, this function is meant to be
80 used with read-only resources.
82 path (str): A URI supported by this PathHandler
84 local_path (str): a file path which exists on the local file system
86 raise NotImplementedError()
89 self, path: str, mode: str =
"r", buffering: int = -1, **kwargs: Any
90 ) -> Union[IO[str], IO[bytes]]:
92 Open a stream to a URI, similar to the built-in `open`.
94 path (str): A URI supported by this PathHandler
95 mode (str): Specifies the mode in which the file is opened. It defaults
97 buffering (int): An optional integer used to set the buffering policy.
98 Pass 0 to switch buffering off and an integer >= 1 to indicate the
99 size in bytes of a fixed-size chunk buffer. When no buffering
100 argument is given, the default buffering policy depends on the
101 underlying I/O implementation.
103 file: a file-like object.
105 raise NotImplementedError()
111 overwrite: bool =
False,
115 Copies a source path to a destination path.
117 src_path (str): A URI supported by this PathHandler
118 dst_path (str): A URI supported by this PathHandler
119 overwrite (bool): Bool flag for forcing overwrite of existing file
121 status (bool): True on success
123 raise NotImplementedError()
125 def _exists(self, path: str, **kwargs: Any) -> bool:
127 Checks if there is a resource at the given URI.
129 path (str): A URI supported by this PathHandler
131 bool: true if the path exists
133 raise NotImplementedError()
135 def _isfile(self, path: str, **kwargs: Any) -> bool:
137 Checks if the resource at the given URI is a file.
139 path (str): A URI supported by this PathHandler
141 bool: true if the path is a file
143 raise NotImplementedError()
145 def _isdir(self, path: str, **kwargs: Any) -> bool:
147 Checks if the resource at the given URI is a directory.
149 path (str): A URI supported by this PathHandler
151 bool: true if the path is a directory
153 raise NotImplementedError()
155 def _ls(self, path: str, **kwargs: Any) -> List[str]:
157 List the contents of the directory at the provided URI.
159 path (str): A URI supported by this PathHandler
161 List[str]: list of contents in given path
163 raise NotImplementedError()
165 def _mkdirs(self, path: str, **kwargs: Any) ->
None:
167 Recursive directory creation function. Like mkdir(), but makes all
168 intermediate-level directories needed to contain the leaf directory.
169 Similar to the native `os.makedirs`.
171 path (str): A URI supported by this PathHandler
173 raise NotImplementedError()
175 def _rm(self, path: str, **kwargs: Any) ->
None:
177 Remove the file (not directory) at the provided URI.
179 path (str): A URI supported by this PathHandler
181 raise NotImplementedError()
186 Handles paths that can be accessed using Python native system calls. This
187 handler uses `open()` and `os.*` calls on the given path.
199 encoding: Optional[str] =
None,
200 errors: Optional[str] =
None,
201 newline: Optional[str] =
None,
202 closefd: bool =
True,
203 opener: Optional[Callable] =
None,
205 ) -> Union[IO[str], IO[bytes]]:
209 path (str): A URI supported by this PathHandler
210 mode (str): Specifies the mode in which the file is opened. It defaults
212 buffering (int): An optional integer used to set the buffering policy.
213 Pass 0 to switch buffering off and an integer >= 1 to indicate the
214 size in bytes of a fixed-size chunk buffer. When no buffering
215 argument is given, the default buffering policy works as follows:
216 * Binary files are buffered in fixed-size chunks; the size of
217 the buffer is chosen using a heuristic trying to determine the
218 underlying device’s “block size” and falling back on
219 io.DEFAULT_BUFFER_SIZE. On many systems, the buffer will
220 typically be 4096 or 8192 bytes long.
221 encoding (Optional[str]): the name of the encoding used to decode or
222 encode the file. This should only be used in text mode.
223 errors (Optional[str]): an optional string that specifies how encoding
224 and decoding errors are to be handled. This cannot be used in binary
226 newline (Optional[str]): controls how universal newlines mode works
227 (it only applies to text mode). It can be None, '', '\n', '\r',
229 closefd (bool): If closefd is False and a file descriptor rather than
230 a filename was given, the underlying file descriptor will be kept
231 open when the file is closed. If a filename is given closefd must
232 be True (the default) otherwise an error will be raised.
233 opener (Optional[Callable]): A custom opener can be used by passing
234 a callable as opener. The underlying file descriptor for the file
235 object is then obtained by calling opener with (file, flags).
236 opener must return an open file descriptor (passing os.open as opener
237 results in functionality similar to passing None).
238 See https://docs.python.org/3/library/functions.html#open for details.
240 file: a file-like object.
258 overwrite: bool =
False,
262 Copies a source path to a destination path.
264 src_path (str): A URI supported by this PathHandler
265 dst_path (str): A URI supported by this PathHandler
266 overwrite (bool): Bool flag for forcing overwrite of existing file
268 status (bool): True on success
272 if os.path.exists(dst_path)
and not overwrite:
273 logger = logging.getLogger(__name__)
274 logger.error(
"Destination file {} already exists.".format(dst_path))
278 shutil.copyfile(src_path, dst_path)
280 except Exception
as e:
281 logger = logging.getLogger(__name__)
282 logger.error(
"Error in file copy - {}".format(str(e)))
285 def _exists(self, path: str, **kwargs: Any) -> bool:
287 return os.path.exists(path)
289 def _isfile(self, path: str, **kwargs: Any) -> bool:
291 return os.path.isfile(path)
293 def _isdir(self, path: str, **kwargs: Any) -> bool:
295 return os.path.isdir(path)
297 def _ls(self, path: str, **kwargs: Any) -> List[str]:
299 return os.listdir(path)
301 def _mkdirs(self, path: str, **kwargs: Any) ->
None:
304 os.makedirs(path, exist_ok=
True)
307 if e.errno != errno.EEXIST:
310 def _rm(self, path: str, **kwargs: Any) ->
None:
317 A class for users to open generic paths or translate generic paths to file names.
320 _PATH_HANDLERS: MutableMapping[str, PathHandler] = OrderedDict()
324 def __get_path_handler(path: str) -> PathHandler:
326 Finds a PathHandler that supports the given path. Falls back to the native
327 PathHandler if no other handler is found.
329 path (str): URI path to resource
331 handler (PathHandler)
333 for p
in PathManager._PATH_HANDLERS.keys():
334 if path.startswith(p):
335 return PathManager._PATH_HANDLERS[p]
336 return PathManager._NATIVE_PATH_HANDLER
340 path: str, mode: str =
"r", buffering: int = -1, **kwargs: Any
341 ) -> Union[IO[str], IO[bytes]]:
343 Open a stream to a URI, similar to the built-in `open`.
345 path (str): A URI supported by this PathHandler
346 mode (str): Specifies the mode in which the file is opened. It defaults
348 buffering (int): An optional integer used to set the buffering policy.
349 Pass 0 to switch buffering off and an integer >= 1 to indicate the
350 size in bytes of a fixed-size chunk buffer. When no buffering
351 argument is given, the default buffering policy depends on the
352 underlying I/O implementation.
354 file: a file-like object.
356 return PathManager.__get_path_handler(path)._open(
357 path, mode, buffering=buffering, **kwargs
362 src_path: str, dst_path: str, overwrite: bool =
False, **kwargs: Any
365 Copies a source path to a destination path.
367 src_path (str): A URI supported by this PathHandler
368 dst_path (str): A URI supported by this PathHandler
369 overwrite (bool): Bool flag for forcing overwrite of existing file
371 status (bool): True on success
375 assert PathManager.__get_path_handler(
377 ) == PathManager.__get_path_handler(dst_path)
378 return PathManager.__get_path_handler(src_path)._copy(
379 src_path, dst_path, overwrite, **kwargs
385 Get a filepath which is compatible with native Python I/O such as `open`
387 If URI points to a remote resource, this function may download and cache
388 the resource to local disk.
390 path (str): A URI supported by this PathHandler
392 local_path (str): a file path which exists on the local file system
394 return PathManager.__get_path_handler(
396 )._get_local_path(path, **kwargs)
399 def exists(path: str, **kwargs: Any) -> bool:
401 Checks if there is a resource at the given URI.
403 path (str): A URI supported by this PathHandler
405 bool: true if the path exists
407 return PathManager.__get_path_handler(path)._exists(
412 def isfile(path: str, **kwargs: Any) -> bool:
414 Checks if there the resource at the given URI is a file.
416 path (str): A URI supported by this PathHandler
418 bool: true if the path is a file
420 return PathManager.__get_path_handler(path)._isfile(
425 def isdir(path: str, **kwargs: Any) -> bool:
427 Checks if the resource at the given URI is a directory.
429 path (str): A URI supported by this PathHandler
431 bool: true if the path is a directory
433 return PathManager.__get_path_handler(path)._isdir(
438 def ls(path: str, **kwargs: Any) -> List[str]:
440 List the contents of the directory at the provided URI.
442 path (str): A URI supported by this PathHandler
444 List[str]: list of contents in given path
446 return PathManager.__get_path_handler(path)._ls(
451 def mkdirs(path: str, **kwargs: Any) ->
None:
453 Recursive directory creation function. Like mkdir(), but makes all
454 intermediate-level directories needed to contain the leaf directory.
455 Similar to the native `os.makedirs`.
457 path (str): A URI supported by this PathHandler
459 return PathManager.__get_path_handler(path)._mkdirs(
464 def rm(path: str, **kwargs: Any) ->
None:
466 Remove the file (not directory) at the provided URI.
468 path (str): A URI supported by this PathHandler
470 return PathManager.__get_path_handler(path)._rm(
477 Register a path handler associated with `handler._get_supported_prefixes`
480 handler (PathHandler)
482 assert isinstance(handler, PathHandler), handler
483 for prefix
in handler._get_supported_prefixes():
484 assert prefix
not in PathManager._PATH_HANDLERS
485 PathManager._PATH_HANDLERS[prefix] = handler
489 PathManager._PATH_HANDLERS = OrderedDict(
491 PathManager._PATH_HANDLERS.items(),
500 Toggles strict kwargs checking. If enabled, a ValueError is thrown if any
501 unused parameters are passed to a PathHandler function. If disabled, only
503 With a centralized file API, there's a tradeoff of convenience and
504 correctness delegating arguments to the proper I/O layers. An underlying
505 `PathHandler` may support custom arguments which should not be statically
506 exposed on the `PathManager` function. For example, a custom `HTTPURLHandler`
507 may want to expose a `cache_timeout` argument for `open()` which specifies
508 how old a locally cached resource can be before it's refetched from the
509 remote server. This argument would not make sense for a `NativePathHandler`.
510 If strict kwargs checking is disabled, `cache_timeout` can be passed to
511 `PathManager.open` which will forward the arguments to the underlying
512 handler. By default, checking is enabled since it is innately unsafe:
513 multiple `PathHandler`s could reuse arguments with different semantic
518 PathManager._NATIVE_PATH_HANDLER._strict_kwargs_check = enable
519 for handler
in PathManager._PATH_HANDLERS.values():
520 handler._strict_kwargs_check = enable
bool _isdir(self, str path, **Any kwargs)
None _rm(self, str path, **Any kwargs)
List[str] _ls(self, str path, **Any kwargs)
bool _exists(self, str path, **Any kwargs)
str _get_local_path(self, str path, **Any kwargs)
Union[IO[str], IO[bytes]] _open(self, str path, str mode="r", int buffering=-1, Optional[str] encoding=None, Optional[str] errors=None, Optional[str] newline=None, bool closefd=True, Optional[Callable] opener=None, **Any kwargs)
bool _copy(self, str src_path, str dst_path, bool overwrite=False, **Any kwargs)
bool _isfile(self, str path, **Any kwargs)
None _mkdirs(self, str path, **Any kwargs)
None _mkdirs(self, str path, **Any kwargs)
bool _exists(self, str path, **Any kwargs)
Union[IO[str], IO[bytes]] _open(self, str path, str mode="r", int buffering=-1, **Any kwargs)
None _check_kwargs(self, Dict[str, Any] kwargs)
str _get_local_path(self, str path, **Any kwargs)
bool _isdir(self, str path, **Any kwargs)
bool _isfile(self, str path, **Any kwargs)
bool _strict_kwargs_check
None _rm(self, str path, **Any kwargs)
List[str] _get_supported_prefixes(self)
List[str] _ls(self, str path, **Any kwargs)
bool _copy(self, str src_path, str dst_path, bool overwrite=False, **Any kwargs)
bool isfile(str path, **Any kwargs)
None mkdirs(str path, **Any kwargs)
List[str] ls(str path, **Any kwargs)
bool isdir(str path, **Any kwargs)
None rm(str path, **Any kwargs)
bool exists(str path, **Any kwargs)
None register_handler(PathHandler handler)
Union[IO[str], IO[bytes]] open(str path, str mode="r", int buffering=-1, **Any kwargs)
str get_local_path(str path, **Any kwargs)
bool copy(str src_path, str dst_path, bool overwrite=False, **Any kwargs)
None set_strict_kwargs_checking(bool enable)
str get_cache_dir(Optional[str] cache_dir=None)