Skip to content

Commit

Permalink
Merge pull request #299 from bohning/fixes
Browse files Browse the repository at this point in the history
Fixes
  • Loading branch information
bohning authored Oct 18, 2024
2 parents 10b046f + 0b10d67 commit aa43691
Show file tree
Hide file tree
Showing 12 changed files with 766 additions and 688 deletions.
1,406 changes: 737 additions & 669 deletions poetry.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ lxml = "*"
mutagen = "*"
pdfme = "*"
pillow = ">=10"
PySide6 = "==6.7.*"
PySide6 = "6.7.3"
requests = "*"
send2trash = "*"
unidecode = "*"
Expand Down
2 changes: 1 addition & 1 deletion src/usdb_syncer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def encode(self) -> str:
def decode(cls, value: str) -> SyncMetaId | None:
try:
number = base64.urlsafe_b64decode(f"{value}=")
except binascii.Error:
except (binascii.Error, ValueError):
return None
return cls.from_bytes(number, "big", signed=True)

Expand Down
4 changes: 3 additions & 1 deletion src/usdb_syncer/db/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,9 @@ def connect(cls, db_path: Path | str, trace: bool = False) -> None:
if cls._local.connection:
raise errors.DatabaseError("Already connected to database!")
cls._local.connection = sqlite3.connect(
db_path, check_same_thread=False, isolation_level=None
db_path, check_same_thread=False, isolation_level=None, timeout=20
)
_logger.debug(f"Connected to database at '{db_path}'.")
if trace:
cls._local.connection.set_trace_callback(_logger.debug)
_validate_schema(cls._local.connection)
Expand All @@ -73,6 +74,7 @@ def close(cls) -> None:
if _DbState._local.connection is not None:
_DbState._local.connection.close()
_DbState._local.connection = None
_logger.debug("Closed database connection.")


@contextlib.contextmanager
Expand Down
3 changes: 3 additions & 0 deletions src/usdb_syncer/gui/mw.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,11 +287,14 @@ def on_done(result: progress.Result) -> None:
self._save_state()
db.close()
self._cleaned_up = True
_logger.debug("Closing after cleanup.")
self.close()

if self._cleaned_up:
_logger.debug("Accepting close event.")
event.accept()
else:
_logger.debug("Close event deferred, cleaning up ...")
run_with_progress("Shutting down ...", DownloadManager.quit, on_done)
event.ignore()

Expand Down
3 changes: 2 additions & 1 deletion src/usdb_syncer/gui/search_tree/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ def rowCount(self, parent: QIndex = QModelIndex()) -> int:
item = cast(TreeItem, parent.internalPointer())
return len(item.children)

def columnCount(self, _parent: QIndex = QModelIndex()) -> int:
# pylint: disable=unused-argument
def columnCount(self, parent: QIndex = QModelIndex()) -> int:
return 1

def index(
Expand Down
2 changes: 1 addition & 1 deletion src/usdb_syncer/gui/settings_dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def _load_settings(self) -> None:
def _setup_path_template(self) -> None:
self.edit_path_template.textChanged.connect(self._on_path_template_changed)
self.edit_path_template.setText(str(settings.get_path_template()))
self.edit_path_template.setPlaceholderText(PathTemplate.DEFAULT_STR)
self.edit_path_template.setPlaceholderText(PathTemplate.default_str)
self.button_default_path_template.pressed.connect(self.edit_path_template.clear)
self.button_insert_placeholder.pressed.connect(
lambda: self.edit_path_template.insert(
Expand Down
4 changes: 2 additions & 2 deletions src/usdb_syncer/path_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class PathTemplate:
"""

_components: list[PathTemplateComponent]
DEFAULT_STR = ":artist: - :title: / :artist: - :title:"
default_str = ":artist: - :title: / :artist: - :title:"

@classmethod
def parse(cls, template: str) -> PathTemplate:
Expand All @@ -81,7 +81,7 @@ def evaluate(self, song: UsdbSong, parent: Path = Path()) -> Path:

@classmethod
def default(cls) -> PathTemplate:
return cls.parse(cls.DEFAULT_STR)
return cls.parse(cls.default_str)

def __str__(self) -> str:
return " / ".join(map(str, self._components))
Expand Down
1 change: 1 addition & 0 deletions src/usdb_syncer/resource_dl.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ def download_image(url: str, logger: Log) -> bytes | None:

def download_and_process_image(
url: str,
*,
target_stem: Path,
meta_tags: ImageMetaTags | None,
details: SongDetails,
Expand Down
23 changes: 11 additions & 12 deletions src/usdb_syncer/song_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ def set_pause(cls, pause: bool) -> None:
@classmethod
def quit(cls) -> None:
if cls._pool:
get_logger(__file__).debug(f"Quitting {len(cls._jobs)} downloads.")
for job in cls._jobs.values():
job.abort = True
cls._pool.waitForDone()
Expand Down Expand Up @@ -115,7 +116,6 @@ class _Locations:
def new(
cls, song: UsdbSong, options: download_options.Options, tempdir: Path
) -> _Locations:
# BUG: suffix is probably wrong
target = options.path_template.evaluate(song, options.song_dir)
if (
_current := song.sync_meta.path.parent if song.sync_meta else None
Expand Down Expand Up @@ -464,11 +464,11 @@ def _maybe_download_cover(ctx: _Context) -> None:
ctx.logger.info("Cover resource is unchanged.")
return
if path := resource_dl.download_and_process_image(
url,
ctx.locations.temp_path(),
ctx.txt.meta_tags.cover,
ctx.details,
resource_dl.ImageKind.COVER,
url=url,
target_stem=ctx.locations.temp_path(),
meta_tags=ctx.txt.meta_tags.cover,
details=ctx.details,
kind=resource_dl.ImageKind.COVER,
max_width=ctx.options.cover.max_size,
):
ctx.out.cover.resource = url
Expand All @@ -491,11 +491,11 @@ def _maybe_download_background(ctx: _Context) -> None:
ctx.logger.info("Background resource is unchanged.")
return
if path := resource_dl.download_and_process_image(
url,
ctx.locations.temp_path(),
ctx.txt.meta_tags.background,
ctx.details,
resource_dl.ImageKind.BACKGROUND,
url=url,
target_stem=ctx.locations.temp_path(),
meta_tags=ctx.txt.meta_tags.background,
details=ctx.details,
kind=resource_dl.ImageKind.BACKGROUND,
max_width=None,
):
ctx.out.background.resource = url
Expand Down Expand Up @@ -730,7 +730,6 @@ def _persist_tempfiles(ctx: _Context) -> None:


def _write_sync_meta(ctx: _Context) -> None:
# BUG: resources are always redownloading
old = ctx.song.sync_meta
sync_meta_id = old.sync_meta_id if old else SyncMetaId.new()
ctx.song.sync_meta = SyncMeta(
Expand Down
2 changes: 2 additions & 0 deletions src/usdb_syncer/usdb_scraper.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ def comment_tags(self) -> list[str]:
def get_usdb_page(
rel_url: str,
method: RequestMethod = RequestMethod.GET,
*,
headers: dict[str, str] | None = None,
payload: dict[str, str] | None = None,
params: dict[str, str] | None = None,
Expand Down Expand Up @@ -248,6 +249,7 @@ def _get_usdb_page_inner(
session: Session,
rel_url: str,
method: RequestMethod = RequestMethod.GET,
*,
headers: dict[str, str] | None = None,
payload: dict[str, str] | None = None,
params: dict[str, str] | None = None,
Expand Down
2 changes: 2 additions & 0 deletions tests/integration/test_song_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
# pylint: disable=unused-argument
def _download_and_process_image(
url: Any,
*,
target_stem: Path,
meta_tags: Any,
details: Any,
Expand Down Expand Up @@ -55,6 +56,7 @@ def _download_video(
def _options(
song_dir: Path,
path_template: str,
*,
audio: bool = False,
video: bool = False,
cover: bool = False,
Expand Down

0 comments on commit aa43691

Please sign in to comment.