Skip to content

Commit

Permalink
Some style fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
PonteIneptique committed Dec 6, 2016
1 parent d8ca233 commit b40072f
Show file tree
Hide file tree
Showing 12 changed files with 222 additions and 108 deletions.
2 changes: 0 additions & 2 deletions MyCapytain/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,4 @@
"""

__name__ = "MyCapytain"
__version__ = "2.0.0b0"
__all__ = ["common", "retrievers", "resources", "resolvers"]
105 changes: 71 additions & 34 deletions MyCapytain/common/reference.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,9 @@
.. moduleauthor:: Thibault Clérice <leponteineptique@gmail.com>
>>> from MyCapytain.common.reference import URN, Reference, Citation
"""
from __future__ import unicode_literals

from past.builtins import basestring
from six import text_type as str
from builtins import range, object
from six import text_type
from copy import copy
import re
from lxml.etree import _Element
Expand Down Expand Up @@ -73,9 +68,9 @@ class Reference(object):
def __init__(self, reference=""):
self.reference = reference
if reference == "":
self.parsed = (self.__model(), self.__model())
self.parsed = (self.__model__(), self.__model__())
else:
self.parsed = self.__parse(reference)
self.parsed = self.__parse__(reference)

@property
def parent(self):
Expand Down Expand Up @@ -182,7 +177,7 @@ def __len__(self):
def __str__(self):
""" Return full reference in string format
:rtype: basestring
:rtype: str
:returns: String representation of Reference Object
:Example:
Expand Down Expand Up @@ -219,9 +214,10 @@ def __ne__(self, other):
"""
return not self.__eq__(other)

def __model(self):
@staticmethod
def __model__():
""" 3-Tuple model for references
First element is full text reference,
Second is list of passage identifiers
Third is subreference
Expand All @@ -231,29 +227,33 @@ def __model(self):
"""
return [None, [], None, None]

def __regexp(self, subreference):
""" Split components of subreference
def __regexp__(self, subreference):
""" Split components of subreference
:param subreference: A subreference
:type subreference: basestring
:type subreference: str
:rtype: List.<Tuple>
:returns: List where first element is a tuple representing different components
"""
return SUBREFERENCE.findall(subreference)[0]

def __parse(self, reference):
""" Parse references informations
def __parse__(self, reference):
""" Parse references information
:param reference: String representation of a reference
:type reference: str
:returns: Tuple representing each part of the reference
:rtype: tuple(str)
"""

ref = reference.split("-")
element = [self.__model(), self.__model()]
element = [self.__model__(), self.__model__()]
for i in range(0, len(ref)):
r = ref[i]
element[i][0] = r
subreference = r.split("@")
if len(subreference) == 2:
element[i][2] = self.__regexp(subreference[1])
element[i][2] = self.__regexp__(subreference[1])
element[i][3] = "@" + subreference[1]
r = subreference[0]
element[i][1] = r.split(".")
Expand All @@ -262,6 +262,13 @@ def __parse(self, reference):

@staticmethod
def convert_subreference(word, counter):
""" Convert a word and a counter into a standard tuple representation
:param word: Word Element of the subreference
:param counter: Index of the Word
:return: Tuple representing the element
:rtype: (str, int)
"""
if len(counter) and word:
return str(word), int(counter)
elif len(counter) == 0 and word:
Expand All @@ -271,8 +278,7 @@ def convert_subreference(word, counter):


class URN(object):

""" A URN object giving all useful sections
""" A URN object giving all useful sections
:param urn: A CTS URN
:type urn: str
Expand Down Expand Up @@ -331,6 +337,11 @@ def __init__(self, urn):

@property
def urn_namespace(self):
""" General Namespace element of the URN
:rtype: str
:return: Namespace part of the URN
"""
return self.__parsed["urn_namespace"]

@urn_namespace.setter
Expand All @@ -340,6 +351,11 @@ def urn_namespace(self, value):

@property
def namespace(self):
""" CTS Namespace element of the URN
:rtype: str
:return: Namespace part of the URN
"""
return self.__parsed["cts_namespace"]

@namespace.setter
Expand All @@ -349,6 +365,11 @@ def namespace(self, value):

@property
def textgroup(self):
""" Textgroup element of the URN
:rtype: str
:return: Textgroup part of the URN
"""
return self.__parsed["textgroup"]

@textgroup.setter
Expand All @@ -358,6 +379,11 @@ def textgroup(self, value):

@property
def work(self):
""" Work element of the URN
:rtype: str
:return: Work part of the URN
"""
return self.__parsed["work"]

@work.setter
Expand All @@ -367,6 +393,11 @@ def work(self, value):

@property
def version(self):
""" Version element of the URN
:rtype: str
:return: Version part of the URN
"""
return self.__parsed["version"]

@version.setter
Expand All @@ -376,6 +407,11 @@ def version(self, value):

@property
def reference(self):
""" Reference element of the URN
:rtype: Reference
:return: Reference part of the URN
"""
return self.__parsed["reference"]

@reference.setter
Expand All @@ -395,7 +431,7 @@ def __len__(self):
.. warning:: Does not take into account the passage !
:Example:
>>> a = URN(urn="urn:cts:latinLit:phi1294.phi002.perseus-lat2:1.1")
>>> a = URN(urn="urn:cts:latinLit:phi1294.phi002.perseus-lat2:1.1")
>>> print(len(a))
"""

Expand All @@ -419,7 +455,7 @@ def __gt__(self, other):
:Example:
>>> a = URN(urn="urn:cts:latinLit:phi1294.phi002.perseus-lat2:1.1")
>>> b = URN(urn="urn:cts:latinLit:phi1294.phi002:1.1")
>>> (a > b) == True #
>>> (a > b) == True
"""
return len(self) > len(other)

Expand Down Expand Up @@ -571,6 +607,10 @@ def upTo(self, key):

@staticmethod
def model():
""" Generate a standard dictionary model for URN inside function
:return: Dictionary of CTS elements
"""
return {
"urn_namespace": None,
"cts_namespace": None,
Expand Down Expand Up @@ -612,7 +652,7 @@ def __parse__(self, urn):


class Citation(object):
""" A citation object gives informations about the scheme
""" A citation object gives informations about the scheme
:param name: Name of the citation (e.g. "book")
:type name: basestring
Expand Down Expand Up @@ -658,11 +698,11 @@ def __init__(self, name=None, xpath=None, scope=None, refsDecl=None, child=None)
self.child = child

@property
def name(self):
def name(self):
""" Type of the citation represented
:type: basestring
:Example: Book, Chapter, Textpart, Section, Poem...
:type: text_type
:example: Book, Chapter, Textpart, Section, Poem...
"""
return self.__name

Expand Down Expand Up @@ -702,9 +742,9 @@ def scope(self, val):

@property
def refsDecl(self):
""" ResfDecl expression of the citation scheme
""" ResfDecl expression of the citation scheme
:type: basestring
:rtype: str
:Example: /tei:TEI/tei:text/tei:body/tei:div//tei:l[@n='$1']
"""
return self.__refsDecl
Expand Down Expand Up @@ -814,7 +854,7 @@ def fill(self, passage=None, xpath=None):

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

return REFERENCE_REPLACER.sub(replacement, xpath)
Expand All @@ -835,17 +875,14 @@ def fill(self, passage=None, xpath=None):
def __getstate__(self):
""" Pickling method
:return:
:return: dict
"""
return copy(self.__dict__)

def __setstate__(self, dic):
self.__dict__ = dic
return self

""" Implementation of Citation for TEI markup
"""

def isEmpty(self):
""" Check if the citation has not been set
Expand Down

0 comments on commit b40072f

Please sign in to comment.