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.10.tar.gz (8.1 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.10-py3-none-any.whl (8.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: kvprocessor-0.1.10.tar.gz
  • Upload date:
  • Size: 8.1 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.10.tar.gz
Algorithm Hash digest
SHA256 aa433a0c4c01c6f4950d428382e1bacb5ea5fe4fa2afb2681fe0201f6c08735b
MD5 5ce75b9521578d0ff3527231de7615f1
BLAKE2b-256 877eba1919cd84b5d0d17799cbf1eae4f97a432f6610983029200d24aab8ea2e

See more details on using hashes here.

File details

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

File metadata

  • Download URL: kvprocessor-0.1.10-py3-none-any.whl
  • Upload date:
  • Size: 8.9 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.10-py3-none-any.whl
Algorithm Hash digest
SHA256 01f6c5515b06b3490f4da239a3a4cd740f505e30d6cb0eddf0eba91d76315c70
MD5 f67ac3942481ef7822255e6afe1ae7e1
BLAKE2b-256 1c1b6e00a8b5f15eeecf17ecf397122966af2d24da7d9dfbb658e61d23ce9ad1

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