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
Metadata
Release files for comfyparse 0.0.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| comfyparse-0.0.2.tar.gz | 12.0 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| comfyparse-0.0.2-py2.py3-none-any.whl | Python 2, Python 3 | none | any | Details |
Total release size: 24.1 kB
Release files / comfyparse-0.0.2.tar.gz
| Download URL | comfyparse-0.0.2.tar.gz |
|---|---|
| Size | 12.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
f03b760408f3f99a631ce762a9562ceeea4a61455418581fd52c5e36ac6607eb
|
|
BLAKE2b-256 checksum How to use checksums |
283e30cbbdf91fd5409b7fbb496dbec465190c31c20db3ebc4c3a698778bd820
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.0 CPython/3.9.14
|
Release files / comfyparse-0.0.2-py2.py3-none-any.whl
| Download URL | comfyparse-0.0.2-py2.py3-none-any.whl |
|---|---|
| Size | 12.0 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
80e657403a580031008a9162dbd2eea93a26f7558ce977860dec6f5470d75a11
|
|
BLAKE2b-256 checksum How to use checksums |
0e5215e7b658981e69ca49498d7dd22684fae663da47f452ded63163d120340a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.0 CPython/3.9.14
|
Security for comfyparse 0.0.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.