-
Notifications
You must be signed in to change notification settings - Fork 9
/
text.py
174 lines (136 loc) · 5.97 KB
/
text.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
from typing import Union
from MyCapytain.common.reference import URN, BaseReferenceSet, BaseReference
from MyCapytain.common.constants import RDF_NAMESPACES
from ..metadata import Collection
from rdflib import Literal
from .inventory import CtsTextMetadata
from ..text import InteractiveTextualNode
__all__ = [
"PrototypeCtsNode",
"PrototypeCtsText",
"PrototypeCtsPassage"
]
class PrototypeCtsNode(InteractiveTextualNode):
""" Initiate a Resource object
:param urn: A URN identifier
:type urn: MyCapytain.common.reference._capitains_cts.URN
:param metadata: Collection Information about the Item
:type metadata: Collection
:param citation: XmlCtsCitation system of the text
:type citation: Citation
:param children: Current node Children's Identifier
:type children: [str]
:param parent: Parent of the current node
:type parent: str
:param siblings: Previous and next node of the current node
:type siblings: str
:param depth: Depth of the node in the global hierarchy of the text tree
:type depth: int
:param resource: Resource used to navigate through the textual graph
:cvar default_exclude: Default exclude for exports
"""
def __init__(self, urn: Union[URN, str] = None, **kwargs):
super(PrototypeCtsNode, self).__init__(identifier=str(urn), **kwargs)
self._urn = None
if urn is not None:
self.urn = urn
@property
def urn(self) -> URN:
""" URN Identifier of the object
"""
return self._urn
@urn.setter
def urn(self, value: Union[URN, str]):
""" Set the urn
:param value: URN to be saved
:raises: *TypeError* when the value is not URN compatible
"""
if isinstance(value, str):
value = URN(value)
elif not isinstance(value, URN):
raise TypeError("New urn must be string or {} instead of {}".format(type(URN), type(value)))
self._urn = value
def get_cts_metadata(self, key: str, lang: str = None) -> Literal:
""" Get easily a metadata from the CTS namespace
:param key: CTS property to retrieve
:param lang: Language in which it should be
:return: Literal value of the CTS graph property
"""
return self.metadata.get_single(RDF_NAMESPACES.CTS.term(key), lang)
def getValidReff(self, level: int = 1, reference: BaseReference = None) -> BaseReferenceSet:
""" Given a resource, CtsText will compute valid reffs
:param level: Depth required. If not set, should retrieve first encountered level (1 based)
:param reference: Subreference (optional)
:returns: List of levels
"""
raise NotImplementedError()
def getLabel(self) -> Collection:
""" Retrieve metadata about the text
:rtype: Collection
:returns: Retrieve Label informations in a Collection format
"""
raise NotImplementedError()
def set_metadata_from_collection(self, text_metadata: CtsTextMetadata):
""" Set the object metadata using its collections recursively
:param text_metadata: Object representing the current text as a collection
:type text_metadata: CtsTextMetadata
"""
edition, work, textgroup = tuple(([text_metadata] + text_metadata.parents)[:3])
for node in textgroup.metadata.get(RDF_NAMESPACES.CTS.groupname):
lang = node.language
self.metadata.add(RDF_NAMESPACES.CTS.groupname, lang=lang, value=str(node))
self.set_creator(str(node), lang)
for node in work.metadata.get(RDF_NAMESPACES.CTS.title):
lang = node.language
self.metadata.add(RDF_NAMESPACES.CTS.title, lang=lang, value=str(node))
self.set_title(str(node), lang)
for node in edition.metadata.get(RDF_NAMESPACES.CTS.label):
lang = node.language
self.metadata.add(RDF_NAMESPACES.CTS.label, lang=lang, value=str(node))
self.set_subject(str(node), lang)
for node in edition.metadata.get(RDF_NAMESPACES.CTS.description):
lang = node.language
self.metadata.add(RDF_NAMESPACES.CTS.description, lang=lang, value=str(node))
self.set_description(str(node), lang)
if not self.citation.is_set() and edition.citation.is_set():
self.citation = edition.citation
class PrototypeCtsPassage(PrototypeCtsNode):
""" CapitainsCtsPassage objects possess metadata informations
:param urn: A URN identifier
:type urn: MyCapytain.common.reference._capitains_cts.URN
:param metadata: Collection Information about the Item
:type metadata: Collection
:param citation: XmlCtsCitation system of the text
:type citation: Citation
:param children: Current node Children's Identifier
:type children: [str]
:param parent: Parent of the current node
:type parent: str
:param siblings: Previous and next node of the current node
:type siblings: str
:param depth: Depth of the node in the global hierarchy of the text tree
:type depth: int
:param resource: Resource used to navigate through the textual graph
:cvar default_exclude: Default exclude for exports
"""
def __init__(self, **kwargs):
super(PrototypeCtsPassage, self).__init__(**kwargs)
@property
def reference(self) -> BaseReference:
return self.urn.reference
class PrototypeCtsText(PrototypeCtsNode):
""" A CTS CtsText
"""
def __init__(self, citation=None, metadata=None, **kwargs):
super(PrototypeCtsText, self).__init__(citation=citation, metadata=metadata, **kwargs)
self._cts = None
@property
def reffs(self) -> BaseReferenceSet:
""" Get all valid reffs for every part of the CtsText
:rtype: [str]
"""
if not self._cts:
self._cts = BaseReferenceSet(
[reff for reffs in [self.getReffs(level=i) for i in range(1, len(self.citation) + 1)] for reff in reffs]
)
return self._cts