Skip to content

Commit

Permalink
Merge pull request #171 from Capitains/empty_reference_master
Browse files Browse the repository at this point in the history
EmptyReference exception along with unit tests
  • Loading branch information
PonteIneptique committed Jun 22, 2018
2 parents 32780b5 + fcd7ec5 commit 2366ebe
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
### 2018-06-22 2.0.7 @sonofmun

- Added exception for empty references

### 2017-11-16 2.0.6 @ponteineptique

- Added a way to specify the joining string for Plaintext export in TEIResource (Fixed #146)
Expand Down
2 changes: 1 addition & 1 deletion MyCapytain/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@
"""

__version__ = "2.0.6"
__version__ = "2.0.7"
4 changes: 4 additions & 0 deletions MyCapytain/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,7 @@ class UndispatchedTextError(Exception, MyCapytainException):
class UnknownCollection(KeyError, MyCapytainException):
""" A collection is unknown to its ancestor
"""

class EmptyReference(SyntaxWarning, MyCapytainException):
""" Error generated when a duplicate is found in Reference
"""
8 changes: 6 additions & 2 deletions MyCapytain/resources/texts/local/capitains/cts.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

import warnings

from MyCapytain.errors import DuplicateReference, MissingAttribute, RefsDeclError
from MyCapytain.errors import DuplicateReference, MissingAttribute, RefsDeclError, EmptyReference
from MyCapytain.common.utils import copyNode, passageLoop, normalizeXpath
from MyCapytain.common.constants import XPATH_NAMESPACES, RDF_NAMESPACES
from MyCapytain.common.reference import URN, Citation, Reference
Expand Down Expand Up @@ -248,6 +248,10 @@ def getValidReff(self, level=None, reference=None, _debug=False):
message = ", ".join(duplicates)
warnings.warn(message, DuplicateReference)
del duplicates
empties = [n for n in passages if n.strip('.') != n or n == '']
if len(empties) > 0:
message = '{} empty reference(s) at citation level {}'.format(len(empties), level)
warnings.warn(message, EmptyReference)

return passages

Expand Down Expand Up @@ -664,4 +668,4 @@ def textObject(self):
:rtype: CapitainsCtsText
"""
return self.__text__
return self.__text__
18 changes: 17 additions & 1 deletion tests/resources/texts/local/commonTests.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,22 @@ def test_warning(self):
"Warning should be DuplicateReference")
self.assertEqual(str(w[0].message), "1", "Warning message should be list of duplicate")

def test_empty_ref_warning(self):
with open("tests/testing_data/texts/empty_references.xml") as xml:
text = CapitainsCtsText(resource=xml)
with warnings.catch_warnings(record=True) as w:
# Cause all warnings to always be triggered.
warnings.simplefilter("always")
for i in [1, 2, 3]:
text.getValidReff(level=i, _debug=True)

self.assertEqual(len(w), 3, "There should be warning on each level")
self.assertEqual(issubclass(w[-1].category, MyCapytain.errors.EmptyReference), True,
"Warning should be EmptyReference")
self.assertEqual(str(w[0].message), "1 empty reference(s) at citation level 1",
"Warning message should indicate number of references and the level at which they occur")


def test_wrong_main_scope(self):
with open("tests/testing_data/texts/sample2.xml", "rb") as f:
with self.assertRaises(MyCapytain.errors.RefsDeclError):
Expand Down Expand Up @@ -836,4 +852,4 @@ def test_first_list(self):
self.assertEqual(
str(passage.lastId), "2.40.8",
"Last reff should be the last"
)
)
88 changes: 88 additions & 0 deletions tests/testing_data/texts/empty_references.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?xml-model href="http://www.stoa.org/epidoc/schema/8.19/tei-epidoc.rng"
schematypens="http://relaxng.org/ns/structure/1.0"?><?xml-model href="http://www.stoa.org/epidoc/schema/8.19/tei-epidoc.rng"
schematypens="http://purl.oclc.org/dsdl/schematron"?>
<TEI xmlns="http://www.tei-c.org/ns/1.0">
<teiHeader type="text">
<fileDesc>
<titleStmt>
<title>Epigrammata</title>
<author>Martial</author>
<sponsor>Perseus Project, Tufts University</sponsor>
<principal>Gregory Crane</principal>
</titleStmt>
<publicationStmt>
<publisher>Trustees of Tufts University</publisher>
<pubPlace>Medford, MA</pubPlace>
<authority>Perseus Project</authority>
</publicationStmt>
<sourceDesc default="false">
<bibl default="false">
<title>M. Valerii Martialis Epigrammaton libri / recognovit W. Heraeus</title>
<author>Martial</author>
<editor role="editor">Wilhelm Heraeus<!--, 1862-1938 (main editor)--></editor>
<editor role="editor">Jacobus Borovskij<!-- (added corrections in 1976, more than 30 years before 2011)--></editor>
<pubPlace>Leipzig</pubPlace>
<date>1925/1976</date>
</bibl>
</sourceDesc>
</fileDesc>
<encodingDesc>
<refsDecl n="CTS">
<cRefPattern n="line"
matchPattern="(\w+).(\w+).(\w+)"
replacementPattern="#xpath(/tei:TEI/tei:text/tei:body/tei:div/tei:div[@n='$1']/tei:div[@n='$2']/tei:l[@n='$3'])">
<p>This pointer pattern extracts book and poem and line</p>
</cRefPattern>
<cRefPattern n="poem"
matchPattern="(\w+).(\w+)"
replacementPattern="#xpath(/tei:TEI/tei:text/tei:body/tei:div/tei:div[@n='$1']/tei:div[@n='$2'])">
<p>This pointer pattern extracts book and poem</p>
</cRefPattern>
<cRefPattern n="book"
matchPattern="(\w+)"
replacementPattern="#xpath(/tei:TEI/tei:text/tei:body/tei:div/tei:div[@n='$1'])">
<p>This pointer pattern extracts book</p>
</cRefPattern>
</refsDecl>
<refsDecl xml:id="TEI.2">
<refState unit="book"/>
<refState unit="poem"/>
<refState unit="line"/>
</refsDecl>
</encodingDesc>
<profileDesc>
<langUsage default="false">
<language ident="eng">English</language>
<language ident="grc">Latin</language>
</langUsage>
</profileDesc>
<revisionDesc>
<change when="2008-01-01" who="Gregory Crane">Initial Markup</change>
<change when="2015-03-06" who="Thibault Clérice">converted to EpiDoc/CTS</change>
</revisionDesc>
</teiHeader>
<text n="urn:cts:latinLit:phi1294.phi002.perseus-lat2"

xml:id="stoa0045.stoa0">
<body>
<div type="edition" n="urn:cts:latinLit:phi1294.phi002.perseus-lat2" xml:lang="lat">
<div type="textpart" subtype="book" n="1">
<div type="textpart" subtype="poem" n="pr">
<l n="">Spero me secutum in libellis meis tale temperamen-</l>
</div>
</div>
<div type="textpart" subtype="book" n="2">
<div type="textpart" subtype="poem" n="">
<l n="1">Spero me secutum in libellis meis tale temperamen-</l>
</div>
</div>
<div type="textpart" subtype="book" n="">
<div type="textpart" subtype="poem" n="1">
<l n="1">Spero me secutum in libellis meis tale temperamen-</l>
</div>
</div>
</div>
</body>
</text>
</TEI>

0 comments on commit 2366ebe

Please sign in to comment.