Access and updates settings from a file as a class.
Project description
Simple-File-Settings
Sometimes, you just need to save and retain a few settings for your desktop program, like a theme preference, or last viewed directory. This is a library intended to easily load and save simple configuration data to and from disk through a type-checked data class.
Usage
First, a basic use case:
pip install simple-file-settings
import os
from simplefilesettings.json import JSONClass
class _Settings(JSONClass):
class Config:
json_file = os.path.join(os.path.expanduser("~"), "config.json")
mqtt_host: str = "mqtt"
mqtt_port: int = 1883
serial_port: str = "COM1"
serial_baud_rate: int = 115200
log_file_directory: str = os.path.join(os.getcwd(), "logs")
force_light_mode: bool = False
joystick_inverted: bool = False
max_moving_map_tracks: int = 5000
takeoff_height: float = 3
Settings = _Settings()
# this will attempt to load the value from the file on disk, or revert to the default
print(Settings.serial_port)
# this will save the change to the config file
Settings.serial_port = "/dev/tty1"
Inherit simplefilesettings.json.JSONClass
and add class attributes with
type hints and optionally default values. Attributes without type hints will not be
loaded or saved. Attributes starting with an underscore will cause an error.
If a default is not provided, None
is assumed.
from simplefilesettings.json import JSONClass
class _Settings(JSONClass):
name: str = "John" # valid
age = 26 # invalid
_height_cm: int # invalid
By default, a JSON file called settings.json
in the current working directory
is used. To change this, add a nested class called Config
with an attribute
json_file
. This accepts any path-like variable.
import os
from simplefilesettings.json import JSONClass
class _Settings(JSONClass):
class Config:
json_file = os.path.join(os.path.expanduser("~"), "config.json")
name: str = "John"
Data types not serializable for the file format (JSON, TOML, YAML, see below) are not supported.
By default, when any attribute is accessed, the configured file will be read.
If the file does not exist, the default value will be used.
If the file has a parse error, it will be deleted automatically.
To only read the file one time, set the Config
value always_read
to False
.
When any attribute has its value set, that will be written to the configured file.
from simplefilesettings.json import JSONClass
class _Settings(JSONClass):
name: str = "John"
Settings = _Settings()
print(Settings.name)
Settings.name = "Bob"
Running this twice will print John
the first time and Bob
the second time.
If JSON isn't your thing, TOML and YAML are available with the [toml]
and [yaml]
extras.
from simplefilesettings.toml import TOMLClass
from simplefilesettings.yaml import YAMLClass
class _TSettings(TOMLClass):
name: str = "John"
class Config:
toml_file = os.path.join(os.path.expanduser("~"), "config.toml")
class _YSettings(YAMLClass):
name: str = "John"
class Config:
yaml_file = os.path.join(os.path.expanduser("~"), "config.yaml")
Development
python -m pip install pipx --upgrade
pipx ensurepath
pipx install poetry
pipx install vscode-task-runner
# (Optionally) Add pre-commit plugin
poetry self add poetry-pre-commit-plugin
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
Hashes for simple_file_settings-0.1.2.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0317abca61569391016562755cbdc8da5dbfbe839689b8000d5bceb86a9ff47a |
|
MD5 | b0765678f49d40fe9b632a9ba6070c49 |
|
BLAKE2b-256 | 45a04f99c7e5e947aca65c212c8dce56a3e63db76ab2d6f95411b8b035be6645 |
Hashes for simple_file_settings-0.1.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 965882c05fa7ed8b2957de6b95b6b69984289d7d9a3b50103be092f79849309e |
|
MD5 | 56a004e6f5cf94fe384ee17abef5712b |
|
BLAKE2b-256 | f5f83061bb3bc2e27210d40af0a85501b804ec8b2e001e83415f76c65bc246f9 |