Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
include LICENSE
include README.rst
include odml/info.json
include doc/section_subclasses.yaml
35 changes: 18 additions & 17 deletions odml/doc.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,21 +141,22 @@ def get_terminology_equivalent(self):
return None
term = terminology.load(self.repository)
return term
def pprint(self, indent=2, max_depth=1, max_length=80, current_depth=0):
"""
Pretty print method to visualize Document-Section trees.

:param indent: number of leading spaces for every child Section or Property.
:param max_depth: maximum number of hierarchical levels printed from the
starting Section.
:param max_length: maximum number of characters printed in one line.
:param current_depth: number of hierarchical levels printed from the
starting Section.
"""
doc_str = "[{} [{}] {}, sections: {}, repository: {}]".format(self.author, self.version,
self.date, len(self._sections), self.repository)
print(doc_str)

for s in self._sections:
s.pprint(current_depth=current_depth+1, max_depth=max_depth,
indent=indent, max_length=max_length)
def pprint(self, indent=2, max_depth=1, max_length=80, current_depth=0):
"""
Pretty print method to visualize Document-Section trees.

:param indent: number of leading spaces for every child Section or Property.
:param max_depth: maximum number of hierarchical levels printed from the
starting Section.
:param max_length: maximum number of characters printed in one line.
:param current_depth: number of hierarchical levels printed from the
starting Section.
"""
doc_str = "[{} [{}] {}, sections: {}, repository: {}]".format(self.author, self.version,
self.date, len(self._sections), self.repository)
print(doc_str)

for s in self._sections:
s.pprint(current_depth=current_depth+1, max_depth=max_depth,
indent=indent, max_length=max_length)
3 changes: 0 additions & 3 deletions odml/tools/rdf_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,6 @@ def __init__(self, odml_documents):

self.section_subclasses = {}

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

Expand Down
16 changes: 10 additions & 6 deletions odml/tools/xmlparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,12 +218,12 @@ def from_string(self, string):
def check_mandatory_arguments(self, data, ArgClass, tag_name, node):
for k, v in ArgClass.arguments:
if v != 0 and not ArgClass.map(k) in data:
self.error("missing element <%s> within <%s> tag" %
self.error("missing element <%s> within <%s> tag\n" %
(k, tag_name) + repr(data), node)

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

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

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

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

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

for node in root:
node.tag = node.tag.lower()
Expand All @@ -299,7 +299,7 @@ def parse_tag(self, root, fmt, insert_children=True):
else:
arguments[tag] = curr_text
else:
self.error("Invalid element <%s> in odML document section <%s>"
self.error("Invalid element <%s> in odML document section <%s> "
% (node.tag, root.tag), node)

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

# Instantiate the current odML object with the parsed attributes.
obj = fmt.create(**arguments)
obj = fmt.create()
try:
obj = fmt.create(**arguments)
except Exception as e:
self.error(str(e), root)

if insert_children:
for child in children:
Expand Down
53 changes: 53 additions & 0 deletions test/resources/ignore_errors.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="odmlTerms.xsl"?>
<?xml-stylesheet type="text/xsl" href="odml.xsl"?>
<odML version="1.1">
<author>D. N. Adams</author>
<repository>hello</repository>
<section>
<property>
<type>int</type>
<value>123</value>
</property>
<property>
<type>int</type>
<uncertainty>[zergrve sdic, 1]</uncertainty>
<value>["string 1", "string 2"]</value>
<reference>The Hitchhiker's guide to the Galaxy (novel)</reference>
<id>wrongid</id>
<name>NoCrewMembers</name>
<definition>Number of crew members</definition>
</property>
<property>
<name>NoBones</name>
<type></type>
<value>235</value>
<id>wrongid4</id>
<definition>Number of bones within the crew member.</definition>
</property>
<type>crew</type>
<definition>Information on the crew</definition>
<id>0f96a050-2d9b-498f-a532-fbfcc6aae55e</id>
<name>TheCrew</name>
<section>
<property>
<type>string</type>
<value>[1, 2]</value>
<id>wrongid2</id>
<name>Nickname</name>
<definition>Nickname(s) of the subject</definition>
</property>
<property>
<type>int</type>
<value>535</value>
<id>wrongid3</id>
<definition>Nickname(s) of the subject</definition>
</property>
<type>ADent</type>
<definition>Information on Arthur Dent</definition>
</section>
</section>
<date>ztuz</date>
<id>79b613eb-a256-46bf-84f6-207df465b8f7</id>
<version>new version</version>
</odML>
10 changes: 10 additions & 0 deletions test/test_parser_xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ def setUp(self):
self.basepath = os.path.join(dir_path, "resources")

self.xml_reader = xmlparser.XMLReader()
self.xml_reader_ignore = xmlparser.XMLReader(ignore_errors=True)

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

with self.assertRaises(InvalidVersionException):
_ = self.xml_reader.from_file(os.path.join(self.basepath, filename))

def test_ignore_errors(self):
filename = "ignore_errors.xml"

with self.assertRaises(ParserException):
_ = self.xml_reader.from_file(os.path.join(self.basepath, filename))

doc = self.xml_reader_ignore.from_file(os.path.join(self.basepath, filename))
doc.pprint()