Embedded configuration and automation language library
Project description
ConfigLang
An embedded configuration and automation language library written in pure C99 with Python bindings. ConfigLang provides a simple, readable syntax for configuration files with support for variables, conditionals, and type safety.
Features
- Pure C99 - No external dependencies
- Python Bindings - Easy integration with Python projects
- Type System - Integer and string types with type checking
- Const Variables - Immutable configuration values
- Conditionals - If/else logic for dynamic configuration
- Variable References - Copy values between variables
- Multiline Strings - Support for complex text values
- Comments - Single-line comments with
# - Save/Load - Persist configuration state to files
Language Syntax
Variables
Set integer or string variables:
set x = 10
set name = "Hello World"
set count = 42
Const Variables
Create immutable variables:
const set max_users = 100
const set app_name = "MyApp"
Attempting to modify a const variable will raise an error.
Variable References
Copy values from one variable to another:
set original = 42
set copy = original
set name = "Config"
set backup = name
Conditionals
Basic if statement:
set value = 60
if value > 50 { set value = 50 }
If-else statement:
set score = 5
if score > 10 { set score = 10 } { set score = 90 }
Nested conditionals:
set x = 55
if x > 50 { set x = 50 } if x < 10 { set x = 10 } { set x = 20 }
Comparison Operators
All standard comparison operators are supported:
>- Greater than<- Less than>=- Greater than or equal<=- Less than or equal==- Equal to!=- Not equal to
Example:
set x = 10
if x >= 10 { set ready = 1 }
if x != 0 { set active = 1 }
Multiline Strings
For complex text content, use multiline delimiters:
set config = #%%%
line 1
line 2
line 3
%%%#
Comments
Single-line comments start with #:
# This is a comment
set x = 10 # Another comment
Installation
C Library
Compile the C library:
# Linux/macOS
gcc -shared -fPIC -std=c99 -o libconfiglang.so configlang.c
# macOS (alternative)
gcc -shared -fPIC -std=c99 -o libconfiglang.dylib configlang.c
# Windows
gcc -shared -o configlang.dll configlang.c
Compile the test program:
gcc -std=c99 -o test test.c configlang.c
./test
Python Package
Install from source:
pip install .
Or install in development mode:
pip install -e .
Usage Examples
C API
#include "configlang.h"
#include <stdio.h>
int main(void) {
// Create instance
ConfigLang* cfg = cfg_create();
// Load configuration
const char* code =
"set port = 8080\n"
"set host = \"localhost\"\n"
"const set max_connections = 100\n";
cfg_load_string(cfg, code);
// Get values
int port;
const char* host;
cfg_get_int(cfg, "port", &port);
cfg_get_string(cfg, "host", &host);
printf("Server: %s:%d\n", host, port);
// Modify value
cfg_set_int(cfg, "port", 9000);
// Save configuration
cfg_save_file(cfg, "server.cfg");
// Cleanup
cfg_destroy(cfg);
return 0;
}
Python API
from configlang import ConfigLang
# Create instance
cfg = ConfigLang()
# Load configuration
code = """
set port = 8080
set host = "localhost"
const set max_connections = 100
if port < 1024 { set port = 8080 }
"""
cfg.load_string(code)
# Get values
print(f"Port: {cfg.get_int('port')}")
print(f"Host: {cfg.get_string('host')}")
# Modify value
cfg.set_int('port', 9000)
# Dictionary-style access
cfg['port'] = 3000
print(f"Port: {cfg['port']}")
# Save configuration
cfg.save_file('server.cfg')
Context Manager (Python)
with ConfigLang() as cfg:
cfg.load_string('set x = 10\nset name = "Test"')
print(cfg.get_int('x'))
# Automatically cleaned up
API Reference
C Functions
| Function | Description |
|---|---|
cfg_create() |
Create a new ConfigLang instance |
cfg_destroy(cfg) |
Destroy instance and free resources |
cfg_load_file(cfg, path) |
Load configuration from file |
cfg_load_string(cfg, code) |
Load configuration from string |
cfg_get_int(cfg, name, out) |
Get integer variable value |
cfg_get_string(cfg, name, out) |
Get string variable value |
cfg_set_int(cfg, name, value) |
Set integer variable value |
cfg_save_file(cfg, path) |
Save configuration to file |
cfg_get_error(cfg) |
Get last error message |
Python Methods
| Method | Description |
|---|---|
ConfigLang() |
Create new instance |
load_file(path) |
Load configuration from file |
load_string(code) |
Load configuration from string |
get_int(name) |
Get integer variable value |
get_string(name) |
Get string variable value |
set_int(name, value) |
Set integer variable value |
save_file(path) |
Save configuration to file |
get_error() |
Get last error message |
get(name) |
Auto-detect type and get value |
Error Codes
| Code | Constant | Description |
|---|---|---|
| 0 | ERR_CFG_OK |
Success |
| -1 | ERR_CFG_NULL_POINTER |
NULL pointer passed |
| -2 | ERR_CFG_FILE_ERROR |
File I/O error |
| -3 | ERR_CFG_PARSE_ERROR |
Syntax error in configuration |
| -4 | ERR_CFG_VARIABLE_NOT_FOUND |
Variable doesn't exist |
| -5 | ERR_CFG_CONST_VIOLATION |
Attempted to modify const variable |
| -6 | ERR_CFG_OUT_OF_MEMORY |
Memory allocation failed |
| -7 | ERR_CFG_TYPE_MISMATCH |
Variable type mismatch |
| -8 | ERR_CFG_UNKNOWN_ERROR |
Unknown error |
Python Exceptions
ConfigLangError- Base exception classParseError- Syntax error in configurationVariableNotFoundError- Variable doesn't existConstViolationError- Attempted to modify const variableTypeMismatchError- Variable type mismatch
Example Configuration File
# Application Configuration
const set app_name = "MyApplication"
const set version = "1.0.0"
# Server Settings
set host = "0.0.0.0"
set port = 8080
set debug = 0
# Apply production settings
if debug == 0 {
set port = 80
set workers = 4
}
# Apply development settings
if debug == 1 {
set port = 8080
set workers = 1
}
# Database Configuration
set db_host = "localhost"
set db_port = 5432
set db_name = "myapp"
# Limits
const set max_connections = 100
const set timeout = 30
# Multiline description
set description = #%%%
This is a multi-line
application description
that spans several lines.
%%%#
Language Grammar
program → statement*
statement → comment | set_stmt | if_stmt
comment → '#' text '\n'
set_stmt → 'const'? 'set' identifier '=' value
if_stmt → 'if' condition '{' statement* '}' else_block?
else_block → 'if' condition '{' statement* '}' else_block?
| '{' statement* '}'
condition → identifier operator value
operator → '>' | '<' | '>=' | '<=' | '==' | '!='
value → integer | string | identifier | multiline
integer → [0-9]+
string → '"' [^"]* '"'
multiline → '#%%%' .* '%%%#'
identifier → [a-zA-Z_][a-zA-Z0-9_]*
Type System
ConfigLang has a simple type system with two types:
- Integer - Signed integers (e.g.,
42,-10,0) - String - Text values (e.g.,
"Hello","Config")
Types are checked at runtime. Attempting to get or set a variable with the wrong type will result in a TYPE_MISMATCH error.
Limitations
- No floating-point numbers (integers only)
- No arithmetic operations (use for configuration, not computation)
- No loops (conditionals only)
- No functions or procedures
- No arrays or complex data structures
- String operations are limited to assignment
Use Cases
ConfigLang is designed for:
- Application configuration files
- Build system configuration
- Environment-specific settings
- Feature flags and toggles
- Embedded system configuration
- Game settings and preferences
- Simple automation scripts
License
MIT License - See LICENSE file for details
Author
hejhdiss
Links
- GitHub: https://github.com/hejhdiss/configlang
- Issues: https://github.com/hejhdiss/configlang/issues
Contributing
Contributions are welcome! Please feel free to submit pull requests or open issues for bugs and feature requests.
Changelog
Version 1.0.0
- Initial release
- C library implementation
- Python bindings
- Full test suite
- Documentation
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file configlang-1.0.1.tar.gz.
File metadata
- Download URL: configlang-1.0.1.tar.gz
- Upload date:
- Size: 112.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
66088ed6dc2f18b1a1bcb63beb77d1a476d6906add9bbc2879d6c0d49d5a1034
|
|
| MD5 |
3ba5d41f0737a58cdd1e3021d5e42299
|
|
| BLAKE2b-256 |
46c39636eb744bdbaafc7f9b2b0acf81dd3d6d1990a1a2974345ad7da307f40a
|
File details
Details for the file configlang-1.0.1-py3-none-any.whl.
File metadata
- Download URL: configlang-1.0.1-py3-none-any.whl
- Upload date:
- Size: 108.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
390cfd908a902ade348f37456c1d55436f7d541c1503faf8b57c3b3b7b920a1b
|
|
| MD5 |
69afdde65ef3b4a6ecd70baf871b584e
|
|
| BLAKE2b-256 |
f78ff1ad19583e6a87d80662653eedff8a70abc646f51758042433237a442b2a
|