Skip to main content

A Python package for processing and validating configuration dictionaries against a custom .kv file format

Project description

kvProcessor

PYPI Package
GitHub
A Python package for processing and validating configuration dictionaries against a custom .kv file format.

Installation

Install via pip:

pip install kvprocessor

File format

# Comments are defined by a "#"
VARIBLENAME<TYPE>:DEFAULTVAULE

Usage

from kvprocessor import LoadEnv, KVProcessor

kv_file_path = "test/test.kv" # Directory to .kv file
kv_processor = KVProcessor(kv_file_path) # Create a KV processor class
kv_keys = kv_processor.return_names() # Gets the keys (VARIBLENAME) from the .kv file
env_list = LoadEnv(kv_keys) # Loads all the ENV varibles that match those keys
validated_config = kv_processor.process_config(env_list) # Verifies that those env varibles exist and are of the correct type
print(validated_config)

This example mimics the one found in the /test directory. With the kv file of:

# This is a comment
DATABASE_NAME<string>:none
DATABASE_USER<string>:none
DATABASE_PASSWORD<string>:none
DATABASE_HOST<string>:none
DATABASE_PORT<string|int>:none
DATABASE_DRIVER<string>:mysql+mysqlconnector
DATABASE_DIALECT<string>:none

You should get a result of: {'DATABASE_NAME': None, 'DATABASE_USER': None, 'DATABASE_PASSWORD': None, 'DATABASE_HOST': None, 'DATABASE_PORT': None, 'DATABASE_DRIVER': None, 'DATABASE_DIALECT': None} This is because the kvProcessor is taking input from the env, and we dont have these env varibles defined. As a result these values default to the defined default value

Using "Namespaces"

This allows you to "import" kv files from a static host.

from kvprocessor import KVProcessor, KVStructLoader

kv_config_url = "https://github.com/Voxa-Communications/VoxaCommunicaitons-Structures/raw/refs/heads/main/struct/config.json" # STATIC url to json config
kv_struct_loader = KVStructLoader(kv_config_url) # Create a KVStructLoader object with the URL of the config file
kv_processor: KVProcessor = kv_struct_loader.from_namespace("voxa.api.user.user_settings") # Loads the KV file from the URL and returns a KVProcessor object
# For example this loads a file in /api/user/user_settings.kv
user_settings = {
    "2FA_ENABLED": True,
    "TELEMETRY": False,
    "AGE": "25",
    "LANGUAGE": "en",
} # Example Dict Structure
validated_config = kv_processor.process_config(user_settings)
print(validated_config)

For an example config.json navigate to test/config.json. This file is just what is found on https://github.com/Voxa-Communications/VoxaCommunicaitons-Structures/blob/main/struct/config.json which is used in this example.

Namespace's config.json

Namespace JSON files, have to be on a static host. They cannot be used locally. The easiest way to do this is to make a github repo, and use the raw file.

A config.json in a namespace should include at least two parts:

  • A "root", the name that preceedes the rest of the namespace. Ex: voxa in voxa.api.user.user_settings
  • A "URL". Ex: https://mysite.example/kvstructures, when the namespace mysite.folder.structure is used (assuming root is set to mysite), will fetch https://mysite.example/kvstructures/folder/structure.kv

Here is an example JSON (Note: on 0.7.1+ the URL is not needed, however a struct needs to be defined):

{
    "root": "voxa",
    "version": "0.1.5",
    "URL": "https://raw.githubusercontent.com/Voxa-Communications/VoxaCommunicaitons-Structures/refs/heads/main/struct/"
}

Building

For building the library locally
Requires: python3.8+, pip, linux system(if using the predefined shell files)

  1. git clone https://github.com/connor33341/kvProcessor.git
  2. cd kvProcessor
  3. bash build.sh

build.sh will also install kvProcessor as a local package, which you will be able to use. If you add new features to your fork, and would like them to be featured on the main repo. Submit a Pull Request

For the nerds

The syntax was already mentioned, however if you would like to see how it parses. The following regex is used to determine the: name, type, and default:

(\w+)<([\w\|]+)>:([\w+]+|none)

With this knowlege, you probably can figure out a way to write .kv files in a weird way. Out of typical standard.

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

kvprocessor-0.1.9.tar.gz (7.8 kB view details)

Uploaded Source

Built Distribution

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

kvprocessor-0.1.9-py3-none-any.whl (8.7 kB view details)

Uploaded Python 3

File details

Details for the file kvprocessor-0.1.9.tar.gz.

File metadata

  • Download URL: kvprocessor-0.1.9.tar.gz
  • Upload date:
  • Size: 7.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for kvprocessor-0.1.9.tar.gz
Algorithm Hash digest
SHA256 2fc3c943b4978c7d0f6bbabfc7f2f47ebbb2e3e740e0051439112ae41014b9ee
MD5 7fe8757e7b2aa62bc113a3c3b89592b4
BLAKE2b-256 c6db14b6f1add8f1b29b8e6e8c86c529f6baea87a41d72ff56f3fff33bdfa67f

See more details on using hashes here.

File details

Details for the file kvprocessor-0.1.9-py3-none-any.whl.

File metadata

  • Download URL: kvprocessor-0.1.9-py3-none-any.whl
  • Upload date:
  • Size: 8.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for kvprocessor-0.1.9-py3-none-any.whl
Algorithm Hash digest
SHA256 19da90183d5fd0e2d9c0afcad2a545d2dc560ee0fa285f5cfe6272fb2cdfea1b
MD5 a125c08f2dbe959549f70aeeab9b2576
BLAKE2b-256 1a9ac587a9601174d6b682196e5b121b16175a1f1e10ac75368c861895a6ae01

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