diff --git a/src/dbetto/catalog.py b/src/dbetto/catalog.py index 0614047..456aa94 100644 --- a/src/dbetto/catalog.py +++ b/src/dbetto/catalog.py @@ -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)