Ejemplo n.º 1
0
def parse(filename, mode='rw', **parser_features):
    """
    Interprets the given string as a filename, URL or XML data string,
    parses it and returns a Python object which represents the given
    document.

    Extra arguments to this function are treated as feature values to pass
    to ``parser.setFeature()``. For example, ``feature_external_ges=False``
    will set ``xml.sax.handler.feature_external_ges`` to False, disabling
    the parser's inclusion of external general (text) entities such as DTDs.

    Raises ``ValueError`` if the first argument is None / empty string.

    Raises ``AttributeError`` if a requested xml.sax feature is not found in
    ``xml.sax.handler``.

    Raises ``xml.sax.SAXParseException`` if something goes wrong
    during parsing.
    """
    if filename is None or (is_string(filename) and filename.strip()) == '':
        raise ValueError('parse() takes a filename, URL or XML string')
    parser = make_parser()
    for feature, value in parser_features.items():
        parser.setFeature(getattr(handler, feature), value)
    sax_handler = Handler(mode)
    parser.setContentHandler(sax_handler)
    if is_string(filename) and (os.path.exists(filename) or is_url(filename)):
        parser.parse(filename)
    else:
        if hasattr(filename, 'read'):
            parser.parse(filename)
        else:
            parser.parse(StringIO(filename))

    return sax_handler.root
Ejemplo n.º 2
0
 def test_is_url(self):
     self.assertFalse(untangle.is_url('foo'))
     self.assertFalse(untangle.is_url('httpfoo'))
     self.assertFalse(untangle.is_url(7))
     self.assertTrue(untangle.is_url('http://foobar'))
     self.assertTrue(untangle.is_url('https://foobar'))
Ejemplo n.º 3
0
 def test_is_url(self):
     self.assertFalse(untangle.is_url('foo'))
     self.assertFalse(untangle.is_url('httpfoo'))
     self.assertFalse(untangle.is_url(7))
Ejemplo n.º 4
0
 def test_is_url(self):
     self.assertFalse(untangle.is_url('foo'))
     self.assertFalse(untangle.is_url('httpfoo'))
     self.assertFalse(untangle.is_url(7))
     self.assertTrue(untangle.is_url('http://foobar'))
     self.assertTrue(untangle.is_url('https://foobar'))
Ejemplo n.º 5
0
    def __init__(self, xml_ai_data: str):
        if not untangle.is_url(xml_ai_data):
            # print("error xml file not found")
            pass  # it might be found anyway - this is a bug in untangle

        self.xml_parser = untangle.parse(xml_ai_data)  # if it is really not found, this fails
Ejemplo n.º 6
0
 def test_is_url(self):
     self.assertFalse(untangle.is_url("foo"))
     self.assertFalse(untangle.is_url("httpfoo"))
     self.assertFalse(untangle.is_url(7))
     self.assertTrue(untangle.is_url("http://foobar"))
     self.assertTrue(untangle.is_url("https://foobar"))
Ejemplo n.º 7
0
 def test_is_url(self):
     self.assertFalse(untangle.is_url('foo'))
     self.assertFalse(untangle.is_url('httpfoo'))
     self.assertFalse(untangle.is_url(7))