-
Notifications
You must be signed in to change notification settings - Fork 380
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
__all__ = ["tssettings"] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
from pathlib import Path | ||
|
||
import toml | ||
from pydantic import BaseModel, Field | ||
Check failure on line 4 in tagstudio/src/core/settings/tssettings.py GitHub Actions / Run MyPy
|
||
|
||
|
||
# NOTE: pydantic also has a BaseSettings class (from pydantic-settings) that allows any settings | ||
# properties to be overwritten with environment variables. as tagstudio is not currently using | ||
# environment variables, i did not base it on that, but that may be useful in the future. | ||
class TSSettings(BaseModel): | ||
dark_mode: bool = Field(default=False) | ||
language: str = Field(default="en-US") | ||
|
||
@staticmethod | ||
def read_settings(path: Path | str, **kwargs) -> "TSSettings": | ||
# library = kwargs.get("library") | ||
settings_data: dict[str, any] = dict() | ||
Check failure on line 17 in tagstudio/src/core/settings/tssettings.py GitHub Actions / Run MyPy
|
||
if path.exists(): | ||
Check failure on line 18 in tagstudio/src/core/settings/tssettings.py GitHub Actions / Run MyPy
|
||
with open(path, "rb").read() as filecontents: | ||
Check failure on line 19 in tagstudio/src/core/settings/tssettings.py GitHub Actions / Run MyPy
Check failure on line 19 in tagstudio/src/core/settings/tssettings.py GitHub Actions / Run MyPy
|
||
if len(filecontents.strip()) != 0: | ||
settings_data = toml.loads(filecontents.decode("utf-8")) | ||
|
||
# if library: #TODO: add library-specific settings | ||
# lib_settings_path = Path(library.folder / "settings.toml") | ||
# lib_settings_data: dict[str, any] | ||
# if lib_settings_path.exists: | ||
# with open(lib_settings_path, "rb") as filedata: | ||
# lib_settings_data = tomllib.load(filedata) | ||
# lib_settings = TSSettings(**lib_settings_data) | ||
|
||
return TSSettings(**settings_data) | ||
|
||
def to_dict(self) -> dict[str, any]: | ||
Check failure on line 33 in tagstudio/src/core/settings/tssettings.py GitHub Actions / Run MyPy
|
||
d = dict[str, any]() | ||
Check failure on line 34 in tagstudio/src/core/settings/tssettings.py GitHub Actions / Run MyPy
|
||
for prop_name, prop_value in self: | ||
d[prop_name] = prop_value | ||
|
||
return d | ||
|
||
def save(self, path: Path | str) -> None: | ||
with open(path, "w") as f: | ||
toml.dump(self.to_dict(), f) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import copy | ||
|
||
from PySide6.QtWidgets import ( | ||
QCheckBox, | ||
QComboBox, | ||
QHBoxLayout, | ||
QLabel, | ||
QVBoxLayout, | ||
) | ||
from src.core.settings import TSSettings | ||
Check failure on line 10 in tagstudio/src/qt/modals/settings_modal.py GitHub Actions / Run MyPy
|
||
from src.qt.widgets.panel import PanelWidget | ||
|
||
|
||
class SettingsModal(PanelWidget): | ||
def __init__(self, settings: TSSettings): | ||
super().__init__() | ||
self.tempSettings = copy.deepcopy(settings) | ||
|
||
self.main = QVBoxLayout(self) | ||
|
||
# --- | ||
self.darkMode_Label = QLabel() | ||
self.darkMode_Value = QCheckBox() | ||
self.darkMode_Row = QHBoxLayout() | ||
self.darkMode_Row.addWidget(self.darkMode_Label) | ||
self.darkMode_Row.addWidget(self.darkMode_Value) | ||
|
||
self.darkMode_Label.setText("Dark Mode") | ||
self.darkMode_Value.setChecked(self.tempSettings.dark_mode) | ||
|
||
self.darkMode_Value.stateChanged.connect( | ||
lambda state: self.set_property("dark_mode", bool(state)) | ||
) | ||
|
||
# --- | ||
self.language_Label = QLabel() | ||
self.language_Value = QComboBox() | ||
self.language_Row = QHBoxLayout() | ||
self.language_Row.addWidget(self.language_Label) | ||
self.language_Row.addWidget(self.language_Value) | ||
|
||
self.language_Label.setText("Language") | ||
language_list = [ # TODO: put this somewhere else | ||
"en-US", | ||
"en-GB", | ||
"es-MX", | ||
# etc... | ||
] | ||
self.language_Value.addItems(language_list) | ||
self.language_Value.setCurrentIndex(language_list.index(self.tempSettings.language)) | ||
self.language_Value.currentTextChanged.connect( | ||
lambda text: self.set_property("language", text) | ||
) | ||
|
||
# --- | ||
self.main.addLayout(self.darkMode_Row) | ||
self.main.addLayout(self.language_Row) | ||
|
||
def set_property(self, prop_name: str, value: any) -> None: | ||
Check failure on line 59 in tagstudio/src/qt/modals/settings_modal.py GitHub Actions / Run MyPy
|
||
setattr(self.tempSettings, prop_name, value) | ||
|
||
def get_content(self) -> TSSettings: | ||
return self.tempSettings |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
dark_mode = true | ||
language = "es-MX" |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import pathlib | ||
|
||
from src.core.settings.tssettings import TSSettings | ||
|
||
CWD = pathlib.Path(__file__) | ||
|
||
|
||
def test_read_settings(): | ||
settings = TSSettings.read_settings(CWD.parent / "example_settings.toml") | ||
assert settings.dark_mode | ||
assert settings.language == "es-MX" |