From: Christian Heller Date: Sat, 30 Nov 2024 18:18:36 +0000 (+0100) Subject: Refactor VideoFile.flags setting. X-Git-Url: https://plomlompom.com/repos/%7B%7B%20web_path%20%7D%7D/decks/static/%7B%7Bprefix%7D%7D/move_up?a=commitdiff_plain;p=ytplom Refactor VideoFile.flags setting. --- diff --git a/src/ytplom/http.py b/src/ytplom/http.py index 0c4a311..9027c98 100644 --- a/src/ytplom/http.py +++ b/src/ytplom/http.py @@ -12,9 +12,8 @@ from jinja2 import ( # type: ignore Environment as JinjaEnv, FileSystemLoader as JinjaFSLoader) import googleapiclient.discovery # type: ignore from ytplom.misc import ( - B64Str, DatetimeStr, FilesWithIndex, FlagsInt, FlagName, - NotFoundException, PlayerUpdateId, QueryId, QueryText, QuotaCost, - UrlStr, YoutubeId, + B64Str, DatetimeStr, FilesWithIndex, FlagName, NotFoundException, + PlayerUpdateId, QueryId, QueryText, QuotaCost, UrlStr, YoutubeId, FILE_FLAGS, PATH_APP_DATA, PATH_CACHE, TIMESTAMP_FMT, YOUTUBE_URL_PREFIX, ensure_expected_dirs, @@ -149,10 +148,7 @@ class _TaskHandler(BaseHTTPRequestHandler): ) -> None: conn = DbConnection() file = VideoFile.get_by_b64(conn, rel_path_b64) - flags = FlagsInt(0) - for flag_name in flag_names: - flags = FlagsInt(file.flags | FILE_FLAGS[flag_name]) - file.flags = flags + file.set_flags([FILE_FLAGS[name] for name in flag_names]) file.save(conn) conn.commit_close() file.ensure_absence_if_deleted() diff --git a/src/ytplom/misc.py b/src/ytplom/misc.py index 5472934..ca478a1 100644 --- a/src/ytplom/misc.py +++ b/src/ytplom/misc.py @@ -311,7 +311,7 @@ class VideoFile(DbData): ) -> None: self.rel_path = rel_path self.yt_id = yt_id - self._flags = flags + self.flags = flags if last_update is None: self._renew_last_update() else: @@ -361,21 +361,16 @@ class VideoFile(DbData): """Return if file absent despite absence of 'delete' flag.""" return not (self.is_flag_set(FlagName('delete')) or self.present) - @property - def flags(self) -> FlagsInt: - """Return value of flags field.""" - return self._flags - - @flags.setter - def flags(self, flags: list[FlagsInt]) -> None: - # self._flags = FlagsInt(0) - # for flag in flags: - # self._flags = self._flags | flag + def set_flags(self, flags: list[FlagsInt]) -> None: + """Set .flags to bitwise OR of FlagsInt in flags.""" + self.flags = FlagsInt(0) + for flag in flags: + self.flags = FlagsInt(self.flags | flag) self._renew_last_update() def is_flag_set(self, flag_name: FlagName) -> bool: """Return if flag of flag_name is set in flags field.""" - return bool(self._flags & FILE_FLAGS[flag_name]) + return bool(self.flags & FILE_FLAGS[flag_name]) def ensure_absence_if_deleted(self) -> None: """If 'delete' flag set, ensure no actual file in filesystem."""