home · contact · privacy
Improve verbosity of sync script.
authorChristian Heller <c.heller@plomlompom.de>
Mon, 23 Dec 2024 16:14:20 +0000 (17:14 +0100)
committerChristian Heller <c.heller@plomlompom.de>
Mon, 23 Dec 2024 16:14:20 +0000 (17:14 +0100)
src/sync.py
src/ytplom/db.py
src/ytplom/misc.py

index 7de9fe1e7f08020dec604a0cfeb20a2c2cc584de..adfcef3b4c825949dd9d6abb0122ee2ae27ddda2 100755 (executable)
@@ -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_)
 
 
index bcc8b30fcfb8a7891422234abddd2e59399a15f5..0fa9d7fed40a6afac0257bc7b9455b8aabc5f96a 100644 (file)
@@ -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 = {}
index 86254bf726440c19f8c6091817c00a47ce3ecd0f..ece413cd253556eb6a30f111b3c1921c6cc46685 100644 (file)
@@ -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,