Quellcode durchsuchen

Fix hard boundary violations ahead of module system refactor

fetch_reservations: make user_cal sync block lazy (try/except ImportError)
so the action can be imported without user_cal present. The block will
gain an is_loaded() guard once the module registry exists.

ical_feeds: import EventRecord from store public API instead of store._backend
ZiB vor 4 Monaten
Ursprung
Commit
2718faef56
2 geänderte Dateien mit 23 neuen und 21 gelöschten Zeilen
  1. 22 20
      room_cal/actions/fetch_reservations.py
  2. 1 1
      room_cal/ical_feeds/__init__.py

+ 22 - 20
room_cal/actions/fetch_reservations.py

@@ -4,7 +4,6 @@ from typing import TYPE_CHECKING
 
 from room_cal.closed import Closed, ClosedData
 from room_cal.reservations import Reservation
-from room_cal.user_cal import UserCal
 from room_cal._utils import FetchResult, safe_json_loads, iter_dates, services as _services, service_name
 
 if TYPE_CHECKING:
@@ -54,7 +53,6 @@ async def fetch_reservations(
     api = _ServiceAPI()
   res_cal = Reservation()
   closed = Closed()
-  user_cal = UserCal()
   all_service_keys = _services()
 
   existing_events = await res_cal.list(start_date, end_date)
@@ -135,23 +133,27 @@ async def fetch_reservations(
     for day, data in closed_set_inputs
   ])
 
-  ucal_events = await user_cal.find_services(start_date, end_date)
-  ucal_by_id: dict[str, object] = {}
-  for e in ucal_events:
-    d = safe_json_loads(e.x_data)
-    if d:
-      rid = d.get('reservation_id')
-      if rid:
-        ucal_by_id[rid] = e
-
-  await asyncio.gather(*[
-    user_cal.delete(event)
-    for rid, event in ucal_by_id.items()
-    if rid not in service_ids
-  ])
-  await asyncio.gather(*[
-    user_cal.set(r['service'], r['id'], r['start_date'], r['end_date'])
-    for r in service_reservations
-  ])
+  try:
+    from room_cal.user_cal import UserCal
+    user_cal = UserCal()
+    ucal_events = await user_cal.find_services(start_date, end_date)
+    ucal_by_id: dict[str, object] = {}
+    for e in ucal_events:
+      d = safe_json_loads(e.x_data)
+      if d:
+        rid = d.get('reservation_id')
+        if rid:
+          ucal_by_id[rid] = e
+    await asyncio.gather(*[
+      user_cal.delete(event)
+      for rid, event in ucal_by_id.items()
+      if rid not in service_ids
+    ])
+    await asyncio.gather(*[
+      user_cal.set(r['service'], r['id'], r['start_date'], r['end_date'])
+      for r in service_reservations
+    ])
+  except ImportError:
+    pass
 
   return FetchResult(data=synced, changed=changed)

+ 1 - 1
room_cal/ical_feeds/__init__.py

@@ -2,7 +2,7 @@ import urllib.request
 from datetime import date, datetime, timedelta
 
 from room_cal._utils import CalendarEntry, EventSource, get_config
-from room_cal.store._backend import EventRecord
+from room_cal.store import EventRecord
 
 _source = EventSource('ical_feeds')