Configuration definition, parsing, and validation, in the style of argparse.
Project description
Configuration definition, parsing, and validation, in the style of argparse.
ComfyParse provides a simple, declarative interface for defining the structure, parsing, and validation of your project’s configuration files. Its interface design and functionality are intentionally similar to and inspired by that of the argparse standard library. It provides built-in as well as custom validation, hooks for converting settings values from text, and automatic help documentation generation. Parsed configurations are returned as a dictionary-like namespace object which mirrors the defined schema and supports both attribute as well as index-based access.
>>> import comfyparse >>> parser = comfyparse.ComfyParser() >>> parser.add_setting("myint", required=True, convert=int) >>> parser.add_setting("mystr", required=False, default="abc") >>> parser.parse_from_string("myint=10; mystr='foo';") Namespace{"myint": 10, "mystr": "foo"} >>> parser.parse_from_string("myint=10;") Namespace{"myint": 10, "mystr": "abc"}
ComfyParse uses its own syntax inspired by the syntax used by Nginx for its configurations, resulting in a minimal but high expressive grammar. Like argparse, ComfyParse automatically validates the configuration and can be directed to convert the settings values from strings, returning a dictionary-like namespace object mirroring the structure of the configuration.
group servers { hosts = [ # end of line comments work "node01", "node02" ] % multiple flavors too timeout = 30.0 } log_path=/var/log/my.log
The configuration language consists of statements and blocks of statements. Statements assign a string or list value to a setting name, and can either be terminated with a newline or a ;. Lists can be nested within lists, and string values can use ' and " as quotes for values or settings names containing literal quotes, escape characters, or whitespace, or just for visual differentiation.
Blocks have a type or “kind”, and uses braces to encapsulate a set of statements. Blocks can also contain sub-blocks, defined using the ConfigBlock.add_block() method. Blocks can also be defined to expect an additional name parameter to differentiate between multiple blocks of the same kind such as the group block named servers in the above example. Block kinds and names can optionally be quoted if needed or desired.
Comments can be made using # or % characters which treats the rest of the line as a comment. Either comment marker can be used interchangably within the same document. Aside from newlines ending a comment or terminating a statement, all whitespace is ignored and arbitrary.
The example below sets up ComfyParse to handle a document such as the example above.
parser = comfyparse.ComfyParser("demoapp") parser.add_setting("log_path", desc="The path where logs go.", required=True) parser.add_setting("log_level", default=1, choices=[1,2,3,4,5], convert=int) group_block = parser.add_block("group", named=True, required=True) group_block.add_setting("hosts", required=True) group_block.add_setting("timeout", default=5.0, convert=float)
Once a schema such as this has been defined, configuration strings can be parsed as follows
# Directly from a string config = parser.parse_config_string(confstr) # Alternatively, from a file path config = parser.parse_config_path("/path/to/file")
Lastly, the parser can generate a reStructuredText string outlining the schema for automatic documentation purposes, or just to provide a starting point when writing your own documentation.
parser.generate_docs()
Installation
ComfyParse can be installed using PyPI and requires no external dependencies.
> pip install comfyparse
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file comfyparse-0.0.2.tar.gz
.
File metadata
- Download URL: comfyparse-0.0.2.tar.gz
- Upload date:
- Size: 12.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.0 CPython/3.9.14
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f03b760408f3f99a631ce762a9562ceeea4a61455418581fd52c5e36ac6607eb |
|
MD5 | 992313970681062a3a2fca23603fdc61 |
|
BLAKE2b-256 | 283e30cbbdf91fd5409b7fbb496dbec465190c31c20db3ebc4c3a698778bd820 |
File details
Details for the file comfyparse-0.0.2-py2.py3-none-any.whl
.
File metadata
- Download URL: comfyparse-0.0.2-py2.py3-none-any.whl
- Upload date:
- Size: 12.0 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.0 CPython/3.9.14
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 80e657403a580031008a9162dbd2eea93a26f7558ce977860dec6f5470d75a11 |
|
MD5 | 3a6162eebf2a85856018a2ef2aa880da |
|
BLAKE2b-256 | 0e5215e7b658981e69ca49498d7dd22684fae663da47f452ded63163d120340a |