From e7a35d23e50928c14ea0ee4773eb8d3aba1dd407 Mon Sep 17 00:00:00 2001 From: Martin Sekera Date: Mon, 7 Oct 2019 01:49:59 +0200 Subject: [PATCH] Add .items() to Config --- dtk/__init__.py | 1 + dtk/argv.py | 3 ++- dtk/aux.py | 64 +++++++++++++++++++++++++++++++++++++++++++++++++ dtk/cfg.py | 3 +++ dtk/dt.py | 16 +++++++++++++ 5 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 dtk/dt.py diff --git a/dtk/__init__.py b/dtk/__init__.py index 78c9094..2df4d07 100644 --- a/dtk/__init__.py +++ b/dtk/__init__.py @@ -4,3 +4,4 @@ from . import argv from . import aux from . import cfg +from . import dt diff --git a/dtk/argv.py b/dtk/argv.py index 1b9ee5f..6d9a2a1 100644 --- a/dtk/argv.py +++ b/dtk/argv.py @@ -131,7 +131,8 @@ class Invocation: else: raise BadKeywordArg(arg) - def execute(self): + def execute(self, context=None): + self.action.context = context self.action(self.cmd, **self.args) def help(self, stream=sys.stdout): diff --git a/dtk/aux.py b/dtk/aux.py index 687fc10..39af873 100644 --- a/dtk/aux.py +++ b/dtk/aux.py @@ -1,6 +1,8 @@ #! /usr/bin/env python3 # encoding: utf-8 +# ---------------------------------------------------------------- + def flatten_dict(root, glue=" ", prefix=[]): lines = [] @@ -13,3 +15,65 @@ def flatten_dict(root, glue=" ", prefix=[]): lines.append((glue.join(new_prefix), v)) return lines + +# ---------------------------------------------------------------- + +import random + +PASSPHRASE_VOWELS = "aeiuAEU" +PASSPHRASE_CONSONANTS = "bcdfghjkmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ23456789" + +def random_bytes(length): + return open("/dev/urandom", "rb").read(length) + +def random_syllable(): + return random.choice(PASSPHRASE_CONSONANTS) + random.choice(PASSPHRASE_VOWELS) + random.choice(PASSPHRASE_CONSONANTS) + +def random_word(syllables=3): + parts = [] + + for _ in range(syllables): + parts.append(random_syllable()) + + return "".join(parts) + +# ---------------------------------------------------------------- + +import hashlib + +def hash_password(p, hexdigest=True): + h = hashlib.blake2b() + + h.update(p.encode("utf-8")) + + return h.hexdigest() if hexdigest else h.digest() + +# ---------------------------------------------------------------- + +def raw_to_hex(raw, separator=" "): + """ + Converts a bytearray (or bytes) into its textual hexadecimal representation. + """ + + output = [] + + for o in raw: + output.append(hex(o)[2:].zfill(2)) + + return separator.join(output) + +def hex_to_raw(text, separator=" "): + """ + Converts a hexadecimal representation of a byte array into a bytearray. + """ + + output = [] + + text = text.replace(separator, "") + + for i in range(len(text)//2): + output.append(int(text[2*i:2*i+2], 16)) + + return bytearray(output) + +# ---------------------------------------------------------------- diff --git a/dtk/cfg.py b/dtk/cfg.py index 5c94a29..979ca88 100644 --- a/dtk/cfg.py +++ b/dtk/cfg.py @@ -30,6 +30,9 @@ class Config: def values(self): return self.raw.values() + def items(self): + return self.raw.items() + def __getitem__(self, name): return self.raw[name] diff --git a/dtk/dt.py b/dtk/dt.py new file mode 100644 index 0000000..fcd103b --- /dev/null +++ b/dtk/dt.py @@ -0,0 +1,16 @@ +#! /usr/bin/env python3 +# encoding: utf-8 + +# ---------------------------------------------------------------- + +import datetime +import pytz + +def now(tz_name="UTC"): + t = datetime.datetime.now() + tz = pytz.timezone(tz_name) + + return tz.localize(t) + +# ---------------------------------------------------------------- +