Shortcuts

PetrelBackend

class mmengine.fileio.PetrelBackend(path_mapping=None, enable_mc=True, conf_path=None)[源代码]

Petrel storage backend (for internal usage).

PetrelBackend supports reading and writing data to multiple clusters. If the file path contains the cluster name, PetrelBackend will read data from specified cluster or write data to it. Otherwise, PetrelBackend will access the default cluster.

参数:
  • path_mapping (dict, optional) – Path mapping dict from local path to Petrel path. When path_mapping={'src': 'dst'}, src in filepath will be replaced by dst. Defaults to None.

  • enable_mc (bool, optional) – Whether to enable memcached support. Defaults to True.

  • conf_path (str, optional) – Config path of Petrel client. Default: None. New in version 0.3.3.

示例

>>> backend = PetrelBackend()
>>> filepath1 = 'petrel://path/of/file'
>>> filepath2 = 'cluster-name:petrel://path/of/file'
>>> backend.get(filepath1)  # get data from default cluster
>>> client.get(filepath2)  # get data from 'cluster-name' cluster

Create a symbolic link pointing to src named dst.

Directly copy src to dst because PetrelBacekend does not support create a symbolic link.

参数:
  • src (str or Path) – A file or directory to be copied.

  • dst (str or Path) – Copy a file or directory to dst.

  • backend_args (dict, optional) – Arguments to instantiate the prefix of uri corresponding backend. Defaults to None.

返回:

Return False because PetrelBackend does not support create a symbolic link.

返回类型:

bool

示例

>>> backend = PetrelBackend()
>>> src = 'petrel://path/of/file'
>>> dst = 'petrel://path/of/your/file'
>>> backend.copy_if_symlink_fails(src, dst)
False
>>> src = 'petrel://path/of/dir'
>>> dst = 'petrel://path/of/your/dir'
>>> backend.copy_if_symlink_fails(src, dst)
False
copyfile(src, dst)[源代码]

Copy a file src to dst and return the destination file.

src and dst should have the same prefix. If dst specifies a directory, the file will be copied into dst using the base filename from src. If dst specifies a file that already exists, it will be replaced.

参数:
  • src (str or Path) – A file to be copied.

  • dst (str or Path) – Copy file to dst.

返回:

The destination file.

返回类型:

str

抛出:

SameFileError – If src and dst are the same file, a SameFileError will be raised.

示例

>>> backend = PetrelBackend()
>>> # dst is a file
>>> src = 'petrel://path/of/file'
>>> dst = 'petrel://path/of/file1'
>>> backend.copyfile(src, dst)
'petrel://path/of/file1'
>>> # dst is a directory
>>> dst = 'petrel://path/of/dir'
>>> backend.copyfile(src, dst)
'petrel://path/of/dir/file'
copyfile_from_local(src, dst)[源代码]

Upload a local file src to dst and return the destination file.

参数:
  • src (str or Path) – A local file to be copied.

  • dst (str or Path) – Copy file to dst.

  • backend_args (dict, optional) – Arguments to instantiate the prefix of uri corresponding backend. Defaults to None.

返回:

If dst specifies a directory, the file will be copied into dst using the base filename from src.

返回类型:

str

示例

>>> backend = PetrelBackend()
>>> # dst is a file
>>> src = 'path/of/your/file'
>>> dst = 'petrel://path/of/file1'
>>> backend.copyfile_from_local(src, dst)
'petrel://path/of/file1'
>>> # dst is a directory
>>> dst = 'petrel://path/of/dir'
>>> backend.copyfile_from_local(src, dst)
'petrel://path/of/dir/file'
copyfile_to_local(src, dst)[源代码]

Copy the file src to local dst and return the destination file.

If dst specifies a directory, the file will be copied into dst using the base filename from src. If dst specifies a file that already exists, it will be replaced.

参数:
  • src (str or Path) – A file to be copied.

  • dst (str or Path) – Copy file to to local dst.

返回:

If dst specifies a directory, the file will be copied into dst using the base filename from src.

返回类型:

str

示例

>>> backend = PetrelBackend()
>>> # dst is a file
>>> src = 'petrel://path/of/file'
>>> dst = 'path/of/your/file'
>>> backend.copyfile_to_local(src, dst)
'path/of/your/file'
>>> # dst is a directory
>>> dst = 'path/of/your/dir'
>>> backend.copyfile_to_local(src, dst)
'path/of/your/dir/file'
copytree(src, dst)[源代码]

Recursively copy an entire directory tree rooted at src to a directory named dst and return the destination directory.

src and dst should have the same prefix.

参数:
  • src (str or Path) – A directory to be copied.

  • dst (str or Path) – Copy directory to dst.

  • backend_args (dict, optional) – Arguments to instantiate the prefix of uri corresponding backend. Defaults to None.

返回:

The destination directory.

返回类型:

str

抛出:

FileExistsError – If dst had already existed, a FileExistsError will be raised.

示例

>>> backend = PetrelBackend()
>>> src = 'petrel://path/of/dir'
>>> dst = 'petrel://path/of/dir1'
>>> backend.copytree(src, dst)
'petrel://path/of/dir1'
copytree_from_local(src, dst)[源代码]

Recursively copy an entire directory tree rooted at src to a directory named dst and return the destination directory.

参数:
  • src (str or Path) – A local directory to be copied.

  • dst (str or Path) – Copy directory to dst.

返回:

The destination directory.

返回类型:

str

抛出:

FileExistsError – If dst had already existed, a FileExistsError will be raised.

示例

>>> backend = PetrelBackend()
>>> src = 'path/of/your/dir'
>>> dst = 'petrel://path/of/dir1'
>>> backend.copytree_from_local(src, dst)
'petrel://path/of/dir1'
copytree_to_local(src, dst)[源代码]

Recursively copy an entire directory tree rooted at src to a local directory named dst and return the destination directory.

参数:
  • src (str or Path) – A directory to be copied.

  • dst (str or Path) – Copy directory to local dst.

  • backend_args (dict, optional) – Arguments to instantiate the prefix of uri corresponding backend. Defaults to None.

返回:

The destination directory.

返回类型:

str

示例

>>> backend = PetrelBackend()
>>> src = 'petrel://path/of/dir'
>>> dst = 'path/of/your/dir'
>>> backend.copytree_to_local(src, dst)
'path/of/your/dir'
exists(filepath)[源代码]

Check whether a file path exists.

参数:

filepath (str or Path) – Path to be checked whether exists.

返回:

Return True if filepath exists, False otherwise.

返回类型:

bool

示例

>>> backend = PetrelBackend()
>>> filepath = 'petrel://path/of/file'
>>> backend.exists(filepath)
True
generate_presigned_url(url, client_method='get_object', expires_in=3600)[源代码]

Generate the presigned url of video stream which can be passed to mmcv.VideoReader. Now only work on Petrel backend.

备注

Now only work on Petrel backend.

参数:
  • url (str) – Url of video stream.

  • client_method (str) – Method of client, ‘get_object’ or ‘put_object’. Default: ‘get_object’.

  • expires_in (int) – expires, in seconds. Default: 3600.

返回:

Generated presigned url.

返回类型:

str

get(filepath)[源代码]

Read bytes from a given filepath with ‘rb’ mode.

参数:

filepath (str or Path) – Path to read data.

返回:

Return bytes read from filepath.

返回类型:

bytes

示例

>>> backend = PetrelBackend()
>>> filepath = 'petrel://path/of/file'
>>> backend.get(filepath)
b'hello world'
get_local_path(filepath)[源代码]

Download a file from filepath to a local temporary directory, and return the temporary path.

get_local_path is decorated by contxtlib.contextmanager(). It can be called with with statement, and when exists from the with statement, the temporary path will be released.

参数:

filepath (str or Path) – Download a file from filepath.

生成器:

Iterable[str] – Only yield one temporary path.

返回类型:

Generator[str | Path, None, None]

示例

>>> backend = PetrelBackend()
>>> # After existing from the ``with`` clause,
>>> # the path will be removed
>>> filepath = 'petrel://path/of/file'
>>> with backend.get_local_path(filepath) as path:
...     # do something here
get_text(filepath, encoding='utf-8')[源代码]

Read text from a given filepath with ‘r’ mode.

参数:
  • filepath (str or Path) – Path to read data.

  • encoding (str) – The encoding format used to open the filepath. Defaults to ‘utf-8’.

返回:

Expected text reading from filepath.

返回类型:

str

示例

>>> backend = PetrelBackend()
>>> filepath = 'petrel://path/of/file'
>>> backend.get_text(filepath)
'hello world'
isdir(filepath)[源代码]

Check whether a file path is a directory.

参数:

filepath (str or Path) – Path to be checked whether it is a directory.

返回:

Return True if filepath points to a directory, False otherwise.

返回类型:

bool

示例

>>> backend = PetrelBackend()
>>> filepath = 'petrel://path/of/dir'
>>> backend.isdir(filepath)
True
isfile(filepath)[源代码]

Check whether a file path is a file.

参数:

filepath (str or Path) – Path to be checked whether it is a file.

返回:

Return True if filepath points to a file, False otherwise.

返回类型:

bool

示例

>>> backend = PetrelBackend()
>>> filepath = 'petrel://path/of/file'
>>> backend.isfile(filepath)
True
join_path(filepath, *filepaths)[源代码]

Concatenate all file paths.

Join one or more filepath components intelligently. The return value is the concatenation of filepath and any members of *filepaths.

参数:
  • filepath (str or Path) – Path to be concatenated.

  • filepaths (str | Path) –

返回:

The result after concatenation.

返回类型:

str

示例

>>> backend = PetrelBackend()
>>> filepath = 'petrel://path/of/file'
>>> backend.join_path(filepath, 'another/path')
'petrel://path/of/file/another/path'
>>> backend.join_path(filepath, '/another/path')
'petrel://path/of/file/another/path'
list_dir_or_file(dir_path, list_dir=True, list_file=True, suffix=None, recursive=False)[源代码]

Scan a directory to find the interested directories or files in arbitrary order.

备注

Petrel has no concept of directories but it simulates the directory hierarchy in the filesystem through public prefixes. In addition, if the returned path ends with ‘/’, it means the path is a public prefix which is a logical directory.

备注

list_dir_or_file() returns the path relative to dir_path. In addition, the returned path of directory will not contains the suffix ‘/’ which is consistent with other backends.

参数:
  • dir_path (str | Path) – Path of the directory.

  • list_dir (bool) – List the directories. Defaults to True.

  • list_file (bool) – List the path of files. Defaults to True.

  • suffix (str or tuple[str], optional) – File suffix that we are interested in. Defaults to None.

  • recursive (bool) – If set to True, recursively scan the directory. Defaults to False.

生成器:

Iterable[str] – A relative path to dir_path.

返回类型:

Iterator[str]

示例

>>> backend = PetrelBackend()
>>> dir_path = 'petrel://path/of/dir'
>>> # list those files and directories in current directory
>>> for file_path in backend.list_dir_or_file(dir_path):
...     print(file_path)
>>> # only list files
>>> for file_path in backend.list_dir_or_file(dir_path, list_dir=False):
...     print(file_path)
>>> # only list directories
>>> for file_path in backend.list_dir_or_file(dir_path, list_file=False):
...     print(file_path)
>>> # only list files ending with specified suffixes
>>> for file_path in backend.list_dir_or_file(dir_path, suffix='.txt'):
...     print(file_path)
>>> # list all files and directory recursively
>>> for file_path in backend.list_dir_or_file(dir_path, recursive=True):
...     print(file_path)
put(obj, filepath)[源代码]

Write bytes to a given filepath.

参数:
  • obj (bytes) – Data to be saved.

  • filepath (str or Path) – Path to write data.

返回类型:

None

示例

>>> backend = PetrelBackend()
>>> filepath = 'petrel://path/of/file'
>>> backend.put(b'hello world', filepath)
put_text(obj, filepath, encoding='utf-8')[源代码]

Write text to a given filepath.

参数:
  • obj (str) – Data to be written.

  • filepath (str or Path) – Path to write data.

  • encoding (str) – The encoding format used to encode the obj. Defaults to ‘utf-8’.

返回类型:

None

示例

>>> backend = PetrelBackend()
>>> filepath = 'petrel://path/of/file'
>>> backend.put_text('hello world', filepath)
remove(filepath)[源代码]

Remove a file.

参数:

filepath (str or Path) – Path to be removed.

抛出:
  • FileNotFoundError – If filepath does not exist, an FileNotFoundError will be raised.

  • IsADirectoryError – If filepath is a directory, an IsADirectoryError will be raised.

返回类型:

None

示例

>>> backend = PetrelBackend()
>>> filepath = 'petrel://path/of/file'
>>> backend.remove(filepath)
rmtree(dir_path)[源代码]

Recursively delete a directory tree.

参数:

dir_path (str or Path) – A directory to be removed.

返回类型:

None

示例

>>> backend = PetrelBackend()
>>> dir_path = 'petrel://path/of/dir'
>>> backend.rmtree(dir_path)
Read the Docs v: latest
Versions
latest
stable
v0.10.3
v0.10.2
v0.10.1
v0.10.0
v0.9.1
v0.9.0
v0.8.5
v0.8.4
v0.8.3
v0.8.2
v0.8.1
v0.8.0
v0.7.4
v0.7.3
v0.7.2
v0.7.1
v0.7.0
v0.6.0
v0.5.0
v0.4.0
v0.3.0
v0.2.0
Downloads
epub
On Read the Docs
Project Home
Builds

Free document hosting provided by Read the Docs.