Skip to content

Commit

Permalink
Fixed some properties to work better, mostly reference
Browse files Browse the repository at this point in the history
  • Loading branch information
PonteIneptique committed Sep 1, 2018
1 parent 6511558 commit a447578
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 70 deletions.
90 changes: 49 additions & 41 deletions MyCapytain/common/reference/_base.py
Expand Up @@ -5,47 +5,6 @@
from abc import abstractmethod


class BaseReference(tuple):
def __new__(cls, start, end=None):
obj = tuple.__new__(cls, (start, end))

return obj

def is_range(self):
return bool(self[1])

@property
def start(self):
""" Quick access property for start part
:rtype: str
"""
return self[0]

@property
def end(self):
""" Quick access property for reference end list
:rtype: str
"""
return self[1]


class BaseReferenceSet(list):
@property
def citation(self):
return self._citation

def __new__(cls, *refs, citation=None):
obj = list.__new__(cls, refs)
obj._citation = None

if citation:
obj._citation = citation

return obj


class BaseCitationSet(Exportable):
""" A citation set is a collection of citations that optionnaly can be matched using a .match() function
Expand Down Expand Up @@ -337,6 +296,55 @@ def depth(self):
return 1


class BaseReference(tuple):
def __new__(cls, *refs):
if len(refs) == 1 and not isinstance(refs[0], tuple):
refs = refs[0], None
obj = tuple.__new__(cls, refs)

return obj

def is_range(self):
return bool(self[1])

@property
def start(self):
""" Quick access property for start part
:rtype: str
"""
return self[0]

@property
def end(self):
""" Quick access property for reference end list
:rtype: str
"""
return self[1]


class BaseReferenceSet(tuple):
def __new__(cls, *refs, citation: BaseCitationSet=None, level: int=1):
if len(refs) == 1 and not isinstance(refs, BaseReference):
refs = refs[0]
obj = tuple.__new__(cls, refs)
obj._citation = None
obj._level = level

if citation:
obj._citation = citation
return obj

@property
def citation(self) -> BaseCitationSet:
return self._citation

@property
def level(self) -> int:
return self._level


class NodeId(object):
""" Collection of directional references for a Tree
Expand Down
38 changes: 26 additions & 12 deletions MyCapytain/common/reference/_capitains_cts.py
Expand Up @@ -114,27 +114,41 @@ class CtsReference(BaseReference):
>>> ref = CtsReference('1.2.3')
"""

def __new__(cls, reference: Union[str, Tuple[str, Optional[str]]]):
def __new__(cls, *references):
# pickle.load will try to feed the tuple back !
if isinstance(reference, tuple):
if reference[1]:
if len(references) == 2:
start, end = references
o = BaseReference.__new__(
CtsReference,
CtsSinglePassageId(start),
CtsSinglePassageId(end)
)
o._str_repr = start + "-" + end
return o

references, *_ = references
if isinstance(references, tuple):
if references[1]:
o = BaseReference.__new__(
CtsReference,
CtsSinglePassageId(reference[0]),
CtsSinglePassageId(reference[1])
CtsSinglePassageId(references[0]),
CtsSinglePassageId(references[1])
)
else:
o = BaseReference.__new__(
CtsReference,
CtsSinglePassageId(reference[0])
CtsSinglePassageId(references[0])
)
elif "-" not in reference:
o = BaseReference.__new__(CtsReference, CtsSinglePassageId(reference))
else:
_start, _end = tuple(reference.split("-"))
o = BaseReference.__new__(CtsReference, CtsSinglePassageId(_start), CtsSinglePassageId(_end))
o._str_repr = references

elif isinstance(references, str):
if "-" not in references:
o = BaseReference.__new__(CtsReference, CtsSinglePassageId(references))
else:
_start, _end = tuple(references.split("-"))
o = BaseReference.__new__(CtsReference, CtsSinglePassageId(_start), CtsSinglePassageId(_end))
o._str_repr = references

o._str_repr = reference
return o

@property
Expand Down
1 change: 1 addition & 0 deletions MyCapytain/common/utils.py
Expand Up @@ -25,6 +25,7 @@
import link_header

from MyCapytain.common.constants import XPATH_NAMESPACES
from MyCapytain.errors import CapitainsXPathError

__strip = re.compile("([ ]{2,})+")
__parser__ = etree.XMLParser(collect_ids=False, resolve_entities=False)
Expand Down
9 changes: 9 additions & 0 deletions MyCapytain/errors.py
Expand Up @@ -79,3 +79,12 @@ class CitationDepthError(UnknownObjectError, MyCapytainException):
class MissingRefsDecl(Exception, MyCapytainException):
""" A text has no properly encoded refsDecl
"""


class CapitainsXPathError(Exception):
def __init__(self, message):
super(CapitainsXPathError, self).__init__()
self.message = message

def __repr__(self):
return "CapitainsXPathError("+self.message+")"
11 changes: 8 additions & 3 deletions MyCapytain/resources/texts/local/capitains/cts.py
Expand Up @@ -162,7 +162,7 @@ def getReffs(self, level=1, subreference=None) -> CtsReferenceSet:
if not subreference:
if hasattr(self, "reference"):
subreference = self.reference
else:
elif not isinstance(subreference, CtsReference):
subreference = CtsReference(subreference)
return self.getValidReff(level, subreference)

Expand Down Expand Up @@ -212,7 +212,7 @@ def getValidReff(self, level: int=None, reference: CtsReference=None, _debug: bo
if level <= len(passages[0]) and reference is not None:
level = len(passages[0]) + 1
if level > len(self.citation):
return CtsReferenceSet()
raise CitationDepthError("The required level is too deep")

nodes = [None] * (level - depth)

Expand Down Expand Up @@ -255,7 +255,12 @@ def getValidReff(self, level: int=None, reference: CtsReference=None, _debug: bo
print(empties)
warnings.warn(message, EmptyReference)

return CtsReferenceSet([CtsReference(reff) for reff in passages])
references = CtsReferenceSet(
[CtsReference(reff) for reff in passages],
citation=self.citation,
level=level
)
return references

def xpath(self, *args, **kwargs):
""" Perform XPath on the passage XML
Expand Down
26 changes: 12 additions & 14 deletions tests/resources/texts/local/commonTests.py
Expand Up @@ -10,7 +10,7 @@

import MyCapytain.errors
from MyCapytain.common.constants import Mimetypes
from MyCapytain.common.reference._capitains_cts import CtsReference, URN, Citation
from MyCapytain.common.reference._capitains_cts import CtsReference, URN, Citation, CtsReferenceSet
from MyCapytain.resources.texts.local.capitains.cts import CapitainsCtsText


Expand Down Expand Up @@ -131,7 +131,7 @@ def testValidReffs(self):
)
self.assertEqual(
self.TEI.getValidReff(reference=CtsReference("2.38-2.39"), level=3),
[CtsReference("2.38.1"), CtsReference("2.38.2"), CtsReference("2.39.1"), CtsReference("2.39.2")]
(CtsReference("2.38.1"), CtsReference("2.38.2"), CtsReference("2.39.1"), CtsReference("2.39.2"))
)

# Test with reference and level autocorrected because too small
Expand All @@ -148,34 +148,32 @@ def testValidReffs(self):

self.assertEqual(
self.TEI.getValidReff(reference=CtsReference("2.1-2.2")),
[CtsReference(ref) for ref in[
CtsReferenceSet(CtsReference(ref) for ref in[
'2.1.1', '2.1.2', '2.1.3', '2.1.4', '2.1.5', '2.1.6', '2.1.7', '2.1.8', '2.1.9', '2.1.10', '2.1.11',
'2.1.12', '2.2.1', '2.2.2', '2.2.3', '2.2.4', '2.2.5', '2.2.6'
]],
]),
"It could be possible to ask for range reffs children")

self.assertEqual(
self.TEI.getValidReff(reference=CtsReference("2.1-2.2"), level=2),
[CtsReference('2.1'), CtsReference('2.2')],
CtsReferenceSet(CtsReference('2.1'), CtsReference('2.2')),
"It could be possible to ask for range References reference at the same level in between milestone")

self.assertEqual(
self.TEI.getValidReff(reference=CtsReference("1.38-2.2"), level=2),
[CtsReference(ref) for ref in ['1.38', '1.39', '2.pr', '2.1', '2.2']],
CtsReferenceSet(CtsReference(ref) for ref in ['1.38', '1.39', '2.pr', '2.1', '2.2']),
"It could be possible to ask for range References reference at the same level in between milestone "
"across higher levels")

self.assertEqual(
self.TEI.getValidReff(reference=CtsReference("1.1.1-1.1.4"), level=3),
[CtsReference(ref) for ref in ['1.1.1', '1.1.2', '1.1.3', '1.1.4']],
CtsReferenceSet(CtsReference(ref) for ref in ['1.1.1', '1.1.2', '1.1.3', '1.1.4']),
"It could be possible to ask for range reffs in between at the same level cross higher level")

# Test when already too deep
self.assertEqual(
self.TEI.getValidReff(reference=CtsReference("2.1.1"), level=3),
[],
# Test level too deep
with self.assertRaises(MyCapytain.errors.CitationDepthError):
"Asking for a level too deep should return nothing"
)
self.TEI.getValidReff(reference=CtsReference("2.1.1"), level=3)

# Test wrong citation
with self.assertRaises(KeyError):
Expand Down Expand Up @@ -264,11 +262,11 @@ def test_xml_with_xml_id(self):
"Word should be there !"
)
self.assertEqual(
text.getReffs(level=2), [CtsReference(ref) for ref in [
text.getReffs(level=2), CtsReferenceSet(CtsReference(ref) for ref in [
'1.C_w_000001', '1.C_w_000002', '1.C_w_000003', '1.C_w_000004', '1.C_w_000005',
'1.C_w_000006', '1.C_w_000007', '2.C_w_000008', '2.C_w_000009', '2.C_w_000010',
'2.C_w_000011', '2.C_w_000012', '2.C_w_000013', '2.C_w_000014'
]],
]),
"XML:IDs and N should be retrieved."
)

Expand Down

0 comments on commit a447578

Please sign in to comment.