'''
VIDEO_FLAGS: dict[FlagName, FlagsInt] = {
- FlagName('foo'): FlagsInt(1 << 0),
- FlagName('bar'): FlagsInt(1 << 1),
- FlagName('baz'): FlagsInt(1 << 2)
+ FlagName('delete'): FlagsInt(1 << 62)
}
self.rel_path = rel_path
self.yt_id = yt_id
self.flags = flags
- self.missing = False
@classmethod
def get_by_yt_id(cls, conn: DatabaseConnection, yt_id: YoutubeId) -> Self:
raise NotFoundException
return cls._from_table_row(row)
+ @property
+ def full_path(self) -> PathStr:
+ """Return self.rel_path suffixed under PATH_DOWNLOADS."""
+ return PathStr(path_join(PATH_DOWNLOADS, self.rel_path))
+
+ @property
+ def present(self) -> bool:
+ """Return if file exists in filesystem."""
+ return path_exists(self.full_path)
+
+ @property
+ def missing(self) -> bool:
+ """Return if file absent despite absence of 'delete' flag."""
+ return not (self.flag_set(FlagName('delete')) or self.present)
+
def flag_set(self, flag_name: FlagName) -> bool:
"""Return if flag of flag_name is set in self.flags."""
return self.flags & VIDEO_FLAGS[flag_name]
+ def ensure_absence_if_deleted(self) -> None:
+ """If 'delete' flag set, ensure no actual file in filesystem."""
+ if self.flag_set(FlagName('delete')) and path_exists(self.full_path):
+ print(f'SYNC: {self.rel_path} set "delete", '
+ 'removing from filesystem.')
+ os_remove(self.full_path)
+
class QuotaLog(DbData):
"""Collects API access quota costs."""
file.save(conn)
self._files = VideoFile.get_all(conn)
for file in self._files:
- if not isfile(path_join(file.rel_path)):
- file.missing = True
+ file.ensure_absence_if_deleted()
chdir(old_cwd)
conn.commit_close()
def ids_to_paths(self) -> DownloadsIndex:
"""Return mapping YoutubeIds:paths of files downloaded to them."""
self._sync_db()
- return {f.yt_id: PathStr(path_join(PATH_DOWNLOADS, f.rel_path))
- for f in self._files}
+ return {f.yt_id: f.full_path for f in self._files}
@property
def ids_unfinished(self) -> set[YoutubeId]:
file.flags |= VIDEO_FLAGS[flag_name]
file.save(conn)
conn.commit_close()
+ file.ensure_absence_if_deleted()
self._send_http(headers=[('Location', f'/video/{yt_id}')], code=302)
def _post_query(self, query_txt: QueryText) -> None: