INI file parser and writer for Mojo - Python configparser compatible
Parse and write INI configuration files in native Mojo with zero Python dependencies. Compatible with Python's configparser module for drop-in replacement in Mojo projects.
- ✅ Python
configparserCompatible - Drop-in replacement for Python's standard library - ✅ Parser & Writer - Both read and write INI files
- ✅ Classic INI Support - Standard
key = valuesyntax with[sections] - ✅ Extended Features - Multiline values, inline comments, value interpolation
- ✅ Multiple Dialects - configparser (default) and classic INI modes
- ✅ Comprehensive Tests - Full test coverage
- ✅ Zero Dependencies - Pure Mojo implementation
Status: ✅ v0.2.0 Released - Production ready with comprehensive testing and benchmarks
Parsing INI Files:
from ini import parse
var config = parse("""
[Database]
host = localhost
port = 5432
user = admin
[Server]
debug = true
timeout = 30
""")
# Access values
print(config["Database"]["host"]) # localhost
print(config["Database"]["port"]) # 5432
print(config["Server"]["debug"]) # trueWriting INI Files:
from ini import to_ini
var data = Dict[String, Dict[String, String]]()
data["App"] = Dict[String, String]()
data["App"]["name"] = "MyApp"
data["App"]["version"] = "1.0"
var ini_text = to_ini(data)
print(ini_text)
# Output:
# [App]
# name = MyApp
# version = 1.0See examples/read_example.mojo and examples/write_example.mojo for complete working examples.
magic add mojo-inigit submodule add https://github.com/databooth/mojo-ini vendor/mojo-iniThen in your Mojo code:
mojo -I vendor/mojo-ini/src your_app.mojocp -r mojo-ini/src/ini your-project/lib/inifrom ini import parse
var config = parse("""
[DEFAULT]
base_url = https://example.com
[API]
endpoint = /api/v1
timeout = 30
""")
print(config["API"]["endpoint"]) # /api/v1
print(config["API"]["timeout"]) # 30from ini import parse_file, write_file
# Read from file
var config = parse_file("config.ini")
# Modify
config["Server"]["port"] = "8080"
# Write back
write_file("config.ini", config)See examples/file_io_example.mojo for a complete example.
Indented lines are treated as continuations (Python configparser behavior):
from ini import parse
var config = parse("""
[Database]
connection_string = postgresql://host:5432/db
?sslmode=require
&timeout=10
""")
# Result: connection_string contains all three lines with newlines
print(config["Database"]["connection_string"])
# postgresql://host:5432/db
# ?sslmode=require
# &timeout=10Important: Lines starting with whitespace are ALWAYS treated as continuations, not as new keys. This matches Python configparser behavior.
⚠️ Coming in v0.3.0 - ConfigParser API with type converters planned for future release.
For now, all values are strings. Manual conversion:
from ini import parse
var config = parse("...")
# Manual type conversion
var port = int(config["Server"]["port"]) # Int
var debug = config["Server"]["debug"] == "true" # Bool
var timeout = float64(config["API"]["timeout"]) # Float64[section]headerskey = valuepairs# commentsand; comments- Multiline values (indented continuation)
- Inline comments
[DEFAULT]section for shared values- Value interpolation:
%(var)sreferences - Type conversion helpers (getint, getboolean, etc.)
- Case-insensitive section/key names (optional)
By design, keys with leading whitespace are treated as multiline value continuations:
# ❌ This does NOT create a key named "indented_key"
[Section]
key1 = value1
indented_key = value2 # This becomes part of key1's value!This matches Python configparser behavior. If you need indented keys, consider:
- TOML: Native support for nested tables (mojo-toml)
- YAML: Indentation-based structure
- Remove indentation: Keep all keys at column 0
Git config files use tabs before keys, which mojo-ini treats as continuations. To use Git configs:
# Convert tabs to standard format
sed 's/^\t//' .git/config > config.iniOr manually remove leading tabs from keys.
mojo-ini aims for high compatibility with Python's configparser:
| Feature | Python configparser | mojo-ini v0.2 |
|---|---|---|
| Basic key=value | ✅ | ✅ |
| [Sections] | ✅ | ✅ |
| [DEFAULT] | ✅ | 🚧 Planned |
| Multiline values | ✅ | ✅ |
| Inline comments | ✅ | ✅ |
| Value interpolation | ✅ | 🚧 Planned |
| Type converters | ✅ | |
| Case insensitive | ✅ | 🚧 Planned |
# Install pixi (if not already installed)
curl -fsSL https://pixi.sh/install.sh | bash
# Install dependencies
pixi install
# Verify Mojo version
pixi run mojo-version# Run all tests
pixi run test-all
# Run individual test suites
pixi run test-lexer
pixi run test-parser
pixi run test-writer
pixi run test-configparser
# Build package
pixi run build-package# Benchmark mojo-ini performance
pixi run benchmark-mojo
# Compare with Python configparser
pixi run benchmark-pythonSee ROADMAP.md for detailed development timeline.
- ✅ Basic INI parsing (sections, key=value)
- ✅ INI writer
- ✅ Comments support (# and ;)
- ✅ Multiline values
- ✅ Comprehensive test suite (46 tests)
- ✅ Performance benchmarks with statistical reporting
- ✅ File I/O helpers (parse_file, write_file)
- 🚧 [DEFAULT] section support
- 🚧 Value interpolation %(var)s
- 🚧 configparser API compatibility
- 🚧 Type converters (getint, getboolean, etc.)
- 🚧 Case-insensitive mode
- 🚧 Git config format support
- 🚧 Advanced interpolation
- CHANGELOG.md - Version history and changes
- docs/planning/ - Technical documentation and design docs
- examples/ - Usage examples
- mojo-toml - TOML 1.0 parser/writer for modern configs
- mojo-dotenv - Environment variable management
Together these provide comprehensive configuration file support for Mojo! 🎯
Contributions welcome! Please:
- Follow existing code style (see mojo-toml for reference)
- Add tests for new features
- Update documentation
- Use Australian English for docs, US spelling for code
MIT License - see LICENSE file for details
Made with 🔥 by DataBooth