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 __init__(self, definition, constants=None, variables=None): """ Creates equation. :param string definition: Equation definition of the form: :param list constants: Name of variables that must be considered constant """ if not variables: variables = [] if not constants: constants = {} Definition.__init__(self,definition) self._in_out = None self._out = None self.setup()
def __init__(self, definition, constants=None, variables=None): """ Creates equation. :param string definition: Equation definition of the form: :param list constants: Name of variables that must be considered constant """ if not variables: variables = [] if not constants: constants = {} Definition.__init__(self, definition) self._in_out = None self._out = None self.setup()
def __init__(self, definition, constants=None): """ Creates differential equation. :param string definition: Equation definition :param list constants: Name of variables that must be considered constant """ if not constants: constants = {} Definition.__init__(self, definition) self._in_out = None self._out = None self.setup() self.__method__ = self._forward_euler
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 createDefinition(self): dlg = DefinitionDialog(self, Definition('enter name', 'dir', '', '')) dlg.show() result = dlg.exec_() if result and dlg.definition: self.configuration.definitions.append(dlg.definition) self._configurationChanged()
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_definition_tree(self): text = ''' <DEFINE> <DEFINE value="x" /> <DEFINE vlaue="y" /> <UNKNOWN> <CUSTOM /> </UNKNOWN> </DEFINE>''' definition = Definition.from_string(text) def_tree = definition.to_lxml_element() self.assertEqual(2, len(def_tree.findall('DEFINE'))) unknown = def_tree.findall('UNKNOWN') self.assertEqual(1, len(unknown)) unknown = unknown[0] custom = list(unknown) self.assertEqual(1, len(custom)) custom = custom[0] self.assertEqual('CUSTOM', custom.tag)
def test_creation(self): definition = Definition() definition = Definition('no attributes') definition = Definition('with attributes', {'a': 'b', 'c': 'd'})
def test_name(self): definition = Definition('name1') self.assertEqual('name1', definition.get_name()) definition.set_name('name2') self.assertEqual('name2', definition.get_name()) definition.set_name(None) self.assertEqual(None, definition.get_name()) definition = Definition(name = 'name1') self.assertEqual('name1', definition.get_name())
def get_definition(widget, msg): definition = Definition(widget.value).get() widget.outputdiv.text = "-".join(definition)
def test_equality(self): self.assertNotEqual(Definition(), 'different type')
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)
def test_attributes(self): definition = Definition('with attributes', {'a': 'b', 'c': 'd'}) self.assertEqual('b', definition.get_attribute('a')) self.assertEqual('d', definition.get_attribute('c')) self.assertEqual({'a': 'b', 'c': 'd'}, definition.get_attributes()) self.assertRaises(KeyError, definition.get_attribute, 'e') definition.set_attribute('e', 'f') self.assertEqual('f', definition.get_attribute('e')) definition.set_attribute('e', 'g') self.assertEqual('g', definition.get_attribute('e')) definition.remove_attribute('e') self.assertRaises(KeyError, definition.get_attribute, 'e') self.assertRaises(KeyError, definition.remove_attribute, 'e') definition = Definition(attributes = {'a': 'b'}) self.assertEqual('b', definition.get_attribute('a')) self.assertRaises(KeyError, definition.get_attribute, 'e')
def test_value(self): self.assertRaises(KeyError, Definition, 'name', {'value': 'x'}) definition = Definition('name') self.assertRaises(KeyError, definition.set_attribute, 'value', 'x')