Easily load and save simple configuration data to and from disk through a type-checked data 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 need to serializable for the selected file format (JSON, TOML, YAML, see below). The following additional types are also supported:
datetime.datetimedatetime.datedatetime.timedatetime.timedeltaenum.Enumpathlib.Path
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 pure JSON isn't your thing, TOML, YAML, and JSON5 are available with the
[toml], [yaml], [json5] extras, respectively.
from simplefilesettings.toml import TOMLClass
from simplefilesettings.yaml import YAMLClass
from simplefilesettings.json5 import JSON5Class
class _TSettings(TOMLClass):
name: str = "Tom"
class Config:
toml_file = os.path.join(os.path.expanduser("~"), "config.toml")
class _YSettings(YAMLClass):
name: str = "Ingy"
class Config:
yaml_file = os.path.join(os.path.expanduser("~"), "config.yaml")
class _JSettings(JSON5Class):
name: str = "Douglas"
class Config:
json5_file = os.path.join(os.path.expanduser("~"), "config.jsonc")
Development
Use the provided devcontainer or run the following for local development:
# Install uv
# https://docs.astral.sh/uv/getting-started/installation/
uv tool install vscode-task-runner
vtr install
Project details
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 simple_file_settings-0.1.7.tar.gz.
File metadata
- Download URL: simple_file_settings-0.1.7.tar.gz
- Upload date:
- Size: 5.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a1a9d7bc3fc762313f12e0fcb29df8fada75c86fae2726bbf864e1036eddaf4d
|
|
| MD5 |
8e702fa1dbd5c3dbf61b71258094c706
|
|
| BLAKE2b-256 |
eaaae165bce0048a9ef95b91800a431ec9839b55b0ba7800b82f85cc97e326b4
|
Provenance
The following attestation bundles were made for simple_file_settings-0.1.7.tar.gz:
Publisher:
publish.yml on NathanVaughn/simple-file-settings
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
simple_file_settings-0.1.7.tar.gz -
Subject digest:
a1a9d7bc3fc762313f12e0fcb29df8fada75c86fae2726bbf864e1036eddaf4d - Sigstore transparency entry: 635330059
- Sigstore integration time:
-
Permalink:
NathanVaughn/simple-file-settings@8a0b4b56b17bce6a604c3779b4532d6f60926866 -
Branch / Tag:
refs/heads/release - Owner: https://github.com/NathanVaughn
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@8a0b4b56b17bce6a604c3779b4532d6f60926866 -
Trigger Event:
push
-
Statement type:
File details
Details for the file simple_file_settings-0.1.7-py3-none-any.whl.
File metadata
- Download URL: simple_file_settings-0.1.7-py3-none-any.whl
- Upload date:
- Size: 7.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e7902367a6c1387f38b0683040ef86dfbae5c8af5c48c7f633d9ab63e660e430
|
|
| MD5 |
1e84b227006cf42dba18431b78b19b65
|
|
| BLAKE2b-256 |
9c041fb75458e625d949cde047c4efbb89a714d4a04a1407073ee149c4f35dc8
|
Provenance
The following attestation bundles were made for simple_file_settings-0.1.7-py3-none-any.whl:
Publisher:
publish.yml on NathanVaughn/simple-file-settings
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
simple_file_settings-0.1.7-py3-none-any.whl -
Subject digest:
e7902367a6c1387f38b0683040ef86dfbae5c8af5c48c7f633d9ab63e660e430 - Sigstore transparency entry: 635330070
- Sigstore integration time:
-
Permalink:
NathanVaughn/simple-file-settings@8a0b4b56b17bce6a604c3779b4532d6f60926866 -
Branch / Tag:
refs/heads/release - Owner: https://github.com/NathanVaughn
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@8a0b4b56b17bce6a604c3779b4532d6f60926866 -
Trigger Event:
push
-
Statement type: