Skip to main content

egos-helpers

A python library for helper functions. Used in the egos-tech projects.

Functions

  • gather_environ: Return a dict of environment variables correlating to the keys dict.
  • get_environ_value: Resolve a single value from an environment variable or its _FILE companion (file-based secrets).
  • short_msg: Truncates the message to chars characters and adds two dots at the end.
  • strtobool: Converts a string to a boolean
  • redact: Replaces in message the param string with replace_value
  • key_to_title: Converts a key in the form a_is_b to a title in the form A Is B
  • slugify: Converts a string to a URL-safe slug of at most max_length characters
  • setup_logger: Sets up and returns a logger, optionally with a GELF handler
  • get_formatter: Builds the log record formatter, which adds source locations at DEBUG

See egos_helpers/core.py for the full description of each function.

egos_helpers.crypto encrypts values an application has to store. It lives behind the crypto extra, so a plain install does not pull it in. See docs/crypto.md.

Usage example

Install egos-helpers:

python3 -m venv .venv
source .venv/bin/activate
# if you don't have a requirements.txt file
pip install -U egos-helpers
# if you do have a requirements.txt file
pip install -U -r requirements.txt

In your python project:

from egos_helpers import gather_environ


class MyClass:
    keys = {
        "key_one": {
            "default": ["one", 2],
            "type": "list",
        },
        "key_two": {
            "hidden": True,
            "default": False,
            "type": "boolean",
        },
        "key_three": {
            "default": {},
            "type": "filter",
        },
        "key_four": {
            "default": None,
            "redact": True,
            "type": "string",
        },
        "key_five": {
            "default": 12,
            "type": "int",
        },
        "key_six": {
            "default": "INFO",
            "type": "enum",
            "values": ["DEBUG", "INFO", "WARNING", "ERROR"],
        },
        "key_seven": {
            "default": "12",
            "deprecated": True,
            "replaced_by": "key_five",
            "type": "int",
        },
    }

    def __init__(self):
        envkeys = gather_environ(keys=MyClass.keys)
        print(envkeys)

With KEY_ONE=one, KEY_THREE=foo=bar, KEY_FOUR=super_secret_string, KEY_FIVE=33, KEY_SIX=WARNING and the rest unset, gather_environ returns:

{
    "key_one": ["one"],
    "key_two": False,
    "key_three": {"foo": "bar"},
    "key_four": "super_secret_string",
    "key_five": 33,
    "key_six": "WARNING",
}

key_seven is deprecated and unset, so it is left out. A key that is unset returns its default exactly as declared, without being converted to the key's type.

File-based secrets

Use get_environ_value to resolve a value from either an environment variable or a <NAME>_FILE file path (the container secrets convention). It reads <NAME>_FILE first (returning the file contents stripped of surrounding whitespace), falls back to <NAME>, and returns None when neither is set. Setting both raises an error. So does an unreadable <NAME>_FILE path.

from egos_helpers import get_environ_value

# MYAPP_TOKEN=secret123           -> "secret123"
# MYAPP_TOKEN_FILE=/run/secrets/t -> contents of the file, stripped
token = get_environ_value("MYAPP_TOKEN")

Encrypted values

A secret the application mints while it runs has to be persisted somewhere, and egos_helpers.crypto keeps it from landing in the store in the clear. Install the extra with pip install -U "egos-helpers[crypto]".

from egos_helpers.crypto import decode_encryption_key, decrypt_secret_text, encrypt_secret

key = decode_encryption_key(get_environ_value("MYAPP_SECRET_KEY"))
blob = encrypt_secret(key, "the-token")  # bytes, safe to store
token = decrypt_secret_text(key, blob)  # "the-token"

Each blob names the key that wrote it, so you can encrypt with a new key and keep the old one around for reads until nothing needs it. docs/crypto.md covers key replacement, binding a value to where it is stored, and the blob format.

Metadata

Release files for egos-helpers 1.4.18

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for egos-helpers 1.4.18
File Size Uploaded Provenance Actions
egos_helpers-1.4.18.tar.gz 17.5 kB Without provenance Details for egos_helpers-1.4.18.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for egos-helpers 1.4.18
File Interpreter ABI Platform Actions
egos_helpers-1.4.18-py3-none-any.whl Python 3 none any Details for egos_helpers-1.4.18-py3-none-any.whl

Total release size: 34.6 kB

Release files / egos_helpers-1.4.18.tar.gz

Download URL egos_helpers-1.4.18.tar.gz
Size 17.5 kB
Tags Source
SHA-256 checksum
How to use checksums
981622c79a56f3c76eff29246c3a4ef6dfbb3ddfc230b577b9a486440ed401db
BLAKE2b-256 checksum
How to use checksums
e7323ddbf2d9954b5cea93d6184bb0f8633a87c394583ebeebab139ebe7827b6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.3.3 CPython/3.14.10 Linux/7.0.0-43-generic

Release files / egos_helpers-1.4.18-py3-none-any.whl

Download URL egos_helpers-1.4.18-py3-none-any.whl
Size 17.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
03339b3c57bf0a1d3dc3fe009fe420040dade707073eb2d4d41bd811a123baef
BLAKE2b-256 checksum
How to use checksums
c2eeee49f18e01558aceb39d67cfdb289ba5bd13c67deb0ff99064b2bf6e083a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.3.3 CPython/3.14.10 Linux/7.0.0-43-generic

Security for egos-helpers 1.4.18

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.4.18 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page