import json import secrets import string from contextlib import contextmanager from pathlib import Path PROJECT_ROOT = Path(__file__).resolve().parent.parent.parent CONTEXT_PATH = PROJECT_ROOT / '.context.json' def set_context(contex: dict): CONTEXT_PATH.write_text(json.dumps(contex, indent=2)) def load_context(): if not CONTEXT_PATH.exists(): return {} return json.loads(CONTEXT_PATH.read_text()) def clean_context(): CONTEXT_PATH.unlink(missing_ok=True) @contextmanager def add_context(contex: dict): existing_context = load_context() set_context(dict(existing_context, **contex)) yield set_context(existing_context) def get_feed_name(report): """Return feed name for filling feed status table in DynamoDB.""" return f'tadas_{report}' def get_context_id(model_version, report): parts = ['tadas', model_version] if report: parts.append(report) rand_id = _gen_rand_id(length=4) return '_'.join(parts) + ' ' + rand_id def _gen_rand_id(length): """Generate uniq id with length chars using a simple random function.""" choices = string.ascii_letters + string.digits return ''.join(secrets.choice(choices) for _ in range(length))