Skip to main content

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

Latest release 20230401: HasFSPath.shortpath: hand call before .fspath set.

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).

Method FSPathBasedSingleton.__init__(self, fspath: Optional[str] = None, lock=None): Initialise the singleton:

On the first call:

  • set .fspath to self._resolve_fspath(fspath)
  • set ._lock to lock (or threading.Lock() if not specified)
  • return True On subsequent calls return False.

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, prefixes=None)

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

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

Create the directory dirpath if missing.

Parameters:

  • dirpath: the required directory path
  • mode: the permissions mode, default 0o777
  • log: log makedirs or mkdir call
  • 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, prefixes=None)

Return path with the first matching leading prefix replaced.

Parameters:

  • environ: environment mapping if not os.environ
  • prefixes: optional 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 20230401: HasFSPath.shortpath: hand call before .fspath set.

Release 20221221: Replace use of cs.env.envsub with os.path.expandvars and drop unused environ parameter.

Release 20220918:

  • FSPathBasedSingleton.init: return True on the first call, False on subsequent calls.
  • FSPathBasedSingleton.init: probe dict for '_lock' instead of using hasattr (which plays poorly this early on with classes with their own getattr).
  • needdir: accept optional log parameter to log mkdir or makedirs.
  • HasFSPath: add a default str.

Release 20220805: Doc update.

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.

Release files for cs-fs 20230401

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for cs-fs 20230401
File Size Uploaded
cs.fs-20230401.tar.gz 6.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for cs-fs 20230401
File Interpreter ABI Platform
cs.fs-20230401-py3-none-any.whl Python 3 none any Details

Total release size: 12.6 kB

Release files / cs.fs-20230401.tar.gz

Download URL cs.fs-20230401.tar.gz
Size 6.2 kB
Tags Source
SHA-256 checksum
How to use checksums
5bfaa227d6f3e6b3b180e197e2d6db0339330e3b2a499b5ac8d70e5ea59215e7
BLAKE2b-256 checksum
How to use checksums
bfa6db8ea822a8d6b1b55eb401e6d16157d78b2aa8dbb78d9003f2692dd03f8e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.6

Release files / cs.fs-20230401-py3-none-any.whl

Download URL cs.fs-20230401-py3-none-any.whl
Size 6.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
84142854c594ee399548a3c4c49d1476ea7e8564654997a70dcf2fc4234f4ae0
BLAKE2b-256 checksum
How to use checksums
4221aa5cc0e889b80c07252824785bed0ec9072c2d30dad875865e58cc36572b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.6
Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page