Skip to main content

inherits from pathlib.Path with methods for hashing, copying, deleting and more

Project description

pathlibutil

PyPI - Python Version PyPI PyPI - Downloads PyPI - License GitHub Workflow Test) Website GitHub tag (with filter) Coverage


pathlibutil.Path inherits from pathlib.Path with some useful built-in python functions from shutil and hashlib

  • Path.hexdigest() to calculate and Path.verify() for verification of hexdigest from a file
  • Path.default_hash to configurate default hash algorithm for Path class (default: 'md5')
  • Path.size() to get size in bytes of a file or directory
    • byteint function decorator converts the return value of int to a ByteInt object
  • Path.read_lines() to yield over all lines from a file until EOF
  • contextmanager to change current working directory with with statement
  • Path.copy() copy a file or directory to a new path destination
  • Path.delete() delete a file or directory-tree
  • Path.move() move a file or directory to a new path destination
  • Path.make_archive() creates and Path.unpack_archive() uncompresses an archive from a file or directory
  • Path.archive_formats to get all available archive formats
  • Path.stat() returns a StatResult object to get file or directory information containing
    • TimeInt objects for atime, ctime, mtime and birthtime
    • ByteInt object for size

Installation

pip install pathlibutil

7zip support

to handle 7zip archives, an extra package py7zr>=0.20.2 is required!

PyPI - Version

# install as extra dependency
pip install pathlibutil[7z]

Usage

from pathlibutil import Path

readme = Path('README.md')

print(f'File size: {readme.size()} Bytes')

Example 1

Read a file and print its content and some file information to stdout.

Path.read_lines()

from pathlibutil import Path

readme = Path("README.md")

print(f"File size: {readme.size()} Bytes")
print(f'File sha1: {readme.hexdigest("sha1")}')

print("File content".center(80, "="))

for line in readme.read_lines(encoding="utf-8"):
  print(line, end="")

print("EOF".center(80, "="))

Example 2

Write a file with md5 checksums of all python files in the pathlibutil-directory.

Path.hexdigest()

from pathlibutil import Path

file = Path("pathlibutil.md5")

with file.open("w") as f:
  f.write(
      f"# MD5 checksums generated with pathlibutil "
      "(https://pypi.org/project/pathlibutil/)\n\n"
  )

  i = 0
  for i, filename in enumerate(Path("./pathlibutil").glob("*.py"), start=1):
    f.write(f"{filename.hexdigest()} *{filename}\n")

print(f"\nwritten: {i:>5} {file.default_hash}-hashes to: {file}")

Example 3

Read a file with md5 checksums and verify them.

Path.verify(), Path.default_hash and contextmanager

from pathlibutil import Path

file = Path("pathlibutil.md5")


def no_comment(line: str) -> bool:
  return not line.startswith("#")


with file.parent as cwd:
  miss = 0
  ok = 0
  fail = 0

  for line in filter(no_comment, file.read_lines()):
    try:
      digest, filename = line.strip().split(" *")
      verification = Path(filename).verify(digest, "md5")
    except ValueError as split_failed:
      continue
    except FileNotFoundError as verify_failed:
      tag = "missing"
      miss += 1
    else:
      if verification:
        tag = "ok"
        ok += 1
      else:
        tag = "fail"
        fail += 1

    print(f'{tag.ljust(len(digest), ".")} *{filename}')

  print(f"\nok: {ok:<5} fail: {fail:<5} missing: {miss}")

Example 4

Search all pycache directories and free the memory and display the number of deleted directories and the amount of memory freed in MB.

Path.delete(), Path.size() and ByteInt

from pathlibutil import Path, ByteInt

mem = ByteInt(0)
i = 0

for i, cache in enumerate(Path(".").rglob("*/__pycache__/"), start=1):
  cache_size = cache.size()
  try:
    cache.delete(recursive=True)
  except OSError:
    print(f"Failed to delete {cache}")
  else:
    mem += cache_size

print(f"{i} cache directories deleted, {mem:.1mb} MB freed.")

Example 5

Inherit from pathlibutil.Path to register new a archive format. Specify a archive as keyword argument in the new subclass, which has to be the suffix without . of the archives. Implement a classmethod _register_archive_format() to register new archive formats.

Path.make_archive(), Path.archive_formats and Path.move()

import pathlibutil
import shutil


class RegisterFooBarFormat(pathlibutil.Path, archive="foobar"):
  @classmethod
  def _register_archive_format(cls):
    """
    implement new register functions for given `archive`
    """
    try:
      import required_package_name
    except ModuleNotFoundError:
      raise ModuleNotFoundError("pip install <required_package_name>")

    def pack_foobar(
        base_name, base_dir, owner=None, group=None, dry_run=None, logger=None
    ) -> str:
      """callable that will be used to unpack archives.

      Args:
          base_name (`str`): name of the file to create
          base_dir (`str`): directory to start archiving from, defaults to `os.curdir`
          owner (`Any`, optional): as passed in `make_archive(*args, owner=None, **kwargs)`. Defaults to None.
          group (`Any`, optional): as passed in `make_archive(*args, group=None, **kwargs)`. Defaults to None.
          dry_run (`Any`, optional): as passed in `make_archive(*args, dry_run=None, **kwargs)`. Defaults to None.
          logger (`logging.Logger`, optional): as passed in `make_archive(*args, logger=None, **kwargs)`. Defaults to None.

      Returns:
          str: path of the new created archive
      """
      raise NotImplementedError("implement your own pack function")

    def unpack_foobar(archive, path, filter=None, extra_args=None) -> None:
      """callable that will be used to unpack archives.

      Args:
          archive (`str`): path of the archive
          path (`str`): directory the archive must be extracted to
          filter (`Any`, optional): as passed in `unpack_archive(*args, filter=None, **kwargs)`. Defaults to None.
          extra_args (`Sequence[Tuple[name, value]]`, optional): additional keyword arguments, specified by `register_unpack_format(*args, extra_args=None, **kwargs)`. Defaults to None.
      """
      raise NotImplementedError("implement your own unpack function")

    shutil.register_archive_format(
        "foobar", pack_foobar, description="foobar archives"
    )
    shutil.register_unpack_format("foobar", [".foo.bar"], unpack_foobar)


file = pathlibutil.Path("README.md")

print(f"available archive formats: {file.archive_formats}")

archive = file.make_archive("README.foo.bar")

backup = archive.move("./backup/")

print(f"archive created: {archive.name} and moved to: {backup.parent}")

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pathlibutil-0.1.7.tar.gz (13.5 kB view details)

Uploaded Source

Built Distribution

pathlibutil-0.1.7-py3-none-any.whl (13.2 kB view details)

Uploaded Python 3

File details

Details for the file pathlibutil-0.1.7.tar.gz.

File metadata

  • Download URL: pathlibutil-0.1.7.tar.gz
  • Upload date:
  • Size: 13.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.11.7 Linux/6.2.0-1018-azure

File hashes

Hashes for pathlibutil-0.1.7.tar.gz
Algorithm Hash digest
SHA256 51704bff9d1c07f942b2ed54ab9a47159c12afb568989baaa58372fbdc70bafb
MD5 6da601616cd57a6a12f0ea409d0af26a
BLAKE2b-256 2a82af5f133df1660feaf52e443dfcf172fae10d025119bd3c74d52db50842c3

See more details on using hashes here.

File details

Details for the file pathlibutil-0.1.7-py3-none-any.whl.

File metadata

  • Download URL: pathlibutil-0.1.7-py3-none-any.whl
  • Upload date:
  • Size: 13.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.11.7 Linux/6.2.0-1018-azure

File hashes

Hashes for pathlibutil-0.1.7-py3-none-any.whl
Algorithm Hash digest
SHA256 0d4a95c3341ba20255cb21ce72977585c51e15da11fc1f76369b08574545ab72
MD5 363a654a800bd96564c0c7dc0e00fa99
BLAKE2b-256 8d52147669984b2642b22e5707d448a96e055f5b043e4ca0e1df2687bb9264d9

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page