Skip to content

Commit

Permalink
feat: address review items
Browse files Browse the repository at this point in the history
  • Loading branch information
python357-1 committed Jan 13, 2025
1 parent 0607843 commit d49d95e
Show file tree
Hide file tree
Showing 10 changed files with 25 additions and 33 deletions.
4 changes: 2 additions & 2 deletions tagstudio/src/core/library/alchemy/library.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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
Expand Down Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion tagstudio/src/core/settings/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .tssettings import TSSettings
from .libsettings import LibSettings
from .tssettings import TSSettings

__all__ = ["TSSettings", "LibSettings"]
24 changes: 11 additions & 13 deletions tagstudio/src/core/settings/libsettings.py
Original file line number Diff line number Diff line change
@@ -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)
toml.dump(dict(self), settings_file)
9 changes: 2 additions & 7 deletions tagstudio/src/core/settings/tssettings.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from pathlib import Path
from typing import Any

import toml
from pydantic import BaseModel, Field
Expand All @@ -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)
Expand All @@ -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)
Expand Down
11 changes: 7 additions & 4 deletions tagstudio/src/core/tscacheddata.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
3 changes: 1 addition & 2 deletions tagstudio/src/qt/modals/file_extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -114,4 +113,4 @@ def save(self):
extensions.append(ext.text().strip().lstrip(".").lower())

# save preference
self.lib.settings.extension_list = extensions
self.lib.settings.extension_list = extensions
2 changes: 1 addition & 1 deletion tagstudio/src/qt/ts_qt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 0 additions & 1 deletion tagstudio/src/qt/widgets/migration_modal.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion tagstudio/tests/macros/test_refresh_dir.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion tagstudio/tests/test_json_migration.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
Expand Down

0 comments on commit d49d95e

Please sign in to comment.