Skip to content

Commit cc8a7a6

Browse files
committed
refactor: improve logic and logging
1 parent 138bac4 commit cc8a7a6

File tree

2 files changed

+41
-26
lines changed

2 files changed

+41
-26
lines changed

src/ndtoolbox/app.py

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,7 @@ def is_keepable(self, this: MediaFile, that: MediaFile) -> MediaFile:
448448
449449
"""
450450
PU.note(f"Compare {SU.gray(this.path)} <=> {SU.gray(that.path)}", 0)
451+
PU.note(f"This folder: {SU.gray(this.folder)}, That folder: {SU.gray(that.folder)}")
451452

452453
# Real album folder files are keepable over files in root, artist or dump folders
453454
left: Folder = this.folder
@@ -456,23 +457,31 @@ def is_keepable(self, this: MediaFile, that: MediaFile) -> MediaFile:
456457
PU.log(f"Compare if file is in album folder: {left.type} || {right.type}", 1)
457458
if left.type != right.type:
458459
if left.type == Folder.Type.ALBUM:
459-
PU.log(f"This is an album folder: {SU.gray(this.folder.beets_path)}", 2)
460+
msg = f"Other is an album folder: {SU.gray(this.folder.beets_path)}"
461+
PU.log(msg, 2)
462+
that.delete_reason = msg
460463
return this
461464
elif right.type == Folder.Type.ALBUM:
462-
PU.log(f"That is an album folder: {SU.gray(that.folder.beets_path)}", 2)
465+
msg = f"Other is an album folder: {SU.gray(that.folder.beets_path)}"
466+
PU.log(msg, 2)
467+
this.delete_reason = msg
463468
return that
464469
# Skip if both are of the same path and type
465470

466471
# Check for dirty folders
467472
left = this.folder.is_dirty
468473
right = that.folder.is_dirty
469474
PU.log(f"Compare dirty folders: {left} || {right}", 1)
470-
if left and (left > 0) or right and (right > 0):
471-
if left < right:
472-
PU.log(f"This folder is dirty: {SU.gray(this.folder.beets_path)}", 2)
475+
if left != right:
476+
if left:
477+
msg = f"This folder is dirty: {SU.gray(this.folder.beets_path)}"
478+
PU.log(msg, 2)
479+
this.delete_reason = msg
473480
return that
474-
elif left > right:
475-
PU.log(f"That folder is dirty: {SU.gray(that.folder.beets_path)}", 2)
481+
elif right:
482+
msg = f"That folder is dirty: {SU.gray(that.folder.beets_path)}"
483+
PU.log(msg, 2)
484+
that.delete_reason = msg
476485
return this
477486
# Skip if both are incomplete
478487

@@ -482,23 +491,29 @@ def is_keepable(self, this: MediaFile, that: MediaFile) -> MediaFile:
482491
if left.missing is not None and right.missing is not None:
483492
PU.log(f"Compare missing tracks: {left.missing}/{left.total} || {right.missing}/{right.total}", 1)
484493
if (left.missing != right.missing) and (left.missing > 0 or right.missing > 0):
485-
if left.missing < right.missing:
486-
PU.log(f"This folder is more complete ({left.missing}/{left.total}): {SU.gray(this.path)}", 2)
494+
if (left.missing < right.missing) and not left.is_dirty:
495+
msg = f"Other folder is more complete ({left.missing}/{left.total}): {SU.gray(this.path)}"
496+
PU.log(msg, 2)
497+
that.delete_reason = msg
487498
return this
488-
elif left.missing > right.missing:
489-
PU.log(f"That folder is more complete ({right.missing}/{right.total}): {SU.gray(that.path)}", 2)
499+
elif (left.missing > right.missing) and not right.is_dirty:
500+
msg = f"Other folder is more complete ({right.missing}/{right.total}): {SU.gray(that.path)}"
501+
PU.log(msg, 2)
502+
this.delete_reason = msg
490503
return that
491504
# Skip if both are incomplete, none has missing tracks or have no information on missing tracks
492505

493-
# If the album folder already contains a keepable, we wanna keep all the items
506+
# If the album folder already contains a keepable, we wanna keep all the items, except for dirty folders
494507
left = this.folder and this.folder.has_keepable
508+
l_dirty = this.folder and this.folder.is_dirty
495509
right = that.folder and that.folder.has_keepable
496-
PU.log(f"Compare if album folder contain a keepable: {left} || {right}", 1)
510+
r_dirty = that.folder and that.folder.is_dirty
511+
PU.log(f"Compare if album folder contain a keepable: {left} +dirty: {l_dirty} || {right} +dirty: {r_dirty}", 1)
497512
if left != right:
498-
if left:
513+
if left and not l_dirty:
499514
that.delete_reason = f"Other album folder already contains a keepable | {SU.gray(this.path)}"
500515
return this
501-
elif right:
516+
elif right and not r_dirty:
502517
this.delete_reason = f"Other album folder already contains a keepable | {SU.gray(that.path)}"
503518
return that
504519
# Skip, if they are the same
@@ -652,6 +667,8 @@ def is_keepable(self, this: MediaFile, that: MediaFile) -> MediaFile:
652667
sys.exit(1)
653668

654669
processor = DuplicateProcessor(config)
670+
# client = BeetsClient(config)
671+
# print("BEETS CLIENT: " + client._query1())
655672

656673
if action == "remove-unsupported":
657674
FileTools.move_by_extension(config.music_dir, config.data_dir, config.remove_extensions, ToolboxConfig.dry_run)

src/ndtoolbox/model.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -260,12 +260,7 @@ def __init__(self, media: MediaFile):
260260
PU.warning(f"Found unknown folder type for {self.beets_path}")
261261

262262
# For performance reasons, we don't load album info for all folders
263-
if (
264-
not self.is_dirty
265-
and not self.beets_album
266-
and not self.type == Folder.Type.ROOT
267-
and not self.type == Folder.Type.ARTIST
268-
):
263+
if not self.is_dirty and not self.beets_album and not self.type == Folder.Type.ROOT:
269264
self._load_album_info()
270265

271266
# Check if folder is dirty
@@ -288,22 +283,25 @@ def _load_album_info(self) -> None:
288283

289284
if not infos:
290285
# TODO Clarify how to handle this case
286+
# self.is_dirty = True
287+
self.type = Folder.Type.UNKNOWN
291288
PU.warning(f"Got no album info for {self.beets_path} > clarify handling")
292-
self.is_dirty = True
293-
if len(infos) > 1:
289+
elif len(infos) > 1:
294290
# Folder contains files form multiples albums. So this is either a dump folder
295291
# or amanually made compilation/mixtape.
296292
self.is_dirty = True
293+
self.type = Folder.Type.UNKNOWN
297294
PU.warning(f"Found self-made compilation, mixtape or dump folder: '{self.beets_path}'")
298295
elif len(infos) == 1:
299296
self.beets_album = infos[0].album
300297
self.total = infos[0].total
301298
self.missing = infos[0].missing
302299
self.is_compilation = infos[0].compilation
300+
self.type = Folder.Type.ALBUM
303301

304-
def map_media_to_file(self, media: MediaFile):
305-
"""Map a media file to an existing file in the album folder."""
306-
self.files[FileUtil.get_file(media.path)] = media
302+
# def map_media_to_file(self, media: MediaFile):
303+
# """Map a media file to an existing file in the album folder."""
304+
# self.files[FileUtil.get_file(media.path)] = media
307305

308306
def __repr__(self) -> str:
309307
"""String representation of the Folder object."""

0 commit comments

Comments
 (0)