Skip to content

Commit

Permalink
Merge pull request #26 from Capitains/dev
Browse files Browse the repository at this point in the history
Texts first implementation
  • Loading branch information
PonteIneptique committed Jul 15, 2015
2 parents e659a9b + 85b919d commit 79e1871
Show file tree
Hide file tree
Showing 18 changed files with 1,573 additions and 33 deletions.
57 changes: 56 additions & 1 deletion MyCapytain/common/reference.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
"""
from __future__ import unicode_literals


from collections import defaultdict
from past.builtins import basestring
from builtins import range, object
Expand All @@ -20,6 +19,7 @@
REFSDECL_SPLITTER = re.compile("/+[a-zA-Z0-9:\[\]@=\\\{\$'\"\.]+")
REFSDECL_REPLACER = re.compile("\$[0-9]+")
SUBREFERENCE = re.compile("(\w*)\[{0,1}([0-9]*)\]{0,1}", re.UNICODE)
REFERENCE_REPLACER = re.compile("(@[a-zA-Z0-9:]+){1}(=){1}([\\\$'\"?0-9]{3,6})")

class Reference(object):

Expand Down Expand Up @@ -468,6 +468,7 @@ class Citation(object):
:type child: Citation
.. automethod:: __iter__
.. automethod:: __len__
"""

def __init__(self, name=None, xpath=None, scope=None, refsDecl=None, child=None):
Expand Down Expand Up @@ -601,3 +602,57 @@ def __iter__(self):
e = e.child
else:
break

def __len__(self):
""" Length method
:rtype: int
:returns: Number of nested citations
"""
return len([item for item in self])

def fill(self, passage=None, xpath=None):
""" Fill the xpath with given informations
:param passage: Passage reference
:type passage: Reference or lsit
:param xpath: If set to True, will return the replaced self.xpath value and not the whole self.refsDecl
:type xpath: Boolean
:rtype: basestring
:returns: Xpath to find the passage
"""
if xpath is True: # Then passage is a string or None
xpath = self.xpath

if passage is None:
replacement = r"\1"
elif isinstance(passage, basestring):
replacement = r"\1\2'" + passage + "'"

return REFERENCE_REPLACER.sub(replacement, xpath)
else:
if isinstance(passage, Reference):
passage = passage[2]
passage = iter(passage)
return REFERENCE_REPLACER.sub(
lambda m: REF_REPLACER(m, passage),
self.refsDecl
)

def REF_REPLACER(match, passage):
""" Helper to replace xpath/scope/refsDecl on iteration with passage value
:param match: A RegExp match
:type match: re.SRE_MATCH
:param passage: A list with subreference informations
:type passage: iter
:rtype: basestring
:return: Replaced string
"""
groups = match.groups()
ref = next(passage)
if ref is None:
return groups[0]
else:
return "{1}='{0}'".format(ref, groups[0])
File renamed without changes.
4 changes: 2 additions & 2 deletions MyCapytain/resources/proto/inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ def __getitem__(self, key):
def __eq__(self, other):
if self is other:
return True
if not isinstance(other, self.__class__):
elif not isinstance(other, self.__class__):
return False
if self.resource is None:
elif self.resource is None:
# Not totally true
return (hasattr(self, "urn") and hasattr(other, "urn") and self.urn == other.urn)
return (hasattr(self, "urn") and hasattr(other, "urn") and self.urn == other.urn) and self.resource == other.resource
Expand Down
119 changes: 91 additions & 28 deletions MyCapytain/resources/proto/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,59 @@
"""
from . import inventory
from past.builtins import basestring

from MyCapytain.resources.proto import inventory
import MyCapytain.common.reference

class Resource(object):
def __init__(self, urn, resource= None):
""" Initiate a Resource object
:param resource: A resource
:type resource: Any
""" Initiate a Resource object
:param urn: A URN identifier
:type urn: MyCapytain.common.reference.URN
:param resource: A resource
:type resource: Any
"""

def __init__(self, urn=None, resource= None):
self.resource = None
self._URN = None

if urn is not None:
self._URN = urn

if resource is not None:
self.resource = resource

@property
def urn(self):
""" URN Identifier of the object
:rtype: MyCapytain.common.reference.URN
"""
self.urn = urn
self.resource = resource
return self._URN

@urn.setter
def urn(self, value):
""" Set the urn
:param value: URN to be saved
:type value: MyCapytain.common.reference.URN
:raises: *TypeError* when the value is not URN compatible
"""
if isinstance(value, basestring):
value = MyCapytain.common.reference.URN(value)
elif not isinstance(value, MyCapytain.common.reference.URN):
raise TypeError()
self._URN = value


class Passage(Resource):
def __init__(self, parent=None, **kwargs):
super(Passage, self).__init__(**kwargs)

def setText(self):
raise NotImplementedError()

Expand All @@ -34,25 +74,22 @@ def getPrev(self):

class Text(Resource):
""" A CTS Text """

def __parseReference(self, reference):
""" Parse a CTS URN
:param reference: A urn or a passage reference
:type reference: str
:rtype: List.str
:returns: List of references
"""
if ":" in reference:
reference = reference.split(":")[-1]
return reference.split("-")
def __init__(self, citation=None, **kwargs):
super(Text, self).__init__(**kwargs)

self._cRefPattern = MyCapytain.common.reference.Citation()

def getValidReff(self, level = None):
if citation is not None:
self.citation = citation

def getValidReff(self, level=1, passage=None):
""" Given a resource, Text will compute valid reffs
:param level: Depth required. If not set, should retrieve deeper level
:param level: Depth required. If not set, should retrieve first encountered level (1 based)
:type level: Int
:rtype: List
:param passage: Subreference (optional)
:type passage: Reference
:rtype: List.basestring
:returns: List of levels
"""
raise NotImplementedError()
Expand All @@ -61,18 +98,44 @@ def getPassage(self, reference):
""" Retrieve a passage and store it in the object
:param reference: Reference of the passage
:type reference: str
:type reference: MyCapytain.common.reference.Reference or List of basestring
:rtype: Passage
:returns: Object representing the passage
:raises: *TypeError* when reference is not a list or a Reference
"""

raise NotImplementedError()

def getLabel(self):
""" Retrieve a passage and store it in the object
""" Retrieve metadata about the text
:param reference: Reference of the passage
:type reference: str
:rtype: dict
:returns: Dictionary with label informations
"""
raise NotImplementedError()
raise NotImplementedError()

@property
def reffs(self):
""" Get the lowest cRefPattern in the hierarchy
:rtype: MyCapytain.resources.texts.tei.Citation
"""
return [reff for reffs in [self.getValidReff(level=i) for i in range(1, len(self.citation) + 1)] for reff in reffs]

@property
def citation(self):
""" Get the lowest cRefPattern in the hierarchy
:rtype: MyCapytain.common.reference.Citation
"""
return self._cRefPattern

@citation.setter
def citation(self, value):
""" Set the cRefPattern
:param value: Citation to be saved
:type value: MyCapytain.common.reference.Citation
"""
if isinstance(value, MyCapytain.common.reference.Citation):
self._cRefPattern = value
Empty file.

0 comments on commit 79e1871

Please sign in to comment.