Submit
Path:
~
/
/
lib
/
python3
/
dist-packages
/
File Content:
drv_libxml2.py
# -*- coding: iso-8859-1 -*- """ A SAX2 driver for libxml2, on top of it's XmlReader API USAGE # put this file (drv_libxml2.py) in PYTHONPATH import xml.sax reader = xml.sax.make_parser(["drv_libxml2"]) # ...and the rest is standard python sax. CAVEATS - Lexical handlers are supported, except for start/endEntity (waiting for XmlReader.ResolveEntity) and start/endDTD - Error callbacks are not exactly synchronous, they tend to be invoked before the corresponding content callback, because the underlying reader interface parses data by chunks of 512 bytes TODO - search for TODO - some ErrorHandler events (warning) - some ContentHandler events (setDocumentLocator, skippedEntity) - EntityResolver (using libxml2.?) - DTDHandler (if/when libxml2 exposes such node types) - DeclHandler (if/when libxml2 exposes such node types) - property_xml_string? - feature_string_interning? - Incremental parser - additional performance tuning: - one might cache callbacks to avoid some name lookups - one might implement a smarter way to pass attributes to startElement (some kind of lazy evaluation?) - there might be room for improvement in start/endPrefixMapping - other? """ __author__ = "St�phane Bidoul <sbi@skynet.be>" __version__ = "0.3" import sys import codecs if sys.version_info[0] < 3: __author__ = codecs.unicode_escape_decode(__author__)[0] StringTypes = (str, unicode) # libxml2 returns strings as UTF8 _decoder = codecs.lookup("utf8")[1] def _d(s): if s is None: return s else: return _decoder(s)[0] else: StringTypes = str # s is Unicode `str` already def _d(s): return s from xml.sax._exceptions import * from xml.sax import xmlreader, saxutils from xml.sax.handler import \ feature_namespaces, \ feature_namespace_prefixes, \ feature_string_interning, \ feature_validation, \ feature_external_ges, \ feature_external_pes, \ property_lexical_handler, \ property_declaration_handler, \ property_dom_node, \ property_xml_string try: import libxml2 except ImportError: raise SAXReaderNotAvailable("libxml2 not available: " \ "import error was: %s" % sys.exc_info()[1]) class Locator(xmlreader.Locator): """SAX Locator adapter for libxml2.xmlTextReaderLocator""" def __init__(self,locator): self.__locator = locator def getColumnNumber(self): "Return the column number where the current event ends." return -1 def getLineNumber(self): "Return the line number where the current event ends." return self.__locator.LineNumber() def getPublicId(self): "Return the public identifier for the current event." return None def getSystemId(self): "Return the system identifier for the current event." return self.__locator.BaseURI() class LibXml2Reader(xmlreader.XMLReader): def __init__(self): xmlreader.XMLReader.__init__(self) # features self.__ns = 0 self.__nspfx = 0 self.__validate = 0 self.__extparams = 1 # parsing flag self.__parsing = 0 # additional handlers self.__lex_handler = None self.__decl_handler = None # error messages accumulator self.__errors = None def _errorHandler(self,arg,msg,severity,locator): if self.__errors is None: self.__errors = [] self.__errors.append((severity, SAXParseException(msg,None, Locator(locator)))) def _reportErrors(self,fatal): for severity,exception in self.__errors: if severity in (libxml2.PARSER_SEVERITY_VALIDITY_WARNING, libxml2.PARSER_SEVERITY_WARNING): self._err_handler.warning(exception) else: # when fatal is set, the parse will stop; # we consider that the last error reported # is the fatal one. if fatal and exception is self.__errors[-1][1]: self._err_handler.fatalError(exception) else: self._err_handler.error(exception) self.__errors = None def parse(self, source): self.__parsing = 1 try: # prepare source and create reader if isinstance(source, StringTypes): reader = libxml2.newTextReaderFilename(source) else: source = saxutils.prepare_input_source(source) stream = source.getCharacterStream() if stream is None: stream = source.getByteStream() input = libxml2.inputBuffer(stream) reader = input.newTextReader(source.getSystemId()) reader.SetErrorHandler(self._errorHandler,None) # configure reader if self.__extparams: reader.SetParserProp(libxml2.PARSER_LOADDTD,1) reader.SetParserProp(libxml2.PARSER_DEFAULTATTRS,1) reader.SetParserProp(libxml2.PARSER_SUBST_ENTITIES,1) reader.SetParserProp(libxml2.PARSER_VALIDATE,self.__validate) else: reader.SetParserProp(libxml2.PARSER_LOADDTD, 0) # we reuse attribute maps (for a slight performance gain) if self.__ns: attributesNSImpl = xmlreader.AttributesNSImpl({},{}) else: attributesImpl = xmlreader.AttributesImpl({}) # prefixes to pop (for endPrefixMapping) prefixes = [] # start loop self._cont_handler.startDocument() while 1: r = reader.Read() # check for errors if r == 1: if not self.__errors is None: self._reportErrors(0) elif r == 0: if not self.__errors is None: self._reportErrors(0) break # end of parse else: if not self.__errors is None: self._reportErrors(1) else: self._err_handler.fatalError(\ SAXException("Read failed (no details available)")) break # fatal parse error # get node type nodeType = reader.NodeType() # Element if nodeType == 1: if self.__ns: eltName = (_d(reader.NamespaceUri()),\ _d(reader.LocalName())) eltQName = _d(reader.Name()) attributesNSImpl._attrs = attrs = {} attributesNSImpl._qnames = qnames = {} newPrefixes = [] while reader.MoveToNextAttribute(): qname = _d(reader.Name()) value = _d(reader.Value()) if qname.startswith("xmlns"): if len(qname) > 5: newPrefix = qname[6:] else: newPrefix = None newPrefixes.append(newPrefix) self._cont_handler.startPrefixMapping(\ newPrefix,value) if not self.__nspfx: continue # don't report xmlns attribute attName = (_d(reader.NamespaceUri()), _d(reader.LocalName())) qnames[attName] = qname attrs[attName] = value reader.MoveToElement() self._cont_handler.startElementNS( \ eltName,eltQName,attributesNSImpl) if reader.IsEmptyElement(): self._cont_handler.endElementNS(eltName,eltQName) for newPrefix in newPrefixes: self._cont_handler.endPrefixMapping(newPrefix) else: prefixes.append(newPrefixes) else: eltName = _d(reader.Name()) attributesImpl._attrs = attrs = {} while reader.MoveToNextAttribute(): attName = _d(reader.Name()) attrs[attName] = _d(reader.Value()) reader.MoveToElement() self._cont_handler.startElement( \ eltName,attributesImpl) if reader.IsEmptyElement(): self._cont_handler.endElement(eltName) # EndElement elif nodeType == 15: if self.__ns: self._cont_handler.endElementNS( \ (_d(reader.NamespaceUri()),_d(reader.LocalName())), _d(reader.Name())) for prefix in prefixes.pop(): self._cont_handler.endPrefixMapping(prefix) else: self._cont_handler.endElement(_d(reader.Name())) # Text elif nodeType == 3: self._cont_handler.characters(_d(reader.Value())) # Whitespace elif nodeType == 13: self._cont_handler.ignorableWhitespace(_d(reader.Value())) # SignificantWhitespace elif nodeType == 14: self._cont_handler.characters(_d(reader.Value())) # CDATA elif nodeType == 4: if not self.__lex_handler is None: self.__lex_handler.startCDATA() self._cont_handler.characters(_d(reader.Value())) if not self.__lex_handler is None: self.__lex_handler.endCDATA() # EntityReference elif nodeType == 5: if not self.__lex_handler is None: self.startEntity(_d(reader.Name())) reader.ResolveEntity() # EndEntity elif nodeType == 16: if not self.__lex_handler is None: self.endEntity(_d(reader.Name())) # ProcessingInstruction elif nodeType == 7: self._cont_handler.processingInstruction( \ _d(reader.Name()),_d(reader.Value())) # Comment elif nodeType == 8: if not self.__lex_handler is None: self.__lex_handler.comment(_d(reader.Value())) # DocumentType elif nodeType == 10: #if not self.__lex_handler is None: # self.__lex_handler.startDTD() pass # TODO (how to detect endDTD? on first non-dtd event?) # XmlDeclaration elif nodeType == 17: pass # TODO # Entity elif nodeType == 6: pass # TODO (entity decl) # Notation (decl) elif nodeType == 12: pass # TODO # Attribute (never in this loop) #elif nodeType == 2: # pass # Document (not exposed) #elif nodeType == 9: # pass # DocumentFragment (never returned by XmlReader) #elif nodeType == 11: # pass # None #elif nodeType == 0: # pass # - else: raise SAXException("Unexpected node type %d" % nodeType) if r == 0: self._cont_handler.endDocument() reader.Close() finally: self.__parsing = 0 def setDTDHandler(self, handler): # TODO (when supported, the inherited method works just fine) raise SAXNotSupportedException("DTDHandler not supported") def setEntityResolver(self, resolver): # TODO (when supported, the inherited method works just fine) raise SAXNotSupportedException("EntityResolver not supported") def getFeature(self, name): if name == feature_namespaces: return self.__ns elif name == feature_namespace_prefixes: return self.__nspfx elif name == feature_validation: return self.__validate elif name == feature_external_ges: return 1 # TODO (does that relate to PARSER_LOADDTD)? elif name == feature_external_pes: return self.__extparams else: raise SAXNotRecognizedException("Feature '%s' not recognized" % \ name) def setFeature(self, name, state): if self.__parsing: raise SAXNotSupportedException("Cannot set feature %s " \ "while parsing" % name) if name == feature_namespaces: self.__ns = state elif name == feature_namespace_prefixes: self.__nspfx = state elif name == feature_validation: self.__validate = state elif name == feature_external_ges: if state == 0: # TODO (does that relate to PARSER_LOADDTD)? raise SAXNotSupportedException("Feature '%s' not supported" % \ name) elif name == feature_external_pes: self.__extparams = state else: raise SAXNotRecognizedException("Feature '%s' not recognized" % \ name) def getProperty(self, name): if name == property_lexical_handler: return self.__lex_handler elif name == property_declaration_handler: return self.__decl_handler else: raise SAXNotRecognizedException("Property '%s' not recognized" % \ name) def setProperty(self, name, value): if name == property_lexical_handler: self.__lex_handler = value elif name == property_declaration_handler: # TODO: remove if/when libxml2 supports dtd events raise SAXNotSupportedException("Property '%s' not supported" % \ name) self.__decl_handler = value else: raise SAXNotRecognizedException("Property '%s' not recognized" % \ name) def create_parser(): return LibXml2Reader()
Submit
FILE
FOLDER
Name
Size
Permission
Action
Babel-2.8.0.egg-info
---
0755
CommandNotFound
---
0755
DistUpgrade
---
0755
Django-3.2.12.egg-info
---
0755
Genshi-0.7.6.egg-info
---
0755
HweSupportStatus
---
0755
Jinja2-3.0.3.egg-info
---
0755
MarkupSafe-2.0.1.egg-info
---
0755
MySQLdb
---
0755
OpenSSL
---
0755
PyGObject-3.42.1.egg-info
---
0755
PyGreSQL-5.1.2.egg-info
---
0755
PyJWT-2.3.0.egg-info
---
0755
PyNaCl-1.5.0.dist-info
---
0755
PyYAML-5.4.1.egg-info
---
0755
Pygments-2.11.2.egg-info
---
0755
S3
---
0755
SecretStorage-3.3.1.egg-info
---
0755
Sphinx-4.3.2.egg-info
---
0755
Trac-1.5.3.egg-info
---
0755
UpdateManager
---
0755
__pycache__
---
0755
_distutils_hack
---
0755
_yaml
---
0755
alabaster
---
0755
alabaster-0.7.12.egg-info
---
0755
apport
---
0755
apt
---
0755
apt_inst-stubs
---
0755
apt_pkg-stubs
---
0755
aptsources
---
0755
asgiref
---
0755
asgiref-3.5.0.egg-info
---
0755
attr
---
0755
attrs-21.2.0.egg-info
---
0755
awscli
---
0755
awscli-1.22.34.egg-info
---
0755
babel
---
0755
blinker
---
0755
boto
---
0755
boto-2.49.0.egg-info
---
0755
botocore
---
0755
botocore-1.23.34.egg-info
---
0755
certifi
---
0755
certifi-2020.6.20.egg-info
---
0755
chardet
---
0755
chardet-4.0.0.egg-info
---
0755
colorama
---
0755
colorama-0.4.4.egg-info
---
0755
cryptography
---
0755
cryptography-3.4.8.egg-info
---
0755
curl
---
0755
dateutil
---
0755
dbus
---
0755
dbus_python-1.2.18.egg-info
---
0755
debian
---
0755
debian_bundle
---
0755
devscripts
---
0755
devscripts-2.22.1ubuntu1.egg-info
---
0755
distlib
---
0755
distlib-0.3.4.egg-info
---
0755
distro
---
0755
distro-1.7.0.egg-info
---
0755
distro_info-1.1+ubuntu0.2.egg-info
---
0755
django
---
0755
docutils
---
0755
docutils-0.17.1.egg-info
---
0755
duplicity
---
0755
duplicity-0.8.21.egg-info
---
0755
fail2ban
---
0755
fail2ban-0.11.2.egg-info
---
0755
fasteners
---
0755
fasteners-0.14.1.egg-info
---
0755
filelock
---
0755
filelock-3.6.0.egg-info
---
0755
future
---
0755
future-0.18.2.egg-info
---
0755
genshi
---
0755
gi
---
0755
hgdemandimport
---
0755
hgext
---
0755
hgext3rd
---
0755
httplib2
---
0755
httplib2-0.20.2.egg-info
---
0755
idna
---
0755
idna-3.3.egg-info
---
0755
imagesize-1.3.0.egg-info
---
0755
importlib_metadata
---
0755
importlib_metadata-4.6.4.egg-info
---
0755
iotop
---
0755
janitor
---
0755
jeepney
---
0755
jeepney-0.7.1.dist-info
---
0755
jinja2
---
0755
jmespath
---
0755
jmespath-0.10.0.egg-info
---
0755
jsonpatch-1.32.egg-info
---
0755
jsonpointer-2.0.egg-info
---
0755
jsonschema
---
0755
jsonschema-3.2.0.egg-info
---
0755
jwt
---
0755
keyring
---
0755
keyring-23.5.0.egg-info
---
0755
launchpadlib
---
0755
launchpadlib-1.10.16.egg-info
---
0755
lazr
---
0755
lazr.restfulclient-0.14.4.egg-info
---
0755
lazr.uri-1.0.6.egg-info
---
0755
libfuturize
---
0755
libpasteurize
---
0755
lockfile
---
0755
lockfile-0.12.2.egg-info
---
0755
magic
---
0755
markupsafe
---
0755
mercurial
---
0755
monotonic-1.6.egg-info
---
0755
more_itertools
---
0755
more_itertools-8.10.0.egg-info
---
0755
mysqlclient-1.4.6.egg-info
---
0755
nacl
---
0755
netifaces-0.11.0.egg-info
---
0755
netplan
---
0755
numpy
---
0755
numpy-1.21.5.egg-info
---
0755
oauthlib
---
0755
oauthlib-3.2.0.egg-info
---
0755
packaging
---
0755
packaging-21.3.egg-info
---
0755
past
---
0755
pexpect
---
0755
pip
---
0755
pip-22.0.2.dist-info
---
0755
pkg_resources
---
0755
platformdirs
---
0755
platformdirs-2.5.1.dist-info
---
0755
psycopg2
---
0755
psycopg2-2.9.2.egg-info
---
0755
ptyprocess
---
0755
ptyprocess-0.7.0.dist-info
---
0755
pyOpenSSL-21.0.0.egg-info
---
0755
pyasn1
---
0755
pyasn1-0.4.8.egg-info
---
0755
pycurl-7.44.1.egg-info
---
0755
pygments
---
0755
pygtkcompat
---
0755
pymacaroons
---
0755
pymacaroons-0.13.0.egg-info
---
0755
pyparsing-2.4.7.egg-info
---
0755
pyrsistent
---
0755
pyrsistent-0.18.1.egg-info
---
0755
pyserial-3.5.egg-info
---
0755
pysvn
---
0755
pysvn-1.9.15.egg-info
---
0755
python_apt-2.4.0+ubuntu4.1.egg-info
---
0755
python_dateutil-2.8.1.egg-info
---
0755
python_debian-0.1.43+ubuntu1.1.egg-info
---
0755
python_magic-0.4.24.egg-info
---
0755
pytz
---
0755
pytz-2022.1.egg-info
---
0755
requests
---
0755
requests-2.25.1.egg-info
---
0755
roman-3.3.egg-info
---
0755
rsa
---
0755
rsa-4.8.egg-info
---
0755
s3cmd-2.2.0.egg-info
---
0755
s3transfer
---
0755
s3transfer-0.5.0.egg-info
---
0755
secretstorage
---
0755
serial
---
0755
setuptools
---
0755
setuptools-59.6.0.egg-info
---
0755
simplejson
---
0755
simplejson-3.17.6.egg-info
---
0755
six-1.16.0.egg-info
---
0755
snowballstemmer
---
0755
snowballstemmer-2.2.0.egg-info
---
0755
softwareproperties
---
0755
sos
---
0755
sos-4.9.2.egg-info
---
0755
sphinx
---
0755
sqlparse
---
0755
sqlparse-0.4.2.egg-info
---
0755
ssh_import_id
---
0755
ssh_import_id-5.11.egg-info
---
0755
systemd
---
0755
trac
---
0755
tracopt
---
0755
twisted
---
0755
uaclient
---
0755
ubuntu_pro_client-8001.egg-info
---
0755
ufw
---
0755
unattended_upgrades-0.1.egg-info
---
0755
urllib3
---
0755
urllib3-1.26.5.egg-info
---
0755
virtualenv
---
0755
virtualenv-20.13.0+ds.dist-info
---
0755
wadllib
---
0755
wadllib-1.3.6.egg-info
---
0755
wheel
---
0755
wheel-0.37.1.egg-info
---
0755
xapian
---
0755
yaml
---
0755
zipp-1.0.0.egg-info
---
0755
_cffi_backend.cpython-310-x86_64-linux-gnu.so
194472 bytes
0644
_dbus_bindings.cpython-310-x86_64-linux-gnu.so
168152 bytes
0644
_dbus_glib_bindings.cpython-310-x86_64-linux-gnu.so
23040 bytes
0644
_pg.cpython-310-x86_64-linux-gnu.so
114376 bytes
0644
_pyrsistent_version.py
23 bytes
0644
_snack.cpython-310-x86_64-linux-gnu.so
47888 bytes
0644
apport_python_hook.py
8063 bytes
0644
apt_inst.cpython-310-x86_64-linux-gnu.so
60064 bytes
0644
apt_pkg.cpython-310-x86_64-linux-gnu.so
347096 bytes
0644
blinker-1.4.egg-info
3901 bytes
0644
command_not_found-0.3.egg-info
189 bytes
0644
deb822.py
273 bytes
0644
debconf.py
6769 bytes
0644
distro_info.py
12528 bytes
0644
drv_libxml2.py
15376 bytes
0644
imagesize.py
13352 bytes
0644
iotop-0.6.egg-info
348 bytes
0644
jsonpatch.py
28813 bytes
0644
jsonpointer.py
9796 bytes
0644
libxml2.py
346308 bytes
0644
libxml2mod.cpython-310-x86_64-linux-gnu.so
437808 bytes
0644
lsb_release.py
14901 bytes
0644
mercurial-6.1.1-py3.10.egg-info
1330 bytes
0644
monotonic.py
7169 bytes
0644
netifaces.cpython-310-x86_64-linux-gnu.so
23232 bytes
0644
pexpect-4.8.0.egg-info
2285 bytes
0644
pg.py
102204 bytes
0644
pgdb.py
63567 bytes
0644
problem_report.py
26867 bytes
0644
pvectorc.cpython-310-x86_64-linux-gnu.so
37552 bytes
0644
pycurl.cpython-310-x86_64-linux-gnu.so
143976 bytes
0644
pyparsing.py
273365 bytes
0644
roman.py
3664 bytes
0644
schedutils-0.6.egg-info
291 bytes
0644
schedutils.cpython-310-x86_64-linux-gnu.so
15136 bytes
0644
six.py
34549 bytes
0644
snack.py
31134 bytes
0644
systemd_python-234.egg-info
586 bytes
0644
ufw-0.36.1.egg-info
263 bytes
0644
zipp.py
6914 bytes
0644
N4ST4R_ID | Naxtarrr