Safemotion Lib
Loading...
Searching...
No Matches
Static Public Member Functions | Static Protected Attributes | List of all members
fastreid.utils.file_io.PathManager Class Reference

Static Public Member Functions

Union[IO[str], IO[bytes]] open (str path, str mode="r", int buffering=-1, **Any kwargs)
 
bool copy (str src_path, str dst_path, bool overwrite=False, **Any kwargs)
 
str get_local_path (str path, **Any kwargs)
 
bool exists (str path, **Any kwargs)
 
bool isfile (str path, **Any kwargs)
 
bool isdir (str path, **Any kwargs)
 
List[str] ls (str path, **Any kwargs)
 
None mkdirs (str path, **Any kwargs)
 
None rm (str path, **Any kwargs)
 
None register_handler (PathHandler handler)
 
None set_strict_kwargs_checking (bool enable)
 

Static Protected Attributes

MutableMapping _PATH_HANDLERS = OrderedDict()
 
 _NATIVE_PATH_HANDLER = NativePathHandler()
 

Detailed Description

A class for users to open generic paths or translate generic paths to file names.

Definition at line 315 of file file_io.py.

Member Function Documentation

◆ copy()

bool fastreid.utils.file_io.PathManager.copy ( str src_path,
str dst_path,
bool overwrite = False,
**Any kwargs )
static
Copies a source path to a destination path.
Args:
    src_path (str): A URI supported by this PathHandler
    dst_path (str): A URI supported by this PathHandler
    overwrite (bool): Bool flag for forcing overwrite of existing file
Returns:
    status (bool): True on success

Definition at line 361 of file file_io.py.

363 ) -> bool:
364 """
365 Copies a source path to a destination path.
366 Args:
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
370 Returns:
371 status (bool): True on success
372 """
373
374 # Copying across handlers is not supported.
375 assert PathManager.__get_path_handler( # type: ignore
376 src_path
377 ) == PathManager.__get_path_handler(dst_path)
378 return PathManager.__get_path_handler(src_path)._copy(
379 src_path, dst_path, overwrite, **kwargs
380 )
381

◆ exists()

bool fastreid.utils.file_io.PathManager.exists ( str path,
**Any kwargs )
static
Checks if there is a resource at the given URI.
Args:
    path (str): A URI supported by this PathHandler
Returns:
    bool: true if the path exists

Definition at line 399 of file file_io.py.

399 def exists(path: str, **kwargs: Any) -> bool:
400 """
401 Checks if there is a resource at the given URI.
402 Args:
403 path (str): A URI supported by this PathHandler
404 Returns:
405 bool: true if the path exists
406 """
407 return PathManager.__get_path_handler(path)._exists( # type: ignore
408 path, **kwargs
409 )
410

◆ get_local_path()

str fastreid.utils.file_io.PathManager.get_local_path ( str path,
**Any kwargs )
static
Get a filepath which is compatible with native Python I/O such as `open`
and `os.path`.
If URI points to a remote resource, this function may download and cache
the resource to local disk.
Args:
    path (str): A URI supported by this PathHandler
Returns:
    local_path (str): a file path which exists on the local file system

Definition at line 383 of file file_io.py.

383 def get_local_path(path: str, **kwargs: Any) -> str:
384 """
385 Get a filepath which is compatible with native Python I/O such as `open`
386 and `os.path`.
387 If URI points to a remote resource, this function may download and cache
388 the resource to local disk.
389 Args:
390 path (str): A URI supported by this PathHandler
391 Returns:
392 local_path (str): a file path which exists on the local file system
393 """
394 return PathManager.__get_path_handler( # type: ignore
395 path
396 )._get_local_path(path, **kwargs)
397

◆ isdir()

bool fastreid.utils.file_io.PathManager.isdir ( str path,
**Any kwargs )
static
Checks if the resource at the given URI is a directory.
Args:
    path (str): A URI supported by this PathHandler
Returns:
    bool: true if the path is a directory

Definition at line 425 of file file_io.py.

425 def isdir(path: str, **kwargs: Any) -> bool:
426 """
427 Checks if the resource at the given URI is a directory.
428 Args:
429 path (str): A URI supported by this PathHandler
430 Returns:
431 bool: true if the path is a directory
432 """
433 return PathManager.__get_path_handler(path)._isdir( # type: ignore
434 path, **kwargs
435 )
436

◆ isfile()

bool fastreid.utils.file_io.PathManager.isfile ( str path,
**Any kwargs )
static
Checks if there the resource at the given URI is a file.
Args:
    path (str): A URI supported by this PathHandler
Returns:
    bool: true if the path is a file

Definition at line 412 of file file_io.py.

412 def isfile(path: str, **kwargs: Any) -> bool:
413 """
414 Checks if there the resource at the given URI is a file.
415 Args:
416 path (str): A URI supported by this PathHandler
417 Returns:
418 bool: true if the path is a file
419 """
420 return PathManager.__get_path_handler(path)._isfile( # type: ignore
421 path, **kwargs
422 )
423

◆ ls()

List[str] fastreid.utils.file_io.PathManager.ls ( str path,
**Any kwargs )
static
List the contents of the directory at the provided URI.
Args:
    path (str): A URI supported by this PathHandler
Returns:
    List[str]: list of contents in given path

Definition at line 438 of file file_io.py.

438 def ls(path: str, **kwargs: Any) -> List[str]:
439 """
440 List the contents of the directory at the provided URI.
441 Args:
442 path (str): A URI supported by this PathHandler
443 Returns:
444 List[str]: list of contents in given path
445 """
446 return PathManager.__get_path_handler(path)._ls( # type: ignore
447 path, **kwargs
448 )
449

◆ mkdirs()

None fastreid.utils.file_io.PathManager.mkdirs ( str path,
**Any kwargs )
static
Recursive directory creation function. Like mkdir(), but makes all
intermediate-level directories needed to contain the leaf directory.
Similar to the native `os.makedirs`.
Args:
    path (str): A URI supported by this PathHandler

Definition at line 451 of file file_io.py.

451 def mkdirs(path: str, **kwargs: Any) -> None:
452 """
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`.
456 Args:
457 path (str): A URI supported by this PathHandler
458 """
459 return PathManager.__get_path_handler(path)._mkdirs( # type: ignore
460 path, **kwargs
461 )
462

◆ open()

Union[IO[str], IO[bytes]] fastreid.utils.file_io.PathManager.open ( str path,
str mode = "r",
int buffering = -1,
**Any kwargs )
static
Open a stream to a URI, similar to the built-in `open`.
Args:
    path (str): A URI supported by this PathHandler
    mode (str): Specifies the mode in which the file is opened. It defaults
        to 'r'.
    buffering (int): An optional integer used to set the buffering policy.
        Pass 0 to switch buffering off and an integer >= 1 to indicate the
        size in bytes of a fixed-size chunk buffer. When no buffering
        argument is given, the default buffering policy depends on the
        underlying I/O implementation.
Returns:
    file: a file-like object.

Definition at line 339 of file file_io.py.

341 ) -> Union[IO[str], IO[bytes]]:
342 """
343 Open a stream to a URI, similar to the built-in `open`.
344 Args:
345 path (str): A URI supported by this PathHandler
346 mode (str): Specifies the mode in which the file is opened. It defaults
347 to 'r'.
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.
353 Returns:
354 file: a file-like object.
355 """
356 return PathManager.__get_path_handler(path)._open( # type: ignore
357 path, mode, buffering=buffering, **kwargs
358 )
359

◆ register_handler()

None fastreid.utils.file_io.PathManager.register_handler ( PathHandler handler)
static
Register a path handler associated with `handler._get_supported_prefixes`
URI prefixes.
Args:
    handler (PathHandler)

Definition at line 475 of file file_io.py.

475 def register_handler(handler: PathHandler) -> None:
476 """
477 Register a path handler associated with `handler._get_supported_prefixes`
478 URI prefixes.
479 Args:
480 handler (PathHandler)
481 """
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
486
487 # Sort path handlers in reverse order so longer prefixes take priority,
488 # eg: http://foo/bar before http://foo
489 PathManager._PATH_HANDLERS = OrderedDict(
490 sorted(
491 PathManager._PATH_HANDLERS.items(),
492 key=lambda t: t[0],
493 reverse=True,
494 )
495 )
496

◆ rm()

None fastreid.utils.file_io.PathManager.rm ( str path,
**Any kwargs )
static
Remove the file (not directory) at the provided URI.
Args:
    path (str): A URI supported by this PathHandler

Definition at line 464 of file file_io.py.

464 def rm(path: str, **kwargs: Any) -> None:
465 """
466 Remove the file (not directory) at the provided URI.
467 Args:
468 path (str): A URI supported by this PathHandler
469 """
470 return PathManager.__get_path_handler(path)._rm( # type: ignore
471 path, **kwargs
472 )
473

◆ set_strict_kwargs_checking()

None fastreid.utils.file_io.PathManager.set_strict_kwargs_checking ( bool enable)
static
Toggles strict kwargs checking. If enabled, a ValueError is thrown if any
unused parameters are passed to a PathHandler function. If disabled, only
a warning is given.
With a centralized file API, there's a tradeoff of convenience and
correctness delegating arguments to the proper I/O layers. An underlying
`PathHandler` may support custom arguments which should not be statically
exposed on the `PathManager` function. For example, a custom `HTTPURLHandler`
may want to expose a `cache_timeout` argument for `open()` which specifies
how old a locally cached resource can be before it's refetched from the
remote server. This argument would not make sense for a `NativePathHandler`.
If strict kwargs checking is disabled, `cache_timeout` can be passed to
`PathManager.open` which will forward the arguments to the underlying
handler. By default, checking is enabled since it is innately unsafe:
multiple `PathHandler`s could reuse arguments with different semantic
meanings or types.
Args:
    enable (bool)

Definition at line 498 of file file_io.py.

498 def set_strict_kwargs_checking(enable: bool) -> None:
499 """
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
502 a warning is given.
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
514 meanings or types.
515 Args:
516 enable (bool)
517 """
518 PathManager._NATIVE_PATH_HANDLER._strict_kwargs_check = enable
519 for handler in PathManager._PATH_HANDLERS.values():
520 handler._strict_kwargs_check = enable

Member Data Documentation

◆ _NATIVE_PATH_HANDLER

fastreid.utils.file_io.PathManager._NATIVE_PATH_HANDLER = NativePathHandler()
staticprotected

Definition at line 321 of file file_io.py.

◆ _PATH_HANDLERS

MutableMapping fastreid.utils.file_io.PathManager._PATH_HANDLERS = OrderedDict()
staticprotected

Definition at line 320 of file file_io.py.


The documentation for this class was generated from the following file: