Skip to main content

VariConfig is a Python package for managing configuration files in a variety of formats and is able to handle variable substitution.

Project description

VariConfig

VariConfig is a flexible and powerful Python package for managing configuration files. It supports both YAML and JSON formats and allows for the use of variables within the configuration files, using template placeholders like {{ var }}.

Features

  • Read Configurations: Load configurations from YAML or JSON files.
  • Template Resolution: Supports variables inside configuration files using template placeholders (e.g., {{ var }}).
  • Nested Configs: Handles nested dictionaries as attribute-based objects for easy access.
  • Dynamic Updates: Update configurations dynamically in the code.
  • Conversion: Convert configurations back to standard Python dictionaries.

Installation

To install VariConfig, use pip:

pip install variconfig

Usage

1. Loading a Configuration

You can load configuration files in YAML or JSON format:

from variconfig import ConfigDict

# Load a YAML file
config = ConfigDict.from_yaml('config.yml')

# Load a JSON file
config = ConfigDict.from_json('config.json')

2. Accessing Configuration Values

Once the configuration is loaded, values can be accessed as object attributes:

# Access configuration values
print(config.data_dir)  # Output: path from config.yml
print(config.logging_config.loggers.matgraphdb.level)  # Output: INFO

3. Template Resolution

Variables can be defined using {{ var }} placeholders in the configuration file. These will be resolved when the configuration is loaded:

root_dir: "."
data_dir: "{{ root_dir }}/data"
log_dir: "{{ root_dir }}/logs"

In the example above, data_dir will be resolved to ./data, and log_dir will be resolved to ./logs.

4. Updating Configurations

You can modify configuration values at runtime:

# Update a configuration value
config.logging_config.loggers.matgraphdb.level = 'DEBUG'

# Print updated value
print(config.logging_config.loggers.matgraphdb.level)  # Output: DEBUG

5. Converting to Dictionary

If you need to convert the configuration back to a standard Python dictionary:

config_dict = config.to_dict()
print(config_dict)

Example Configuration (YAML)

root_dir: "."
data_dir: "{{ root_dir }}/data"
log_dir: "{{ root_dir }}/logs"
db_name: 'VariConfig'

logging_config:
  version: 1
  disable_existing_loggers: False
  formatters:
    simple:
      format: '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
      datefmt: '%Y-%m-%d %H:%M:%S'
  handlers:
    console:
      class: logging.StreamHandler
      formatter: simple
      stream: ext://sys.stdout
    file:
      class: logging.FileHandler
      formatter: simple
      filename: "{{ log_dir }}/variconfig.log"
      mode: a
  loggers:
    variconfig:
      level: INFO
      handlers: [console]
      propagate: no

License

This project is licensed under the MIT License - see the LICENSE file for details.

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

variconfig-0.0.3.tar.gz (20.8 kB view details)

Uploaded Source

Built Distribution

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

variconfig-0.0.3-py3-none-any.whl (11.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: variconfig-0.0.3.tar.gz
  • Upload date:
  • Size: 20.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.20

File hashes

Hashes for variconfig-0.0.3.tar.gz
Algorithm Hash digest
SHA256 8da228d37e76bdcb8bbfe5acecedab078f456005596616349bbe5d6fcdad5789
MD5 aac43558c68b5bc2e09aacd0dbda41b7
BLAKE2b-256 9840123fba47a3d1b4253c42cabfd298113e924a7b2864d186eaae6f3b5d8046

See more details on using hashes here.

File details

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

File metadata

  • Download URL: variconfig-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 11.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.20

File hashes

Hashes for variconfig-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 aa2014b05f3623ff46eb13d9905b255ce546de1f0c64def43960136e2c52fb79
MD5 dfe502265a80024aeacb7a33c325626c
BLAKE2b-256 46c34c781403262a8159dfd415919cd6609713f6db7b55c8343b9afebd8a8555

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