Python module to gracefully handle a .config file/environment variables for scripts, with built in masking for sensitive options. Provides a Splunk friendly formatted logger instance.
Project description
Panaetius
This package provides:
- Functionality to read user variables from a
config.yml
or environment variables. - A convenient default logging formatter printing
json
that can save to disk and rotate. - Utility functions.
Config
options
skip_header_init
If skip_header_init=True
then the config_path
will not use the header_variable
as the
sub-directory in the config_path
.
E.g
CONFIG = panaetius.Config("tembo", "~/tembo/.config", skip_header_init=True)
Will look in ~/tembo/config/config.yml
.
If skip_header_init=False
then would look in ~/tembo/config/tembo/config.yml
.
Module
Convenient to place in a package/sub-package __init__.py
.
See Tembo for an example: https://github.com/tembo-pages/tembo-core/blob/main/tembo/cli/__init__.py
Example snippet to use in a module:
"""Subpackage that contains the CLI application."""
import os
from typing import Any
import panaetius
from panaetius.exceptions import LoggingDirectoryDoesNotExistException
if (config_path := os.environ.get("TEMBO_CONFIG")) is not None:
CONFIG: Any = panaetius.Config("tembo", config_path, skip_header_init=True)
else:
CONFIG = panaetius.Config(
"tembo", "~/tembo/.config", skip_header_init=True
)
panaetius.set_config(CONFIG, "base_path", "~/tembo")
panaetius.set_config(CONFIG, "template_path", "~/tembo/.templates")
panaetius.set_config(CONFIG, "scopes", {})
panaetius.set_config(CONFIG, "logging.level", "DEBUG")
panaetius.set_config(CONFIG, "logging.path")
try:
logger = panaetius.set_logger(
CONFIG, panaetius.SimpleLogger(logging_level=CONFIG.logging_level)
)
except LoggingDirectoryDoesNotExistException:
_LOGGING_PATH = CONFIG.logging_path
CONFIG.logging_path = ""
logger = panaetius.set_logger(
CONFIG, panaetius.SimpleLogger(logging_level=CONFIG.logging_level)
)
logger.warning("Logging directory %s does not exist", _LOGGING_PATH)
This means in ./tembo/cli/cli.py
you can
import tembo.cli
# access the CONFIG instance + variables from the config.yml
tembo.cli.CONFIG
Script
Create ./config/config.yml
in the same directory as the script.
In the script initialise a CONFIG
object:
import pathlib
import panaetius
CONFIG = panaetius.Config(
"teenagers_scraper", str(pathlib.Path(__file__).parents[0] / ".config"), skip_header_init=True
)
Set variables in the same way as the module above.
quickstart logging
import panaetius
def get_logger():
logging_dir = pathlib.Path(__file__).parents[0] / "logs"
logging_dir.mkdir(parents=True, exist_ok=True)
CONFIG = panaetius.Config("training_data_into_gcp", skip_header_init=True)
panaetius.set_config(CONFIG, "logging.level", "DEBUG")
panaetius.set_config(CONFIG, "logging.path", logging_dir)
return panaetius.set_logger(CONFIG, panaetius.SimpleLogger(logging_level=CONFIG.logging_level))
Utility Functions
Squasher
Squashes a json object or Python dictionary into a single level dictionary.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file panaetius-2.3.4.tar.gz
.
File metadata
- Download URL: panaetius-2.3.4.tar.gz
- Upload date:
- Size: 9.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.12 CPython/3.7.12 Darwin/20.3.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | da6884c12b1b79be4b338946cb4552cfcabf96e631f28068f95f1cf0842262f2 |
|
MD5 | 0992c32596e38ac925ae23d4e7f129ad |
|
BLAKE2b-256 | ab12ff7333d8e0598bb562f74bbd79f874e60bc03d1c682f5d1f2c83bd2812c8 |
File details
Details for the file panaetius-2.3.4-py3-none-any.whl
.
File metadata
- Download URL: panaetius-2.3.4-py3-none-any.whl
- Upload date:
- Size: 9.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.12 CPython/3.7.12 Darwin/20.3.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 26b3f5bc213ea6e2076eecd46ff3adb8dba08a287756c948cc09a2bf504bc4d8 |
|
MD5 | 5c8b98181d88f0727c51938952425aff |
|
BLAKE2b-256 | 80eaba63383c3d84e243bc9110c828f4a95f91c7d0d670c199385a4c2a7126a7 |