diff --git a/MANIFEST.in b/MANIFEST.in index 00ee1263..cdd06f95 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -1,3 +1,4 @@ include LICENSE include README.rst include odml/info.json +include doc/section_subclasses.yaml diff --git a/odml/doc.py b/odml/doc.py index 4a33ccea..9fb01ce9 100644 --- a/odml/doc.py +++ b/odml/doc.py @@ -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) diff --git a/odml/tools/rdf_converter.py b/odml/tools/rdf_converter.py index 143691ef..ceb18885 100644 --- a/odml/tools/rdf_converter.py +++ b/odml/tools/rdf_converter.py @@ -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') diff --git a/odml/tools/xmlparser.py b/odml/tools/xmlparser.py index 70b3bb3d..1ecea9c2 100644 --- a/odml/tools/xmlparser.py +++ b/odml/tools/xmlparser.py @@ -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) @@ -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]) @@ -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() @@ -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,): @@ -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: diff --git a/test/resources/ignore_errors.xml b/test/resources/ignore_errors.xml new file mode 100644 index 00000000..36d3680e --- /dev/null +++ b/test/resources/ignore_errors.xml @@ -0,0 +1,53 @@ + + + + + D. N. Adams + hello +
+ + int + 123 + + + int + [zergrve sdic, 1] + ["string 1", "string 2"] + The Hitchhiker's guide to the Galaxy (novel) + wrongid + NoCrewMembers + Number of crew members + + + NoBones + + 235 + wrongid4 + Number of bones within the crew member. + + crew + Information on the crew + 0f96a050-2d9b-498f-a532-fbfcc6aae55e + TheCrew +
+ + string + [1, 2] + wrongid2 + Nickname + Nickname(s) of the subject + + + int + 535 + wrongid3 + Nickname(s) of the subject + + ADent + Information on Arthur Dent +
+
+ ztuz + 79b613eb-a256-46bf-84f6-207df465b8f7 + new version +
diff --git a/test/test_parser_xml.py b/test/test_parser_xml.py index c7bbb747..a83ac349 100644 --- a/test/test_parser_xml.py +++ b/test/test_parser_xml.py @@ -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" @@ -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()