Skip to main content

Assorted filesystem related utility functions, some of which have been bloating cs.fileutils for too long.

Project description

Assorted filesystem related utility functions, some of which have been bloating cs.fileutils for too long.

Latest release 20220530: FSPathBasedSingleton._resolve_fspath: new envvar and default_attr parameters.

Function atomic_directory(*da, **dkw)

Decorator for a function which fills in a directory which calls the function against a temporary directory then renames the temporary to the target name on completion.

Parameters:

  • infill_func: the function to fill in the target directory
  • make_placeholder: optional flag, default False: if true an empty directory will be make at the target name and after completion it will be removed and the completed directory renamed to the target name

Function fnmatchdir(dirpath, fnglob)

Return a list of the names in dirpath matching the glob fnglob.

Class FSPathBasedSingleton(cs.obj.SingletonMixin, HasFSPath)

The basis for a SingletonMixin based on realpath(self.fspath).

Class HasFSPath

An object with a .fspath attribute representing a filesystem location.

Function is_clean_subpath(subpath: str)

Test that subpath is clean:

  • not empty or '.' or '..'
  • not an absolute path
  • normalised
  • does not walk up out of its parent directory

Examples:

>>> is_clean_subpath('')
False
>>> is_clean_subpath('.')

Function longpath(path, environ=None, prefixes=None)

Return path with prefixes and environment variables substituted. The converse of shortpath().

Function needdir(dirpath, mode=511, *, use_makedirs=False)

Create the directory dirpath if missing.

Parameters:

  • dirpath: the required directory path
  • mode: the permissions mode, default 0o777
  • use_makedirs: optional creation mode, default False; if true, use os.makedirs, otherwise os.mkdir

Function rpaths(dirpath='.', *, only_suffixes=None, skip_suffixes=None, sort_paths=False)

Yield relative file paths from a directory.

Parameters:

  • dirpath: optional top directory, default '.'
  • only_suffixes: optional iterable of suffixes of interest; if provided only files ending in these suffixes will be yielded
  • skip_suffixes: optional iterable if suffixes to ignore; if provided files ending in these suffixes will not be yielded
  • sort_paths: optional flag specifying that filenames should be sorted, default False

Function shortpath(path, environ=None, prefixes=None)

Return path with the first matching leading prefix replaced.

Parameters:

  • environ: environment mapping if not os.environ
  • prefixes: iterable of (prefix,subst) to consider for replacement; each prefix is subject to environment variable substitution before consideration The default considers "$HOME/" for replacement by "~/".

Release Log

Release 20220530: FSPathBasedSingleton._resolve_fspath: new envvar and default_attr parameters.

Release 20220429:

  • New HasFSPath and FSPathBasedSingleton.
  • Add longpath and shortpath from cs.fileutils.
  • New is_clean_subpath(subpath).
  • New needdir(path).
  • New fnmatchdir(dirpath,fnglob) pulled out from HasFSPath.fnmatch(fnglob).

Release 20220327: New module cs.fs to contain more filesystem focussed functions than cs.fileutils, which is feeling a bit bloated.

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

cs.fs-20220530.tar.gz (5.6 kB view details)

Uploaded Source

Built Distribution

cs.fs-20220530-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

Details for the file cs.fs-20220530.tar.gz.

File metadata

  • Download URL: cs.fs-20220530.tar.gz
  • Upload date:
  • Size: 5.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.1 readme-renderer/30.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.7 tqdm/4.62.3 importlib-metadata/4.8.2 keyring/23.3.0 rfc3986/1.5.0 colorama/0.4.4 CPython/3.9.13

File hashes

Hashes for cs.fs-20220530.tar.gz
Algorithm Hash digest
SHA256 774b37a779deb500c70fb38b0ddfd442a21fc47bbf05f5c457642e6c3c645912
MD5 bc46443d4d279ecb0819df48110c8f18
BLAKE2b-256 d3f1007b1701a087dfda15d0ae46c2588b3b974f298cc2454dd097aad0455206

See more details on using hashes here.

File details

Details for the file cs.fs-20220530-py3-none-any.whl.

File metadata

  • Download URL: cs.fs-20220530-py3-none-any.whl
  • Upload date:
  • Size: 5.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.1 readme-renderer/30.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.7 tqdm/4.62.3 importlib-metadata/4.8.2 keyring/23.3.0 rfc3986/1.5.0 colorama/0.4.4 CPython/3.9.13

File hashes

Hashes for cs.fs-20220530-py3-none-any.whl
Algorithm Hash digest
SHA256 47795e5574b3eb172592f2905975dccc859459a62c85c1602ea068ae721249cd
MD5 48c77c3f8e4c92d366c4d97e9825dd92
BLAKE2b-256 5915f687c18528e44251b03de0f416fbfe79dcc0e7ed0f8c02302c0c95fb7ae3

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