A CLI tool to generate type-hinted Python config classes from .ini files with automatic file watching and hot reloading capabilities.
- 🔧 Auto-generate type-hinted Python classes from INI configuration files
- 🔍 Intelligent type inference (int, float, boolean, string)
- 🔄 Hot reloading - automatically reload configuration when files change
- 🛡️ Sensitive data protection - passwords and API keys are only masked when using print_all()
- 🎯 Smart path detection - automatically find config files in common locations
- 💡 IDE-friendly - full autocomplete and type hints support
- 🏗️ Singleton pattern - ensure single configuration instance across your app
pip install ini2py[system]
mode = development
debug = true
port = 8080
timeout = 30.5
[database]
host = localhost
port = 5432
name = myapp
user = admin
password = secret123
[redis]
host = 127.0.0.1
port = 6379
db = 0
[ai_service]
api_key = sk-1234567890abcdef
model = gpt-4
temperature = 0.7Run the CLI tool in your project directory:
ini2pyThe tool will:
- Auto-detect your
config.inifile - Suggest an appropriate output directory
- Generate type-hinted Python classes
Example output:
Path to your config.ini file [./config/config.ini]:
Path to the output directory for generated files [./src/config]:
Reading configuration from: ./config/config.ini
Generating schema.py...
Successfully generated ./src/config/schema.py
Generating manager.py...
Successfully generated ./src/config/manager.py
Configuration generation complete!
from src.config.manager import ConfigManager
# Initialize configuration manager (singleton pattern)
config = ConfigManager()
# Access configuration with full type hints and autocomplete
mode = config.system.mode # str
debug = config.system.debug # bool
port = config.system.port # int
timeout = config.system.timeout # float
# Access sensitive data normally
api_key = config.ai_service.api_key # Returns actual value
password = config.database.password # Returns actual value
# Use print_all() to safely display configuration with masked sensitive data
print("Database Config:")
print(config.database.print_all(return_type='list'))
# Output: ['host: localhost', 'port: 5432', 'password: se****23', ...]
# Or get properties without masking (be careful!)
print(config.database.return_properties(return_type='list', mask_sensitive=False))# Specify custom paths
ini2py --config /path/to/config.ini --output /path/to/output/The generated config manager automatically watches for file changes:
import time
from src.config.manager import ConfigManager
config = ConfigManager()
# The config will automatically reload when config.ini is modified
while True:
print(f"Current port: {config.system.port}")
time.sleep(2)By default, sensitive data (passwords, API keys, etc.) is accessible normally. Use the print_all() method to safely display configuration with automatic masking.
Sensitive keywords detected:
password,pwdapi_token,tokensecret,keyappkey
# Normal access returns actual values
password = config.database.password # "secret123"
api_key = config.ai_service.api_key # "sk-1234567890abcdef"
# Use print_all() for safe display
config.database.print_all() # password shown as "se****23"
config.ai_service.print_all('dict') # api_key shown as "sk-12**************ef"
# Manual control with return_properties
raw = config.database.return_properties(mask_sensitive=False) # Actual values
masked = config.database.return_properties(mask_sensitive=True) # Masked valuesini2py automatically searches for configuration files in these locations:
./config.ini./config/config.ini./conf/config.ini../config.ini(up to 5 levels up)
src/config/
├── schema.py # Type-hinted schema classes
└── manager.py # Configuration manager with hot reloading
Each INI section becomes a typed schema class:
class SystemSchema(ConfigSchema):
"""[system]"""
@property
def mode(self) -> str:
return self._config_section.get('mode')
@property
def debug(self) -> bool:
return self._config_section.getboolean('debug')
@property
def port(self) -> int:
return self._config_section.getint('port')ini2py --helpOptions:
--config, -c: Path to input config.ini file--output, -o: Directory for generated files--help: Show help message
- Python 3.8+
- click >= 8.0
- watchdog >= 2.1.6
git clone https://github.com/joneshong/ini2py.git
cd ini2py
pip install -e .python -m pytest tests/ini2py/
├── ini2py/
│ ├── __init__.py
│ ├── cli.py # Main CLI logic
│ └── templates/ # Code generation templates
│ ├── schema.py.tpl
│ └── manager.py.tpl
├── tests/
├── examples/
├── README.md
└── pyproject.toml
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- Updated README documentation to clarify sensitive data handling
- Added Chinese (Traditional) documentation (README.zh.md)
- Added
print_all()method for safe display with automatic sensitive data masking - Fixed CI/CD issues for cross-platform compatibility
- Improved code formatting with Black and isort
- Initial release
- Basic INI to Python class generation
- Type inference support
- Hot reloading with watchdog
- Sensitive data protection
- Smart path detection
JonesHong - GitHub
If you encounter any issues or have questions:
- Open an issue
- Check the examples directory
- Review the generated code documentation