gummy.utils.compress_utils module

Utility programs for handling compression and decompression

gummy.utils.compress_utils.get_mimetype_mimetypes(path)[source]

Guess the type of a file based on its URL (filename).

Parameters

path (str) – filename

Returns

  • None (if the type can’t be guessed (no or unknown suffix)

  • a string of the form type/subtype (otherwise)

Examples

>>> get_mimetype_mimetypes("gummy.zip")
'application/zip'
>>> get_mimetype_mimetypes("gummy.tar.gz")
'application/x-tar'
# Check the difference when `path` does not exist.
>>> os.path.exists("gummy.png")
True
>>> get_mimetype_mimetypes("gummy.png")
'image/png'
>>> os.path.exists("gummy_.png")
False
>>> get_mimetype_mimetypes("gummy_.png")
'image/png'
gummy.utils.compress_utils.get_mimetype_libmagic(path)[source]

Accepts a filename and returns the detected filetype.

Parameters

path (str) – filename

Returns

  • None (if the file does not exist)

  • a string of the form type/subtype (otherwise)

Return type

str

Examples

>>> get_mimetype_libmagic("gummy.zip")
'application/zip'
>>> get_mimetype_libmagic("gummy.tar.gz")
'application/x-tar'
# Check the difference when `path` does not exist.
>>> os.path.exists("gummy.png")
True
>>> get_mimetype_libmagic("gummy.png")
'image/png'
>>> os.path.exists("gummy_.png")
False
>>> print(get_mimetype_mimetypes("gummy_.png"))
None
gummy.utils.compress_utils.get_mimetype(path)

Accepts a filename and returns the detected filetype.

Parameters

path (str) – filename

Returns

  • None (if the file does not exist)

  • a string of the form type/subtype (otherwise)

Return type

str

Examples

>>> get_mimetype_libmagic("gummy.zip")
'application/zip'
>>> get_mimetype_libmagic("gummy.tar.gz")
'application/x-tar'
# Check the difference when `path` does not exist.
>>> os.path.exists("gummy.png")
True
>>> get_mimetype_libmagic("gummy.png")
'image/png'
>>> os.path.exists("gummy_.png")
False
>>> print(get_mimetype_mimetypes("gummy_.png"))
None
gummy.utils.compress_utils.is_compressed(ext)[source]

Check whether file is compressed or not from the extensions.

gummy.utils.compress_utils.extract_from_compressed(path, ext=None, dirname='.', verbose=True)[source]

Extract files from compressed file.

Parameters
  • path (str) – path/to/compressed_file.

  • ext (str) – Extract only files with this extension from compressed files. If None, all files will be extracted.

  • dirname (str) – Where the extracted file will be stored.

  • verbose (bool) – Whether print names in extracted file or not.

Returns

Paths of extracted files.

Return type

list

class gummy.utils.compress_utils.GummyAbstExtractor[source]

Bases: object

File Extractor.

classmethod extract_from_compressed(path, ext=None, dirname='.', verbose=True)[source]

Extract files from compressed file.

Parameters
  • path (str) – path/to/compressed_file.

  • ext (str) – Extract only files with this extension from compressed files. If None, all files will be extracted.

  • dirname (str) – Where the extracted file will be stored.

  • verbose (bool) – Whether print names in extracted file or not.

Returns

Paths of extracted files.

Return type

list

abstract static open_compressed_file(path)[source]

Open a compressed file.

abstract static get_namelist(compressed_f)[source]

Get name list in the extracted file.

class gummy.utils.compress_utils.ZipExtractor[source]

Bases: gummy.utils.compress_utils.GummyAbstExtractor

Extractor for Zip file.

>>> import zipfile
>>> with zipfile.ZipFile(path) as f:
...     for name in f.get_namelist():
...         print(name)
static open_compressed_file(path)[source]

Open a compressed file.

static get_namelist(compressed_f)[source]

Get name list in the extracted file.

class gummy.utils.compress_utils.TarExtractor[source]

Bases: gummy.utils.compress_utils.GummyAbstExtractor

Extractor for Tar file.

>>> import tarfile
>>> with tarfile.open(path) as f:
...     for m in f.getmembers():
...         name = m.name
...         print(name)
static open_compressed_file(path)[source]

Open a compressed file.

static get_namelist(compressed_f)[source]

Get name list in the extracted file.