Skip to content

Commit

Permalink
dev(narugo): add docs for function is_archive_or_compressed
Browse files Browse the repository at this point in the history
  • Loading branch information
narugo1992 committed Aug 19, 2024
1 parent a83e7ec commit b0d01a1
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
15 changes: 15 additions & 0 deletions docs/source/api_doc/utils/archive.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
hfutils.utils.archive
=================================

.. currentmodule:: hfutils.utils.archive

.. automodule:: hfutils.utils.archive


is_archive_or_compressed
---------------------------

.. autofunction:: is_archive_or_compressed



1 change: 1 addition & 0 deletions docs/source/api_doc/utils/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ hfutils.utils
.. toctree::
:maxdepth: 3

archive
binary
download
number
Expand Down
36 changes: 36 additions & 0 deletions hfutils/utils/archive.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
"""
This module provides functionality for identifying archive and compressed files based on their filenames.
It includes a comprehensive list of known archive and compressed file extensions, as well as patterns for
identifying split archives and other generic compressed file formats. The main
function :func:`is_archive_or_compressed` can be used to determine if a given filename
represents an archive or compressed file.
The module is useful for file handling operations where it's necessary to distinguish between regular files
and archives or compressed files.
"""

import os.path
import re
from typing import Union
Expand Down Expand Up @@ -98,6 +110,30 @@


def is_archive_or_compressed(filename: Union[str, os.PathLike]) -> bool:
"""
Determine if the given filename represents an archive or compressed file.
This function checks the filename against a list of known archive and compressed file extensions,
as well as patterns for split archives and other generic compressed file formats.
:param filename: The name of the file to check. Can be a string or a path-like object.
:type filename: Union[str, os.PathLike]
:return: True if the filename represents an archive or compressed file, False otherwise.
:rtype: bool
:raises TypeError: If the filename is not a string or path-like object.
Usage:
>>> is_archive_or_compressed('example.zip')
True
>>> is_archive_or_compressed('document.txt')
False
>>> is_archive_or_compressed('archive.tar.gz')
True
>>> is_archive_or_compressed('split_archive.zip.001')
True
"""
if not isinstance(filename, (str, os.PathLike)):
raise TypeError(f'Unknown file name type - {filename!r}')
filename = os.path.basename(os.path.normcase(str(filename)))
Expand Down

0 comments on commit b0d01a1

Please sign in to comment.