Skip to content

Commit

Permalink
support lists of categories
Browse files Browse the repository at this point in the history
  • Loading branch information
ggmarshall committed Jan 14, 2025
1 parent f7091d4 commit 20071fe
Showing 1 changed file with 29 additions and 27 deletions.
56 changes: 29 additions & 27 deletions src/dbetto/catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,36 +76,38 @@ def read_from(file_name):
entries = {}
for props in PropsStream.get(file_name):
timestamp = props["valid_from"]
system = "all" if props.get("category") is None else props["category"]
system = props.get("category", "all")
if not isinstance(system, list):
system = [system]
file_key = props["apply"]
if system not in entries:
entries[system] = []
mode = "append" if props.get("mode") is None else props["mode"]
mode = "reset" if len(entries[system]) == 0 else mode
if mode == "reset":
new = file_key
elif mode == "append":
new = entries[system][-1].file.copy() + file_key
elif mode == "remove":
new = entries[system][-1].file.copy()
for file in file_key:
new.remove(file)
elif mode == "replace":
new = entries[system][-1].file.copy()
if len(file_key) != 2:
msg = f"Invalid number of elements in replace mode: {len(file_key)}"
for sys in system:
if sys not in entries:
entries[sys] = []
mode = props.get("mode", "append")
mode = "reset" if len(entries[sys]) == 0 else mode
if mode == "reset":
new = file_key
elif mode == "append":
new = entries[system][-1].file.copy() + file_key
elif mode == "remove":
new = entries[system][-1].file.copy()
for file in file_key:
new.remove(file)
elif mode == "replace":
new = entries[system][-1].file.copy()
if len(file_key) != 2:
msg = f"Invalid number of elements in replace mode: {len(file_key)}"
raise ValueError(msg)
new.remove(file_key[0])
new += [file_key[1]]
else:
msg = f"Unknown mode for {timestamp}"
raise ValueError(msg)
new.remove(file_key[0])
new += [file_key[1]]

else:
msg = f"Unknown mode for {timestamp}"
raise ValueError(msg)

if timestamp in [entry.valid_from for entry in entries[system]]:
msg = f"Duplicate timestamp: {timestamp}, use reset mode instead with a single entry"
raise ValueError(msg)
entries[system].append(Catalog.Entry(time.unix_time(timestamp), new))
if timestamp in [entry.valid_from for entry in entries[system]]:
msg = f"Duplicate timestamp: {timestamp}, use reset mode instead with a single entry"
raise ValueError(msg)
entries[system].append(Catalog.Entry(time.unix_time(timestamp), new))

for system, value in entries.items():
entries[system] = sorted(value, key=lambda entry: entry.valid_from)
Expand Down

0 comments on commit 20071fe

Please sign in to comment.