ksconf.util package


ksconf.util.compare module

ksconf.util.compare.cmp_sets(a, b)

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

ksconf.util.compare.file_compare(fn1, fn2)
ksconf.util.compare.fileobj_compare(f1, f2)

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, *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: str | Path | Callable | None, mode='w', **open_kwargs) IO

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: str | Path | Callable | None) str

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.


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, algorithm='sha256')
ksconf.util.file.relwalk(top, topdown=True, onerror=None, followlinks=False)

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.


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, owner=None, app=None)
ksconf.util.rest.build_rest_url(base, service, owner=None, app=None)

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.


Module contents


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