Skip to content

Commit 1a2fe01

Browse files
committed
Initial release 1.0.0
1 parent 31e4392 commit 1a2fe01

29 files changed

+1202
-3
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ coverage.xml
5050
.hypothesis/
5151
.pytest_cache/
5252
cover/
53+
*config/
5354

5455
# Translations
5556
*.mo
@@ -123,7 +124,7 @@ celerybeat.pid
123124
.env
124125
.venv
125126
env/
126-
venv/
127+
venv*/
127128
ENV/
128129
env.bak/
129130
venv.bak/

README.md

+65-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,65 @@
1-
# Simple-TOML-Configurator
2-
A versatile Python library designed to streamline the handling and organization of configuration settings across various types of applications. This library facilitates the management of configuration values through a user-friendly interface and stores these settings in TOML file format for easy readability and accessibility.
1+
# Simple TOML Configurator
2+
3+
The **Simple TOML Configurator** is a versatile Python library designed to streamline the handling and organization of configuration settings across various types of applications. This library facilitates the management of configuration values through a user-friendly interface and stores these settings in TOML file format for easy readability and accessibility.
4+
5+
## Features
6+
7+
1. **Effortless Configuration Management:** The heart of the library is the `Configuration` class, which simplifies the management of configuration settings. It provides intuitive methods to load, update, and store configurations, ensuring a smooth experience for developers.
8+
9+
2. **Universal Applicability:** The **Simple TOML Configurator** is designed to work seamlessly with any type of Python application, ranging from web frameworks like Flask, Django, and FastAPI to standalone scripts and command-line utilities.
10+
11+
3. **TOML File Storage:** Configuration settings are stored in TOML files, a popular human-readable format. This enables developers to review, modify, and track configuration changes easily.
12+
13+
4. **Attribute-Based Access:** Accessing configuration values is straightforward, thanks to the attribute-based approach. Settings can be accessed and updated as attributes, making it convenient for both reading and modifying values.
14+
15+
5. **Updating Configurations:** The library enables the updating of configuration settings from a dictionary, ensuring that the changes are accurately reflected both in-memory and in the stored TOML file.
16+
17+
6. **Default Values:** Developers can define default values for various configuration sections and keys. The library automatically incorporates new values and manages the removal of outdated ones.
18+
19+
7. **Customization Capabilities:** The `Configuration` class can be extended and customized to cater to application-specific requirements. Developers can implement custom logic with getters and setters to handle unique settings or scenarios.
20+
21+
## Usage Example
22+
23+
```python
24+
from simple_toml_configurator import Configuration
25+
26+
# Define default configuration values
27+
default_config = {
28+
"app": {
29+
"ip": "0.0.0.0",
30+
"host": "",
31+
"port": 5000,
32+
"upload_folder": "uploads"
33+
}
34+
"mysql": {
35+
"user": "root",
36+
"password": "root"
37+
"databases": {
38+
"prod": "db1",
39+
"dev": "db2"
40+
}
41+
}
42+
}
43+
44+
# Initialize the Simple TOML Configurator
45+
settings = Configuration()
46+
settings.init_config("config", default_config, "app_config")
47+
# Stores an app_config.toml file in the `config` folder at the current working directory.
48+
49+
# Access and update configuration values
50+
print(settings.app_ip) # Output: '0.0.0.0'
51+
settings.update_config({"app_ip": "1.2.3.4"})
52+
print(settings.app_ip) # Output: '1.2.3.4'
53+
54+
# Access all settings as a dictionary
55+
all_settings = settings.get_settings()
56+
print(all_settings)
57+
# Output: {'app_ip': '1.2.3.4', 'app_host': '', 'app_port': 5000, 'app_upload_folder': 'uploads'}
58+
59+
# Modify values directly in the config dictionary
60+
settings.config["mysql"]["databases"]["prod"] = "db3"
61+
settings.update()
62+
print(settings.mysql_databases["prod"]) # Output: 'db3'
63+
```
64+
65+
The **Simple TOML Configurator** empowers developers to efficiently manage configuration settings across a wide range of Python applications. Whether you're building a web application, a command-line tool, or a standalone script, this library provides the tools you need to maintain and access configuration values with ease and clarity.

changelog.md

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Changelog
2+
3+
All notable changes to this project will be documented in this file.
4+
5+
The format is based on [Keep a Changelog],
6+
and this project adheres to [Semantic Versioning].
7+
8+
## [Unreleased]
9+
10+
- /
11+
12+
## [1.0.0] - 2023-08-27
13+
14+
- initial release
15+
16+
<!-- Links -->
17+
[keep a changelog]: https://keepachangelog.com/en/1.0.0/
18+
[semantic versioning]: https://semver.org/spec/v2.0.0.html
19+
20+
<!-- Versions -->
21+
[unreleased]: https://github.com/gilbn/simple-toml-configurator/compare/v0.0.2...HEAD
22+
[1.0.0]: https://github.com/gilbn/simple-toml-configurator/releases/tag/v0.0.1

docs/configurator.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Simple TOML Configurator Docs
2+
::: src.simple_toml_configurator.toml_configurator

docs/css/code_select.css

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.language-pycon .gp, .language-pycon .go { /* Generic.Prompt, Generic.Output */
2+
-webkit-user-select: none;
3+
user-select: none;
4+
}

docs/examples.md

Whitespace-only changes.

docs/exceptions.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Exceptions
2+
::: src.simple_toml_configurator.exceptions

docs/flask-custom-example.md

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Custom Flask Example
2+
3+
The `Configuration` class can be extended and customized to cater to application-specific requirements. Developers can implement custom logic with getters and setters to handle unique settings or scenarios.
4+
5+
## Folder contents
6+
7+
```
8+
├── __init__.py
9+
├── app.py
10+
├── utils.py
11+
└── extenstions
12+
└── config.py
13+
```
14+
15+
This example uses a custom Configuration class in the config.py module that inherits from `Configuration`.
16+
17+
??? example "CustomConfiguration"
18+
```python
19+
class CustomConfiguration(Configuration):
20+
def __init__(self):
21+
super().__init__()
22+
23+
@property
24+
def logging_debug(self):
25+
return getattr(self, "_logging_debug")
26+
27+
@logging_debug.setter
28+
def logging_debug(self, value: bool):
29+
if not isinstance(value, bool):
30+
raise ValueError(f"value must be of type bool not {type(value)}")
31+
self._logging_debug = value
32+
log_level = "DEBUG" if value else "INFO"
33+
configure_logging(log_level)
34+
```
35+
36+
The custom class uses a property with a setter that executes the `configure_logging` function from `utils.py` whenever the logging_debug attribute is updated. Thus setting the log level to "DEBUG" if the value is True.
37+
38+
This will change the log level on you Flask app without restarting it.
39+
40+
### Code examples
41+
42+
??? example "extensions/config.py"
43+
```python title="config.py"
44+
--8<-- "examples/custom/extensions/config.py"
45+
```
46+
47+
??? example "utils.py"
48+
```python title="utils.py"
49+
--8<-- "examples/custom/utils.py"
50+
```
51+
52+
!!! example "app.py"
53+
```python title="app.py"
54+
--8<-- "examples/custom/app.py"
55+
```

docs/flask-simple-example.md

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Simple Flask Example
2+
3+
## Folder contents
4+
5+
```
6+
├── __init__.py
7+
├── app.py
8+
└── extenstions
9+
└── config.py
10+
```
11+
12+
This is a simple example using flask showing how to update the TOML configuration
13+
14+
### Code examples
15+
16+
??? example "extensions/config.py"
17+
```python title="config.py"
18+
--8<-- "examples/standard/extensions/config.py"
19+
```
20+
21+
!!! example "app.py"
22+
```python title="app.py"
23+
--8<-- "examples/standard/app.py"
24+
```

docs/index.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
--8<-- "README.md"

docs/installation.md

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Installation
2+
3+
To use the Simple TOML Configurator in your Python projects, you can install it using the `pip` package manager. The package is available on PyPI (Python Package Index).
4+
5+
## Prerequisites
6+
7+
Before installing, make sure you have Python and `pip` installed on your system. You can check if `pip` is installed by running:
8+
9+
```bash
10+
pip --version
11+
```
12+
13+
If `pip` is not installed, follow the [official `pip` installation guide](https://pip.pypa.io/en/stable/installing/) to get it set up.
14+
15+
## Installation Steps
16+
17+
To install the Simple TOML Configurator package, open your terminal or command prompt and run the following command:
18+
19+
```bash
20+
pip install simple-toml-configurator
21+
```
22+
23+
This command will download and install the package along with its dependencies. After the installation is complete, you can start using the package in your Python projects.
24+
25+
See [Usage](/usage-examples) or [Examples](flask-simple-example)or for more information on how to use the package.

docs/requirements.txt

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
mkdocs>=1.4.0
2+
mkdocstrings[python]>=0.19.0
3+
mkdocs-material>=8.5.6
4+
mkdocs-section-index>=0.3.4
5+
mkdocs-redirects>=1.0.1
6+
mkdocs-git-revision-date-localized-plugin
7+
mkdocs-minify-plugin
8+
pyspelling
9+
mkdocs-include-markdown-plugin>=4.0.4
10+
mkdocs-awesome-pages-plugin>=2.4.0
11+
mkdocs-macros-plugin>=0.4.18
12+
pymdown-extensions>=3.3.2
13+
mike

docs/usage-examples.md

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
## Usage Examples
2+
3+
### Initializing Configuration
4+
5+
To get started with the Simple TOML Configurator, you need to initialize the configuration settings using the `init_config` method. This sets up the default values and configuration file paths.
6+
7+
```python
8+
from simple_toml_configurator import Configuration
9+
10+
# Define default configuration values
11+
default_config = {
12+
"app": {
13+
"ip": "0.0.0.0",
14+
"host": "",
15+
"port": 5000,
16+
"upload_folder": "uploads"
17+
}
18+
}
19+
20+
# Initialize the Simple TOML Configurator
21+
settings = Configuration()
22+
settings.init_config("config", default_config, "app_config")
23+
```
24+
25+
### Accessing Configuration Values
26+
27+
You can access configuration values as attributes of the `settings` instance. This attribute-based access makes it straightforward to retrieve settings.
28+
29+
```python
30+
# Access configuration values
31+
ip_address = settings.app_ip
32+
port_number = settings.app_port
33+
upload_folder = settings.app_upload_folder
34+
```
35+
36+
### Updating Configuration Settings
37+
38+
Updating configuration settings is seamless with the Simple TOML Configurator. Use the `update_config` method to modify values while ensuring consistency across instances.
39+
40+
```python
41+
# Update a configuration value
42+
settings.update_config({"app_ip": "1.2.3.4"})
43+
```
44+
45+
### Accessing All Settings
46+
47+
Retrieve all configuration settings as a dictionary using the `get_settings` method. This provides an overview of all configured values.
48+
49+
```python
50+
# Get all configuration settings
51+
all_settings = settings.get_settings()
52+
```
53+
54+
### Direct Configuration Modification
55+
56+
You can directly modify configuration values within the `config` dictionary. After making changes, use the `update` method to write the updated configuration to the file.
57+
58+
```python
59+
# Modify values directly and update configuration
60+
settings.config["app"]["ip"] = "0.0.0.0"
61+
settings.update()
62+
```
63+
64+
### Customization with Inheritance
65+
66+
For advanced use cases, you can create a custom configuration class by inheriting from `Configuration`. This allows you to add custom logic and properties tailored to your application.
67+
68+
```python
69+
from simple_toml_configurator import Configuration
70+
71+
class CustomConfiguration(Configuration):
72+
def __init__(self):
73+
super().__init__()
74+
75+
# Add custom properties with getters and setters
76+
@property
77+
def custom_setting(self):
78+
return getattr(self, "_custom_setting")
79+
80+
@custom_setting.setter
81+
def custom_setting(self, value):
82+
# Custom logic for updating custom_setting
83+
self._custom_setting = value
84+
# Additional actions can be performed here
85+
86+
# Initialize and use the custom configuration
87+
custom_settings = CustomConfiguration()
88+
custom_settings.init_config("config", default_config, "custom_config")
89+
```

examples/custom/__init__.py

Whitespace-only changes.

examples/custom/app.py

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
2+
from flask import Flask, jsonify, request, url_for,redirect
3+
from extensions.config import settings
4+
import logging
5+
6+
logger = logging.getLogger(__name__)
7+
8+
def create_app():
9+
app = Flask(__name__)
10+
app.config["SECRET_KEY"] = settings.config.get("app").get("flask_secret_key")
11+
app.config['APP_PORT'] = settings.config.get("app").get("port")
12+
app.config['APP_IP'] = settings.config.get("app").get("ip")
13+
app.config['APP_HOST'] = settings.config.get("app").get("host")
14+
app.config["DEBUG"] = settings.config.get("app").get("debug")
15+
return app
16+
17+
app = create_app()
18+
19+
# simple route that returns the config
20+
@app.route("/")
21+
def app_settings():
22+
return jsonify(
23+
{
24+
"response": {
25+
"data": {
26+
"configuration": settings.get_settings(),
27+
"toml_config": settings.config,
28+
}
29+
}
30+
})
31+
32+
# Update settings route
33+
@app.route("/update", methods=["POST"])
34+
def update_settings():
35+
"""Update settings route"""
36+
data = request.get_json() # {"logging_debug": True, "app_debug": True}
37+
settings.update_config(data)
38+
return redirect(url_for("app_settings"))
39+
40+
# Get settings value route
41+
@app.route("/logger", methods=["GET"])
42+
def get_settings():
43+
"""Sets logging_debug to True or False"""
44+
value = False if settings.logging_debug else True
45+
settings.update_config({"logging_debug": value})
46+
return jsonify({"debug_logging": settings.logging_debug})
47+
48+
if __name__ == "__main__":
49+
app.run(port=app.config.get("APP_PORT"), host=app.config.get("APP_IP"), debug=app.config.get("APP_DEBUG"))

0 commit comments

Comments
 (0)