def test_text_in_file(self, dummy_text): dummy_text = repr(dummy_text) filename = "/ZLPB20100101145823.xml" with tempfile.TemporaryDirectory() as tmppath: with open(tmppath + filename, "w") as opus_file: opus_file.write(dummy_text) settings = {"integrations.opus.import.xml_path": tmppath} ofr = get_opus_filereader(settings=settings) files_list = ofr.list_opus_files() assert len(files_list) == 1 text_in = ofr.read_latest() assert text_in == dummy_text
def parser(target_file: Path, opus_id: Optional[int] = None) -> Tuple[List, List]: """Read an opus file and return units and employees""" text_input = get_opus_filereader().read_file(target_file) data = xmltodict.parse(text_input) data = data["kmd"] units = data.get("orgUnit", []) employees = data.get("employee", []) if opus_id is not None: employees = list(filter(lambda x: int(x["@id"]) == opus_id, employees)) units = list(filter(lambda x: int(x["@id"]) == opus_id, units)) return units, employees
def find_type(opus_id, full_history): """Check if the object with the given id is a unit or an employee.""" dumps = get_opus_filereader().list_opus_files() # Search in newest file first opus_files = sorted(dumps, reverse=True) if not full_history: opus_files = [first(opus_files)] for f in opus_files: units, employees = opus_helpers.parser(dumps[f], opus_id=opus_id) employees, terminated_employees = opus_helpers.split_employees_leaves( employees) employees = list(employees) if units: return "organisationenhed", only(units) elif employees: return "bruger", only(employees) terminated_employees = list(terminated_employees) if terminated_employees: msg = "Employee was terminated, try --full-history" else: msg = f"No object with {opus_id=} was found." raise ValueError(msg)
def test_empty_path(self): with tempfile.TemporaryDirectory() as tmppath: settings = {"integrations.opus.import.xml_path": tmppath} ofr = get_opus_filereader(settings=settings) assert len(ofr.list_opus_files()) == 0
def test_local(self): settings = {} ofr = get_opus_filereader(settings=settings) assert isinstance(ofr, LocalOpusReader)
def read_available_dumps() -> Dict[datetime.datetime, str]: dumps = get_opus_filereader().list_opus_files() assert len(dumps) > 0, "No Opus files found!" return dumps