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.8.tar.gz (7.3 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.8-py3-none-any.whl (7.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: kvprocessor-0.1.8.tar.gz
  • Upload date:
  • Size: 7.3 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.8.tar.gz
Algorithm Hash digest
SHA256 553e7081659b03ae23eadc301d0ddbfdf2b3cf31011622c4e8ab4c70d0dc00c3
MD5 0d437bcd3568d620dcfc20c25b6190ef
BLAKE2b-256 0d1197ba36a8a30c85535f9343a82adde93f438e86e38c27d899a328e9a040b8

See more details on using hashes here.

File details

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

File metadata

  • Download URL: kvprocessor-0.1.8-py3-none-any.whl
  • Upload date:
  • Size: 7.8 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.8-py3-none-any.whl
Algorithm Hash digest
SHA256 01a2ea34041f31e7db63b1c05b48b5b837a0e2f3874243c12a91eb2078986c1f
MD5 d4c01e417598bb7abc1a5b2e58d3738d
BLAKE2b-256 d64542d23ad7e7f7e8ed4128c98d7f10d58500842e05320594b1ef40d5ea7602

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