Skip to content

Commit

Permalink
Merge pull request #1 from NathanVaughn/dependabot/pip/main/all-9d643…
Browse files Browse the repository at this point in the history
…f9ad5

Bump the all group with 2 updates
  • Loading branch information
NathanVaughn authored Mar 28, 2024
2 parents 16b092f + 4535ad7 commit 69cda09
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 25 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
strategy:
fail-fast: false
matrix:
python_version: ["3.12", "3.11", "3.10", "3.9", "3.8"]
python_version: ["3.12", "3.11", "3.10", "3.9"]

steps:
- name: Checkout Code
Expand Down
59 changes: 51 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,53 @@
# Simple-File-Settings

This is a package intended to easily load and save simple configuration data
transparently through a class.
[![Black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)
[![pre-commit](https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit)](https://github.com/pre-commit/pre-commit)
[![GitHub license](https://img.shields.io/github/license/NathanVaughn/simple-file-settings)](https://github.com/NathanVaughn/simple-file-settings/blob/main/LICENSE)
[![PyPi versions](https://img.shields.io/pypi/pyversions/simple-file-settings)](https://pypi.org/project/simple-file-settings)
[![PyPi downloads](https://img.shields.io/pypi/dm/simple-file-settings)](https://pypi.org/project/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:

```bash
pip install simple-file-settings
```

```python
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 default values. Attributes without type hints will not be
loaded or saved. Attributes without a default value, or starting with an
Expand Down Expand Up @@ -34,12 +77,12 @@ class _Settings(JSONClass):
name: str = "John"
```

Nested classes are not supported.
Data types not JSON serializable (objects, datetimes, etc.) 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 is not valid JSON,
it will be deleted automatically. To only read the file one time, set the `Config`
value `always_read` to `False`.
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 is not valid JSON, 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.

Expand Down Expand Up @@ -83,7 +126,7 @@ class _YSettings(YAMLClass):
python -m pip install pipx --upgrade
pipx ensurepath
pipx install poetry
pipx install vscode-task-runner
pipx install simple-file-settings
# (Optionally) Add pre-commit plugin
poetry self add poetry-pre-commit-plugin
```
26 changes: 13 additions & 13 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
tomli = { version = "^2.0.1", python = "<3.11", optional = true } # not needed in 3.11

[tool.poetry.group.dev.dependencies]
pytest = "^7.4.3"
pytest-cov = "^4.1.0"
pytest = "^8.1.1"
pytest-cov = "^5.0.0"
pre-commit = "^3.7.0"

[tool.poetry.extras]
Expand Down
2 changes: 1 addition & 1 deletion simplefilesettings/toml.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import tomli_w

try:
import tomllib
import tomllib # type: ignore
except ImportError: # pragma: no cover
import tomli as tomllib # type: ignore

Expand Down

0 comments on commit 69cda09

Please sign in to comment.