Skip to content

Commit bbc0a51

Browse files
committed
Add file parsing function to convert YAML/JSON documents to defs.Document objects
1 parent bf247e7 commit bbc0a51

1 file changed

Lines changed: 21 additions & 0 deletions

File tree

application/cmd/cre_main.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1075,6 +1075,27 @@ def generate_embeddings(db_url: str) -> None:
10751075
database = db_connect(path=db_url)
10761076
prompt_client.PromptHandler(database, load_all_embeddings=True)
10771077

1078+
def parse_file(filename: str, yamldocs: List[Any], scollection) -> Optional[List[defs.Document]]:
1079+
"""
1080+
Parse a list of dictionaries (YAML/JSON documents) into defs.Document objects.
1081+
Returns None and logs a critical error if any element is not a dict.
1082+
"""
1083+
if not all(isinstance(doc, dict) for doc in yamldocs):
1084+
logger.critical("Malformed file %s, skipping", filename)
1085+
return None
1086+
1087+
def normalize_links(doc: dict) -> dict:
1088+
"""Make sure link dicts use 'ltype' key instead of 'type'."""
1089+
if "links" in doc:
1090+
for link in doc["links"]:
1091+
if "type" in link and "ltype" not in link:
1092+
link["ltype"] = link.pop("type")
1093+
# Recursively normalize nested documents (if any)
1094+
if "document" in link and isinstance(link["document"], dict):
1095+
normalize_links(link["document"])
1096+
return doc
1097+
1098+
return [defs.Document.from_dict(normalize_links(dict(doc))) for doc in yamldocs]
10781099

10791100
def populate_neo4j_db(cache: str):
10801101
if (

0 commit comments

Comments
 (0)