def get_parser(document=None, namespaces=1, validate=0, external_ges=1, logfile=None, doc_factory=new_document): import xml if hasattr(xml, "use_pyxml"): xml.use_pyxml() import xml.sax.sax2exts import xml.sax.handler handler = POM.ContentHandler(document, doc_factory=doc_factory, logfile=logfile) errorhandler = POM.ErrorHandler(logfile) # create parser parser = xml.sax.sax2exts.XMLParserFactory.make_parser() parser.setFeature(xml.sax.handler.feature_namespaces, namespaces) parser.setFeature(xml.sax.handler.feature_validation, validate) parser.setFeature(xml.sax.handler.feature_external_ges, external_ges) parser.setFeature(xml.sax.handler.feature_external_pes, 0) parser.setFeature(xml.sax.handler.feature_string_interning, 1) # set handlers parser.setContentHandler(handler) parser.setDTDHandler(handler) parser.setEntityResolver(handler) parser.setErrorHandler(errorhandler) return parser
def get_dtd_compiler(fo, mixinmodule=None, doctype=None): import xml if hasattr(xml, "use_pyxml"): # per Gentoo bug #367729 xml.use_pyxml() from xml.parsers.xmlproc.dtdparser import DTDParser generator = sourcegen.get_sourcefile(fo) dh = DTDConsumerForSourceGeneration(generator, mixinmodule, doctype) parser = DTDParser() parser.set_dtd_consumer(dh) return parser
def get_parser(document=None, namespaces=0, validate=0, external_ges=1, logfile=None, doc_factory=POM.new_document): import xml if hasattr(xml, "use_pyxml"): xml.use_pyxml() import xml.sax.sax2exts import xml.sax.handler import new handler = ContentHandler(document, doc_factory=doc_factory, logfile=logfile) errorhandler = ErrorHandler(logfile) # create parser parser = xml.sax.sax2exts.XMLParserFactory.make_parser() parser.setFeature(xml.sax.handler.feature_namespaces, namespaces) parser.setFeature(xml.sax.handler.feature_validation, validate) parser.setFeature(xml.sax.handler.feature_external_ges, external_ges) parser.setFeature(xml.sax.handler.feature_external_pes, 0) parser.setFeature(xml.sax.handler.feature_string_interning, 1) # set handlers parser.setContentHandler(handler) parser.setDTDHandler(handler) parser.setEntityResolver(handler) parser.setErrorHandler(errorhandler) # since the xml API provides some generic parser I can't just # subclass I have to "patch" the object in-place with this trick. # This is to a) make the API compatible with the HTMLParser, and b) # allow specifing the encoding and other headers in the request. parser.parse_orig = parser.parse def parse(self, url, data=None, encoding=POM.DEFAULT_ENCODING, useragent=None, accept=None): from pycopia.WWW import urllibplus fo = urllibplus.urlopen(url, data, encoding, useragent=useragent, accept=accept) if logfile: from pycopia import UserFile fo = UserFile.FileWrapper(fo, logfile=logfile) return self.parse_orig(fo) parser.parse = new.instancemethod(parse, parser, parser.__class__) return parser
# basic idea from http://code.activestate.com/recipes/496923/ # provided by Alex Greif # import string import xml if hasattr(xml, "use_pyxml"): xml.use_pyxml() from xml.marshal import generic class Marshaller(generic.Marshaller): tag_unicode = 'unicode' tag_boolean = 'boolean' def m_unicode(self, value, dict): name = self.tag_unicode L = ['<' + name + '>'] s = value.encode('utf-8') if '&' in s or '>' in s or '<' in s: s = s.replace('&', '&') s = s.replace('<', '<') s = s.replace('>', '>') L.append(s) L.append('</' + name + '>') return L def m_bool(self, value, dict): name = self.tag_boolean L = ['<' + name + '>'] L.append(value and 'True' or 'False')
# GNU General Public License for more details. """ Import nmap XML output into the network model. Even though nmap reports discovered hosts as hosts, they are actually interfaces. This importer creates unattached interfaces. A user/administrator will have to go and assign created interfaces to hosts. """ import xml if hasattr(xml, "use_pyxml"): xml.use_pyxml() import xml.sax.sax2exts import xml.sax.handler from pycopia.logging import warn from pycopia import ipv4 from pycopia.db import models # States the XML host scanner can be in NOTINTERESTED = 0 INTERESTED = 1 INTERFACE_TYPE_ID = 6 # ethernetCsmacd INTERFACE_TYPE = None