Skip to main content

Configuration manager for medium-sized projects (created for personal use)

Project description

Static Config Class

Configuration manager for medium-sized projects. Maintains a configuration inside a static class. Intended for personal use so breaking changes might be introduced at any point.

installation

The package can simply be installed with pip

pip install static-config

usage

To use the configuration manager you have to set it up first by giving it your configuration.

Your configuration has to be provided via a path to a JSON file

{
  "key": "value",
  "bool_key": true,
  "int_key": 1,
  "float_key": 3.14159,
  "list_key": ["one", "two", "three"],
  "dict_key": {
    "key": "value"
  }
}

data/tests/config.json

The setup function then is called like this

from static_config_class import Config
from pathlib import Path

config_json = Path.cwd() / 'data/tests/config.json'
Config.setup(config_json)

If configuration access is attempted before setup, a SetupFirstError is raised.

reading/writing the configuration

Ater setup you can use the Methods Config.get() and Config.set() to gain read and write access on the configuration.

...
# read the value of configuration key 'key'
some_config_data = Config.get('key')

# write 'value' to configuration key 'key'
Config.set('key', 'value')

using a json schema

Optionally, you can provide a JSON schema to validate the configuration during setup and value changes.

The schema is provided as path to a JSON schema

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "title": "Test Config",
  "description": "An example config to test this package",
  "type": "object",

  "properties": {
    "key": {
      "type": "string"
    },
    "bool_key": {
      "type": "boolean"
    },
    "int_key": {
      "type": "integer"
    },
    "float_key": {
      "type": "number"
    },
    "list_key": {
      "type": "array"
    },
    "dict_key": {
      "type": "object"
    }
  },

  "additionalProperties": false,
  "required": ["key", "bool_key"]
}

data/tests/schema.json

Setup with schema is straight forward. The schema is used to initially validate the config during setup and consecutively each time the config is changed. Raises a ConfigValidationError if the config is not conforming to the schema.

from static_config_class import Config
from pathlib import Path

config_json = Path.cwd() / 'data/tests/config.json'
schema_json = Path.cwd() / 'data/tests/schema.json'
Config.setup(config_json)

Using a schema allows to allow/prohibit the later extensions of known configuration keys and specification of keys that require a value during setup. Furthermore, the type keyword can be used to enforce runtime typechecking.

immutable values

During setup, you can pass a Collection containing all the configuration keys whose values are not allowed to be changed.

from static_config_class import Config
from pathlib import Path

config_json = Path.cwd() / 'data/tests/config.json'
Config.setup(config_json, immutable=['key', 'bool_key'])

If the Config.set() method is used to update these, an ImmutableError is raised.

persisting the configuration

If you changed your configuration during runtime and want to persist it as json file you can use the Config.write() function.

Config.write('out/updated_configuration.json')

identifying the configuration

If you need to have a short reference of the current configuration state, you can calculate the configurations MD5-hash with the Config.identifier() function.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

static_config_class-0.0.3.tar.gz (9.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

static_config_class-0.0.3-py3-none-any.whl (7.3 kB view details)

Uploaded Python 3

File details

Details for the file static_config_class-0.0.3.tar.gz.

File metadata

  • Download URL: static_config_class-0.0.3.tar.gz
  • Upload date:
  • Size: 9.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.0

File hashes

Hashes for static_config_class-0.0.3.tar.gz
Algorithm Hash digest
SHA256 cb01673f4f7473064305f390ab00e87924ecf188ccd21ed925453a37149cb3b1
MD5 70890f09b79e7a8d91fd888b1518b7d4
BLAKE2b-256 bfc378048d5896d198bce6b8821902dd503bba7d67fb008be4160ad927fbd15c

See more details on using hashes here.

File details

Details for the file static_config_class-0.0.3-py3-none-any.whl.

File metadata

File hashes

Hashes for static_config_class-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 514a7c6614759e6de49b143a06b3cbee79dd2c739007d9d559292d6231575812
MD5 db9615e0512eb58a5a9231def46a2e6e
BLAKE2b-256 e82a89b570cc25227fd053384638aad66860fbe8a840a5f4a6c06dbb10810793

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page