Skip to content

Commit

Permalink
Better DTS compliancy
Browse files Browse the repository at this point in the history
  • Loading branch information
PonteIneptique committed Aug 29, 2024
1 parent b0d414f commit 9d9a99c
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 71 deletions.
40 changes: 23 additions & 17 deletions dapitains/app/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,24 @@ def collection_view(
else:
return msg_4xx(f"nav parameter has a wrong value {nav}", code=400)

def inject_json(related: Collection) -> Dict:
if related.resource:
inj = {
"collection": templates["collection"].partial({"id": related.identifier}).uri,
"document": templates["document"].partial({"resource": related.identifier}).uri,
}
if related.citeStructure:
inj["navigation"] = templates["navigation"].partial({"resource": related.identifier}).uri
else:
inj ={"collection": templates["collection"].partial({"id": related.identifier}).uri}

inj.update({
"totalParents": coll.total_parents,
"totalChildren": coll.total_children
})

return inj

return Response(json.dumps({
"@context": "https://distributed-text-services.github.io/specifications/context/1-alpha1.json",
"dtsVersion": "1-alpha",
Expand All @@ -60,23 +78,11 @@ def collection_view(
"collection": templates["collection"].uri,
"member": [
related.json(
inject=dict(
**{
"collection": templates["collection"].partial({"id": related.identifier}).uri,
"document": templates["document"].partial({"resource": related.identifier}).uri,
},
**(
{
"navigation": templates["navigation"].partial({"resource": related.identifier}).uri,
} if related.citeStructure else {}
)
) if related.resource else related.json({
"collection": templates["collection"].partial({"id": related.identifier}).uri
})
inject=inject_json(related)
)
for related in related_collections
]
}), mimetype="application/json", status=200)
}), mimetype="application/ld+json", status=200)


def document_view(resource, ref, start, end, tree) -> Response:
Expand Down Expand Up @@ -148,7 +154,7 @@ def navigation_view(resource, ref, start, end, tree, down, templates: Dict[str,
return msg_4xx(f"Range is missing one of its parameters (start or end)", code=400)

# Start the response
out = {
out = {
"@context": "https://distributed-text-services.github.io/specifications/context/1-alpha1.json",
"dtsVersion": "1-alpha",
"@type": "Navigation",
Expand Down Expand Up @@ -190,7 +196,7 @@ def navigation_view(resource, ref, start, end, tree, down, templates: Dict[str,
else:
out["ref"] = start

return Response(json.dumps(out), mimetype="application/json", status=200)
return Response(json.dumps(out), mimetype="application/ld+json", status=200)


def create_app(
Expand Down Expand Up @@ -218,7 +224,7 @@ def index_route():
"navigation" : navigation_template.uri,
"document": document_template.uri
}),
mimetype="application/json"
mimetype="application/ld+json"
)

@app.route("/collection/")
Expand Down
2 changes: 2 additions & 0 deletions dapitains/app/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ def json(self, inject: Optional[Dict[str, Any]] = None):
if key != self.default_tree:
data["citationTrees"].append(self.citeStructure[key])
self.citeStructure[key]["identifier"] = key
for tree in data["citationTrees"]:
tree["@type"] = "CitationTree"
if self.dublin_core: # ToDo: Fix the way it's presented to adapt to dts view
data["dublinCore"] = self.dublin_core
if self.extensions:
Expand Down
2 changes: 1 addition & 1 deletion dapitains/app/navigation.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def generate_paths(data: List[Dict[str, Any]], path: Optional[List[int]] = None)

def recurse(items, current_path):
for index, item in enumerate(items):
ref = item.get('ref')
ref = item.get('identifier')
if ref:
# Record the path for the current reference
paths[ref] = current_path + [index]
Expand Down
2 changes: 1 addition & 1 deletion dapitains/tei/citeStructure.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class CitableUnit:
def json(self):
out = {
"citeType": self.citeType,
"ref": self.ref,
"identifier": self.ref,
"level": self.level,
"parent": self.parent
}
Expand Down
48 changes: 24 additions & 24 deletions tests/test_citeStructure.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,20 @@ def test_parsing():
assert parser.generate_xpath("Luke") == "//body/div[@n='Luke']"

assert [root.json() for root in parser.find_refs(root=TEI, structure=parser.structure)] == [
{'citeType': 'book', 'ref': 'Luke', 'parent': None, 'level': 1, 'members': [
{'citeType': 'chapter', 'ref': 'Luke 1', 'parent': 'Luke', 'level': 2, 'members': [
{'citeType': 'verse', 'ref': 'Luke 1:1', 'parent': 'Luke 1', 'level': 3},
{'citeType': 'verse', 'ref': 'Luke 1:2', 'parent': 'Luke 1', 'level': 3},
{'citeType': 'bloup', 'ref': 'Luke 1#1', 'parent': 'Luke 1', 'level': 3}
{'citeType': 'book', 'identifier': 'Luke', 'parent': None, 'level': 1, 'members': [
{'citeType': 'chapter', 'identifier': 'Luke 1', 'parent': 'Luke', 'level': 2, 'members': [
{'citeType': 'verse', 'identifier': 'Luke 1:1', 'parent': 'Luke 1', 'level': 3},
{'citeType': 'verse', 'identifier': 'Luke 1:2', 'parent': 'Luke 1', 'level': 3},
{'citeType': 'bloup', 'identifier': 'Luke 1#1', 'parent': 'Luke 1', 'level': 3}
]
}
]},
{'citeType': 'book', 'ref': 'Mark', 'parent': None, 'level': 1, 'members': [
{'citeType': 'chapter', 'ref': 'Mark 1', 'parent': 'Mark', 'level': 2, 'members': [
{'citeType': 'verse', 'ref': 'Mark 1:1', 'parent': 'Mark 1', 'level': 3},
{'citeType': 'verse', 'ref': 'Mark 1:2', 'parent': 'Mark 1', 'level': 3},
{'citeType': 'bloup', 'ref': 'Mark 1#1', 'parent': 'Mark 1', 'level': 3},
{'citeType': 'verse', 'ref': 'Mark 1:3', 'parent': 'Mark 1', 'level': 3}
{'citeType': 'book', 'identifier': 'Mark', 'parent': None, 'level': 1, 'members': [
{'citeType': 'chapter', 'identifier': 'Mark 1', 'parent': 'Mark', 'level': 2, 'members': [
{'citeType': 'verse', 'identifier': 'Mark 1:1', 'parent': 'Mark 1', 'level': 3},
{'citeType': 'verse', 'identifier': 'Mark 1:2', 'parent': 'Mark 1', 'level': 3},
{'citeType': 'bloup', 'identifier': 'Mark 1#1', 'parent': 'Mark 1', 'level': 3},
{'citeType': 'verse', 'identifier': 'Mark 1:3', 'parent': 'Mark 1', 'level': 3}
]}
]}
]
Expand All @@ -85,17 +85,17 @@ def test_cite_data():
refs = parser.find_refs(root=TEI, structure=parser.structure)
refs = [ref.json() for ref in refs]
assert refs == [
{'citeType': 'book', 'ref': '1', 'parent': None, 'level': 1, 'dublinCore': {
{'citeType': 'book', 'identifier': '1', 'parent': None, 'level': 1, 'dublinCore': {
'http://purl.org/dc/terms/title': ['Introduction', 'Introduction'],
'http://purl.org/dc/terms/creator': ['John Doe']}},
{'citeType': 'book', 'ref': '2', 'parent': None, 'level': 1, 'dublinCore': {'http://purl.org/dc/terms/title': ["Background", 'Contexte']}},
{'citeType': 'book', 'ref': '3', 'parent': None, 'level': 1, 'dublinCore': {
{'citeType': 'book', 'identifier': '2', 'parent': None, 'level': 1, 'dublinCore': {'http://purl.org/dc/terms/title': ["Background", 'Contexte']}},
{'citeType': 'book', 'identifier': '3', 'parent': None, 'level': 1, 'dublinCore': {
'http://purl.org/dc/terms/title': ['Methodology', 'Méthodologie'],
'http://purl.org/dc/terms/creator': ['Albert Einstein']}},
{'citeType': 'book', 'ref': '4', 'parent': None, 'level': 1, 'dublinCore': {
{'citeType': 'book', 'identifier': '4', 'parent': None, 'level': 1, 'dublinCore': {
'http://purl.org/dc/terms/title': ['Results', 'Résultats'],
'http://purl.org/dc/terms/creator': ['Isaac Newton']}},
{'citeType': 'book', 'ref': '5', 'parent': None, 'level': 1, 'dublinCore': {
{'citeType': 'book', 'identifier': '5', 'parent': None, 'level': 1, 'dublinCore': {
'http://purl.org/dc/terms/title': ['Conclusion', 'Conclusion'],
'http://purl.org/dc/terms/creator': ['Marie Curie']
}}]
Expand All @@ -109,24 +109,24 @@ def test_advanced_cite_data():
refs = parser.find_refs(root=TEI, structure=parser.structure)
refs = [ref.json() for ref in refs]
assert refs == [
{'citeType': 'part', 'ref': 'part-1', 'parent': None, 'level': 1, 'members': [
{'citeType': 'book', 'ref': 'part-1.1', 'parent': 'part-1', 'level': 2, 'dublinCore': {
{'citeType': 'part', 'identifier': 'part-1', 'parent': None, 'level': 1, 'members': [
{'citeType': 'book', 'identifier': 'part-1.1', 'parent': 'part-1', 'level': 2, 'dublinCore': {
'http://purl.org/dc/terms/title': ['Introduction', 'Introduction'],
'http://purl.org/dc/terms/creator': ['John Doe']}},
{'citeType': 'book', 'ref': 'part-1.2', 'parent': 'part-1', 'level': 2, 'dublinCore': {
{'citeType': 'book', 'identifier': 'part-1.2', 'parent': 'part-1', 'level': 2, 'dublinCore': {
'http://purl.org/dc/terms/title': ["Background", 'Contexte']
}}
], 'extension': {"http://foo.bar/part": ["1"]}},
{'citeType': 'part', 'ref': 'part-2', 'parent': None, 'level': 1, 'members': [
{'citeType': 'book', 'ref': 'part-2.3', 'parent': 'part-2', 'level': 2, 'dublinCore': {
{'citeType': 'part', 'identifier': 'part-2', 'parent': None, 'level': 1, 'members': [
{'citeType': 'book', 'identifier': 'part-2.3', 'parent': 'part-2', 'level': 2, 'dublinCore': {
'http://purl.org/dc/terms/title': ['Methodology', 'Méthodologie'],
'http://purl.org/dc/terms/creator': ['Albert Einstein']}},
{'citeType': 'book', 'ref': 'part-2.4', 'parent': 'part-2', 'level': 2, 'dublinCore': {
{'citeType': 'book', 'identifier': 'part-2.4', 'parent': 'part-2', 'level': 2, 'dublinCore': {
'http://purl.org/dc/terms/title': ['Results', 'Résultats'],
'http://purl.org/dc/terms/creator': ['Isaac Newton']}}
], 'extension': {"http://foo.bar/part": ["2"]}},
{'citeType': 'part', 'ref': 'part-3', 'parent': None, 'level': 1, 'members': [
{'citeType': 'book', 'ref': 'part-3.5', 'parent': 'part-3', 'level': 2, 'dublinCore': {
{'citeType': 'part', 'identifier': 'part-3', 'parent': None, 'level': 1, 'members': [
{'citeType': 'book', 'identifier': 'part-3.5', 'parent': 'part-3', 'level': 2, 'dublinCore': {
'http://purl.org/dc/terms/title': ['Conclusion', 'Conclusion'],
'http://purl.org/dc/terms/creator': ['Marie Curie']
}}
Expand Down
56 changes: 28 additions & 28 deletions tests/test_db_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ def test_simple_path():
}
assert strip_members(
get_member_by_path(refs[doc.default_tree], paths[doc.default_tree]["Luke"])
) == {'citeType': 'book', 'ref': 'Luke', "level": 1, "parent": None}, "Check that members are stripped"
) == {'citeType': 'book', 'identifier': 'Luke', "level": 1, "parent": None}, "Check that members are stripped"
assert get_member_by_path(
refs[doc.default_tree], paths[doc.default_tree]["Mark 1:3"]
) == {'citeType': 'verse', 'ref': 'Mark 1:3', "level": 3, "parent": "Mark 1"}
) == {'citeType': 'verse', 'identifier': 'Mark 1:3', "level": 3, "parent": "Mark 1"}


def test_navigation():
Expand All @@ -68,58 +68,58 @@ def test_navigation():
end=None,
down=1
) == ([
{'citeType': 'book', 'ref': 'Luke', "level": 1, "parent": None},
{'citeType': 'book', 'ref': 'Mark', "level": 1, "parent": None}
{'citeType': 'book', 'identifier': 'Luke', "level": 1, "parent": None},
{'citeType': 'book', 'identifier': 'Mark', "level": 1, "parent": None}
], None, None), "Check that base function works"

assert get_nav(refs[doc.default_tree], paths[doc.default_tree], start_or_ref="Luke 1:1", end="Luke 1#1", down=0) == (
[
{'citeType': 'verse', 'ref': 'Luke 1:1', "level": 3, "parent": "Luke 1"},
{'citeType': 'verse', 'ref': 'Luke 1:2', "level": 3, "parent": "Luke 1"},
{'citeType': 'bloup', 'ref': 'Luke 1#1', "level": 3, "parent": "Luke 1"}
{'citeType': 'verse', 'identifier': 'Luke 1:1', "level": 3, "parent": "Luke 1"},
{'citeType': 'verse', 'identifier': 'Luke 1:2', "level": 3, "parent": "Luke 1"},
{'citeType': 'bloup', 'identifier': 'Luke 1#1', "level": 3, "parent": "Luke 1"}
],
{'citeType': 'verse', 'ref': 'Luke 1:1', "level": 3, "parent": "Luke 1"},
{'citeType': 'bloup', 'ref': 'Luke 1#1', "level": 3, "parent": "Luke 1"}
{'citeType': 'verse', 'identifier': 'Luke 1:1', "level": 3, "parent": "Luke 1"},
{'citeType': 'bloup', 'identifier': 'Luke 1#1', "level": 3, "parent": "Luke 1"}
), "Check that ?start/end works"

assert get_nav(refs[doc.default_tree], paths[doc.default_tree], start_or_ref="Luke 1:1", end="Mark 1:2", down=0) == (
[
{'citeType': 'verse', 'ref': 'Luke 1:1', "level": 3, "parent": "Luke 1"},
{'citeType': 'verse', 'ref': 'Luke 1:2', "level": 3, "parent": "Luke 1"},
{'citeType': 'bloup', 'ref': 'Luke 1#1', "level": 3, "parent": "Luke 1"},
{'citeType': 'verse', 'ref': 'Mark 1:1', "level": 3, "parent": "Mark 1"},
{'citeType': 'verse', 'ref': 'Mark 1:2', "level": 3, "parent": "Mark 1"}
{'citeType': 'verse', 'identifier': 'Luke 1:1', "level": 3, "parent": "Luke 1"},
{'citeType': 'verse', 'identifier': 'Luke 1:2', "level": 3, "parent": "Luke 1"},
{'citeType': 'bloup', 'identifier': 'Luke 1#1', "level": 3, "parent": "Luke 1"},
{'citeType': 'verse', 'identifier': 'Mark 1:1', "level": 3, "parent": "Mark 1"},
{'citeType': 'verse', 'identifier': 'Mark 1:2', "level": 3, "parent": "Mark 1"}
],
{'citeType': 'verse', 'ref': 'Luke 1:1', "level": 3, "parent": "Luke 1"},
{'citeType': 'verse', 'ref': 'Mark 1:2', "level": 3, "parent": "Mark 1"}
{'citeType': 'verse', 'identifier': 'Luke 1:1', "level": 3, "parent": "Luke 1"},
{'citeType': 'verse', 'identifier': 'Mark 1:2', "level": 3, "parent": "Mark 1"}
), "Check that ?start/end works across parents"

assert get_nav(refs[doc.default_tree], paths[doc.default_tree], start_or_ref="Luke 1", down=1) == (
[
{'citeType': 'chapter', 'ref': 'Luke 1', "level": 2, "parent": "Luke"},
{'citeType': 'verse', 'ref': 'Luke 1:1', "level": 3, "parent": "Luke 1"},
{'citeType': 'verse', 'ref': 'Luke 1:2', "level": 3, "parent": "Luke 1"},
{'citeType': 'bloup', 'ref': 'Luke 1#1', "level": 3, "parent": "Luke 1"}
{'citeType': 'chapter', 'identifier': 'Luke 1', "level": 2, "parent": "Luke"},
{'citeType': 'verse', 'identifier': 'Luke 1:1', "level": 3, "parent": "Luke 1"},
{'citeType': 'verse', 'identifier': 'Luke 1:2', "level": 3, "parent": "Luke 1"},
{'citeType': 'bloup', 'identifier': 'Luke 1#1', "level": 3, "parent": "Luke 1"}
],
{'citeType': 'chapter', 'ref': 'Luke 1', "level": 2, "parent": "Luke"},
{'citeType': 'chapter', 'identifier': 'Luke 1', "level": 2, "parent": "Luke"},
None
), "Check that ?ref works"

assert get_nav(refs[doc.default_tree], paths[doc.default_tree], start_or_ref="Luke", down=1) == (
[
{'citeType': 'book', 'ref': 'Luke', "level": 1, "parent": None},
{'citeType': 'chapter', 'ref': 'Luke 1', "level": 2, "parent": "Luke"},
{'citeType': 'book', 'identifier': 'Luke', "level": 1, "parent": None},
{'citeType': 'chapter', 'identifier': 'Luke 1', "level": 2, "parent": "Luke"},
],
{'citeType': 'book', 'ref': 'Luke', "level": 1, "parent": None},
{'citeType': 'book', 'identifier': 'Luke', "level": 1, "parent": None},
None
), "Check that ?ref works"

assert get_nav(refs[doc.default_tree], paths[doc.default_tree], start_or_ref=None, end=None, down=2) == (
[
{'citeType': 'book', 'ref': 'Luke', "level": 1, "parent": None},
{'citeType': 'chapter', 'ref': 'Luke 1', "level": 2, "parent": "Luke"},
{'citeType': 'book', 'ref': 'Mark', "level": 1, "parent": None},
{'citeType': 'chapter', 'ref': 'Mark 1', "level": 2, "parent": "Mark"}
{'citeType': 'book', 'identifier': 'Luke', "level": 1, "parent": None},
{'citeType': 'chapter', 'identifier': 'Luke 1', "level": 2, "parent": "Luke"},
{'citeType': 'book', 'identifier': 'Mark', "level": 1, "parent": None},
{'citeType': 'chapter', 'identifier': 'Mark 1', "level": 2, "parent": "Mark"}
],
None,
None
Expand Down

0 comments on commit 9d9a99c

Please sign in to comment.