def parse_xml(self): try: import libxml2 import libxslt import urllib2 xslt_path = os.path.join(os.path.dirname(__file__), "") xslt_path = "%s/xslt/%s" % (xslt_path, XML_XSD_CFDI_VERSION) xslt_path_file = "%s/%s" % (xslt_path, XML_XSLT_CDF_NAME) xslt_complements_path = "%s/complementos" % xslt_path response = open(xslt_path_file) xslt = response.read() xslt = xslt.replace('{{XSLT_COMPLEMENTS_PATH}}', xslt_complements_path) styledoc = libxml2.parseMemory(xslt, len(xslt)) style = libxslt.parseStylesheetDoc(styledoc) doc = libxml2.parseMemory(self.xml_invoice, len(self.xml_invoice)) result = style.applyStylesheet(doc, None) self.original_string = str(result) self.original_string = self.original_string.replace( '<?xml version="1.0" encoding="UTF-8"?>\n', '') self.original_string = self.original_string.replace('\n', '') self.original_string = self.original_string.replace('&', '&') self.original_string = self.original_string.replace('"', '"') self.original_string = self.original_string.replace('<', '<') self.original_string = self.original_string.replace('>', '>') self.original_string = self.original_string.replace(''', '´') self.original_string = self.original_string.strip() print self.original_string return self.original_string except Exception, e: pprint.pprint(e)
def apply_sheet_data(sheet, data): styledoc = libxml2.parseMemory(sheet, len(sheet)) style = libxslt.parseStylesheetDoc(styledoc) doc = libxml2.parseMemory(data, len(data)) result = style.applyStylesheet(doc, None) res = style.saveResultToString(result) style.freeStylesheet() doc.freeDoc() result.freeDoc() return res
def __parse_site(self): url = "http://waterdata.usgs.gov/nwis/inventory?search_site_no=%s&format=sitefile_output&sitefile_output_format=xml&column_name=agency_cd&column_name=site_no&column_name=station_nm&column_name=dec_lat_va&column_name=dec_long_va&column_name=alt_va" % self.__site p = dplatform.get_platform() try: fn, headers = p.retrieve_url(url) content = open(fn).read() except Exception as e: printlog( "Mapsrc", " :[NSGS] Failed to fetch info for %s: %s" % (self.__site, e)) self.set_name("NSGS NWIS Site %s" % self.__site) return doc = libxml2.parseMemory(content, len(content)) ctx = doc.xpathNewContext() base = "/usgs_nwis/site/" self._basename = _xdoc_getnodeval(ctx, base + "station_nm") self.set_name(self._basename) self.set_latitude(float(_xdoc_getnodeval(ctx, base + "dec_lat_va"))) self.set_longitude(float(_xdoc_getnodeval(ctx, base + "dec_long_va"))) self.set_altitude(float(_xdoc_getnodeval(ctx, base + "alt_va")))
def sxw2rml(sxw_file, xsl, output='.', save_pict=False): import libxslt import libxml2 tool = PyOpenOffice(output, save_pict = save_pict) res = tool.unpackNormalize(sxw_file) styledoc = libxml2.parseDoc(xsl) style = libxslt.parseStylesheetDoc(styledoc) doc = libxml2.parseMemory(res,len(res)) result = style.applyStylesheet(doc, None) root = result.xpathEval("/document/stylesheet") if root: root=root[0] images = libxml2.newNode("images") for img in tool.images: node = libxml2.newNode('image') node.setProp('name', img) node.setContent( base64.encodestring(tool.images[img])) images.addChild(node) root.addNextSibling(images) try: xml = style.saveResultToString(result) return xml except: return result
def parseFB2(self, data): doc = libxml2.parseMemory(data, len(data)) current = doc.children # skip comment while current: if current.name == 'FictionBook': break current = current.next current = current.children while current: if current.name == 'stylesheet': self.parseStylesheet(current) elif current.name == 'description': self.parseDescription(current) elif current.name == 'body': self.parseBody(current) elif current.name == 'binary': self.parseBinary(current) current = current.next doc.freeDoc()
def tearDown(self): try: self.db.close() except: pass try: import MySQLdb settings = pokernetworkconfig.Config([]) settings.doc = libxml2.parseMemory(settings_xml, len(settings_xml)) settings.header = settings.doc.xpathNewContext() parameters = settings.headerGetProperties("/server/database")[0] db = MySQLdb.connect(host = parameters["host"], port = int(parameters.get("port", '3306')), user = parameters["root_user"], passwd = parameters["root_password"], db = 'mysql') try: db.query("REVOKE ALL PRIVILEGES, GRANT OPTION FROM '%s'" % parameters['user']) db.query("drop user '%s'" % parameters['user']) except: db.query("delete from user where user = '******'" % parameters['user']) db.query("FLUSH PRIVILEGES") db.close() except Exception, e: print e assert("Unable to delete the user, " + parameters["user"] + "; some tests will fail incorrectly.")
def setUp(self): self.destroyDb() self.settings = pokernetworkconfig.Config([]) self.settings.doc = libxml2.parseMemory(settings_xml, len(settings_xml)) self.settings.header = self.settings.doc.xpathNewContext() self.db = PokerDatabase(self.settings)
def test07_multipleRowsInVersionTable(self): """Test for when the database already exists""" import MySQLdb from pokernetwork.pokerdatabase import ExceptionUpgradeFailed settings = pokernetworkconfig.Config([]) settings.doc = libxml2.parseMemory(settings_xml, len(settings_xml)) settings.header = settings.doc.xpathNewContext() self.settings = settings parameters = settings.headerGetProperties("/server/database")[0] self.db = pokerdatabase.PokerDatabase(self.settings) self.db.db.query("DROP TABLE IF EXISTS server;") self.db.db.query( """CREATE TABLE server (version VARCHAR(16) NOT NULL) ENGINE=InnoDB CHARSET=utf8;""" ) self.db.db.query("""INSERT INTO server (version) VALUES ("1.1.0");""") self.db.db.query("""INSERT INTO server (version) VALUES ("1.2.0");""") try: self.db.setVersionInDatabase("1.3.0") except ExceptionUpgradeFailed, euf: # handle trouble self.assertEquals( euf.args[0], "UPDATE server SET version = '1.3.0': changed 2 rows, expected one or zero" )
def test(conf, inputs, outputs): libxml2.initParser() xcontent = ( "<connection><dbname>" + inputs["dbname"]["value"] + "</dbname><user>" + inputs["user"]["value"] + "</user><password>" + inputs["password"]["value"] + "</password><host>" + inputs["host"]["value"] + "</host><port>" + inputs["port"]["value"] + "</port></connection>" ) doc = libxml2.parseMemory(xcontent, len(xcontent)) styledoc = libxml2.parseFile(conf["main"]["dataPath"] + "/" + inputs["type"]["value"] + "/conn.xsl") # print >> sys.stderr,conf["main"]["dataPath"]+"/"+inputs["type"]["value"]+"/conn.xsl" style = libxslt.parseStylesheetDoc(styledoc) result = style.applyStylesheet(doc, None) # print >> sys.stderr,"("+result.content+")" ds = osgeo.ogr.Open(result.content) if ds is None: conf["lenv"]["message"] = zoo._("Unable to connect to ") + inputs["name"]["value"] return 4 else: outputs["Result"]["value"] = zoo._("Connection to ") + inputs["name"]["value"] + zoo._(" successfull") ds = None return 3
def _antes_de_assinar_ou_verificar(self, raiz): # Converte etree para string xml = etree.tostring(raiz, xml_declaration=True, encoding='utf-8') # Ativa funções criptográficas self._ativar_funcoes_criptograficas() # Colocamos o texto no avaliador XML FIXME: descobrir forma de evitar o uso do libxml2 neste processo doc_xml = libxml2.parseMemory(xml, len(xml)) # Cria o contexto para manipulação do XML via sintaxe XPATH ctxt = doc_xml.xpathNewContext() ctxt.xpathRegisterNs(u'sig', NAMESPACE_SIG) # Separa o nó da assinatura noh_assinatura = ctxt.xpathEval(u'//*/sig:Signature')[0] # Buscamos a chave no arquivo do certificado chave = xmlsec.cryptoAppKeyLoad( filename=str(self.certificado.caminho_arquivo), format=xmlsec.KeyDataFormatPkcs12, pwd=str(self.senha), pwdCallback=None, pwdCallbackCtx=None, ) # Cria a variável de chamada (callable) da função de assinatura assinador = xmlsec.DSigCtx() # Atribui a chave ao assinador assinador.signKey = chave return doc_xml, ctxt, noh_assinatura, assinador
def validate(xml_file): """ Returns None if the xml config is valid, prints a list of validation errors otherwise. """ xml_data = xml_file.read() try: xml_doc = libxml2.parseMemory(xml_data, len(xml_data)) except libxml2.parserError: error("Could not parse '%s', check it contains valid XML " "configuration." % (xml_file.name,)) return xml_context = xml_doc.xpathNewContext() dtd = libxml2.parseDTD(None, "validxml.dtd") ret = xml_doc.validateDtd(xml_context, dtd) if ret == 0: error("XML in '%s' not valid, see error(s) above." % (xml_file.name,)) dtd.freeDtd() xml_doc.freeDoc() xml_context.xpathFreeContext() del dtd del xml_doc del xml_context xml_file.close()
def load(self): """ Updates the self.text with the contents of a url """ username = raw_input("Google Account Email Address: ") password = getpass.getpass("Google Account Password: "******"\n")[0] reader_request = urllib2.Request('http://www.google.com/reader/atom/user/10841948028353920346/state/com.google/reading-list') reader_request.add_header('Cookie', reader_sid) reader_connection = urllib2.urlopen(reader_request) reader_data = reader_connection.read() reader_connection.close() reader_data_xml = libxml2.parseMemory(reader_data, len(reader_data)) reader_context = reader_data_xml.xpathNewContext() reader_context.xpathRegisterNs("atom", "http://www.w3.org/2005/Atom") reader_feed = reader_context.xpathEval( "//atom:entry/atom:title/text()") data = '' for i in range(len(reader_feed)): feed_item = reader_feed[i] data += feed_item.__str__() + "\n\n" #update the text in the page self.layout.document.text = data
def load(self): """ Updates the self.text with the contents of a url """ #load the resource (needs to be async eventually) request = urllib2.Request(self.kwargs['url']) connection = urllib2.urlopen(request) data = connection.read() connection.close() #perform an xpath expression if needed if 'xpath' in self.kwargs: data_xml = libxml2.parseMemory(data, len(data)) data = '' context = data_xml.xpathNewContext() expression = '|'.join(self.kwargs['xpath']) # set up for union results = context.xpathEval(expression) for i in range(len(results)): result = results[i] #pad the results in if they are not the first path's result if i % len(self.kwargs['xpath']) == 0: data += "* %s *\n" % str(result) else: data += "%s\n" % result #put whitespace between rows (after each item) if (i+1) % len(self.kwargs['xpath']) == 0: data += "\n" #update the text in the page self.layout.document.text = data
def test01_Init(self): """test01_Init Test Poker auth : get_auth_instance""" db = None settings = pokernetworkconfig.Config([]) settings.doc = libxml2.parseMemory(settings_xml, len(settings_xml)) settings.header = settings.doc.xpathNewContext() auth = pokerauth.get_auth_instance(db, settings)
def setUp(self): testclock._seconds_reset() self.destroyDb() self.settings = settings = pokernetworkconfig.Config([]) settings.doc = libxml2.parseMemory(settings_xml, len(settings_xml)) settings.header = settings.doc.xpathNewContext() self.db = pokerdatabase.PokerDatabase(settings) self.service = pokerservice.PokerService(settings) self.default_money = 10000000
def setUp(self): testclock._seconds_reset() random.seed(1) settings = pokernetworkconfig.Config([]) settings.doc = libxml2.parseMemory(settings_xml, len(settings_xml)) settings.header = settings.doc.xpathNewContext() self.service = MockService(settings) self.clients = {} self.tourney = None
def test02_AlternatePokerAuth(self): """test02_AlternatePokerAuth Test Poker auth : get_auth_instance alternate PokerAuth""" db = None settings = pokernetworkconfig.Config([]) settings.doc = libxml2.parseMemory(settings_alternate_xml, len(settings_alternate_xml)) settings.header = settings.doc.xpathNewContext() auth = pokerauth.get_auth_instance(db, settings) self.failUnless(hasattr(auth, "gotcha"))
def test02_AlternatePokerAuth(self): """test02_AlternatePokerAuth Test Poker auth : get_auth_instance alternate PokerAuth""" db = None settings = pokernetworkconfig.Config([]) settings.doc = libxml2.parseMemory(settings_alternate_xml, len(settings_alternate_xml)) settings.header = settings.doc.xpathNewContext() auth = pokerauth.get_auth_instance(db, settings) self.failUnless(hasattr(auth, 'gotcha'))
def testSkipSpaces(self): xmlRaw = """<Application> <Window> <VBox> <Button value="5" label="'click my clicker'"/> </VBox> </Window> </Application>""" xmlObj = libxml2.parseMemory(xmlRaw, len(xmlRaw)) app = skin.Application.fromXmlObj(xmlObj.children)
def testSkipSpaces (self): xmlRaw= """<Application> <Window> <VBox> <Button value="5" label="'click my clicker'"/> </VBox> </Window> </Application>""" xmlObj= libxml2.parseMemory (xmlRaw, len (xmlRaw)) app = skin.Application.fromXmlObj(xmlObj.children, skin)
def setUp(self): self.destroyDb() settings = pokernetworkconfig.Config([]) settings.doc = libxml2.parseMemory(settings_xml, len(settings_xml)) settings.header = settings.doc.xpathNewContext() self.service = pokerservice.PokerService(settings) if verbose >= 0: self.service.verbose = verbose site = server.Site(resource.IResource(self.service)) self.p = reactor.listenTCP(0, site, interface="127.0.0.1") self.port = self.p.getHost().port self.cookie = None
def do_update(self): p = dplatform.get_platform() try: fn, headers = p.retrieve_url(self.__url) content = open(fn).read() except Exception as e: printlog( "Mapsrc", " :[NBDC] Failed to fetch info for %i: %s" % (self.__buoy, e)) self.set_name("NBDC %s" % self.__buoy) return try: doc = libxml2.parseMemory(content, len(content)) except Exception as e: printlog( "Mapsrc", " :[NBDC] Failed to parse document %s: %s" % (self.__url, e)) self.set_name("NBDC Unknown Buoy %s" % self.__buoy) return ctx = doc.xpathNewContext() ctx.xpathRegisterNs("georss", "http://www.georss.org/georss") base = "/rss/channel/item/" self.set_name(_xdoc_getnodeval(ctx, base + "title")) try: s = _xdoc_getnodeval(ctx, base + "description") except Exception as e: printlog( "Mapsrc", " :[Buoy %s] Unable to get description: %s" % (self.__buoy, e)) return for i in ["<strong>", "</strong>", "<br />", "°"]: s = s.replace("%s" % i, "") self.set_comment(s) self.set_timestamp(time.time()) try: slat, slon = _xdoc_getnodeval(ctx, base + "georss:point").split(" ", 1) except Exception as e: utils.log_exception() printlog( "Mapsrc", " :[Buoy %s]: Result has no georss:point" % self.__buoy) return self.set_latitude(float(slat)) self.set_longitude(float(slon)) printlog("Mapsrc", " :[Buoy %s] Done with update" % self.__buoy)
def test03_schemaFileMissing(self): settings = pokernetworkconfig.Config([]) settings.doc = libxml2.parseMemory(settings_missing_schema_xml, len(settings_missing_schema_xml)) settings.header = settings.doc.xpathNewContext() self.settings = settings try: self.db = pokerdatabase.PokerDatabase(self.settings) assert("Schema file was missing so this line should not be reached.") except UserWarning, uw: self.assertEqual(uw.args[0], "PokerDatabase: schema /this/is/not/a/file/and/should/not/be/there/not-my-schema-go-away.sql file not found")
def test04_rootBothUsers(self): settings = pokernetworkconfig.Config([]) settings.doc = libxml2.parseMemory(settings_root_both_users_xml, len(settings_root_both_users_xml)) settings.header = settings.doc.xpathNewContext() self.settings = settings try: self.db = pokerdatabase.PokerDatabase(self.settings) except OperationalError, oe: self.assertEquals(oe.args[0], 1396) self.assertEquals(oe.args[1], "Operation CREATE USER failed for 'root'@'%'")
def setUp(self): self.destroyDb() settings = pokernetworkconfig.Config([]) settings.doc = libxml2.parseMemory(settings_xml, len(settings_xml)) settings.header = settings.doc.xpathNewContext() self.service = pokerservice.PokerService(settings) if verbose >=0: self.service.verbose = verbose site = server.Site(resource.IResource(self.service)) self.p = reactor.listenTCP(0, site, interface="127.0.0.1") self.port = self.p.getHost().port self.cookie = None
def process_block(blk, name_root, ext): # process blocks accordingly # Typical block structure # # Either # <div class="body refbody"> # (<div class="section"> ... </div>)+ # </div> # or # <div class="topic reference apiRef apiPackage cppModule" id="group__CUDART__STREAM"><a name="group__CUDART__STREAM" shape="rect"> # <h4 class="fake_sectiontitle member_header"> Typedefs </h4> # <dl class="members"> ...... </dl> # <h4 class="fake_sectiontitle member_header">Functions</h4> # <dl class="members"> ...... </dl> # <div class="description"> # <h4 class="sectiontitle">Typedefs</h4> # <dl class="description"> ... many typedefs ... <d/> # </div> # <div class="description"> # <h4 class="sectiontitle">Functions</h4> # <dl class="description"> ... many functions .. <dl/> # </div> # </div> # description of each entry (in <dl class="description"> # <dt class="description"></dt> # <dd class="description"><div class="section"> .. </div></dd> # print "div %s"%(blk[blk.find("id="):blk.find("\"", blk.find("id=")+4)+1]) blk = preprocess_block(blk) doc = libxml2.parseMemory(blk, len(blk)) ctxt = doc.xpathNewContext() res = ctxt.xpathEval('/div/div') #print "Number of xpath children: %d"%len(res) for node in res: node_title = "" if node.xpathEval('./h3'): node_title = node.xpathEval('./h3')[0].content print "Node title: %s"%node_title map_handler = {"body refbody":handle_ref_node, "topic reference apiRef apiPackage cppModule": handle_man_node, "topic reference apiRef apiClassifier cppClassifier cppStruct cppGlobalStruct": handle_annotate_node} ctxt_class = node.prop("class") if ctxt_class in map_handler: map_handler[ctxt_class](node, node_title) else: print "ERROR: Inappropriate class for ctxt (%s)" % ctxt_class
def assina_xml(self, xml): self._checar_certificado() self._inicializar_cripto() try: doc_xml = libxml2.parseMemory(xml.encode('utf-8'), len(xml.encode('utf-8'))) signNode = xmlsec.TmplSignature(doc_xml, xmlsec.transformInclC14NId(), xmlsec.transformRsaSha1Id(), None) doc_xml.getRootElement().addChild(signNode) refNode = signNode.addReference( xmlsec.transformSha1Id(), None, '#NFe43150602261542000143550010000000761792265342', None) refNode.addTransform(xmlsec.transformEnvelopedId()) refNode.addTransform(xmlsec.transformInclC14NId()) keyInfoNode = signNode.ensureKeyInfo() keyInfoNode.addX509Data() dsig_ctx = xmlsec.DSigCtx() chave = xmlsec.cryptoAppKeyLoad(filename=str(self.arquivo), format=xmlsec.KeyDataFormatPkcs12, pwd=str(self.senha), pwdCallback=None, pwdCallbackCtx=None) dsig_ctx.signKey = chave dsig_ctx.sign(signNode) status = dsig_ctx.status dsig_ctx.destroy() if status != xmlsec.DSigStatusSucceeded: raise RuntimeError( 'Erro ao realizar a assinatura do arquivo; status: "' + str(status) + '"') xpath = doc_xml.xpathNewContext() xpath.xpathRegisterNs('sig', NAMESPACE_SIG) certificados = xpath.xpathEval( '//sig:X509Data/sig:X509Certificate') for i in range(len(certificados) - 1): certificados[i].unlinkNode() certificados[i].freeNode() xml = doc_xml.serialize() return xml finally: doc_xml.freeDoc() self._finalizar_cripto()
def __init__(self, filename): self._filename = filename f = open(self._filename) data = f.read() f.close() if not data: raise Exception("Form file %s is empty!" % filename) self.fields = [] self.doc = libxml2.parseMemory(data, len(data)) self.process_form(self.doc)
def test03_authWithAutoCreate(self): """test03_authWithAutoCreate Test Poker auth : Try basic auth with autocreate on""" db = self.db settings = pokernetworkconfig.Config([]) autocreate_xml = settings_xml.replace('<server auto_create_account="no" ', '<server auto_create_account="yes" ') settings.doc = libxml2.parseMemory(autocreate_xml, len(autocreate_xml)) settings.header = settings.doc.xpathNewContext() auth = pokerauth.get_auth_instance(db, None, settings) self.assertEquals(auth.auth(PACKET_LOGIN,('joe_schmoe', 'foo')), ((4, 'joe_schmoe', 1), None)) self.assertEquals(log_history.get_all()[-1], 'created user. serial: 4, name: joe_schmoe') self.failUnless(len(self.checkIfUserExistsInDB('joe_schmoe')) == 1)
def setUpServer(self): settings = pokernetworkconfig.Config([]) settings.doc = libxml2.parseMemory(settings_xml_server, len(settings_xml_server)) settings.header = settings.doc.xpathNewContext() # # Setup database # self.db = PokerDatabase(settings) # # Setup server # self.service = pokerservice.PokerService(settings) self.service.verbose = 6
def assina_xml(self, xml, reference): self._checar_certificado() self._inicializar_cripto() try: doc_xml = libxml2.parseMemory( xml, len(xml)) signNode = xmlsec.TmplSignature(doc_xml, xmlsec.transformInclC14NId(), xmlsec.transformRsaSha1Id(), None) doc_xml.getRootElement().addChild(signNode) refNode = signNode.addReference(xmlsec.transformSha1Id(), None, reference, None) refNode.addTransform(xmlsec.transformEnvelopedId()) refNode.addTransform(xmlsec.transformInclC14NId()) keyInfoNode = signNode.ensureKeyInfo() keyInfoNode.addX509Data() dsig_ctx = xmlsec.DSigCtx() chave = xmlsec.cryptoAppKeyLoad(filename=str(self.arquivo), format=xmlsec.KeyDataFormatPkcs12, pwd=str(self.senha), pwdCallback=None, pwdCallbackCtx=None) dsig_ctx.signKey = chave dsig_ctx.sign(signNode) status = dsig_ctx.status dsig_ctx.destroy() if status != xmlsec.DSigStatusSucceeded: raise RuntimeError( 'Erro ao realizar a assinatura do arquivo; status: "' + str(status) + '"') xpath = doc_xml.xpathNewContext() xpath.xpathRegisterNs('sig', NAMESPACE_SIG) certificados = xpath.xpathEval( '//sig:X509Data/sig:X509Certificate') for i in range(len(certificados) - 1): certificados[i].unlinkNode() certificados[i].freeNode() xml = doc_xml.serialize() return xml finally: doc_xml.freeDoc()
def test05_missingRootUser(self): import MySQLdb settings = pokernetworkconfig.Config([]) settings.doc = libxml2.parseMemory(settings_missing_root_users_xml, len(settings_missing_root_users_xml)) settings.header = settings.doc.xpathNewContext() self.settings = settings try: self.db = pokerdatabase.PokerDatabase(self.settings) assert("Root user information was missing so this line should not be reached.") except MySQLdb.OperationalError, oe: # handle trouble self.assertEquals(oe.args[1], "Unknown database 'pokernetworktest'") self.assertEquals(oe.args[0], 1049)
def getLicenseName(self): """Extract the license name from the returned licensing document.""" if self._license_doc is None: return None try: d = libxml2.parseMemory(self._license_doc, len(self._license_doc)) c = d.xpathNewContext() uri = c.xpathEval('//result/license-name')[0].content except libxml2.parserError: return None return uri
def generate_latex_view(cad): styledoc = libxml2.parseFile(os.path.join(XSL_DIR, 'latex_view.xsl')) style = libxslt.parseStylesheetDoc(styledoc) doc = libxml2.parseMemory(cad, len(cad)) xmldoc = style.applyStylesheet(doc, {}) retval = style.saveResultToString(xmldoc) style.freeStylesheet() doc.freeDoc() xmldoc.freeDoc() return retval
def doc_open(self): if os.path.isfile(self.out_file): schema = libxml2.parseMemory(schema_xml, len(schema_xml)) rngp = schema.relaxNGNewDocParserCtxt().relaxNGParse() ctxt = rngp.relaxNGNewValidCtxt() self.doc = libxml2.parseFile(self.out_file) if self.doc.relaxNGValidateDoc(ctxt) is 0: self.xml = self.doc.children else: raise ArticlePoster_Exception("Can't parse old XML: " + self.out_file) else: self.doc = libxml2.newDoc("1.0") self.xml = self.doc.newChild(None, 'posts', None)
def test03_authWithAutoCreate(self): """test03_authWithAutoCreate Test Poker auth : Try basic auth with autocreate on""" db = self.db settings = pokernetworkconfig.Config([]) autocreate_xml = settings_xml.replace('<server auto_create_account="no" ', "<server ") settings.doc = libxml2.parseMemory(autocreate_xml, len(autocreate_xml)) settings.header = settings.doc.xpathNewContext() auth = pokerauth.get_auth_instance(db, settings) clear_all_messages() self.assertEquals(auth.auth("joe_schmoe", "foo"), ((4, "joe_schmoe", 1), None)) self.assertEquals( get_messages(), ["user joe_schmoe does not exist, create it", "creating user joe_schmoe", "create user with serial 4"], ) self.failUnless(len(self.checkIfUserExistsInDB("joe_schmoe")) == 1)
def test06_databaseAlreadyExists(self): """Test for when the database already exists""" import MySQLdb settings = pokernetworkconfig.Config([]) settings.doc = libxml2.parseMemory(settings_root_both_users_xml, len(settings_root_both_users_xml)) settings.header = settings.doc.xpathNewContext() self.settings = settings parameters = settings.headerGetProperties("/server/database")[0] db = MySQLdb.connect(host = parameters["host"], port = int(parameters.get("port", '3306')), user = parameters["root_user"], passwd = parameters["root_password"]) db.query("CREATE DATABASE " + parameters["name"]) db.close() self.db = pokerdatabase.PokerDatabase(self.settings) self.assertEquals(self.db.getVersion(), '1.0.5')
def setUp(self): self.tearDown() settings = pokernetworkconfig.Config([]) settings.doc = libxml2.parseMemory(settings_xml, len(settings_xml)) settings.header = settings.doc.xpathNewContext() self.settings = settings r = re.compile("""INSERT\s+INTO\s+server\s+\(\s*version\s*\)\s+VALUES\s*\("([\d\.]+)"\s*\)""", flags=re.I) infile = open(actualSchemaFile, "r") self.pokerdbVersion = "0.0.0" for line in infile: m = re.match(r, line) if m: self.pokerdbVersion = m.group(1) break infile.close() # We should be able to find the version number self.assertNotEquals(self.pokerdbVersion, "0.0.0")
class MapNBDCBuoy(MapPointThreaded): def do_update(self): p = dplatform.get_platform() try: fn, headers = p.retrieve_url(self.__url) content = file(fn).read() except Exception, e: print "[NBDC] Failed to fetch info for %i: %s" % (self.__buoy, e) self.set_name("NBDC %s" % self.__buoy) return try: doc = libxml2.parseMemory(content, len(content)) except Exception, e: print "[NBDC] Failed to parse document %s: %s" % (self.__url, e) self.set_name("NBDC Unknown Buoy %s" % self.__buoy) return
def test09_forceTestPokerNetworkTooOld(self): settings = pokernetworkconfig.Config([]) settings.doc = libxml2.parseMemory(settings_xml, len(settings_xml)) settings.header = settings.doc.xpathNewContext() self.settings = settings parameters = settings.headerGetProperties("/server/database")[0] try: self.db = pokerdatabase.PokerDatabase(self.settings) import pokernetwork.version ver = pokernetwork.version.Version("32767.32767.32767") realDBVersion = self.db.version self.db.version = ver self.db.checkVersion() assert("Should have gotten ExceptionSoftwareTooOld and this line should not have been reached.") except pokerdatabase.ExceptionSoftwareTooOld, edto: self.assertEquals(edto.args, ()) self.db.version = realDBVersion # Restore original version
def get_gmail_unread_count(self): """ Returns an integer representing the number of unread emails in a gmail inbox. """ email_url = "https://gmail.google.com/gmail/feed/atom" email_request = urllib2.Request(email_url) email_auth = base64.encodestring("%s:%s" % (self.username, self.password))[:-1] email_request.add_header("Authorization", "Basic %s" % email_auth) email_connection = urllib2.urlopen(email_request) email_data = email_connection.read() email_connection.close() email_data_xml = libxml2.parseMemory(email_data, len(email_data)) email_context = email_data_xml.xpathNewContext() email_context.xpathRegisterNs("rss", "http://purl.org/atom/ns#") email_counter = email_context.xpathEval("//rss:fullcount/text()")[0].__str__() return int(email_counter)
def get_docs(self): """ Returns a list of docs in you google apps account. """ docs_request = urllib2.Request("https://docs.google.com/feeds/documents/private/full") headers = self.get_google_apps_clientlogin_headers() for key in headers.keys(): docs_request.add_header(key, headers[key]) docs_connection = urllib2.urlopen(docs_request) docs_data = docs_connection.read() docs_connection.close() docs_data_xml = libxml2.parseMemory(docs_data, len(docs_data)) docs_context = docs_data_xml.xpathNewContext() docs_context.xpathRegisterNs("atom", "http://www.w3.org/2005/Atom") docs_list = docs_context.xpathEval("//atom:feed//atom:entry/atom:title/text()") return [doc.__str__() for doc in docs_list]
def test03_authWithAutoCreate(self): """test03_authWithAutoCreate Test Poker auth : Try basic auth with autocreate on""" db = self.db settings = pokernetworkconfig.Config([]) autocreate_xml = settings_xml.replace( '<server auto_create_account="no" ', '<server ') settings.doc = libxml2.parseMemory(autocreate_xml, len(autocreate_xml)) settings.header = settings.doc.xpathNewContext() auth = pokerauth.get_auth_instance(db, settings) clear_all_messages() self.assertEquals(auth.auth('joe_schmoe', 'foo'), ((4, 'joe_schmoe', 1), None)) self.assertEquals(get_messages(), [ 'user joe_schmoe does not exist, create it', 'creating user joe_schmoe', 'create user with serial 4' ]) self.failUnless(len(self.checkIfUserExistsInDB('joe_schmoe')) == 1)
def setUp(self): self.settings = pokernetworkconfig.Config([]) self.settings.doc = libxml2.parseMemory(settings_mysql_xml, len(settings_mysql_xml)) self.settings.header = self.settings.doc.xpathNewContext() self.parameters = self.settings.headerGetProperties("/server/auth")[0] self.db = MySQLdb.connect(host=self.parameters["host"], port=int(self.parameters.get("port", '3306')), user=self.parameters["user"], passwd=self.parameters["password"]) self.db.query("CREATE DATABASE %s" % self.parameters["db"]) self.db.query("USE %s" % self.parameters["db"]) self.db.query( "CREATE TABLE %s (username varchar(20), password varchar(20), privilege int)" % self.parameters["table"]) self.db.query( "INSERT INTO users (username, password, privilege) VALUES ('testuser', 'testpassword', %i)" % User.REGULAR)
def extract(soap): """extract the saml message from the SOAP envelope *soap*.""" # we use libxml2 for parsing as it is needed for signing as well from libxml2 import parseMemory ps = parseMemory(soap, len(soap)) try: r = ps.getRootElement() child = r.get_children() # this is indeed the first child while child is not None and child.get_name() != "Body": child = child.next # may raise `AttributeError: get_children` when *soap* is not valid message = child.get_children() # This must be an SAML `Request` or `Response` or an SOAP `Fault`. # We do not distinguish `Fault` and map it to `BadMessage`, as well if message.get_name() not in ("Request", "Response"): raise BadMessage(soap) message.reconciliateNs(ps) return message.serialize() finally: ps.freeDoc()