From 79dec6d20fb35946ecf1097fb096c9f6632a19ba Mon Sep 17 00:00:00 2001 From: Christian Heller Date: Sun, 23 Feb 2025 09:40:25 +0100 Subject: [PATCH] Fix variously broken deletion code. --- src/ytplom/http.py | 2 +- src/ytplom/misc.py | 39 ++++++++++++++++++++++----------------- 2 files changed, 23 insertions(+), 18 deletions(-) diff --git a/src/ytplom/http.py b/src/ytplom/http.py index 12fa9bc..7f446dd 100644 --- a/src/ytplom/http.py +++ b/src/ytplom/http.py @@ -126,7 +126,7 @@ class _TaskHandler(PlomHttpHandler): file.set_flags({FILE_FLAGS[FlagName('delete')]}) file.save(conn) conn.commit() - file.ensure_absence_if_deleted() + file.ensure_unlinked_if_deleted() self._redirect(Path(self.postvars.first_for('redir_target'))) def _update_file(self) -> None: diff --git a/src/ytplom/misc.py b/src/ytplom/misc.py index db5fe54..1d4ade8 100644 --- a/src/ytplom/misc.py +++ b/src/ytplom/misc.py @@ -387,19 +387,21 @@ class VideoFile(DbData): self.last_update = _now_string() return super().save(conn) - def test_deletion(self, do_raise: bool) -> bool: - """If 'delete' flag set, return True or raise NotFound, else False.""" - if self.is_flag_set(FlagName('delete')): - if do_raise: - raise NotFoundException('not showing entry marked as deleted') - return True - return False + @property + def deleted(self) -> bool: + """Return if 'delete' flag set.""" + return self.is_flag_set(FlagName('delete')) + + def ensure_not_deleted(self) -> None: + """If 'delete' flag set, raise appropriate NotFoundException.""" + if self.deleted: + raise NotFoundException('not showing entry marked as deleted') @classmethod def get_one(cls, conn: DbConn, id_: str | Hash) -> Self: """Extend super by .test_deletion.""" file = super().get_one(conn, id_) - file.test_deletion(do_raise=True) # pylint: disable=no-member + file.ensure_not_deleted() # pylint: disable=no-member # NB: mypy recognizes file as VideoFile without below assert and # if-isinstance-else, yet less type-smart pylint only does due to the # latter (also the reason for the disable=no-member above; but wouldn't @@ -411,9 +413,13 @@ class VideoFile(DbData): @classmethod def get_all(cls, conn: DbConn) -> list[Self]: - """Extend super by excluding matches in .test_deletion.""" - files = super().get_all(conn) - return [f for f in files if not f.test_deletion(do_raise=False)] + """Extend super by excluding deleteds.""" + return [f for f in super().get_all(conn) if not f.deleted] + + @classmethod + def get_deleteds(cls, conn: DbConn) -> list[Self]: + """Get _only_ deleteds.""" + return [f for f in super().get_all(conn) if f.deleted] @classmethod def get_by_yt_id(cls, conn: DbConn, yt_id: YoutubeId) -> Self: @@ -423,7 +429,7 @@ class VideoFile(DbData): if not row: raise NotFoundException(f'no entry for file to Youtube ID {yt_id}') file = cls._from_table_row(row) - file.test_deletion(do_raise=False) + file.ensure_not_deleted() return file @classmethod @@ -523,7 +529,7 @@ class VideoFile(DbData): """Return if flag of flag_name is set in flags field.""" return bool(self.flags & FILE_FLAGS[flag_name]) - def ensure_absence_if_deleted(self) -> None: + def ensure_unlinked_if_deleted(self) -> None: """If 'delete' flag set, ensure no actual file in filesystem.""" if self.is_flag_set(FlagName('delete')) and self.present: self.unlink_locally() @@ -536,8 +542,7 @@ class VideoFile(DbData): @classmethod def purge_deleteds(cls, conn: DbConn) -> None: """For all of .is_flag_set("deleted"), remove file _and_ DB entry.""" - for file in [f for f in cls.get_all(conn) - if f.is_flag_set(FlagName('delete'))]: + for file in cls.get_deleteds(conn): if file.present: file.unlink_locally() print(f'SYNC: purging off DB: {file.digest.b64} ({file.rel_path})') @@ -799,9 +804,9 @@ class DownloadsManager: yt_id=yt_id, tags_str=VideoFile.tags_default.joined) file.save(conn) + for file in VideoFile.get_deleteds(conn): + file.ensure_unlinked_if_deleted() self._files = VideoFile.get_all(conn) - for file in self._files: - file.ensure_absence_if_deleted() chdir(old_cwd) conn.commit() -- 2.30.2