mini_buildd.misc module

mini_buildd.misc.singularize(s)

Singularize some english nouns from plural.

mini_buildd.misc.esplit(s, sep)

Unlike python str.strip(sep), this delivers an empty list for the empty string.

mini_buildd.misc.uniq(iterable)

Make list unique

Sometimes, a list should be unique, but you don’t want to use set (for example, as it does not keep order or can’t be JSON-serialized).

There does not seem to be a simple builtin for this. Since python 3.7, dict is guaranteed to be ordered, so we can use this approach.

mini_buildd.misc.mdget(d, keys, default=None)

Get nested value from multidimensional dict, return default if any ‘path part’ is missing.

Rather use this instead of code like d.get(foo, {}).get(bar, {}).get(mykey, mydefaultvalue).

>>> d = {"foo": {"bar": {"mykey": "myvalue"}}}
>>> mdget(d, ["foo", "bar", "mykey"], "default_value")
'myvalue'
>>> mdget(d, ["fpp", "bar", "mykey"], "default_value")
'default_value'
>>> mdget(d, ["foo", "bsr", "mykey"], "default_value")
'default_value'
>>> mdget(d, ["foo", "bar", "mykex"], "default_value")
'default_value'
class mini_buildd.misc.Datetime

Bases: object

Datetime tools – always use same/comparable (aware && UTC) datetime objects.

EPOCH = datetime.datetime(1970, 1, 1, 0, 0, tzinfo=datetime.timezone.utc)
SINCE_OPTIONS = ['1 hour ago', '1 day ago', '1 week ago', '1 month ago', '1 year ago', 'epoch']
classmethod now()

Shortcut to be used for all internal UTC stamps.

classmethod from_stamp(stamp)
classmethod from_iso(isostr)
classmethod from_path(path)
classmethod timecode(stamp=None)

Timecode string from current timestamp.

classmethod is_naive(date_obj)
classmethod check_aware(date_obj)
classmethod parse(date_string, default=None)

Use keyword ‘epoch’ for a big-bang timestamp. Otherwise most formats should work (like ‘2 weeks ago’).

class mini_buildd.misc.Snake(name)

Bases: object

Case style conversion to (lowercase) snake.

>>> Snake("CamelCase").from_camel()
'camel_case'
>>> Snake("Kebab-Case").from_kebab()
'kebab_case'
from_camel()
from_kebab()
class mini_buildd.misc.Field(field)

Bases: object

Changes field name handling (custom prefix && (snake) name conversion).

CPREFIX = 'X-Mini-Buildd-'
class mini_buildd.misc.CField(field)

Bases: Field

class mini_buildd.misc.StopWatch

Bases: object

close()
delta()
to_json()
class mini_buildd.misc.Singleton

Bases: type

destroy()
class mini_buildd.misc.TmpDir(**kwargs)

Bases: object

Temporary dir class (use with with contextlib.closing()..., or as mixin class)

close()
mini_buildd.misc.tmp_dir(**kwargs)

Temporary dir context manager (use with with)

mini_buildd.misc.nop(*_args, **_kwargs)
mini_buildd.misc.attempt(func, *args, retval_on_failure=None, **kwargs)

Run function, warn-log exception on error, but continue.

>>> attempt(lambda x: x, "ypsilon")
'ypsilon'
>>> attempt(lambda x: x, "ypsilon", retval_on_failure="xylophon", unknown_arg="xanthippe")
'xylophon'
>>> attempt(lambda x: x/0, "ypsilon", retval_on_failure="xylophon")
'xylophon'
mini_buildd.misc.measure(func, *args, **kwargs)
mini_buildd.misc.strip_epoch(version)

Strip the epoch from a version string.

mini_buildd.misc.guess_default_dirchroot_backend(overlay, aufs)
mini_buildd.misc.subst_placeholders(template, placeholders)

Substitute placeholders in string from a dict.

>>> subst_placeholders("Repoversionstring: %IDENTITY%%CODEVERSION%", { "IDENTITY": "test", "CODEVERSION": "60" })
'Repoversionstring: test60'
class mini_buildd.misc.Hash(path)

Bases: object

Shortcut to get hashsums from file.

>>> Hash("test-data/unix.txt").md5()
'cc3d5ed5fda53dfa81ea6aa951d7e1fe'
>>> Hash("test-data/unix.txt").sha1()
'8c84f6f36dd2230d3e9c954fa436e5fda90b1957'
get(hash_type='md5')

Get any hash from file contents.

md5()
sha1()
mini_buildd.misc.get_cpus()
mini_buildd.misc.list_get(list_, index, default=None)
mini_buildd.misc.rmdirs(path)

Remove path recursively. Succeed even if it does not exist in the first place.

mini_buildd.misc.json_pretty(json_obj)