From: Christian Heller Date: Mon, 23 Dec 2024 16:14:20 +0000 (+0100) Subject: Improve verbosity of sync script. X-Git-Url: https://plomlompom.com/repos/%22https:/validator.w3.org/error?a=commitdiff_plain;h=17d645d2401a48ebce13656c4d69681cd7a32485;p=ytplom Improve verbosity of sync script. --- diff --git a/src/sync.py b/src/sync.py index 7de9fe1..adfcef3 100755 --- a/src/sync.py +++ b/src/sync.py @@ -36,24 +36,27 @@ def sync_objects(host_names: tuple[str, str], """Equalize both DB's object collections; prefer newer states to older.""" id_name = 'id_' if 'id' == cls.id_name else cls.id_name obj_colls = cls.get_all(dbs[0]), cls.get_all(dbs[1]) - for obj_0 in [obj for obj in obj_colls[0] if obj not in obj_colls[1]]: + for obj_0 in [obj for obj in obj_colls[0] # only pick objs without equal + if obj not in obj_colls[1]]: # in 2nd coll, even if same ID id_ = getattr(obj_0, id_name) sync_down = True + to_str = obj_0 msg_verb = 'adding' - for obj_1 in [obj for obj in obj_colls[1] # other collection has … - if id_ == getattr(obj, id_name)]: # … obj, but non-equal + direction = f'{host_names[1]}->{host_names[0]}' + for obj_1 in [obj for obj in obj_colls[1] # pick those from 2nd coll + if id_ == getattr(obj, id_name)]: # of same ID as obj_0 msg_verb = 'updating' if hasattr(obj_0, ATTR_NAME_LAST_UPDATE): last_update_0 = getattr(obj_0, ATTR_NAME_LAST_UPDATE) last_update_1 = getattr(obj_1, ATTR_NAME_LAST_UPDATE) sync_down = last_update_0 > last_update_1 if not sync_down: - print(f'SYNC: {msg_verb} ' - f'{host_names[1]}->{host_names[0]} {id_}') + direction = f'{host_names[0]}->{host_names[1]}' + to_str = obj_1 obj_1.save(dbs[0]) if sync_down: - print(f'SYNC: {msg_verb} {host_names[0]}->{host_names[1]} {id_}') obj_0.save(dbs[1]) + print(f'SYNC {cls.__name__}: {msg_verb} {direction} {id_} {to_str}') def sync_relations(host_names: tuple[str, str], @@ -63,9 +66,10 @@ def sync_relations(host_names: tuple[str, str], """To dbs[1] add YT yt_video->yt_q_colls[0] mapping not in yt_q_colls[1]""" yt_q_colls = tuple(YoutubeQuery.get_all_for_video(db, yt_result.id_) for db in dbs) + direction = f'adding {host_names[1]}->{host_names[0]} mapping' + result = f'result {yt_result.id_} ({yt_result})' for q in [q for q in yt_q_colls[0] if q not in yt_q_colls[1]]: - print(f'SYNC: adding {host_names[1]}->{host_names[0]} mapping ' - f'of result {yt_result.id_} to query {q.id_}') + print(f'SYNC: {direction} of query {q.id_} ({q}) to {result}') yt_result.save_to_query(dbs[1], q.id_) diff --git a/src/ytplom/db.py b/src/ytplom/db.py index bcc8b30..0fa9d7f 100644 --- a/src/ytplom/db.py +++ b/src/ytplom/db.py @@ -113,6 +113,7 @@ class DbData: """Abstraction of common DB operation.""" id_name: str = 'id' _table_name: str + _str_field: str _cols: tuple[str, ...] def __eq__(self, other: Any) -> bool: @@ -123,6 +124,9 @@ class DbData: return False return True + def __str__(self) -> str: + return str(getattr(self, self._str_field)) + @classmethod def _from_table_row(cls, row: Row) -> Self: kwargs = {} diff --git a/src/ytplom/misc.py b/src/ytplom/misc.py index 86254bf..ece413c 100644 --- a/src/ytplom/misc.py +++ b/src/ytplom/misc.py @@ -177,6 +177,7 @@ class Config: class YoutubeQuery(DbData): """Representation of YouTube query (without results).""" _table_name = 'yt_queries' + _str_field = 'text' _cols = ('id_', 'text', 'retrieved_at') def __init__(self, @@ -255,6 +256,7 @@ class YoutubeQuery(DbData): class YoutubeVideo(DbData): """Representation of YouTube video metadata as provided by their API.""" _table_name = 'yt_videos' + _str_field = 'title' _cols = ('id_', 'title', 'description', 'published_at', 'duration', 'definition') @@ -318,6 +320,7 @@ class VideoFile(DbData): """Collects data about downloaded files.""" id_name = 'digest' _table_name = 'files' + _str_field = 'rel_path' _cols = ('digest', 'rel_path', 'flags', 'yt_id', 'last_update', 'tags_str') last_update: DatetimeStr rel_path: Path @@ -466,6 +469,7 @@ class VideoFile(DbData): class QuotaLog(DbData): """Collects API access quota costs.""" _table_name = 'quota_costs' + _str_field = 'timestamp' _cols = ('id_', 'timestamp', 'cost') def __init__(self,