def load(filename, validate=True): """Generate a python object :param filename: the XML filename we should load :param validate: validate the XML before generating the python object. :type filename: str :type validate: bool :return: the generated python object :rtype: :class:`Element` """ tree = etree.parse(filename) dtd_url = tree.docinfo.system_url path = isinstance(filename, basestring) and os.path.dirname(filename) or None dtd_str = utils.get_dtd_content(dtd_url, path) if validate: utils.validate_xml(tree, dtd_str) dic = dtd_parser.parse(dtd_str=dtd_str) root = tree.getroot() obj = dic[root.tag]() obj.load_from_xml(root) obj._xml_filename = filename obj._xml_dtd_url = dtd_url obj._xml_encoding = tree.docinfo.encoding return obj
def load(filename, validate=True): """Generate a python object :param filename: the XML filename we should load :param validate: validate the XML before generating the python object. :type filename: str :type validate: bool :return: the generated python object :rtype: :class:`Element` """ tree = etree.parse(filename) dtd_url = tree.docinfo.system_url path = isinstance(filename, basestring) and os.path.dirname(filename) or None dtd_str = utils.get_dtd_content(dtd_url, path) if validate: utils.validate_xml(tree, dtd_str) dic = dtd_parser.parse(dtd_str=dtd_str, cache_key='xmltool.parse.%s' % dtd_url) root = tree.getroot() obj = dic[root.tag]() obj.load_from_xml(root) obj.filename = filename obj.dtd_url = dtd_url obj.encoding = tree.docinfo.encoding return obj
def write(self, filename=None, encoding=None, dtd_url=None, validate=True, transform=None): filename = filename or self._xml_filename if not filename: raise Exception('No filename given') dtd_url = dtd_url or self._xml_dtd_url if not dtd_url: raise Exception('No dtd url given') encoding = encoding or self._xml_encoding or DEFAULT_ENCODING xml = self.to_xml() if validate: dtd_str = utils.get_dtd_content(dtd_url, os.path.dirname(filename)) utils.validate_xml(xml, dtd_str) doctype = ('<!DOCTYPE %(root_tag)s SYSTEM "%(dtd_url)s">' % { 'root_tag': self._tagname, 'dtd_url': dtd_url, }) xml_str = etree.tostring(xml.getroottree(), pretty_print=True, xml_declaration=True, encoding=encoding, doctype=doctype) if transform: xml_str = transform(xml_str) open(filename, 'w').write(xml_str)
def write(self, filename=None, encoding=None, dtd_url=None, dtd_str=None, validate=True, transform=None): filename = filename or self.filename if not filename: raise Exception('No filename given') dtd_url = dtd_url or self.dtd_url dtd_str = dtd_str or self.dtd_str if not dtd_url and not dtd_str: raise Exception('No dtd given') encoding = encoding or self.encoding or DEFAULT_ENCODING xml = self.to_xml() if validate: if not dtd_str: dtd_str = utils.get_dtd_content(dtd_url, os.path.dirname(filename)) utils.validate_xml(xml, dtd_str) doctype = '<!DOCTYPE %(root_tag)s SYSTEM "%(dtd_url)s">' % { 'root_tag': self.tagname, 'dtd_url': dtd_url } # Some etree versions are not valid according to StrictVersion so we # split it. etree_version = '.'.join(etree.__version__.split('.')[:2]) if StrictVersion(etree_version) < StrictVersion('2.3'): xml_str = etree.tostring( xml.getroottree(), pretty_print=True, xml_declaration=True, encoding=encoding, ) xml_str = xml_str.replace('<%s' % self.tagname, '%s\n<%s' % (doctype, self.tagname)) else: xml_str = etree.tostring(xml.getroottree(), pretty_print=True, xml_declaration=True, encoding=encoding, doctype=doctype) if transform: xml_str = transform(xml_str) open(filename, 'w').write(xml_str)
def load_archetypes_from_xml(self, xml_fname, abilities, fail_fast): """ Load all the archetypes in an xml file. """ archetypes_in_file = [] # parse the xml doc = parse_xml(xml_fname) if doc is None: raise Exception("Errors in %s" % xml_fname) # validate error_log = validate_xml(doc) if error_log is not None: print("Errors (XSD)!") print(error_log) raise Exception("Errors in %s" % xml_fname) root = doc.getroot() archetype_nodes = root.xpath("//archetype") for archetype_node in archetype_nodes: archetype = Archetype(abilities, xml_fname) archetype.load(archetype_node, fail_fast) archetypes_in_file.append(archetype) return archetypes_in_file
def validate(self): valid = True error_log = validate_xml(self.doc) if error_log is not None: print("Errors (XSD)!") valid = False print("\t%s" % error_log) return valid
def __init__(self, fname): self.fname = fname self.weapons = [] self.doc = parse_xml(fname) result = validate_xml(self.doc) if result is not None: raise Exception(result) return
def write(self, filename=None, encoding=None, dtd_url=None, dtd_str=None, validate=True, transform=None): filename = filename or self.filename if not filename: raise Exception('No filename given') dtd_url = dtd_url or self.dtd_url dtd_str = dtd_str or self.dtd_str if not dtd_url and not dtd_str: raise Exception('No dtd given') encoding = encoding or self.encoding or DEFAULT_ENCODING xml = self.to_xml() if validate: if not dtd_str: dtd_str = utils.get_dtd_content(dtd_url, os.path.dirname(filename)) utils.validate_xml(xml, dtd_str) doctype = '<!DOCTYPE %(root_tag)s SYSTEM "%(dtd_url)s">' % { 'root_tag': self.tagname, 'dtd_url': dtd_url} # Some etree versions are not valid according to StrictVersion so we # split it. etree_version = '.'.join(etree.__version__.split('.')[:2]) if StrictVersion(etree_version) < StrictVersion('2.3'): xml_str = etree.tostring( xml.getroottree(), pretty_print=True, xml_declaration=True, encoding=encoding, ) xml_str = xml_str.replace('<%s' % self.tagname, '%s\n<%s' % (doctype, self.tagname)) else: xml_str = etree.tostring( xml.getroottree(), pretty_print=True, xml_declaration=True, encoding=encoding, doctype=doctype) if transform: xml_str = transform(xml_str) open(filename, 'w').write(xml_str)
def write(self, filename=None, encoding=None, dtd_url=None, validate=True, transform=None): filename = filename or self._xml_filename if not filename: raise Exception("No filename given") dtd_url = dtd_url or self._xml_dtd_url if not dtd_url: raise Exception("No dtd url given") encoding = encoding or self._xml_encoding or DEFAULT_ENCODING xml = self.to_xml() if validate: dtd_str = utils.get_dtd_content(dtd_url, os.path.dirname(filename)) utils.validate_xml(xml, dtd_str) doctype = '<!DOCTYPE %(root_tag)s SYSTEM "%(dtd_url)s">' % {"root_tag": self._tagname, "dtd_url": dtd_url} xml_str = etree.tostring( xml.getroottree(), pretty_print=True, xml_declaration=True, encoding=encoding, doctype=doctype ) if transform: xml_str = transform(xml_str) open(filename, "w").write(xml_str)
def validate(self): valid = True error = validate_xml(self.doc) # if there's been a validation error print some information about it if error is not None: print("Invalid xml %s!" % self.fname) print(error) # print out the context #print(get_error_context(self.fname, error.line)) valid = False return valid
def load_patron_from_xml(self, xml_fname, ability_groups, fail_fast): """ Load a patron in an xml file. """ # parse the xml doc = parse_xml(xml_fname) if doc is None: raise Exception("Errors in %s" % xml_fname) # validate error_log = validate_xml(doc) if error_log is not None: print("Errors (XSD)!") print(error_log) raise Exception("Errors in %s" % xml_fname) root = doc.getroot() patron = Patron(xml_fname) patron.load(root, ability_groups, fail_fast) return patron