def test_from_lxml(self): text = '<DEFINE name="http.error.file.404" value="404.html" />' definition = Definition.from_lxml_element(etree.XML(text)) self.assertTrue(isinstance(definition, DefinitionElement)) text = '<DEFINE name="x"><DEFINE value="y" /><DEFINE value="z" /></DEFINE>' definition = Definition.from_lxml_element(etree.XML(text)) self.assertTrue(isinstance(definition, DefinitionTree))
def from_lxml_element(root): '''Factory to produce MIMEType from lxml.etree.Element object.''' if root.tag != 'MIME': raise AttributeError('Expected MIME tag.') mime = root.get('mime', None) handler = root.get('handler', None) param = root.get('param', None) self_executed = root.get('self', None) known_attrib = set(['mime', 'handler', 'param', 'self']) custom_attrib = {} for key, value in root.attrib.iteritems(): if key not in known_attrib: custom_attrib[key] = value path = None extension = set() filters = [] definitions = [] custom = [] for child in list(root): if child.tag == 'PATH': path = child.get('regex') elif child.tag == 'FILTER': filters.append(child.get('value')) elif child.tag == 'EXTENSION': extension.add(child.get('value')) elif child.tag == 'DEFINE': definitions.append(Definition.from_lxml_element(child)) else: custom.append(child) mime = MIMEType(mime, handler, param, extension, path, filters, self_executed, definitions) mime.custom = custom mime.custom_attrib = custom_attrib return mime
def from_lxml_element(root): '''Factory to produce MIMEType from lxml.etree.Element object.''' if root.tag != 'MIME': raise AttributeError('Expected MIME tag.') mime = root.get('mime', None) handler = root.get('handler', None) param = root.get('param', None) self_executed = root.get('self', None) known_attrib = set(['mime', 'handler', 'param', 'self']) custom_attrib = {} for key, value in root.attrib.iteritems(): if key not in known_attrib: custom_attrib[key] = value path = None extension = set() filters = [] definitions = [] custom = [] for child in list(root): if child.tag == 'PATH': path = child.get('regex') elif child.tag == 'FILTER': filters.append(child.get('value')) elif child.tag == 'EXTENSION': extension.add(child.get('value')) elif child.tag == 'DEFINE': definitions.append(Definition.from_lxml_element(child)) else: custom.append(child) mime = MIMEType(mime, handler, param, extension, path, filters, self_executed, definitions) mime.custom = custom mime.custom_attrib = custom_attrib return mime
def test_to_string(self): def_list = DefinitionList(self.definitions) text = '<wrap>{0}</wrap>'.format(def_list) copy = DefinitionList() for lxml_definition in list(etree.XML(text)): copy.add_definition(Definition.from_lxml_element(lxml_definition)) self.assertEqual(def_list, copy)
def from_lxml_element(root): if root.tag == 'USER': return User.from_lxml_element(root) if root.tag == 'CONDITION': return Condition.from_lxml_element(root) elif root.tag == 'PERMISSION': return Permission.from_lxml_element(root) elif root.tag == 'RETURN': return Return.from_lxml_element(root) elif root.tag == 'DEFINE': return Definition.from_lxml_element(root) else: raise AttributeError( '{0} is not allowed in security files'.format(root.tag))
def from_lxml_element(root): if root.tag == 'USER': return User.from_lxml_element(root) if root.tag == 'CONDITION': return Condition.from_lxml_element(root) elif root.tag == 'PERMISSION': return Permission.from_lxml_element(root) elif root.tag == 'RETURN': return Return.from_lxml_element(root) elif root.tag == 'DEFINE': return Definition.from_lxml_element(root) else: raise AttributeError( '{0} is not allowed in security files'.format(root.tag))
def test_bad_root_tag(self): text = '<ERROR name="http.error.file.404" value="404.html" />' self.assertEqual('ERROR', Definition.from_string(text).tag) self.assertEqual('ERROR', Definition.from_lxml_element( etree.XML(text)).tag)