Skip to content

Commit 15bb570

Browse files
authored
Merge pull request #325 from fschrader1992/master
RDF Converter, XML Reader Updates
2 parents 2777b48 + b86309e commit 15bb570

File tree

6 files changed

+92
-26
lines changed

6 files changed

+92
-26
lines changed

MANIFEST.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
include LICENSE
22
include README.rst
33
include odml/info.json
4+
include doc/section_subclasses.yaml

odml/doc.py

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -141,21 +141,22 @@ def get_terminology_equivalent(self):
141141
return None
142142
term = terminology.load(self.repository)
143143
return term
144-
def pprint(self, indent=2, max_depth=1, max_length=80, current_depth=0):
145-
"""
146-
Pretty print method to visualize Document-Section trees.
147-
148-
:param indent: number of leading spaces for every child Section or Property.
149-
:param max_depth: maximum number of hierarchical levels printed from the
150-
starting Section.
151-
:param max_length: maximum number of characters printed in one line.
152-
:param current_depth: number of hierarchical levels printed from the
153-
starting Section.
154-
"""
155-
doc_str = "[{} [{}] {}, sections: {}, repository: {}]".format(self.author, self.version,
156-
self.date, len(self._sections), self.repository)
157-
print(doc_str)
158144

159-
for s in self._sections:
160-
s.pprint(current_depth=current_depth+1, max_depth=max_depth,
161-
indent=indent, max_length=max_length)
145+
def pprint(self, indent=2, max_depth=1, max_length=80, current_depth=0):
146+
"""
147+
Pretty print method to visualize Document-Section trees.
148+
149+
:param indent: number of leading spaces for every child Section or Property.
150+
:param max_depth: maximum number of hierarchical levels printed from the
151+
starting Section.
152+
:param max_length: maximum number of characters printed in one line.
153+
:param current_depth: number of hierarchical levels printed from the
154+
starting Section.
155+
"""
156+
doc_str = "[{} [{}] {}, sections: {}, repository: {}]".format(self.author, self.version,
157+
self.date, len(self._sections), self.repository)
158+
print(doc_str)
159+
160+
for s in self._sections:
161+
s.pprint(current_depth=current_depth+1, max_depth=max_depth,
162+
indent=indent, max_length=max_length)

odml/tools/rdf_converter.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,6 @@ def __init__(self, odml_documents):
4343

4444
self.section_subclasses = {}
4545

46-
# TODO doc/section_subclasses.yaml has to be exported on install, otherwise
47-
# the RDFWriter is broken. Below is a quick and dirty fix to at least
48-
# unbreak on install.
4946
subclass_path = os.path.join(dirname(dirname(dirname(abspath(__file__)))),
5047
'doc', 'section_subclasses.yaml')
5148

odml/tools/xmlparser.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -218,12 +218,12 @@ def from_string(self, string):
218218
def check_mandatory_arguments(self, data, ArgClass, tag_name, node):
219219
for k, v in ArgClass.arguments:
220220
if v != 0 and not ArgClass.map(k) in data:
221-
self.error("missing element <%s> within <%s> tag" %
221+
self.error("missing element <%s> within <%s> tag\n" %
222222
(k, tag_name) + repr(data), node)
223223

224224
def is_valid_argument(self, tag_name, ArgClass, parent_node, child=None):
225225
if tag_name not in ArgClass.arguments_keys:
226-
self.error("Invalid element <%s> inside <%s> tag" %
226+
self.error("Invalid element <%s> inside <%s> tag\n" %
227227
(tag_name, parent_node.tag),
228228
parent_node if child is None else child)
229229

@@ -247,7 +247,7 @@ def warn(self, msg, elem):
247247

248248
def parse_element(self, node):
249249
if node.tag not in self.tags:
250-
self.error("Invalid element <%s>" % node.tag, node)
250+
self.error("Invalid element <%s> " % node.tag, node)
251251
return None # won't be able to parse this one
252252
return getattr(self, "parse_" + node.tag)(node, self.tags[node.tag])
253253

@@ -273,7 +273,7 @@ def parse_tag(self, root, fmt, insert_children=True):
273273
continue
274274

275275
# We currently do not support XML attributes.
276-
self.error("Attribute not supported, ignoring '%s=%s'" % (k, v), root)
276+
self.error("Attribute not supported, ignoring '%s=%s' " % (k, v), root)
277277

278278
for node in root:
279279
node.tag = node.tag.lower()
@@ -299,7 +299,7 @@ def parse_tag(self, root, fmt, insert_children=True):
299299
else:
300300
arguments[tag] = curr_text
301301
else:
302-
self.error("Invalid element <%s> in odML document section <%s>"
302+
self.error("Invalid element <%s> in odML document section <%s> "
303303
% (node.tag, root.tag), node)
304304

305305
if sys.version_info > (3,):
@@ -310,7 +310,11 @@ def parse_tag(self, root, fmt, insert_children=True):
310310
self.check_mandatory_arguments(check_args, fmt, root.tag, root)
311311

312312
# Instantiate the current odML object with the parsed attributes.
313-
obj = fmt.create(**arguments)
313+
obj = fmt.create()
314+
try:
315+
obj = fmt.create(**arguments)
316+
except Exception as e:
317+
self.error(str(e), root)
314318

315319
if insert_children:
316320
for child in children:

test/resources/ignore_errors.xml

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<?xml-stylesheet type="text/xsl" href="odmlTerms.xsl"?>
3+
<?xml-stylesheet type="text/xsl" href="odml.xsl"?>
4+
<odML version="1.1">
5+
<author>D. N. Adams</author>
6+
<repository>hello</repository>
7+
<section>
8+
<property>
9+
<type>int</type>
10+
<value>123</value>
11+
</property>
12+
<property>
13+
<type>int</type>
14+
<uncertainty>[zergrve sdic, 1]</uncertainty>
15+
<value>["string 1", "string 2"]</value>
16+
<reference>The Hitchhiker's guide to the Galaxy (novel)</reference>
17+
<id>wrongid</id>
18+
<name>NoCrewMembers</name>
19+
<definition>Number of crew members</definition>
20+
</property>
21+
<property>
22+
<name>NoBones</name>
23+
<type></type>
24+
<value>235</value>
25+
<id>wrongid4</id>
26+
<definition>Number of bones within the crew member.</definition>
27+
</property>
28+
<type>crew</type>
29+
<definition>Information on the crew</definition>
30+
<id>0f96a050-2d9b-498f-a532-fbfcc6aae55e</id>
31+
<name>TheCrew</name>
32+
<section>
33+
<property>
34+
<type>string</type>
35+
<value>[1, 2]</value>
36+
<id>wrongid2</id>
37+
<name>Nickname</name>
38+
<definition>Nickname(s) of the subject</definition>
39+
</property>
40+
<property>
41+
<type>int</type>
42+
<value>535</value>
43+
<id>wrongid3</id>
44+
<definition>Nickname(s) of the subject</definition>
45+
</property>
46+
<type>ADent</type>
47+
<definition>Information on Arthur Dent</definition>
48+
</section>
49+
</section>
50+
<date>ztuz</date>
51+
<id>79b613eb-a256-46bf-84f6-207df465b8f7</id>
52+
<version>new version</version>
53+
</odML>

test/test_parser_xml.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ def setUp(self):
1212
self.basepath = os.path.join(dir_path, "resources")
1313

1414
self.xml_reader = xmlparser.XMLReader()
15+
self.xml_reader_ignore = xmlparser.XMLReader(ignore_errors=True)
1516

1617
def test_invalid_root(self):
1718
filename = "invalid_root.xml"
@@ -36,3 +37,12 @@ def test_invalid_version(self):
3637

3738
with self.assertRaises(InvalidVersionException):
3839
_ = self.xml_reader.from_file(os.path.join(self.basepath, filename))
40+
41+
def test_ignore_errors(self):
42+
filename = "ignore_errors.xml"
43+
44+
with self.assertRaises(ParserException):
45+
_ = self.xml_reader.from_file(os.path.join(self.basepath, filename))
46+
47+
doc = self.xml_reader_ignore.from_file(os.path.join(self.basepath, filename))
48+
doc.pprint()

0 commit comments

Comments
 (0)