From d49d95e908375921d1e70afecb76590488e50e97 Mon Sep 17 00:00:00 2001 From: python357-1 Date: Mon, 13 Jan 2025 01:06:17 -0600 Subject: [PATCH] feat: address review items --- tagstudio/src/core/library/alchemy/library.py | 4 ++-- tagstudio/src/core/settings/__init__.py | 2 +- tagstudio/src/core/settings/libsettings.py | 24 +++++++++---------- tagstudio/src/core/settings/tssettings.py | 9 ++----- tagstudio/src/core/tscacheddata.py | 11 +++++---- tagstudio/src/qt/modals/file_extension.py | 3 +-- tagstudio/src/qt/ts_qt.py | 2 +- tagstudio/src/qt/widgets/migration_modal.py | 1 - tagstudio/tests/macros/test_refresh_dir.py | 1 - tagstudio/tests/test_json_migration.py | 1 - 10 files changed, 25 insertions(+), 33 deletions(-) diff --git a/tagstudio/src/core/library/alchemy/library.py b/tagstudio/src/core/library/alchemy/library.py index 69dc6ecdb..0418a669d 100644 --- a/tagstudio/src/core/library/alchemy/library.py +++ b/tagstudio/src/core/library/alchemy/library.py @@ -41,6 +41,7 @@ TS_FOLDER_NAME, ) from ...enums import LibraryPrefs +from ...settings import LibSettings from .db import make_tables from .enums import FieldTypeEnum, FilterState, TagColor from .fields import ( @@ -50,7 +51,6 @@ TextField, _FieldID, ) -from ...settings import LibSettings from .joins import TagField, TagSubtag from .models import Entry, Folder, Preferences, Tag, TagAlias, ValueType from .visitors import SQLBoolExpressionBuilder @@ -202,7 +202,7 @@ def migrate_json_to_sqlite(self, json_lib: JsonLibrary): ) # Preferences - self.settings.extension_list = [ x.strip(".") for x in json_lib.ext_list] + self.settings.extension_list = [x.strip(".") for x in json_lib.ext_list] self.settings.is_exclude_list = json_lib.is_exclude_list end_time = time.time() diff --git a/tagstudio/src/core/settings/__init__.py b/tagstudio/src/core/settings/__init__.py index ea387ea10..d72c42eaa 100644 --- a/tagstudio/src/core/settings/__init__.py +++ b/tagstudio/src/core/settings/__init__.py @@ -1,4 +1,4 @@ -from .tssettings import TSSettings from .libsettings import LibSettings +from .tssettings import TSSettings __all__ = ["TSSettings", "LibSettings"] diff --git a/tagstudio/src/core/settings/libsettings.py b/tagstudio/src/core/settings/libsettings.py index 9686a54a1..ffad59889 100644 --- a/tagstudio/src/core/settings/libsettings.py +++ b/tagstudio/src/core/settings/libsettings.py @@ -1,40 +1,38 @@ from pathlib import Path -from pydantic import BaseModel, Field -import toml + import structlog +import toml +from pydantic import BaseModel, Field logger = structlog.get_logger(__name__) + class LibSettings(BaseModel): is_exclude_list: bool = Field(default=True) - extension_list: list[str] = Field(default = [".json", ".xmp", ".aae"]) + extension_list: list[str] = Field(default=[".json", ".xmp", ".aae"]) page_size: int = Field(default=500) db_version: int = Field(default=2) filename: str = Field(default="") @staticmethod def open(path_value: Path | str) -> "LibSettings": - path: Path - if not isinstance(path_value, Path): - path = Path(path_value) - else: - path = path_value + path: Path = Path(path_value) if not isinstance(path_value, Path) else path_value if path.exists(): - with open(path, "r") as settings_file: + with open(path) as settings_file: filecontents = settings_file.read() if len(filecontents.strip()) != 0: settings_data = toml.loads(filecontents) settings_data["filename"] = str(path) return LibSettings(**settings_data) - - #either settings file did not exist or was empty - either way, use default settings + + # either settings file did not exist or was empty - either way, use default settings settings = LibSettings(**dict(filename=str(path))) return settings - + def save(self): if not (parent_path := Path(self.filename).parent).exists(): parent_path.mkdir() with open(self.filename, "w") as settings_file: - toml.dump(dict(self), settings_file) \ No newline at end of file + toml.dump(dict(self), settings_file) diff --git a/tagstudio/src/core/settings/tssettings.py b/tagstudio/src/core/settings/tssettings.py index fdb7d0c78..074f0c7a5 100644 --- a/tagstudio/src/core/settings/tssettings.py +++ b/tagstudio/src/core/settings/tssettings.py @@ -1,5 +1,4 @@ from pathlib import Path -from typing import Any import toml from pydantic import BaseModel, Field @@ -24,7 +23,7 @@ class TSSettings(BaseModel): def read_settings(path: Path | str) -> "TSSettings": path_value = Path(path) if path_value.exists(): - with open(path, "r") as file: + with open(path) as file: filecontents = file.read() if len(filecontents.strip()) != 0: settings_data = toml.loads(filecontents) @@ -34,11 +33,7 @@ def read_settings(path: Path | str) -> "TSSettings": return TSSettings(**dict(filename=str(path))) def save(self, path: Path | str | None = None) -> None: - path_value: Path - if isinstance(path, str): - path_value = Path(path) - else: - path_value = Path(self.filename) + path_value: Path = Path(path) if isinstance(path, str) else Path(self.filename) if not path_value.parent.exists(): path_value.parent.mkdir(parents=True, exist_ok=True) diff --git a/tagstudio/src/core/tscacheddata.py b/tagstudio/src/core/tscacheddata.py index c8efd3e7d..23da27378 100644 --- a/tagstudio/src/core/tscacheddata.py +++ b/tagstudio/src/core/tscacheddata.py @@ -29,18 +29,21 @@ def open(path_value: Path | str | None = None) -> "TSCachedData": elif isinstance(path_value, Path): path = path_value else: - logger.info("no cache location was specified, using ", default_cache_location=default_cache_location) + logger.info( + "no cache location was specified, using ", + default_cache_location=default_cache_location, + ) path = default_cache_location - + if path.exists(): - with open(path, "r") as cache_file: + with open(path) as cache_file: filecontents = cache_file.read() if len(filecontents.strip()) != 0: cache_data = toml.loads(filecontents) cache_data["path"] = str(path) logger.info("opening cache file at ", cache_location=path) return TSCachedData(**cache_data) - + return TSCachedData(**dict(path=str(default_cache_location))) def save(self): diff --git a/tagstudio/src/qt/modals/file_extension.py b/tagstudio/src/qt/modals/file_extension.py index 59e6a6de7..aa7a504a9 100644 --- a/tagstudio/src/qt/modals/file_extension.py +++ b/tagstudio/src/qt/modals/file_extension.py @@ -16,7 +16,6 @@ QVBoxLayout, QWidget, ) -from src.core.enums import LibraryPrefs from src.core.library import Library from src.qt.translations import Translations from src.qt.widgets.panel import PanelWidget @@ -114,4 +113,4 @@ def save(self): extensions.append(ext.text().strip().lstrip(".").lower()) # save preference - self.lib.settings.extension_list = extensions \ No newline at end of file + self.lib.settings.extension_list = extensions diff --git a/tagstudio/src/qt/ts_qt.py b/tagstudio/src/qt/ts_qt.py index a498201d5..0f7b2c511 100644 --- a/tagstudio/src/qt/ts_qt.py +++ b/tagstudio/src/qt/ts_qt.py @@ -67,7 +67,7 @@ VERSION_BRANCH, ) from src.core.driver import DriverMixin -from src.core.enums import LibraryPrefs, MacroID +from src.core.enums import MacroID from src.core.library.alchemy import Library from src.core.library.alchemy.enums import ( FieldTypeEnum, diff --git a/tagstudio/src/qt/widgets/migration_modal.py b/tagstudio/src/qt/widgets/migration_modal.py index 27b3f8c6c..b42c0aeb9 100644 --- a/tagstudio/src/qt/widgets/migration_modal.py +++ b/tagstudio/src/qt/widgets/migration_modal.py @@ -20,7 +20,6 @@ from sqlalchemy import and_, select from sqlalchemy.orm import Session from src.core.constants import TS_FOLDER_NAME -from src.core.enums import LibraryPrefs from src.core.library.alchemy.enums import FieldTypeEnum, TagColor from src.core.library.alchemy.fields import TagBoxField, _FieldID from src.core.library.alchemy.joins import TagField, TagSubtag diff --git a/tagstudio/tests/macros/test_refresh_dir.py b/tagstudio/tests/macros/test_refresh_dir.py index 89012cd72..4326d6f82 100644 --- a/tagstudio/tests/macros/test_refresh_dir.py +++ b/tagstudio/tests/macros/test_refresh_dir.py @@ -2,7 +2,6 @@ from tempfile import TemporaryDirectory import pytest -from src.core.enums import LibraryPrefs from src.core.utils.refresh_dir import RefreshDirTracker CWD = pathlib.Path(__file__).parent diff --git a/tagstudio/tests/test_json_migration.py b/tagstudio/tests/test_json_migration.py index 23488753f..330b07742 100644 --- a/tagstudio/tests/test_json_migration.py +++ b/tagstudio/tests/test_json_migration.py @@ -5,7 +5,6 @@ import pathlib from time import time -from src.core.enums import LibraryPrefs from src.qt.widgets.migration_modal import JsonMigrationModal CWD = pathlib.Path(__file__)