def _get_obj_from_str_id(str_id, dtd_url=None, dtd_str=None): # Will raise an exception if both dtd_url or dtd_str are None or set dic = dtd_parser.parse(dtd_url=dtd_url, dtd_str=dtd_str) splitted = str_id.split(":") prefixes = [] s = splitted.pop(0) prefixes += [s] cls = dic[s] obj = cls() index = None while splitted: s = splitted.pop(0) prefixes += [s] tmp_cls = obj._get_sub_element(s) if not tmp_cls: raise Exception("Unsupported tag %s" % s) if issubclass(tmp_cls, ListElement): # Remove the id index = splitted.pop(0) if len(splitted) > 1: prefixes += [index] index = None s = splitted.pop(0) prefixes += [s] obj = obj.add(s) return obj, prefixes, index
def _get_obj_from_str_id(str_id, dtd_url=None, dtd_str=None, data=None): """Load object according to the given str_id ..note:: If data is passed load the data to this object """ # Will raise an exception if both dtd_url or dtd_str are None or set dic = dtd_parser.parse(dtd_url=dtd_url, dtd_str=dtd_str) splitted = str_id.split(':') s = splitted.pop(0) cls = dic[s] obj = cls() if data: obj.load_from_dict(data) index = None while splitted: s = splitted.pop(0) obj = obj.get_or_add(s, index=index) if len(splitted) > 0: index = None if isinstance(obj, list): index = int(splitted.pop(0)) if isinstance(obj, elements.TextElement) and obj.text is None: obj.set_text('') 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 update(filename, data, validate=True, transform=None): """Update the file named filename with data. :param filename: the XML filename we should update :param data: the result of the submitted data. :param validate: validate the updated XML before writing it. :type filename: str :type data: dict style like: dict, webob.MultiDict, ... :type validate: bool :param transform: function to transform the XML string just before writing it. :type transform: function :return: the object generated from the data :rtype: :class:`Element` """ data = utils.unflatten_params(data) encoding = data.pop('_xml_encoding') dtd_url = data.pop('_xml_dtd_url') if len(data) != 1: raise Exception('Bad data') root_tag = data.keys()[0] dic = dtd_parser.parse(dtd_url=dtd_url, path=os.path.dirname(filename)) obj = dic[root_tag]() obj.load_from_dict(data) obj.write(filename, encoding, dtd_url=dtd_url, validate=validate, transform=transform) return obj
def new(dtd_url, root_tag, form_action=None, form_attrs=None): dic = dtd_parser.parse(dtd_url=dtd_url) obj = dic[root_tag]() # Merge the following line with the function which generate the form! hidden_inputs = ('<input type="hidden" name="_xml_filename" ' 'id="_xml_filename" value="" />' '<input type="hidden" name="_xml_dtd_url" ' 'id="_xml_dtd_url" value="%s" />' '<input type="hidden" name="_xml_encoding" ' 'id="_xml_encoding" value="%s" />') % ( dtd_url, elements.DEFAULT_ENCODING, ) attrs = {} if form_attrs: attrs = form_attrs.copy() if 'id' not in attrs: attrs['id'] = 'xmltool-form' if form_action: attrs['action'] = form_action attrs_str = ' '.join(['%s="%s"' % tple for tple in attrs.items()]) html = ['<form method="POST" %s>' % attrs_str] html += [hidden_inputs] html += [obj._to_html()] html += ['</form>'] return ''.join(html)
def new(dtd_url, root_tag, form_action=None): dic = dtd_parser.parse(dtd_url=dtd_url) obj = dic[root_tag]() # Merge the following line with the function which generate the form! hidden_inputs = ('<input type="hidden" name="_xml_filename" ' 'id="_xml_filename" value="" />' '<input type="hidden" name="_xml_dtd_url" ' 'id="_xml_dtd_url" value="%s" />' '<input type="hidden" name="_xml_encoding" ' 'id="_xml_encoding" value="%s" />') % ( dtd_url, elements.DEFAULT_ENCODING, ) html = [] if form_action: html += [ '<form action="%s" method="POST" ' 'id="xmltool-form">' % form_action ] else: html += ['<form method="POST" id="xmltool-form">'] html += [hidden_inputs] html += [obj.to_html()] html += ['</form>'] return ''.join(html)
def new(dtd_url, root_tag, form_action=None): dic = dtd_parser.parse(dtd_url=dtd_url) obj = dic[root_tag]() # Merge the following line with the function which generate the form! hidden_inputs = ( '<input type="hidden" name="_xml_filename" ' 'id="_xml_filename" value="" />' '<input type="hidden" name="_xml_dtd_url" ' 'id="_xml_dtd_url" value="%s" />' '<input type="hidden" name="_xml_encoding" ' 'id="_xml_encoding" value="%s" />' ) % ( dtd_url, elements.DEFAULT_ENCODING, ) html = [] if form_action: html += ['<form action="%s" method="POST" ' 'id="xmltool-form">' % form_action] else: html += ['<form method="POST" id="xmltool-form">'] html += [hidden_inputs] html += [obj.to_html()] html += ['</form>'] return ''.join(html)
def update(filename, data, validate=True, transform=None): """Update the file named filename with data. :param filename: the XML filename we should update :param data: the result of the submitted data. :param validate: validate the updated XML before writing it. :type filename: str :type data: dict style like: dict, webob.MultiDict, ... :type validate: bool :param transform: function to transform the XML string just before writing it. :type transform: function :return: the object generated from the data :rtype: :class:`Element` """ data = utils.unflatten_params(data) encoding = data.pop('_xml_encoding') dtd_url = data.pop('_xml_dtd_url') if len(data) != 1: raise Exception('Bad data') root_tag = data.keys()[0] dic = dtd_parser.parse(dtd_url=dtd_url) obj = dic[root_tag]() obj.load_from_dict(data) obj.write(filename, encoding, dtd_url, validate, transform) 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) 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 _get_obj_from_str_id(str_id, dtd_url=None, dtd_str=None): # Will raise an exception if both dtd_url or dtd_str are None or set dic = dtd_parser.parse(dtd_url=dtd_url, dtd_str=dtd_str) splitted = str_id.split(':') prefixes = [] s = splitted.pop(0) prefixes += [s] cls = dic[s] obj = cls() index = None while splitted: s = splitted.pop(0) prefixes += [s] tmp_cls = obj._get_sub_element(s) if not tmp_cls: raise Exception('Unsupported tag %s' % s) if issubclass(tmp_cls, ListElement): # Remove the id index = splitted.pop(0) if len(splitted) > 1: prefixes += [index] index = None s = splitted.pop(0) prefixes += [s] obj = obj.add(s) return obj, prefixes, index
def new(dtd_url, root_tag, form_action=None, form_attrs=None): dic = dtd_parser.parse(dtd_url=dtd_url) obj = dic[root_tag]() # Merge the following line with the function which generate the form! hidden_inputs = ( '<input type="hidden" name="_xml_filename" ' 'id="_xml_filename" value="" />' '<input type="hidden" name="_xml_dtd_url" ' 'id="_xml_dtd_url" value="%s" />' '<input type="hidden" name="_xml_encoding" ' 'id="_xml_encoding" value="%s" />' ) % ( dtd_url, elements.DEFAULT_ENCODING, ) attrs = {} if form_attrs: attrs = form_attrs.copy() if 'id' not in attrs: attrs['id'] = 'xmltool-form' if form_action: attrs['action'] = form_action attrs_str = ' '.join(['%s="%s"' % tple for tple in attrs.items()]) html = ['<form method="POST" %s>' % attrs_str] html += [hidden_inputs] html += [obj._to_html()] html += ['</form>'] return ''.join(html)
def create(root_tag, dtd_url=None, dtd_str=None): """Create a python object for the given root_tag :param root_tag: The root tag to create :param dtd_url: The dtd url :param dtd_str: The dtd as string """ dic = dtd_parser.parse(dtd_url=dtd_url, dtd_str=dtd_str) if root_tag not in dic: raise Exception('Bad root_tag %s, ' 'it\'s not supported by the dtd' % root_tag) obj = dic[root_tag]() obj.dtd_url = dtd_url obj.dtd_str = dtd_str obj.encoding = elements.DEFAULT_ENCODING return obj