ksconf.util package

Submodules

ksconf.util.compare module

ksconf.util.compare.cmp_sets(a: Set[T], b: Set[T]) Tuple[List[T], List[T], List[T]]

Result tuples in format (a-only, common, b-only)

ksconf.util.compare.file_compare(fn1: str | PathLike, fn2: str | PathLike) bool
ksconf.util.compare.fileobj_compare(f1: IO, f2: IO) bool

ksconf.util.completers module

ksconf.util.completers.DirectoriesCompleter(*args, **kwargs)
ksconf.util.completers.FilesCompleter(*args, **kwargs)
ksconf.util.completers.autocomplete(*args, **kwargs)

ksconf.util.file module

class ksconf.util.file.ReluctantWriter(path: str | PathLike, *args, **kwargs)

Bases: object

Context manager to intelligently handle updates to an existing file. New content is written to a temp file, and then compared to the current file’s content. The file file will be overwritten only if the contents changed.

ksconf.util.file.atomic_open(name: Path, temp_name: Path | str | Callable[[Path], Path] | None, mode='w', **open_kwargs) Generator[IO, None, None]

Context manager to atomically write to a file stream. Like the open() context manager, a file handle returned when the context is entered. Upon successful completion, the temporary file is renamed into place; thus providing an atomic update operation.

See atomic_writer() for behaviors regarding the temp_name parameter option.

This function can be used nearly any place that with open(myfile, mode="w") as stream

ksconf.util.file.atomic_writer(dest: Path, temp_name: Path | str | Callable[[Path], Path] | None) Generator[Path, None, None]

Context manager to atomically update a destination. When entering the context, a temporary file name is returned. When the context is successfully exited, the temporary file is renamed into place. Either way, the temporary file is removed.

The name of the temporary file can be controlled via temp_name. If a str is provided, it will be used as a suffix. If a Path is provided, that will be used as the literal temporary file name. If a callable is given, the dest path will be passed into the callable to determine the temporary file. Alternatively, the entire _atomic_ nature of this function can be disabled by passing temp_name=None.

ksconf.util.file.dir_exists(directory)

Ensure that the directory exists

ksconf.util.file.expand_glob_list(iterable, do_sort=False)
ksconf.util.file.file_fingerprint(path, compare_to=None)
ksconf.util.file.file_hash(path: str | PathLike, algorithm='sha256') str
ksconf.util.file.relwalk(top: str | PathLike, topdown=True, onerror=None, followlinks=False) Iterable[Tuple[str, List[str], List[str]]]

Relative path walker Like os.walk() except that it doesn’t include the “top” prefix in the resulting ‘dirpath’.

ksconf.util.file.secure_delete(path: Path, passes=3)

A simple file shred technique. If there’s demand, this could be expanded. But for now, ‘secure’ means just slightly more secure that unlink().

Adapted from from Ansible’s _shred_file_custom()

ksconf.util.file.smart_copy(src, dest)

Copy (overwrite) file only if the contents have changed.

ksconf.util.file.splglob_simple(pattern)

Return a splglob that either matches a full path or match a simple file

ksconf.util.file.splglob_to_regex(pattern, re_flags=None)

ksconf.util.rest module

ksconf.util.rest.build_rest_namespace(base: str, owner: str | None = None, app: str | None = None) str
ksconf.util.rest.build_rest_url(base: str, service: str, owner: str | None = None, app: str | None = None) str

ksconf.util.terminal module

class ksconf.util.terminal.TermColor(stream)

Bases: object

Simple color setting helper class that’s a context manager wrapper around a stream. This ensure that the color is always reset at the end of a session.

color(*codes)
reset()
write(content)

Module contents

ksconf.util.debug_traceback()

If the ‘KSCONF_DEBUG’ environmental variable is set, then show a stack trace.

ksconf.util.decorator_with_opt_kwargs(decorator: Callable) Callable

Make a decorator that can work with or without args. Heavily borrowed from: https://gist.github.com/ramonrosa/402af55633e9b6c273882ac074760426 Thanks to GitHub user ramonrosa