Skip to content

Commit

Permalink
Parents are parenting
Browse files Browse the repository at this point in the history
  • Loading branch information
PonteIneptique committed Aug 24, 2024
1 parent 724617a commit 48b1ddf
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 6 deletions.
11 changes: 5 additions & 6 deletions dapitains/metadata/xml_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Catalog:
objects: Dict[str, Collection] = field(default_factory=dict)


def parse_metadata(xml: ET.Element) -> Dict[str, Any]:
def parse_metadata(xml: ET.Element) -> Tuple[Dict[str, Any], List[str]]:
""" Parse Metadata
:param xml: Collection/Resource tag
Expand Down Expand Up @@ -50,9 +50,8 @@ def parse_metadata(xml: ET.Element) -> Dict[str, Any]:
parents = []
for node in xml.xpath("./parent/text()"):
parents.append(str(node))
obj["parents"] = parents

return obj
return obj, parents


def parse_collection(xml: ET.Element, basedir: str, tree: Catalog) -> Collection:
Expand All @@ -62,8 +61,10 @@ def parse_collection(xml: ET.Element, basedir: str, tree: Catalog) -> Collection
:param basedir: Directory used to resolve filepath, that are relative to the main object
:param tree: Catalog that is updated with objects.
"""
obj = parse_metadata(xml)
obj, parents = parse_metadata(xml)
obj = Collection(**obj, resource=xml.tag == "resource")
for parent in parents:
tree.relationships.append((parent, obj.identifier))
tree.objects[obj.identifier] = obj
if xml.attrib.get("filepath") and obj.resource:
obj.filepath = os.path.normpath(os.path.join(basedir, xml.attrib["filepath"]))
Expand All @@ -74,8 +75,6 @@ def parse_collection(xml: ET.Element, basedir: str, tree: Catalog) -> Collection
else:
_, child = ingest_catalog(os.path.join(basedir, member.attrib["filepath"]), tree)
tree.relationships.append((obj.identifier, child.identifier))
for parent in child.parents:
tree.relationships.append((parent, child.identifier))
return obj


Expand Down
1 change: 1 addition & 0 deletions tests/catalog/example-sub-collection.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<resource identifier="https://example.org/resource1" filepath="../tei/multiple_tree.xml">
<title>Historical Document</title>
<description>A document about historical events.</description>
<parent>https://foo.bar/default</parent>
<dublinCore>
<subject xmlns="http://purl.org/dc/terms/">World War II</subject>
<language xmlns="http://purl.org/dc/terms/">en</language>
Expand Down
1 change: 1 addition & 0 deletions tests/test_catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,6 @@ def test_ingestion():
assert sorted(tree.relationships) == [
('https://example.org/collection1', 'https://example.org/resource1'),
('https://foo.bar/default', 'https://example.org/collection1'),
('https://foo.bar/default', 'https://example.org/resource1',),
('https://foo.bar/default', 'https://foo.bar/text')
]

0 comments on commit 48b1ddf

Please sign in to comment.