_open module#
- class cache_manager._open.Opener(path, ext=None, needed=None, large=True, default_mode='r', encoding=None)[source]#
Opens a file.
This class opens a file, extracts it in case it is a gzip, tar.gz, tar.bz2 or zip archive, selects the requested files if you only need certain files from a multifile archive, reads the data from the file, or returns the file pointer, as you request. It examines the file type and size. All these tasks are performed automatically upon instantiation.
- Args:
- path:
Path to the file.
- ext:
Extension of the file, such as “zip”, “tar.gz”, etc. Optional, defaults to
None
.- needed:
A list of paths to be extracted within an archive. If not provided, all paths will be included. Optional, defaults to
None
.- large:
Stores the file pointers instead of the contents of the files themselves. Optional, defaults to
True
.- default_mode:
Reading mode for the file objects: “r” (normal) or “rb” (binary). Optional, defaults to
"r"
(normal mode).- encoding:
Encoding for the stored file objects. Optional, defaults to
None
.
- Attrs:
- path:
Path to the file.
- ext:
Extension of the file.
- needed:
Paths to be extracted for compressed files.
- large:
Whether to store the file pointers instead of actual file contents.
- default_mode:
File reading mode, “r” (normal) or “rb” (binary).
- encoding:
Encoding type for text (e.g. ‘utf-8’, ‘ascii’, etc.).
- fileobj:
Stores the file object instance.
- result:
Content(s) of the file(s). Resulting content will depend whether the
large
attribute (i.e. file-object pointer or actual content) and in the case of compressed folders, the different files are stored as a dictionary where keys are file names and values are either file pointers or contents.- size:
Size of the file. In case of
.gz
format, the size of the compressed folder.- sizes: Size of the files when compressd format, stored as a dictionary
where keys are the file names.
- tarfile:
Pointer to the tar-compressed file object (only when file is in
.tar
format).- gzfile:
Pointer to the gz-compressed file object (only when file is in
.gz
format).- zipfile:
Pointer to the zip-compressed file object (only when file is in
.zip
format).
- open()[source]#
Loads the file object if exists on the disk. Stores the pointer under
fileobj
attribute. To obtain the contents,extract
must be called.
- open_gz()[source]#
Extracts files from
.gz
file. Resulting files are stored under the attributeresult
.
- open_tar()[source]#
Extracts files from
.tar
file. Resulting files are stored under the attributeresult
.