def _parse(self, path): try: doc = etree.parse(path) except etree.LxmlError as err: # A workaround for cases when lxml (quite strangely) # sets the filename to <string>. if err.error_log[0].filename == "<string>": filename = self._path else: filename = err.error_log[0].filename loc = { "file": os.path.basename(filename), "line": err.error_log[0].line, "col": err.error_log[0].column } exc = XmlProcessingError(err.error_log[0].message) exc.set_loc(loc) raise exc except Exception as err: loc = { "file": os.path.basename(self._path), "line": None, "col": None } exc = XmlProcessingError(str(err)) exc.set_loc(loc) raise exc return doc
def _parse(self, path): try: if path.startswith('https'): doc = etree.parse(urlopen(path)) else: doc = etree.parse(path) except etree.LxmlError as err: # A workaround for cases when lxml (quite strangely) # sets the filename to <string>. if err.error_log[0].filename == "<string>": filename = self._path else: filename = err.error_log[0].filename loc = {"file": os.path.basename(filename), "line": err.error_log[0].line, "col": err.error_log[0].column} exc = XmlProcessingError(err.error_log[0].message) exc.set_loc(loc) raise exc except Exception as err: loc = {"file": os.path.basename(self._path), "line": None, "col": None} exc = XmlProcessingError(str(err)) exc.set_loc(loc) raise exc return doc
def parse(self): doc = self._parse(self._path) self._remove_comments(doc) # Due to a weird implementation of XInclude in lxml, the # XmlParser resolves included documents on it's own. # # To be able to tell later on where each tag was located # in the XML document, we add a '__file' attribute to # each element of the tree during the parsing. # # However, these special attributes are of course not # valid according to our schemas. To solve this, a copy of # the tree is made and the '__file' attributes are removed # before validation. # # XXX This is a *EXTREMELY* dirty hack. Ideas/proposals # for cleaner solutions are more than welcome! root_tag = self._init_loc(doc.getroot(), self._path) self._expand_xinclude(root_tag, os.path.dirname(self._path)) self._template_proc.process_aliases(root_tag) try: self._validate(doc) except: err = self._schema.error_log[0] loc = {"file": os.path.basename(err.filename), "line": err.line, "col": err.column} exc = XmlProcessingError(err.message) exc.set_loc(loc) raise exc return self._process(root_tag)
def parse(self): doc = self._parse(self._path) self._remove_comments(doc) # Due to a weird implementation of XInclude in lxml, the # XmlParser resolves included documents on it's own. # # To be able to tell later on where each tag was located # in the XML document, we add a '__file' attribute to # each element of the tree during the parsing. # # However, these special attributes are of course not # valid according to our schemas. To solve this, a copy of # the tree is made and the '__file' attributes are removed # before validation. # # XXX This is a *EXTREMELY* dirty hack. Ideas/proposals # for cleaner solutions are more than welcome! root_tag = self._init_loc(doc.getroot(), self._path) self._expand_xinclude(root_tag, os.path.dirname(self._path)) self._template_proc.process_aliases(root_tag) try: self._validate(doc) except: err = self._schema.error_log[0] loc = { "file": os.path.basename(err.filename), "line": err.line, "col": err.column } exc = XmlProcessingError(err.message) exc.set_loc(loc) raise exc return self._process(root_tag)