From 3f13014fdbefd42f3bff98a002c573a36dc7a301 Mon Sep 17 00:00:00 2001 From: phernandez Date: Tue, 3 Dec 2024 18:39:42 -0600 Subject: [PATCH] refactor error logic in EventService _read_entity_file --- src/basic_memory/services.py | 70 +++++++++++++++++------------------ tests/.coverage | Bin 53248 -> 0 bytes 2 files changed, 34 insertions(+), 36 deletions(-) delete mode 100644 tests/.coverage diff --git a/src/basic_memory/services.py b/src/basic_memory/services.py index 78224f38..6d0dded6 100644 --- a/src/basic_memory/services.py +++ b/src/basic_memory/services.py @@ -69,46 +69,44 @@ class EntityService: async def _read_entity_file(self, entity_id: str) -> Entity: """Read entity data from filesystem.""" + entity_path = self.entities_path / f"{entity_id}.md" + if not entity_path.exists(): + raise EntityNotFoundError(f"Entity file not found: {entity_id}") + + # Only wrap the actual file read operation try: - entity_path = self.entities_path / f"{entity_id}.md" - if not entity_path.exists(): - raise EntityNotFoundError(f"Entity file not found: {entity_id}") - content = entity_path.read_text().split("\n") - - # Parse markdown content - # First line should be "# Name" - name = content[0].lstrip("# ").strip() - - # Parse metadata (type) - entity_type = "" - observations = [] - - # Parse content sections - in_observations = False - for line in content[1:]: # Skip the title line - line = line.strip() - if not line: - continue - - if line.startswith("type: "): - entity_type = line.replace("type: ", "").strip() - elif line == "## Observations": - in_observations = True - elif in_observations and line.startswith("- "): - observations.append(Observation(content=line[2:])) - - return Entity( - id=entity_id, - name=name, - entity_type=entity_type, - observations=observations - ) - - except EntityNotFoundError: - raise except Exception as e: raise FileOperationError(f"Failed to read entity file: {str(e)}") from e + + # Parse markdown content + # First line should be "# Name" + name = content[0].lstrip("# ").strip() + + # Parse metadata (type) + entity_type = "" + observations = [] + + # Parse content sections + in_observations = False + for line in content[1:]: # Skip the title line + line = line.strip() + if not line: + continue + + if line.startswith("type: "): + entity_type = line.replace("type: ", "").strip() + elif line == "## Observations": + in_observations = True + elif in_observations and line.startswith("- "): + observations.append(Observation(content=line[2:])) + + return Entity( + id=entity_id, + name=name, + entity_type=entity_type, + observations=observations + ) async def _update_db_index(self, entity: Entity) -> DbEntity: """Update database index with entity data.""" diff --git a/tests/.coverage b/tests/.coverage deleted file mode 100644 index 27c3bc5ec97a5de6fadfce830d48d471e84e91d9..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 53248 zcmeI4U2GiH702)F%UVl=DlU+oEz%dF zB!PrzciH0->QGvJQU!WR2ij}sFwx?W+ngMpy!?k@yRv7GaYe^AIYEQ|M+U5+guW?{ zERmpwA2eC}df<4C6&+3~XT!pVPkLU65^Dp$JsbI|<$AH1Ug$=y?ns~|HmRAOYZIM*H;BNIB2Xu=XqRsi|_Tv)66c3Hfik?G85% z)ksbp`CHZ8s&3?`#(wLzxkX#{?Hy>&3^})GQmgB^=el~M@uyV3=2UEk=T9j%f;`~h zNTG6Uk0Hn_^@Z?Ui@tBwoNnaD@ftOLZH|J{^FJ7}D-#pOm2!l(pn7hS4mmt(~wNWCO|V2Fa{Of0WGnNSZ_DOxuOZ z@$s$9Nz2vd(u1L%ZP}H*dyQVwBY0fmPkz7%PU&}n8zzEpuYSjgR@BY9b22Gj%@-;^ z-@BFKtU_&$!@}h!aZz7-{&)8F`$+$Rm?^Avlx-_lwv=#m=OI6hsC*)Bmtg5>2 zx3uFWa|6|QB&2h?p^A05c~<&GUq-mbGL~1Xu}<0K!o&O->U3HEqJwtZ@tWy;#nW&i zmBxPVoP3^S5TS~yN}V#KE_#E^OlX?RBUUDVrbA3vYTIch-JKg{lf?5Y`X+J7Z1k`b z>Bwzs#YJLeV#hk~gsKr}%A~n;vTEMW4Y;*xa~$$2d5FKt^(G9va`>>(>!zJC8E0xC zP3mnYQ#+032At%E%)Y;zqyd+{?QHfFX156kN0T$~;Fh#tJ)w}5O26w2Px1oakY5Y> z#|8o*00JNY0w4eaAOHd&00JNY0wA#a2$)9BuzCHTlm8TQodRqi00JNY0w4eaAOHd& z00JNY0w4eacPD{T&b(JL<8s>N<{Sm-VW_~>L(_ykICqEMMBl-E=X&|%)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