def __init__(self, namespaceHandling=0, bufsize=2**16 - 20): xmlreader.IncrementalParser.__init__(self, bufsize) self._source = xmlreader.InputSource() self._parser = None self._namespaces = namespaceHandling self._lex_handler_prop = None self._parsing = 0 self._entity_stack = [] self._external_ges = 1 self._interning = None
def _parse_using_sax_parser(self, xml_input): from xml.sax import make_parser, xmlreader, SAXParseException source = xmlreader.InputSource() source.setByteStream(self._to_stream(xml_input)) reader = make_parser() reader.setContentHandler(self) try: reader.parse(source) except SAXParseException, e: raise PropertyListParseError(e)
def resolveEntity(self, publicId, systemId): """Respove catalog entities based on file path.""" filepath = os.path.join(XML_CACHE, base64.urlsafe_b64encode(publicId)) if os.path.isfile(filepath): source = xmlreader.InputSource() with open(filepath, 'r') as f: source.setByteStream(StringIO(f.read())) return source return systemId
def default_test(): """ Тестовая функция. """ rep_file = None xml_file = None xml_file = open(sys.argv[1], 'r') input_source = xmlreader.InputSource() input_source.setByteStream(xml_file) xml_reader = xml.sax.make_parser() xml_parser = icXML2DICTReader() xml_reader.setContentHandler(xml_parser) # включаем опцию namespaces xml_reader.setFeature(xml.sax.handler.feature_namespaces, 1) xml_reader.parse(input_source) xml_file.close()
def test_simple(self): class MyResolver(xml.sax.handler.EntityResolver): def __init__(self): self.invoked = 0 def resolveEntity(self, publicId, systemId): self.invoked = 1 return systemId reader = _makeParser(_drv_xmlproc, 0) resolver = MyResolver() reader.setEntityResolver(resolver) inps = xmlreader.InputSource() inps.setByteStream(StringIO(self.document)) inps.setSystemId("tmp1.xml") reader.parse(inps) self.failUnless(resolver.invoked)
def _parseXmlCat(self, data): """ Parse an XML Catalog, as specified in http://www.oasis-open.org/committees/entity/spec-2001-08-06.html. Partially implemented. """ self.prefer_public = [True] self.base = [self.uri] # Since we have the catalog data already, parse it. source = xmlreader.InputSource(self.uri) source.setByteStream(cStringIO.StringIO(data)) from Ft.Xml.Sax import CreateParser p = CreateParser() p.setFeature('http://xml.org/sax/features/external-parameter-entities', False) p.setContentHandler(self) p.parse(source) # are these explicit dels needed? del self.prefer_public del self.base return
def execute(self, operation, *args): """ Prepare and execute a database operation (query or command). Parameters may be provided as sequence or mapping and will be bound to variables in the operation. Parameter style for WSManDb is %-formatting, as in: cur.execute('select * from table where id=%d', id) cur.execute('select * from table where strname=%s', name) Please consult online documentation for more examples and guidelines. """ if not self._connection: raise ProgrammingError("Cursor closed.") if not self._connection._conkwargs: raise ProgrammingError("Connection closed.") self.description = None self.rownumber = -1 del self._rows[:] self._keybindings.clear() good_sql = False # for this method default value for params cannot be None, # because None is a valid value for format string. if (args != () and len(args) != 1): raise TypeError("execute takes 1 or 2 arguments (%d given)" % (len(args) + 1, )) if args != (): operation = operation % args[0] operation = operation.encode('unicode-escape') if operation.upper() == 'SELECT 1': operation = 'SELECT * FROM __Namespace' good_sql = True try: props, classname, where = WQLPAT.match(operation).groups('') except: raise ProgrammingError("Syntax error in the query statement.") if where: try: self._keybindings.update( eval('(lambda **kws:kws)(%s)' % ANDPAT.sub(',', where))) if [v for v in self._keybindings.values() if type(v) is list]: kbkeys = '' if props != '*': kbkeys = ',%s' % ','.join(self._keybindings.keys()) operation = 'SELECT %s%s FROM %s' % (props, kbkeys, classname) elif self._dialect: self._keybindings.clear() except: self._keybindings.clear() if props == '*': self._props = [] else: self._props = [p for p in props.replace(' ', '').split(',')] try: if self._dialect: xml_req = XML_REQ % ( 'ExecQuery', '"/>\n<NAMESPACE NAME="'.join( self._namespace.split('/')), EXECQUERY_IPARAM % (EXECQUERY_IPARAM % operation, self._dialect)) else: pLst = [p for p in set(self._props) \ if p.upper() not in ('__PATH','__CLASS','__NAMESPACE')] pLst.extend(self._keybindings.keys()) xml_req = XML_REQ % ( 'EnumerateInstances', '"/>\n<NAMESPACE NAME="'.join( self._namespace.split('/')), ''.join( (CLNAME_IPARAM % classname, QUALS_IPARAM % 'FALSE', pLst and PL_IPARAM % '</VALUE>\n<VALUE>'.join(pLst) or ''))) xml_resp = self._connection._wbem_request(xml_req) if xml_resp and good_sql: self._rows.append((1L, )) self.description = (('1', CIM_UINT64, None, None, None, None, None), ) self.rownumber = 0 return xml_handler = CIMHandler(self) parser = make_parser() parser.setContentHandler(xml_handler) inpsrc = xmlreader.InputSource() inpsrc.setByteStream(StringIO(xml_resp)) parser.parse(inpsrc) if self.description: self.rownumber = 0 except InterfaceError, e: raise InterfaceError(e)
self.country = self.countries[0] if self.countries else '' self.city = self.cities[0] if self.cities else '' self.city = forceUpper(self.city) self.orgname = forceUpper(self.orgname.replace('&','&')) if not store_db: print '{:.8} {} {} {:3} {:3} {:4} {:10} {:15} {:3} {:.30}'.format(self.patint,self.file_date,self.grant_date,self.class_one,self.class_two,self.ipc_ver,self.ipc_code,self.city,self.country,self.orgname) patents.append((self.patnum,self.file_date,self.grant_date,self.class_one,self.class_two,self.ipc_ver,self.ipc_code,self.city,self.country,self.orgname)) if len(patents) == batch_size: commitBatch() # do parsing grant_handler = GrantHandler() input_source = xmlreader.InputSource() input_source.setEncoding('iso-8859-1') input_source.setByteStream(open(in_fname)) parser = make_parser() parser.setContentHandler(grant_handler) parser.parse(input_source) # clear out the rest if len(patents) > 0: commitBatch() if store_db: # commit to db and close conn.commit() cur.close()
def _saxParse(testCase, driver, ns, h, xmls): reader = _makeParser(driver, ns) reader.setContentHandler(h) inps = xmlreader.InputSource() inps.setByteStream(StringIO(xmls)) reader.parse(inps)
"""