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

The .kv file format is a simple key-value configuration format with support for type validation and default values. Each line in a .kv file follows this syntax:

VARIABLENAME<TYPE>:DEFAULTVALUE
  • VARIABLENAME: The name of the variable.
  • TYPE: The expected type(s) of the variable. Multiple types can be separated by |.
  • DEFAULTVALUE: The default value for the variable. Use none if no default value is provided.
  • Comments: Both comments as a new line, or inline are supprorted, with the # character.

Example .kv file:

DATABASE_NAME<string>:none
DATABASE_PORT<int>:3306
ENABLE_LOGGING<bool>:true
MAX_CONNECTIONS<int|none>:none

KV Manifests

A KV manifest is a file that defines namespaces and their relationships. It is used to organize and manage configurations across multiple .kv files. Each line in a manifest file follows this syntax:

namespace1:namespace2
  • namespace1: The namespace that dosent exist as a file, but has the value as namespace2.
  • namespace2: The full namespace path

Example manifest file:

# A valid manifest
root:database
root:logging
database:connection

Validating a manifest:

You can validate a manifest using the KVManifestLoader:

from kvprocessor.kvmanifestloader import KVManifestLoader

manifest_path = "test/manifest.txt"
loader = KVManifestLoader(manifest_path)
loader.validate_manifest()  # Validates the manifest structure

Config.json

The config.json file is used by the KVStructLoader to define the structure and metadata of the configuration. It includes details such as the version, root namespace, and manifest file.

Example config.json:

Note: This example uses features from 0.1.10, the current version is 0.2.14+. Some extra parameters may be needed.

{
    "version": "0.1.10",
    "root": "root",
    "manifest": "manifest.txt",
    "platform": "github",
    "owner": "Voxa-Communications",
    "repo": "VoxaCommunicaitons-Structures",
    "branch": "main"
}

Using KVStructLoader with config.json:

from kvprocessor import KVStructLoader

kv_config_url = "https://github.com/Voxa-Communications/VoxaCommunicaitons-Structures/raw/refs/heads/main/struct/config.json"
kv_struct_loader = KVStructLoader(kv_config_url)
kv_processor = kv_struct_loader.from_namespace("root.database.connection")

Usage

KVProcessor

from kvprocessor import KVProcessor
from kvprocessor.kvenvloader import load_env

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 = load_env(kv_keys)  # Loads all the ENV variables that match those keys
validated_config = kv_processor.process_config(env_list)  # Verifies that those env variables exist and are of the correct type
print(validated_config)

KVStructLoader

from kvprocessor import KVStructLoader

kv_config_url = "https://github.com/Voxa-Communications/VoxaCommunicaitons-Structures/raw/refs/heads/main/struct/config.json"
kv_struct_loader = KVStructLoader(kv_config_url)
kv_processor = kv_struct_loader.from_namespace("root.database.connection")
user_settings = {
    "DATABASE_NAME": "test_db",
    "DATABASE_PORT": 5432,
}
validated_config = kv_processor.process_config(user_settings)
print(validated_config)

KVFileMerger

from kvprocessor import KVFileMerger

file1 = "test/file1.kv"
file2 = "test/file2.kv"
merger = KVFileMerger(file1, file2)
merged_file = merger.merge("merged.kv")  # Merges two KV files into a new file
print(f"Merged file created at: {merged_file}")

KVFileUtils

from kvprocessor.kvfileutils import search_kv_files, copy_kv_file, delete_kv_file

# Search for KV files in a directory
kv_files = search_kv_files("test")
print(f"Found KV files: {kv_files}")

# Copy a KV file
copy_kv_file("test/test.kv", "test/copy_test.kv")
print("KV file copied.")

# Delete a KV file
delete_kv_file("test/copy_test.kv")
print("KV file deleted.")

KVFileDiffChecker

from kvprocessor import KVFileDiffChecker

file1 = "test/file1.kv"
file2 = "test/file2.kv"
diff_checker = KVFileDiffChecker(file1, file2)
differences = diff_checker.diff()
print(f"Differences between files: {differences}")

KVValidator

from kvprocessor import validate_kv_file

kv_file_path = "test/test.kv"
is_valid = validate_kv_file(kv_file_path)
print(f"KV file is valid: {is_valid}")

Additional Data Types

The library supports additional data types such as datetime, date, time, and decimal. These can be used in .kv files as follows:

EVENT_DATE<datetime>:none
PRICE<decimal>:none

Example usage:

from kvprocessor import KVProcessor

kv_file_path = "test/test.kv"  # Path to your .kv file
kv_processor = KVProcessor(kv_file_path)

# Example configuration with additional data types
config = {
    "EVENT_DATE": "2025-05-01T12:00:00",
    "PRICE": "19.99",
}

validated_config = kv_processor.process_config(config)
print(validated_config)

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.

CLI

At the current moment, there exists no documentation on this. If you would like to find usage, visit the file kvprocessor\cli.py.

Basic Usage:

python kvprocessor/cli.py --version

Library Modules

For a complete list, visit kvprocessor\__init__.py. A breif list of main modules, will be listed here.

  • kvprocessor.kvprocessor, Exports: KVProcessor
  • kvprocessor.kvstructloader, Exports: KVStructLoader
  • kvprocessor.kvmanifestloader, Exports: KVManifestLoader

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 knowledge, 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.2.14.tar.gz (19.0 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.2.14-py3-none-any.whl (20.5 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for kvprocessor-0.2.14.tar.gz
Algorithm Hash digest
SHA256 a3f5f7955ad630f03001c789b125e66d8da3c5afcf2c9956e4fa1a9285eac032
MD5 a814f1339469ab0a97abdff57364694a
BLAKE2b-256 0cafbbc35f25fc7aaa0b755aa86b4559c4bc0838840aa8a6ce6c0e73da7429ac

See more details on using hashes here.

File details

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

File metadata

  • Download URL: kvprocessor-0.2.14-py3-none-any.whl
  • Upload date:
  • Size: 20.5 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.2.14-py3-none-any.whl
Algorithm Hash digest
SHA256 30e436a6adb690879bbd67176c47c2a667684746df35fe00c2db9f47c2aac69c
MD5 a8dd2365a5b34980fd33bcc75ff2cd00
BLAKE2b-256 fce4b0ba4aaa632afd995eaf5c9acbae4574c208c20e714a67285d1aa0f7d189

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