_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).

close()[source]#

Closes the file.

extract()[source]#

Calls the right extracting method for a compressed file according to the format.

static iterfile(fileobj)[source]#

Returns an iterator over the lines of a file.

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 attribute result.

open_plain()[source]#

Opens a plain text file. Resulting file is stored under the attribute result.

open_tar()[source]#

Extracts files from .tar file. Resulting files are stored under the attribute result.

open_zip()[source]#

Extracts files from .zip file. Resulting files are stored under the attribute result.

set_type()[source]#

Determines the file type based on the extension.