fairly.file package

Submodules

fairly.file.local module

LocalFile class module.

LocalFile class is used to perform operations on local files.

Usage example:

>>> file = LocalFile("/path/to/local/file/filename.txt")
>>> file.type
    application/text
>>> file.size
    543
>>> file.is_archive
    False
class fairly.file.local.LocalFile(fullpath: str, basepath: str = None, md5: str = None)[source]

Bases: File

LocalFile class.

Class Attributes:

CHUNK_SIZE: Chunk size in bytes to calculate MD5 checksum (default = 65536). NO_EXTRACT: List of file extensions which should not be extracted.

_fullpath

Full path of the local file.

Type:

str

CHUNK_SIZE = 262144
NO_EXTRACT = ['.docx', '.xlsx', '.pptx']
extract(path: str = None, notify: Callable = None) List[source]

Extracts archive file contents to a specified directory.

Parameters:
  • path – Path of the directory to extract to. Default is the current working directory.

  • notify

    Notification callback function. Three arguments are provided to the callback function:

    • file (LocalFile): File object of the extracted local file.

    • current_size (int): Current total uncompressed size of

      extracted files.

    • total_size (int): Total uncompressed size of the archive.

Raises:
  • ValueError("Invalid path") – If path is not a directory path.

  • ValueError("Invalid archive item {name}") – If archive item path is not valid.

  • ValueError("Invalid archive file") – If file is not an archive file.

Returns:

List of names of extracted files (str).

property fullpath: str

Full path of the local file.

property is_archive: bool

Checks if file is an archive file.

Returns:

True if file is an archive file, False otherwise.

match(val: str) bool[source]

Checks if file matches the specified file identifier.

File fullpath is compared with the specified identifier in addition to the properties checked by File.match().

Parameters:

val (str) – File identifier.

Returns:

True if file matches the specified file identifier, False otherwise.

property md5: str

MD5 checksum of the local file.

MD5 checksum is only calculated once and cached for subsequent calls.

property type: str

Content type of the local file.

fairly.file.remote module

RemoteFile class module.

RemoteFile class is used to perform operations on remote files.

class fairly.file.remote.RemoteFile(url: str, id: str = None, path: str = None, size: int = None, type: str = None, md5: str = None)[source]

Bases: File

RemoteFile class.

_url

URL address of the remote file.

Type:

str

_id

Identifier of the remote file.

Type:

str

_headers

HTTP headers of the remote file.

Type:

Dict

property headers: Dict

HTTP headers of the remote file.

property id: str

Identifier of the remote file.

match(val: str) bool[source]

Checks if remote file matches the specified file identifier.

File URL address and id are compared with the specified identifier in addition to the properties checked by File.match().

Parameters:

val (str) – File identifier.

Returns:

True if file matches the specified file identifier, False otherwise.

property md5: str

MD5 checksum of the remote file.

Content-MD5 header is used to get the MD5 checksum. It is only calculated once and cached for subsequent calls.

property name: str

Name of the remote file.

property size: int

Size of the remote file in bytes.

Content-Length header is used to get the size. It is only calculated once and cached for subsequent calls.

property type: str

Content type of the remote file.

Content type is guessed by using the URL address. If it fails, then Content-Type header is used to get the content type. It is only calculated once and cached for subsequent calls.

property url: str

URL address of the remote file.

Module contents

File class module.

File class is used to store file information in a standardized manner. It is an abstract class.

Implementations:
  • LocalFile

  • RemoteFile

class fairly.file.File[source]

Bases: ABC

File class.

_name

Name of the file including its extension.

Type:

str

_path

Path of the file including its name.

Type:

str

_size

Size of the file in bytes.

Type:

int

_type

Content type of the file.

Type:

str

_md5

MD5 checksum of the file.

Type:

str

_extension

Extension of the file.

Type:

str

property extension: str

Extension of the file.

property is_simple: bool

Checks if file is a simple file.

A simple file does not include any directories in its path, e.g. the path is equal to the name.

Returns:

True if the file is simple, False otherwise.

match(val: str) bool[source]

Checks if file matches the specified file identifier.

File name, path, and MD5 checksum are compared with the specified identifier for matching.

Parameters:

val (str) – File identifier.

Returns:

True if file matches the specified file identifier, False otherwise.

property md5: str

MD5 checksum of the file.

property name: str

Name of the file including its extension.

property path: str

Path of the file including its name.

property size: int

Size of the file in bytes.

property type: str

Content type of the file.