def test_01_merge_two_records_one_field(self):
        """
        PRIORITY: 2 records, 1 field, 2 origins.
        """
        marcxml = """<collections><collection>
  <record>
    <datafield tag="300" ind1=" " ind2=" ">
      <subfield code="a">10</subfield>
      <subfield code="7">A&amp;A</subfield>
    </datafield>
    <datafield tag="980" ind1="" ind2="">
        <subfield code="a">ASTRONOMY</subfield>
        <subfield code="7">ADS metadata</subfield>
    </datafield>
  </record>
  <record>
    <datafield tag="300" ind1=" " ind2=" ">
      <subfield code="a">15</subfield>
      <subfield code="7">NED</subfield>
    </datafield>
  </record>
</collection></collections>"""
        expected = """<collections><collection><record>
  <datafield tag="300" ind1=" " ind2=" ">
    <subfield code="a">10</subfield>
    <subfield code="7">A&amp;A</subfield>
  </datafield>
  <datafield tag="980" ind1="" ind2="">
        <subfield code="a">ASTRONOMY</subfield>
        <subfield code="7">ADS metadata</subfield>
    </datafield>
</record></collection></collections>"""
        merged_record = m.merge_records_xml(libxml2.parseDoc(marcxml))[0]
        self.assertEqual(merged_record, create_record_from_libxml_obj(libxml2.parseDoc(expected), logger)[0])
예제 #2
0
파일: views.py 프로젝트: tickletik/ppi
def _xml_company_text(code, name):
    company = CompanyPage.objects.get(name=name)
    language = LanguageChoice.objects.get(code=code)
    pref = LanguagePref.objects.get(company=company, language=language)

    t = loader.get_template("xslt/company.xslt")
    c = Context({})
    xslt = libxslt.parseStylesheetDoc(libxml2.parseDoc(t.render(c)))

    doc = "<%s>%s</%s>" % ("company", pref.description, "company") 
    doc = libxml2.parseDoc(doc.encode("utf-8"))
    res = libxslt.stylesheet.applyStylesheet(xslt, doc, {})
    content = res.content

    """
    I don't know how to remove the stupid declaration yet, and I need
    this up and running today.  Figure it out later and get rid of this
    """

    p = re.compile('<\?xml version="1.0"\?>')
    content = p.sub("", content)


    info = dict()
    info['title'] = pref.title
    info['description'] = content 

    t = loader.get_template('company/content.xml')
    c = Context({'language':info,})
    return t.render(c)
예제 #3
0
    def testUpdateRecord(self):
        # Add an entry and then change it
        id = self.db.addRecord(None, testEntry)    
        self.assert_(id > 0)

        record = self.db.getRecord(None, id)

        doc = libxml2.parseDoc(record)
        
        newTitle = "modified title"
        title = doc.xpathEval("/item/title")[0]
        title.setContent(newTitle)

        self.db.updateRecord(None, id, doc.serialize())
        
        # Make sure our change is really there
        record = self.db.getRecord(None, id)                
        doc = libxml2.parseDoc(record)
        
        title = doc.xpathEval("/item/title")[0].content
        self.assertEquals(title, newTitle)
        
        # Try updating with empty content
        self.assertRaises(BaseDatabase.ParseError, self.db.updateRecord, None, id, "")

        # Try updating with no content
        self.assertRaises(BaseDatabase.ParseError, self.db.updateRecord, None, id, None)
        
        self.db.deleteRecord(None, id)
예제 #4
0
 def editPost(self, postID, title, body, categories):
     postID = self.cleanPostID(postID)
     
     result = self.runRequest("GET", postID)
     itemXML = result.read().strip()
     
     item = libxml2.parseDoc(itemXML)
     
     # Update the title
     elem = item.xpathEval("/item/title")[0]        
     elem.setContent(title)
     
     # Update the description
     elem = item.xpathEval("/item/description")[0]        
     # This won't work if the content is XML
     elem.unlinkNode()
     
     bodyDoc = libxml2.parseDoc("<description>" + body + "</description>")
     item.getRootElement().addChild(bodyDoc.getRootElement())
     
     # Update the list of categories. 
     results = item.xpathEval("/item/category")
     # First we have to remove the existing categories
     for category in results:
         category.unlinkNode()
         
     # Then add the new categories
     for category in categories:
         item.getRootElement().newChild(None, "category", category.strip())
         
     result = self.runRequest("PUT", postID, item.serialize())
     
     return result
예제 #5
0
 def get_weather_from_google(self, location_id, hl = ""):
     GOOGLE_WEATHER_URL = "http://www.google.com/ig/api?weather=%s&hl=%s"
     curr_conds = ["condition", "temp_c", "humidity", "wind_condition", "icon"]
     fore_conds = ["day_of_week", "low", "high", "condition", "icon"]
     data1 = {}
     data2 = []
     
     handler = urllib.urlopen(GOOGLE_WEATHER_URL % (location_id, hl))
     xml_response = handler.read()
     handler.close()
     try:
         xml_response = re.sub("Umidit.", "Umidita'", xml_response)
         xml_response = re.sub("pi.\ ", "piu' ", xml_response)
         dom = libxml2.parseDoc(xml_response)
     except:
         hl = "us"
         handler = urllib.urlopen(GOOGLE_WEATHER_URL % (location_id, hl))
         xml_response = handler.read()
         handler.close()
         dom = libxml2.parseDoc(xml_response)
     
     for e in dom.xpathEval("//weather//current_conditions"):
         for v in curr_conds:
             data1[v] = e.xpathEval(v)[0].properties.content
     for e in dom.xpathEval("//weather//forecast_conditions"):
         temp = {}
         for v in fore_conds:
             temp[v] = e.xpathEval(v)[0].properties.content
         if hl != "it":
             temp["low"] = int(round((int(temp["low"]) - 32) * 5 / 9)) # F to C in us
             temp["high"] = int(round((int(temp["high"]) - 32) * 5 / 9))
         data2.append(temp)
     dom.freeDoc()
     
     return {"current_conditions" : data1, "forecast_conditions": data2}
def unblockHost():
	mac = raw_input("\n   Enter the target host to unblock [mac]: ")
	
	founded = False
	for host in host_list:
		if str(libxml2.parseDoc(str(host)).xpathEval('/node/node-id/text()')[0])[5:22] == mac:
			founded = True
			targetHost = host
			break
	if founded == False:
		print "\n   => Host not founded in network topology\n"
		return

	ip = libxml2.parseDoc(str(targetHost)).xpathEval('/node/addresses/ip/text()')[0]
	tpID=str(libxml2.parseDoc(str(host)).xpathEval('/node/attachment-points/tp-id/text()')[0]).split(':')
	switch = tpID[0]+':'+tpID[1]
	port = tpID[2]

	print "\n   => Host founded in network topology"
	print "   ======>    MAC: ", mac
	print "   ======>     IP: ", ip
	print "   ======> SWITCH: ", switch
	print "   ======>   PORT: ", port

	url = 'http://localhost:8181/restconf/config/opendaylight-inventory:nodes/node/'+switch+'/table/0/flow/block-'+mac
	connection = pycurl.Curl()
	connection.setopt(connection.URL, url)
	connection.setopt(connection.USERPWD, 'admin:admin')
	connection.setopt(connection.CUSTOMREQUEST, 'DELETE')
	connection.perform()
	connection.close()

	print "   => Request to unblock host has been sended\n"
예제 #7
0
 def _initDBStatus(self):
     """
     db-status is used to track modifications to the database so that cache 
     files can be re-generated. It also contains the next id that will be 
     used when creating new entries.
     """
     # See if there's already a database status document in the database.        
     try:
         dbStatus = self.getRecord(None, DB_STATUS)
         doc = libxml2.parseDoc(dbStatus)
         self.dbStatus = doc
         
         self.modifiedTime = float(doc.xpathEval("/status/db-status")[0].content)            
         self.nextID = int(doc.xpathEval("/status/next-id")[0].content)
        
     except NotFoundError:
          # If not we need to add it to the database.
         metadata = "<status><db-status>0</db-status><next-id>" + str(INITIAL_ID) + "</next-id></status>"
         
         file = open(self.dbPath + str(DB_STATUS) + ".xml", "w")
         file.write(metadata)
         file.close()
             
         self.dbStatus = libxml2.parseDoc(metadata)
         self.modifiedTime = 0
         self.nextID = INITIAL_ID
def getTopology():
	url = 'http://localhost:8181/restconf/operational/network-topology:network-topology'
	storage = StringIO()
	connection = pycurl.Curl()
	connection.setopt(connection.URL, url)
	connection.setopt(connection.USERPWD, 'admin:admin')
	connection.setopt(connection.HTTPHEADER, ['Accept: application/xml'])
	connection.setopt(connection.WRITEFUNCTION, storage.write)
	connection.perform()
	connection.close()

	content = storage.getvalue()
	topology = libxml2.parseDoc(re.sub(' xmlns="[^"]+"', '', storage.getvalue()))
	nodes=topology.xpathEval('/network-topology/topology/node')

	#Clear previous entries
	for i in range(len(switch_list)):
		switch_list.pop()
	for i in range(len(host_list)):
		host_list.pop()

	for node in nodes:
		node_id=str(libxml2.parseDoc(str(node)).xpathEval('/node/node-id/text()')[0])
		if node_id.find("openflow")>= 0:
			switch_list.append(node)
		if node_id.find("host")>= 0:
			host_list.append(node)
예제 #9
0
파일: keywords.py 프로젝트: Akanksha18/eden
def searchInfo(keyword):
    #import urllib2 as u
    #import libxml2 as l
    f = u.urlopen("http://dev.healthscapes.org/geoserver/wms?request=GetCapabilities&version=1.1.0&namespace=hsd")
    d = l.parseDoc(f.read())
    ct = d.xpathNewContext()
    nodes = ct.xpathEval("//Layer[KeywordList/Keyword='"+keyword+"']")

    title = []
    desc = []
    layer = []
    for node in nodes:
        lyr = l.parseDoc(node.serialize())
        title.append(lyr.xpathEval("/Layer/Title")[0].content)
        desc.append(lyr.xpathEval("/Layer/Abstract")[0].content)
        layer.append(lyr.xpathEval("/Layer/Name")[0].content)

# if len(title) == 0:
#     title = "'null'"
# 
# if len(desc) == 0:
#     desc = "'null'"
# 
# if len(layer) == 0:
#     layer = "'null'"
# 
    return {'layers': layer, 'desc': desc, 'name': title}
예제 #10
0
 def endElement(self, tag):
     ""
     self._current+="</%s>" % (tag,)
     self._level -= 1
     if self._level > 1:
         return
     if self._level==1:
         xml=self._head+self._current+self._tail
         doc=libxml2.parseDoc(xml)
         try:
             node = doc.getRootElement().children
             try:
                 node1 = node.docCopyNode(self._doc, 1)
                 try:
                     self._root.addChild(node1)
                     self._handler.stanza(self._doc, node1)
                 except Exception, e:
                     node1.unlinkNode()
                     node1.freeNode()
                     del node1
                     raise e
             finally:
                 del node
         finally:
             doc.freeDoc()
     else:
         xml=self._head+self._tail
         doc=libxml2.parseDoc(xml)
         try:
             self._handler.stream_end(self._doc)
             self._doc.freeDoc()
             self._doc = None
             self._root = None
         finally:
             doc.freeDoc()
예제 #11
0
def main():
    # We get the token
    getToken = urllib.urlopen('http://api.kewego.com/app/getToken/?appKey=75affcd3efe9071a0184877f96c897fe').read()
    xmlFile = libxml2.parseDoc(getToken)
    root = xmlFile.children
    child = root.children
    counter = 1
    while counter < 3:
	token=child.content
	child = child.next
        counter = counter + 1
    xmlFile.freeDoc()
    
    # We check the token
    url = 'http://api.kewego.com/app/checkToken/?appToken='.__add__(token)  
    checkToken = urllib.urlopen(url).read()
    xmlFile = libxml2.parseDoc(checkToken)
    response = xmlFile.content.strip()
    xmlFile.freeDoc()
    
    if response == 'Invalid Token':
        output = 'kewego API check is CRITICAL: Invalid Token'
        exitstatus = 2
    else:
        # Response is 'true'
        output = 'kewego API check is OK: Token was accepted'
        exitstatus = 0

    print '%s' %(output)
    sys.exit (exitstatus)
예제 #12
0
  def testAddRecord(self):
      # Add an entry
      id = self.db.addRecord(None, testEntry)    
      self.assert_(id > 0)
      
      # Retrieve the entry we just added
      record = self.db.getRecord(None, id)
      
      # Compare it to the original document
      doc = libxml2.parseDoc(record)
      testDoc = libxml2.parseDoc(testEntry)
      
      title = doc.xpathEval("/item/title")[0].content        
      testTitle = testDoc.xpathEval("/item/title")[0].content        
      
      self.assertEqual(testTitle, title)
      
      description = doc.xpathEval("/item/description")[0].content
      testDescription = testDoc.xpathEval("/item/description")[0].content
      self.assertEqual(testDescription, description)
      
      # Test adding an empty entry, should get a ParseError        
      self.assertRaises(BaseDatabase.ParseError, self.db.addRecord, None, "")
      
      # Test adding an non-wellformed entry, should get a ParseError
      badEntry = "<document>"
      self.assertRaises(BaseDatabase.ParseError, self.db.addRecord, None, badEntry)
      
      # Test adding a None entry
      self.assertRaises(BaseDatabase.ParseError, self.db.addRecord, None, None)
      
      # Remove the test entry
      print "before"
      self.db.deleteRecord(None, id)
      print "after"
      # Add an entry pre-parsed
      doc = libxml2.parseDoc(testEntry)
      
      id = self.db.addRecord(None, doc)    
      self.assert_(id > 0)
      
      # Retrieve the entry we just added
      record = self.db.getRecord(None, id)
      
      # Compare it to the original document
      doc = libxml2.parseDoc(record)
      testDoc = libxml2.parseDoc(testEntry)
      
      title = doc.xpathEval("/item/title")[0].content        
      testTitle = testDoc.xpathEval("/item/title")[0].content        
      
      self.assertEqual(testTitle, title)
      
      description = doc.xpathEval("/item/description")[0].content
      testDescription = testDoc.xpathEval("/item/description")[0].content
      self.assertEqual(testDescription, description)
 
      # Remove the second test entry
      self.db.deleteRecord(None, id)
예제 #13
0
def main(argv):
    print libxml2.parseDoc(__createFileDetails('123',
                                            'aef123456',
                                            'kids.png',
                                            [ { 'type': 'EXIF',
                                                'data': [ { 'name': 'lat', 'value': '123.45' },
                                                          { 'name': 'lng', 'value': '-123.45' }] } ])
                        )
예제 #14
0
    def testGetPathDataForPost(self):
        # Create a new entry
        result = self.runRequest("POST", REQUEST_URL, testEntry)
        location = result.getheader("Location")

        # Read the content to clear the request
        result.read()

        # Test a query for a single XML result
        result = self.runRequest("GET", location + "/item/title")
        self.assertEqual(result.status, 200)
        self.assertEqual(result.reason, "OK")

        data = result.read().strip()
        doc = libxml2.parseDoc(data)
        title = doc.xpathEval("/results/title")[0].content
        self.assertEqual("Test Post 1", title)

        # Test a query for a single text result
        result = self.runRequest("GET", location + "/item/title/text()")
        self.assertEqual(result.status, 200)
        self.assertEqual(result.reason, "OK")

        data = result.read().strip()
        self.assertEqual('<?xml version="1.0"?>\n<results>Test Post 1</results>', data)

        # Test a query for multiple XML results
        result = self.runRequest("GET", location + "/item/category")
        self.assertEqual(result.status, 200)
        self.assertEqual(result.reason, "OK")

        # We should get two categories in the results
        data = result.read().strip()
        doc = libxml2.parseDoc(data)
        categories = doc.xpathEval("/results/category")
        self.assertEqual(len(categories), 2)

        # Test a query for multiple text results
        result = self.runRequest("GET", location + "/item/category/text()")
        self.assertEqual(result.status, 200)
        self.assertEqual(result.reason, "OK")

        # We should get two categories, one per line in the results
        # categories = result.read().strip().split("\n")
        # self.assertEqual(len(categories), 2)
        # self.assert_(categories[0] == "XML" or categories[1] == "XML")
        # Read the content to clear the request
        result.read()

        # Remove the entry
        result = self.runRequest("DELETE", location)

        # Read the content to clear the request
        result.read()

        # Test a query against a non-existent resource
        result = self.runRequest("GET", location + "/item/title")
        self.assertEqual(result.status, 404)
    def test_04_merge_three_records_two_fields(self):
        """
        3 records, 6 fields, 6 origins.
        """
        marcxml = """<collections><collection>
  <record>
    <datafield tag="300" ind1=" " ind2=" ">
      <subfield code="a">10</subfield>
      <subfield code="7">A&amp;A</subfield>
    </datafield>
    <datafield tag="773" ind1=" " ind2=" ">
      <subfield code="a">Libération</subfield>
      <subfield code="7">STI</subfield>
    </datafield>
    <datafield tag="980" ind1="" ind2="">
      <subfield code="a">ASTRONOMY</subfield>
      <subfield code="7">ADS metadata</subfield>
    </datafield>
  </record>
  <record>
    <datafield tag="773" ind1=" " ind2=" ">
      <subfield code="a">Le Monde</subfield>
      <subfield code="7">AAS</subfield>
    </datafield>
    <datafield tag="300" ind1=" " ind2=" ">
      <subfield code="a">15</subfield>
      <subfield code="7">NED</subfield>
    </datafield>
  </record>
  <record>
    <datafield tag="300" ind1=" " ind2=" ">
      <subfield code="a">5</subfield>
      <subfield code="7">ADS metadata</subfield>
    </datafield>
    <datafield tag="773" ind1=" " ind2=" ">
      <subfield code="a">L'Express</subfield>
      <subfield code="7">OCR</subfield>
    </datafield>
  </record>
</collection></collections>"""
        expected = """<collections><collection><record>
  <datafield tag="300" ind1=" " ind2=" ">
    <subfield code="a">5</subfield>
    <subfield code="7">ADS metadata</subfield>
  </datafield>
  <datafield tag="773" ind1=" " ind2=" ">
    <subfield code="a">Le Monde</subfield>
    <subfield code="7">AAS</subfield>
  </datafield>
  <datafield tag="980" ind1="" ind2="">
    <subfield code="a">ASTRONOMY</subfield>
    <subfield code="7">ADS metadata</subfield>
  </datafield>
</record></collection></collections>"""
        merged_record = m.merge_records_xml(libxml2.parseDoc(marcxml))[0]
        self.assertEqual(merged_record, create_record_from_libxml_obj(libxml2.parseDoc(expected), logger)[0])
예제 #16
0
def getVmHost(xml):
    doc = libxml2.parseDoc(xml)
    ctxt = doc.xpathNewContext()
    res = ctxt.xpathEval("//host")
    if len(res) == 1:
        for i in res:
            hosthref = i.prop("href")
    hostxml = rhevGet(hosthref)
    doc = libxml2.parseDoc(hostxml)
    ctx = doc.xpathNewContext()
    res = ctx.xpathEval("/host/address")
    return res[0].getContent()
예제 #17
0
def msiXSLTransform(inxml,  xsl, outxml,  rei):
    inxml = inxml.parseForStr()
    xsl = xsl.parseForStr()
    print 'msiXSLTransform: inxml',  inxml
    print 'msiXSLTransform: xsl',  xsl

    doc = libxml2.parseDoc(inxml)
    styledoc = libxml2.parseDoc(xsl)
    
    style = libxslt.parseStylesheetDoc(styledoc)
    
    result = style.applyStylesheet(doc, None)
    result = str(result)
    print 'msiXSLTransform: result',  result
    irods.fillStrInMsParam(outxml,  result)
예제 #18
0
   def testUpdateNode(self):
       # create a tree and then set a node to a new value
       doc = XMLFragment()
       doc.addNode("/root/child")
       doc.updateNode("/root/child", "text")
       
       self.assertEqual(doc.serialize(), '<?xml version="1.0"?>\n<root><child>text</child></root>\n')
       
       # add another child and then update both to the same value
       doc.addNode("/root/child")
       doc.updateNode("/root/child", "newText")
       
       self.assertEqual(doc.serialize(), '<?xml version="1.0"?>\n<root><child>newText</child><child>newText</child></root>\n')
       
       # Now just update the second child.
       doc.updateNode("/root/child[2]", "text")
       
       self.assertEqual(doc.serialize(), '<?xml version="1.0"?>\n<root><child>newText</child><child>text</child></root>\n')
       
       # Now update an attribute
       doc.addNode("/root/child[1]/@attr", "text")
       doc.updateNode("/root/child[1]/@attr", "newText")
       self.assertEqual(doc.serialize(), '<?xml version="1.0"?>\n<root><child attr="newText">newText</child><child>text</child></root>\n')
       
       # change a node to a string that should be parsed as markup
       doc = XMLFragment()
       doc.addNode("/root/child", "text")
       doc.updateNode("/root/child", "<test>text</test>", 1)
       self.assertEqual(doc.serialize(), '<?xml version="1.0"?>\n<root><child><test>text</test></child></root>\n')
       
       # replace the content of a node with more then one child
       doc = XMLFragment()
       doc.addNode("/root/child", "<test>text</test><test>text2</test>", 1)
       doc.updateNode("/root/child", "<test>text3</test><test>text4</test>", 1)
       self.assertEqual(doc.serialize(), '<?xml version="1.0"?>\n<root><child><test>text3</test><test>text4</test></child></root>\n')
      
       # add a pre-parsed node to the document
       doc = XMLFragment()
       doc.addNode("/root/child")
       markup = libxml2.parseDoc("<test>text</test>")
       doc.updateNode("/root/child", markup.getRootElement())
       self.assertEqual(doc.serialize(), '<?xml version="1.0"?>\n<root><child><test>text</test></child></root>\n')
 
       # Update a node that doesn't already exist.
       doc = XMLFragment()
       markup = libxml2.parseDoc("<test>text</test>")
       doc.updateNode("/root/child", markup.getRootElement())
       self.assertEqual(doc.serialize(), '<?xml version="1.0"?>\n<root><child><test>text</test></child></root>\n')
    def convert_content(self, req, input_type, source, output_type):
        #extract all data resources
        datadir = resource_filename(__name__, 'data')

        html = wiki_to_html(source, self.env, req)
	options = dict(output_xhtml=1, add_xml_decl=1, indent=1, tidy_mark=0, input_encoding='utf8', output_encoding='utf8', doctype='auto', wrap=0, char_encoding='utf8')
	xhtml = parseString(html.encode("utf-8"), **options)

	xhtml2dbXsl = u"""<?xml version="1.0"?>
<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:import href=\"file:///""" + urllib.pathname2url(resource_filename(__name__, 'data/html2db/html2db.xsl')) + """\" />
  <xsl:output method="xml" indent="no" encoding="utf-8"/>
  <xsl:param name="document-root" select="'chapter'"/>
</xsl:stylesheet>
"""

	normalizedHeadingsXsl = u"""<?xml version="1.0"?>
<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:import href=\"file:///""" + urllib.pathname2url(resource_filename(__name__, 'data/headingsNormalizer/headingsNormalizer.xsl')) + """\" />
  <xsl:output method="xml" indent="no" encoding="utf-8"/>
  <xsl:param name="defaultTopHeading" select="'""" + req.path_info[6:] + """'"/>
</xsl:stylesheet>
"""

	xhtml_xmldoc = libxml2.parseDoc(str(xhtml))

	normalizedHeadingsXsl_xmldoc = libxml2.parseDoc(normalizedHeadingsXsl)
	normalizedHeadingsXsl_xsldoc = libxslt.parseStylesheetDoc(normalizedHeadingsXsl_xmldoc)
	xhtml2_xmldoc = normalizedHeadingsXsl_xsldoc.applyStylesheet(xhtml_xmldoc, None)

	nhstring = normalizedHeadingsXsl_xsldoc.saveResultToString(xhtml2_xmldoc)

	xhtml2dbXsl_xmldoc = libxml2.parseDoc(xhtml2dbXsl)
	xhtml2dbXsl_xsldoc = libxslt.parseStylesheetDoc(xhtml2dbXsl_xmldoc)
	docbook_xmldoc = xhtml2dbXsl_xsldoc.applyStylesheet(xhtml2_xmldoc, None)

	dbstring = xhtml2dbXsl_xsldoc.saveResultToString(docbook_xmldoc)

	xhtml_xmldoc.freeDoc()
	normalizedHeadingsXsl_xsldoc.freeStylesheet()
	xhtml2dbXsl_xsldoc.freeStylesheet()
	xhtml2_xmldoc.freeDoc()
	docbook_xmldoc.freeDoc()
	return (dbstring, 'text/plain') #application/docbook+xml
    def _parseFirewalld(self, format):
        ret = ''
        for content in [
            content
            for key, content in self.environment.items()
            if key.startswith(
                otopicons.NetEnv.FIREWALLD_SERVICE_PREFIX
            )
        ]:
            doc = None
            ctx = None
            try:
                doc = libxml2.parseDoc(content)
                ctx = doc.xpathNewContext()
                nodes = ctx.xpathEval("/service/port")
                for node in nodes:
                    ret += format.format(
                        protocol=node.prop('protocol'),
                        port=node.prop('port'),
                    )
            finally:
                if doc is not None:
                    doc.freeDoc()
                if ctx is not None:
                    ctx.xpathFreeContext()

        return ret
예제 #21
0
    def generic_decode_credentials(self, credentials, provider_data, target):
        # convenience function for simple creds (rhev-m and vmware currently)
        doc = libxml2.parseDoc(credentials)

        self.username = None
        _usernodes = doc.xpathEval("//provider_credentials/%s_credentials/username" % (target))
        if len(_usernodes) > 0:
            self.username = _usernodes[0].content
        else:
            try:
                self.username = provider_data['username']
            except KeyError:
                raise ImageFactoryException("No username specified in config file or in push call")
        self.provider_account_identifier = self.username

        _passnodes = doc.xpathEval("//provider_credentials/%s_credentials/password" % (target))
        if len(_passnodes) > 0:
            self.password = _passnodes[0].content
        else:
            try:
                self.password = provider_data['password']
            except KeyError:
                raise ImageFactoryException("No password specified in config file or in push call")

        doc.freeDoc()
예제 #22
0
	def parse(self, xml, pvars):
		import libxml2
		doc = libxml2.parseDoc(xml)
		
		pvars = self.getVars(pvars)
		vals = {}
		for k, v in pvars.items():
			res = doc.xpathEval(v)
			vals[k] = []
			for r in res:
				
				#if not vals.has_key(r.name):
				#	vals[r.name] = []
				
				if r.type == 'element':
					#vals[r.name].append(self.xmlToDict(minidom.parseString(str(r)))[r.name])
					vals[k].append(self.xmlToDict(minidom.parseString(str(r)))[r.name])
				elif r.type == 'attribute':
					vals[k].append(r.content)
				else:
					print("UNKNOWN TYPE")
			
			if len(vals[k]) == 1:
				vals[k] = vals[k][0]
			elif len(vals[k]) == 0:
				vals[k] = None

		return vals
예제 #23
0
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
예제 #24
0
    def testEditPost(self):
        # Create a new entry
        result = self.runRequest("POST", REQUEST_URL, testEntry)
        location = result.getheader("Location")

        # Read the content to clear the request
        result.read()

        # Update the content of that entry
        result = self.runRequest("PUT", location, testEntry2)
        self.assertEqual(result.status, 205)
        self.assertEqual(result.reason, "Reset Content")
        result.read()

        # Make sure the content is updated
        result = self.runRequest("GET", location)
        self.assertEqual(result.status, 200)

        data = result.read().strip()
        doc = libxml2.parseDoc(data)
        title = doc.xpathEval("/item/title")[0].content
        self.assertEqual("Test Post 2", title)

        # Remove the entry
        result = self.runRequest("DELETE", location)

        # Read the content to clear the request
        result.read()

        # Try updating the entry again to test updating a non-existent entry
        result = self.runRequest("PUT", location, testEntry2)
        self.assertEqual(result.status, 404)
예제 #25
0
def main():
	content_type = validate_content_type(sys.argv[2])
        conn = http_connect(sys.argv[1])
	req = conn.getresponse()

        # check response code
        if req.status == 200:
                doc = libxml2.parseDoc(req.read())
                root = doc.children
                for child in root:
			# check what units are configured for reporting
			if content_type == "temperature" and child.name == "tempUnits":
				global unit
				unit = "f" if str(child.content).lower() == "fahrenheit" else "c"

			# found temperature elements!
			if content_type == "temperature" and (child.type == "element" and re.search("temperature",str(child.properties))):
				find_current_reading_details("temperature",child)

			# found humidity elements!
			if content_type == "humidity" and (child.type == "element" and re.search("humidity",str(child.properties))):
				find_current_reading_details("humidity",child)

                # close XML document
                doc.freeDoc()

        # non HTTP/200 OK was returned
        else:
                print "An HTTP/200 OK was NOT returned."
                sys.exit(1)

        # close connection
        conn.close()
예제 #26
0
    def testGetPost(self):
        # Create a new entry
        result = self.runRequest("POST", REQUEST_URL, testEntry)
        location = result.getheader("Location")

        # Read the content to clear the request
        result.read()

        # Test retrieving that entry
        result = self.runRequest("GET", location)
        self.assertEqual(result.status, 200)
        self.assertEqual(result.reason, "OK")

        data = result.read().strip()
        doc = libxml2.parseDoc(data)
        title = doc.xpathEval("/item/title")[0].content
        self.assertEqual("Test Post 1", title)

        # Remove the entry
        result = self.runRequest("DELETE", location)

        # Read the content to clear the request
        result.read()

        # Test retrieving it again for a non-existent entry
        result = self.runRequest("GET", location)
        self.assertEqual(result.status, 404)
예제 #27
0
def testSaveIntoOutputBuffer():
    """
    Similar to the previous two tests, except this time we invoke the save
    methods on the output buffer object and pass in an XML node object.
    """
    input = '<foo>Hello</foo>'
    expected = '''\
<?xml version="1.0" encoding="UTF-8"?>
<foo>Hello</foo>
'''
    f = str_io()
    doc = libxml2.parseDoc(input)
    buf = libxml2.createOutputBuffer(f, 'UTF-8')
    buf.saveFileTo(doc, 'UTF-8')
    if f.getvalue() != expected:
        print('outputBuffer.saveFileTo() call failed.')
        print('     got: %s' % repr(f.getvalue()))
        print('expected: %s' % repr(expected))
        sys.exit(1)
    f = str_io()
    buf = libxml2.createOutputBuffer(f, 'UTF-8')
    buf.saveFormatFileTo(doc, 'UTF-8', 1)
    if f.getvalue() != expected:
        print('outputBuffer.saveFormatFileTo() call failed.')
        print('     got: %s' % repr(f.getvalue()))
        print('expected: %s' % repr(expected))
        sys.exit(1)
    doc.freeDoc()
예제 #28
0
def static_file_merging():
    """runs the record merger from a static XML in a file bypassing the extraction"""
    static_file = "misc/2011ApJ...741...91C.xml"
    #static_file = "misc/1999PASP..111..438F.xml"
    #static_file = "misc/1984A&A...130...97L.xml"
    logger.warn(static_file)
    return merge_records_xml(libxml2.parseDoc(open(static_file, "r").read()))
예제 #29
0
def get_xml_path(xml, path=None, func=None):
    """
    Return the content from the passed xml xpath, or return the result
    of a passed function (receives xpathContext as its only arg)
    """
    doc = None
    ctx = None
    result = None

    try:
        doc = libxml2.parseDoc(xml)
        ctx = doc.xpathNewContext()

        if path:
            ret = ctx.xpathEval(path)
            if ret != None:
                if type(ret) == list:
                    if len(ret) >= 1:
                        result = ret[0].content
                else:
                    result = ret

        elif func:
            result = func(ctx)

        else:
            raise ValueError(_("'path' or 'func' is required."))
    finally:
        if doc:
            doc.freeDoc()
        if ctx:
            ctx.xpathFreeContext()
    return result
예제 #30
0
파일: fiskal.py 프로젝트: jerko/fiskal_v70
def received(self, context):
    self.poruka_odgovor = context.reply
 
    libxml2.initParser()
    libxml2.substituteEntitiesDefault(1)
 
    xmlsec.init()
    xmlsec.cryptoAppInit(None)
    xmlsec.cryptoInit()
 
    mngr = xmlsec.KeysMngr()
    xmlsec.cryptoAppDefaultKeysMngrInit(mngr)
    #mngr.certLoad(verifyCertFile, xmlsec.KeyDataFormatPem, xmlsec.KeyDataTypeTrusted)
    mngr.certLoad(certFile, xmlsec.KeyDataFormatPem, xmlsec.KeyDataTypeTrusted)
  
    doc = libxml2.parseDoc(context.reply)
    xmlsec.addIDs(doc, doc.getRootElement(), ['Id'])
    node = xmlsec.findNode(doc.getRootElement(), xmlsec.NodeSignature, xmlsec.DSigNs)
    dsig_ctx = xmlsec.DSigCtx(mngr)
    dsig_ctx.verify(node)
    if(dsig_ctx.status == xmlsec.DSigStatusSucceeded): self.valid_signature = 1
 
    xmlsec.cryptoShutdown()
    xmlsec.cryptoAppShutdown()
    xmlsec.shutdown()
    libxml2.cleanupParser()
    return context
def deleteSecurityGroupRule():
    respData = restclient.get(
        NSX_URL + '/api/2.0/services/policy/securitypolicy/' +
        NSX_SECURITYGROUP_ID, 'getSecurityGroup')
    respDoc = libxml2.parseDoc(respData)
    xp = respDoc.xpathNewContext()
    ruleNodes = xp.xpathEval("//actionsByCategory/action")
    for rule in ruleNodes:
        xp.setContextNode(rule)
        objectId = xp.xpathEval("objectId")[0].getContent()

        if objectId == NSX_RULE_DEL_ID:
            rule.unlinkNode()
            break
    respData = respDoc.serialize('UTF-8', 1)
    respData = restclient.put(
        NSX_URL + '/api/2.0/services/policy/securitypolicy/' +
        NSX_SECURITYGROUP_ID, respData, 'deleteSecurityGroupRule')
    output(restclient.getDebugInfo() + restclient.prettyPrint(respData))
예제 #32
0
    def add_child(self, obj):
        """
        Insert the passed XMLBuilder object into our XML document. The
        object needs to have an associated mapping via XMLChildProperty
        or an error is thrown.
        """
        xmlprop = self._find_child_prop(obj.__class__)
        xml = obj.get_xml_config()
        xmlprop.append(self, obj)
        self._set_child_xpaths()

        if not obj._xmlstate.is_build:
            use_xpath = obj.get_root_xpath().rsplit("/", 1)[0]
            indent = 2 * obj.get_root_xpath().count("/")
            newnode = libxml2.parseDoc(self.xml_indent(xml, indent)).children
            parentnode = _build_xpath_node(self._xmlstate.xml_ctx, use_xpath)
            # Tack newnode on the end
            _add_pretty_child(parentnode, newnode)
        obj._parse_with_children(None, self._xmlstate)
예제 #33
0
def generate_html_exam_view(xmldoc, readonly):

    #outfile = os.path.splitext(file)[0] + '.html'

    styledoc = libxml2.parseFile('xsl/exam_html_view-gen.xsl')
    style = libxslt.parseStylesheetDoc(styledoc)

    params = {}
    if readonly: params['readonly'] = '"1"'

    doc = libxml2.parseDoc(xmldoc)
    result = style.applyStylesheet(doc, params)

    htmldoc = style.saveResultToString(result)
    style.freeStylesheet()
    doc.freeDoc()
    result.freeDoc()

    return htmldoc
def listRouters():
    respData = restclient.get(NSX_URL + '/api/4.0/edges', 'listRouters')
    outputstr = restclient.getDebugInfo() + restclient.prettyPrint(respData)

    respDoc = libxml2.parseDoc(respData)
    xp = respDoc.xpathNewContext()
    nodes = xp.xpathEval("//pagedEdgeList/edgePage/edgeSummary/objectId")

    for node in nodes:
        edgeId = node.content
        outputstr += "\nTry to get router(%s) static and default routes:\n" % (
            edgeId)
        respData = restclient.get(
            "%s/api/4.0/edges/%s/routing/config/static" % (NSX_URL, edgeId),
            'listRouters')
        outputstr += restclient.getDebugInfo() + restclient.prettyPrint(
            respData)

    output(outputstr)
예제 #35
0
 def show_data(self, data, dir):
     if self.settings.get("pretty_print") and data:
         try:
             d = libxml2.parseDoc(to_utf8(data))
         except libxml2.parserError:
             cjc_globals.screen.redraw(
             )  # workaroud for libxml2 messing the screen
         except:
             raise
         else:
             data = d.getRootElement().serialize("utf-8", 1)
     if type(data) is UnicodeType:
         pass
     elif type(data) is StringType:
         data = unicode(data, "utf-8", "replace")
     else:
         data = ` data `
     self.buffer.append_themed("xmlconsole." + dir, data)
     self.buffer.update()
예제 #36
0
def GetPanoramaMetadata(panoid=None, lat=None, lon=None, radius=2000):
    url = '%s?'
    url += 'output=xml'  # metadata output
    url += '&v=4'  # version
    url += '&dm=1'  # include depth map
    url += '&pm=1'  # include pano map
    if panoid == None:
        url += '&ll=%s,%s'  # lat/lon to search at
        url += '&radius=%s'  # search radius
        url = url % (BaseUri, lat, lon, radius)
    else:
        url += '&panoid=%s'  # panoid to retrieve
        url = url % (BaseUri, panoid)

    findpanoxml = GetUrlContents(url)
    if not findpanoxml.find('data_properties'):
        return None
    # print findpanoxml
    return PanoramaMetadata(libxml2.parseDoc(findpanoxml))
예제 #37
0
def createQOS():
    respData = restclient.get(
        "%s/api/4.0/edges/%s/vnics/%s" %
        (NSX_URL, NSX_EDGE_ID, NSX_EDGE_INTERFACE_INDEX), 'getQOS')

    respDoc = libxml2.parseDoc(respData)
    xp = respDoc.xpathNewContext()
    vnicNode = xp.xpathEval("//vnic")[0]
    isConnected = xp.xpathEval("//vnic/isConnected")[0].getContent()
    if isConnected != 'true':
        print "[Warning] Disconneted interface cannot create QOS policy."
        return False

    ispNodes = xp.xpathEval("//vnic/inShapingPolicy")
    ospNodes = xp.xpathEval("//vnic/outShapingPolicy")
    if len(ispNodes) > 0 or len(ospNodes) > 0:
        print "[Warning] The interface's QOS policy is exist."
        return False

    newIspNode = vnicNode.newChild(None, 'inShapingPolicy', None)
    newIspNode.newChild(None, 'averageBandwidth',
                        inShapingPolicy_averageBandwidth)
    newIspNode.newChild(None, 'peakBandwidth', inShapingPolicy_peakBandwidth)
    newIspNode.newChild(None, 'enabled', inShapingPolicy_enabled)
    newIspNode.newChild(None, 'burstSize', '0')
    newIspNode.newChild(None, 'inherited', 'false')

    newOspNode = vnicNode.newChild(None, 'outShapingPolicy', None)
    newOspNode.newChild(None, 'averageBandwidth',
                        outShapingPolicy_averageBandwidth)
    newOspNode.newChild(None, 'peakBandwidth', outShapingPolicy_peakBandwidth)
    newOspNode.newChild(None, 'enabled', outShapingPolicy_enabled)
    newOspNode.newChild(None, 'burstSize', '0')
    newOspNode.newChild(None, 'inherited', 'false')

    createData = respDoc.serialize('UTF-8', 1)

    respData = restclient.put(
        "%s/api/4.0/edges/%s/vnics/%s" %
        (NSX_URL, NSX_EDGE_ID, NSX_EDGE_INTERFACE_INDEX), createData,
        'createQOS')
    output(restclient.getDebugInfo() + restclient.prettyPrint(respData))
    return True
예제 #38
0
    def SendReport(self, clientid, xmlbzb64):
        decompr = bz2.BZ2Decompressor()
        xmldoc = libxml2.parseDoc(decompr.decompress(base64.b64decode(xmlbzb64)))

        # Save a copy of the report on the file system
        # Make sure we have a directory to write files into
        self.__mkdatadir(self.config.datadir + '/queue/')
        fname = self.__getfilename('queue/', ('%s' % clientid), '.xml', False)
        xmldoc.saveFormatFileEnc(fname,'UTF-8',1)
        if self.debug:
            print "Copy of report: %s" % fname

        # Register the submission and put it in a parse queue
        rterid = rtevaldb.register_submission(self.config, clientid, fname,
                                                        debug=self.debug, noaction=self.nodbaction)
        if self.nodbaction:
            rterid = 999999999 # Fake ID when no database registration is done

        return rterid
def getFloatingIP():
    respData = restclient.get(
        '%s/api/4.0/edges/%s/nat/config' % (NSX_URL, NSX_EDGE_ID),
        'getFloatingIP')
    outputStr = (restclient.getDebugInfo() + restclient.prettyPrint(respData))

    respDoc = libxml2.parseDoc(respData)
    xp = respDoc.xpathNewContext()
    rules = xp.xpathEval("//nat/natRules/natRule")

    outputStr += "\n\nFloatingIP(%s) NAT rules:\n" % (NSX_FLOATING_IP)
    for rule in rules:
        xp.setContextNode(rule)
        origAddr = xp.xpathEval("originalAddress")[0].getContent()
        transAddr = xp.xpathEval("translatedAddress")[0].getContent()
        if NSX_FLOATING_IP in [origAddr, transAddr]:
            outputStr += (rule.serialize('UTF-8', 1) + '\n')

    output(outputStr)
예제 #40
0
def testSaveDocToBuffer():
    """
    Regression test for bug #154294.
    """
    input = '<foo>Hello</foo>'
    expected = '''\
<?xml version="1.0" encoding="UTF-8"?>
<foo>Hello</foo>
'''
    f = str_io()
    buf = libxml2.createOutputBuffer(f, 'UTF-8')
    doc = libxml2.parseDoc(input)
    doc.saveFileTo(buf, 'UTF-8')
    doc.freeDoc()
    if f.getvalue() != expected:
        print('xmlDoc.saveFileTo() call failed.')
        print('     got: %s' % repr(f.getvalue()))
        print('expected: %s' % repr(expected))
        sys.exit(1)
def getPortFlow():
    # get lsId and portKey from input parameter NSX_PORT_ID
    pids = NSX_PORT_ID.split('.')
    if len(pids) != 2:
        print "[Error] NSX_PORT_ID:%s is wrong format." % (NSX_PORT_ID)
        return False
    lsId = pids[0]
    portKey = pids[1]

    # get ls object by NSX API
    respData = restclient.get(NSX_URL + '/api/2.0/vdn/virtualwires/' + lsId,
                              'getPortFlow')
    #outputstr = restclient.getDebugInfo() + restclient.prettyPrint(respData)

    # get port group list by vSphere API
    #outputstr = "\nTry to get backing port info by vSphere API:\n"
    dpgMap = getPortGroupMap()
    #outputstr += str(dpgMap)

    #outputstr += "\n\n\nTry to take out port info from vSphere and NSX API response:\n"
    respDoc = libxml2.parseDoc(respData)
    xp = respDoc.xpathNewContext()
    lsName = xp.xpathEval("//virtualWire/name")[0].content
    tenantId = xp.xpathEval("//virtualWire/tenantId")[0].content
    dpgId = xp.xpathEval(
        "//virtualWire/vdsContextWithBacking/backingValue")[0].content

    dpg = dpgMap[dpgId]
    if dpg == None:
        print "[Error] NSX_DPG_ID:%s not found." % (dpgId)
        return False

    # try to find connected port in all the attached vms by portKey
    for vm in dpg.vm:
        for dev in vm.config.hardware.device:
            if isinstance(dev, vim.vm.device.VirtualEthernetCard):
                if dev.connectable.connected and portKey == dev.backing.port.portKey:
                    showPortFlow(vm, dev)
                    return True

    print "[Error] NSX_PORT_ID:%s not found." % (NSX_PORT_ID)
    return False
예제 #42
0
def get_ifaces(dom):
    global macs
    global if_cnt
    if_cnt = 0
    macs = ''
    xml = dom.XMLDesc(0)
    #print 'get_ifaces xml =====> ', xml
    doc = None
    try:
        doc = libxml2.parseDoc(xml)
    except:
        return []
    ctx = doc.xpathNewContext()
    ifaces = []
    try:
        ret = ctx.xpathEval("/domain/devices/interface")

        for node in ret:
            devinf = None
            for child in node.children:
                if child.name == "target":
                    devinf = child.prop("dev")
                    #print 'get_ifaces child.name(target) child.prop(dev) ==> ', devinf

                if child.name == "mac":
                    devmac = child.prop("address")
                    #print 'get_ifaces child.name(mac) child.prop(address) ==> ', devmac
                    #print 'MAC ==> ', devmac.upper()
                    if if_cnt == 0:
                        macs = devmac.upper()
                    else:
                        macs += (',' + devmac.upper())
                    if_cnt += 1

            if devinf != None:
                ifaces.append(devinf)
    finally:
        if ctx != None:
            ctx.xpathFreeContext()
        if doc != None:
            doc.freeDoc()
    return ifaces
예제 #43
0
def listPATNATs():
    respData = restclient.get(
        '%s/api/4.0/edges/%s/nat/config' % (NSX_URL, NSX_EDGE_ID),
        'listPATNATs')
    outputStr = (restclient.getDebugInfo() + restclient.prettyPrint(respData))

    respDoc = libxml2.parseDoc(respData)
    xp = respDoc.xpathNewContext()
    rules = xp.xpathEval("//nat/natRules/natRule")

    outputStr += "\n\nPAT NAT rules:\n"
    for rule in rules:
        xp.setContextNode(rule)
        action = xp.xpathEval("action")[0].getContent()
        origPort = xp.xpathEval("originalPort")[0].getContent()
        transPort = xp.xpathEval("translatedPort")[0].getContent()
        if origPort != 'any' or transPort != 'any':
            outputStr += (rule.serialize('UTF-8', 1) + '\n')

    output(outputStr)
예제 #44
0
def load_data():
    path1 = PROJECT + "db/teachingdata.updated.set01-2012-10-10/"
    path2 = PROJECT + "db/teachingdata.set02/"
    videos = []
    users = {}
    reviews = []
    for path in [path1]:
        for fname in os.listdir(path):
            #        print path + fname
            data = codecs.open(path + fname,
                               encoding='utf-8-sig').read().encode("utf-8")
            #        print data.encode("utf-8")
            doc = libxml2.parseDoc(data)
            video, userlist = parse(doc)
            doc.freeDoc()

            videos.append(video)
            users.update(userlist)
            reviews += video['reviews']
    return videos, users, reviews
예제 #45
0
def xml_parse_wrapper(xml, parse_func, *args, **kwargs):
    """
    Parse the passed xml string into an xpath context, which is passed
    to parse_func, along with any extra arguments.
    """
    import libxml2

    doc = None
    ctx = None
    ret = None
    try:
        doc = libxml2.parseDoc(xml)
        ctx = doc.xpathNewContext()
        ret = parse_func(doc, ctx, *args, **kwargs)
    finally:
        if ctx != None:
            ctx.xpathFreeContext()
        if doc != None:
            doc.freeDoc()
    return ret
예제 #46
0
    def extractError(self, ret):
        """extractError(ret)"""
        errorCode = ""
        errorDescription = ""
        try:
            doc = libxml2.parseDoc(ret)
            xp = doc.xpathNewContext()

            node = xp.xpathEval("//error/errorCode")
            if len(node):
                errorCode = node[0].content

            node = xp.xpathEval("//error/details")
            if len(node):
                errorDescription = node[0].content

            doc.freeDoc()
        except:
            return ret
        return errorCode + ' ' + errorDescription
예제 #47
0
def _xml_parse_wrapper(xml, parse_func, *args, **kwargs):
    """
    Parse the passed xml string into an xpath context, which is passed
    to parse_func, along with any extra arguments.
    """
    doc = None
    ctx = None
    ret = None

    try:
        doc = libxml2.parseDoc(xml)
        ctx = doc.xpathNewContext()
        _ovf_register_namespace(ctx)
        ret = parse_func(doc, ctx, *args, **kwargs)
    finally:
        if ctx is not None:
            ctx.xpathFreeContext()
        if doc is not None:
            doc.freeDoc()
    return ret
예제 #48
0
    def get_info(self, vid):
        """ Return top-level info about video file """
        info = {'vid': vid}
        xml = self.get_url(
            'http://www.veoh.com/service/getMediaInfo.xml?%s' %
            urllib.urlencode(self.veoh_client_info),
            '<MediaIdList><MediaId permalinkId="%s"/></MediaIdList>' %
            info['vid'])
        doc = libxml2.parseDoc(xml)
        for i in ('FileHash', 'Size', 'Title', 'Extension', 'Duration',
                  'PieceHashFile', 'UrlRoot'):
            result = doc.xpathEval('/Response/QueueEntry/Video/%s' % i)
            info[i.lower()] = result[0].get_content().strip()

        # save info file to be analyzed
        if self.debug:
            f = open('info.xml', 'wb')
            f.write(xml)
            f.close()
        return info
예제 #49
0
    def __init__(self, spl_set_id, spl_id=None):

        self.spl_set_id = spl_set_id.encode()
        self.dir = os.path.join(
            settings.APP_HOME, "structured_product_label/data/%s" % spl_set_id)

        if (spl_id == None):
            path = "%s/*.xml" % self.dir
            spl_id = os.path.split(glob.glob(path)[0])[1].split(".xml")[0]

        self.xml = os.path.join(self.dir, "%s.xml" % spl_id)

        self.spl_id = spl_id.encode()
        self.node = URIRef(spl['%s/%s.xml' % (self.spl_id, self.spl_id)])
        self.model = bound_graph()
        self.model.add((self.node, rdf["type"], SPL.spl_type))

        f = open(self.xml)
        d = libxml2.parseDoc(f.read())
        f.close()
        c = d.xpathNewContext()
        c.xpathRegisterNs("spl", "urn:hl7-org:v3")
        org_name = c.xpathEval(
            "//spl:representedOrganization//spl:name")[0].content
        org_name = URIRef(org_name)
        self.model.add((self.node, spl["representedOrganization"], org_name))

        ndcs = [
            x.content for x in c.xpathEval(
                "//spl:code[@codeSystem='2.16.840.1.113883.6.69']/@code")
        ]
        for ndc in ndcs:
            if len(ndc.split("-")) != 2:
                continue  # dont' need to distinguish 20- vs. 40-pill bottles...
            ndc = URIRef(ndc)
            self.model.add((self.node, spl["NDC"], ndc))

        for image in glob.glob("%s/*.jpg" % self.dir):
            image = os.path.split(image)[1].encode()
            self.model.add((self.node, spl['image'],
                            host['%s/%s' % (self.spl_set_id, image)]))
예제 #50
0
	def decode(self, data):
		"""
		Verifies a signature.
		"""
		doc = libxml2.parseDoc(data)
		
		certificates = self.getProperty("expected_certificates", [ DEFAULT_SIGNING_CERTIFICATE ])
		certificatesDb = {}
		for c in certificates:
			cert, ski = SoapSecurity.loadCertFromPem(c)
			certificatesDb[ski] = cert
		
		cert = SoapSecurity.verifyMessage(doc, certificatesDb = certificatesDb)
		
		if not cert:
			raise Exception("This message has not been signed by the claimed party.")

		ret = {}
		ret['message'] = data
		ret['signedBy'] = ('certificate', cert.as_pem().strip())			
		return (ret, "XML data verified as signed by '%s'" % cert.get_subject().as_text())
예제 #51
0
    def mergeXMLDocs(self, parent, child, xpath, xpathContext):
        """
        Merge the child XML fragment into the parent XML fragment at the location specified by the xpath
        The following arguments are required
        - parent, the parent XML fragment
        - child, the child XML fragment
        - xpath, the xpath specifying the location of the merge
        - xpathContext, an xpathContext if the parent XML has a namespace
        """
        childutf8 = child.serialize()
        childDoc = libxml2.parseDoc(self.convertText(childutf8))
        childRootNode = childDoc.getRootElement()

        if xpathContext == None:
            location = parent.xpathEval(xpath)[0]
            location.addChild(childRootNode)
            return parent
        else:
            location = xpathContext.xpathEval(xpath)[0]
            location.addChild(childRootNode)
            return parent
예제 #52
0
파일: gen-index.py 프로젝트: shaohef/kimchi
def main():
    if len(sys.argv) < 2:
        sys.exit("Missing input files")

    input_files = sys.argv[1:]

    pages = {}
    for f in sorted(input_files):
        with open(f) as fd:
            xml = fd.read()
            doc = libxml2.parseDoc(xml)
            node = doc.xpathEval('/cshelp/title')[0]
            name = node.children.content
            pages[f.replace('.dita', '.html')] = name
            doc.freeDoc()

    print HTML_HEAD
    for page, name in pages.iteritems():
        html = '  <a href="/help/%s">%s</a><br />\n'
        print html % (page, name)
    print HTML_TAIL
예제 #53
0
파일: xpath.py 프로젝트: kbm1422/husky
    def load(self, filename=None, xmlstring=None):
        if xmlstring:
            if filename:
                raise Exception(
                    "can't specify param 'filename' and 'xmlstring' at the same time"
                )
            else:
                self.__doc = libxml2.parseDoc(xmlstring)
        elif filename:
            logger.info("load xml: %s", filename)
            self.__filename = filename
            self.__doc = libxml2.parseFile(filename)
            # XML_PARSE_NOBLANKS XML_DTD_NO_DTD
            # self._doc = libxml2.readFile(filename, encoding=None, options=libxml2.XML_PARSE_NOBLANKS)
        else:
            raise Exception(
                "'filename' or 'xmlstring' is required as an alternative param"
            )

        self.__xpc = self.__doc.xpathNewContext()
        logger.log(logging.NOTSET, "load xml string: \n%s", self.__doc)
def getSubnet():
    respData = restclient.get(
        "%s/api/4.0/edges/%s/vnics/%s" %
        (NSX_URL, NSX_SUBNET_EDGE_ID, NSX_SUBNET_GET_INDEX), 'getSubnet')

    respDoc = libxml2.parseDoc(respData)
    xp = respDoc.xpathNewContext()

    addressGroups = xp.xpathEval("//vnic/addressGroups/addressGroup")
    for addressGroup in addressGroups:
        xp.setContextNode(addressGroup)
        if xp.xpathEval("primaryAddress")[0].content.find('.') > 0:
            addressGroup.newChild(None, 'ipVersion', '4')
        elif xp.xpathEval("primaryAddress")[0].content.find(':') > 0:
            addressGroup.newChild(None, 'ipVersion', '6')
            addressGroup.newChild(None, 'ipv6AddressMode', 'static')
            addressGroup.newChild(None, 'ipv6RaMode', 'static')

    respData = respDoc.serialize('UTF-8', 1)
    outputstr = restclient.getDebugInfo() + restclient.prettyPrint(respData)
    output(outputstr)
예제 #55
0
    def encode(self, template):
        """
		Signs the message.
		"""
        if not isinstance(template, basestring):
            raise Exception('This codec requires a string')

        pemcert = self.getProperty('signing_certificate')
        if not pemcert:
            pemcert = DEFAULT_SIGNING_CERTIFICATE
        pemkey = self.getProperty('signing_key')
        if not pemkey:
            pemkey = DEFAULT_SIGNING_PRIVATE_KEY
        cert, ski = SoapSecurity.loadCertFromPem(pemcert)
        privkey = SoapSecurity.loadKeyFromPem(pemkey)

        self.log("Signing certificate & private keys loaded")
        doc = libxml2.parseDoc(template)
        # Sign the body only
        xpc = doc.xpathNewContext()
        xpc.xpathRegisterNs("soap", SoapSecurity.NS_SOAP)
        tbs = xpc.xpathEval("/soap:Envelope/soap:Body")
        signedMessage = SoapSecurity.signMessage(doc,
                                                 cert,
                                                 privkey,
                                                 tbsElements=tbs)

        self.log("Message signed")
        root = signedMessage.getRootElement()
        self.log("Serializing signed message")

        encoding = self.getProperty('encoding', 'utf-8')
        ret = ''
        if self.getProperty('write_prolog', True):
            ret = '<?xml version="1.0" encoding="%s"?>\n' % encoding
        ret += root.serialize(
            encoding=encoding,
            format=(self.getProperty('prettyprint', False) and 1 or 0))

        return (ret, "Signed XML data")
예제 #56
0
def sanitize_gtkbuilder(filename):
    """
    GTKBuilder XML made by glade on f16 doesn't work on RHEL6. If on an old
    GTK version, strip out the bits that cause problems
    """
    import gtk
    ver = gtk.gtk_version
    xml = file(filename).read()

    if (ver[0] > 2 or
        (ver[0] == 2 and ver[1] > 18)):
        # Skip altering for gtk > 2.18
        return xml

    import libxml2

    doc = None
    ctx = None
    ret = xml
    try:
        doc = libxml2.parseDoc(xml)
        ctx = doc.xpathNewContext()

        nodes = ctx.xpathEval("//child[@internal-child='selection']")
        if nodes:
            logging.debug("%s: Altering gtkbuilder XML for old gtk compat",
                          os.path.basename(filename))

        for node in nodes:
            node.unlinkNode()
            node.freeNode()

        ret = doc.serialize()
    finally:
        if doc:
            doc.freeDoc()
        if ctx:
            ctx.xpathFreeContext()

    return ret
예제 #57
0
def convert_rpc_xml_to_args(cmd):
    interface = method = args = doc = None
    try:
        doc = libxml2.parseDoc(cmd)
        cmd = doc.getRootElement()

        interface = cmd.prop('interface')
        method    = cmd.prop('method')

        # FIXME: If the interface.method is not known you get back a dummy
        # rpc_def with zero parameters defined, but if the incoming call has
        # parameters we'll try to iterate through them generating an IndexError
        # exception when this code executes: rpc_def.positional_args[arg_position]
        #
        # We either need to detect and report the failed rpc_def lookup earlier
        # and/or we need to not iterate on unknown parameters.
        rpc_def = interface_registry.get_rpc_def(interface, method)

        arg_nodes = xml_get_child_elements_by_name(cmd, 'arg')
        args = preextend_list(len(arg_nodes))
        for arg_node in arg_nodes:
            arg_name      = arg_node.prop('name')
            arg_type      = arg_node.prop('type')
            arg_position  = int(arg_node.prop('position'))
            rpc_arg       = rpc_def.positional_args[arg_position]
            if rpc_arg.obj_type is not None:
                if arg_type == 'xml':
                    arg_value = rpc_arg.obj_type(arg_node, obj_name=arg_name)
                else:
                    arg_value = rpc_arg.obj_type(arg_node.content)
            else:
                arg_value = arg_node.content
            args[arg_position] = arg_value

    finally:
        if doc is not None:
            doc.freeDoc()

    return interface, method, args
예제 #58
0
def blkstats(params):
    """Domain block device statistic"""
    logger = params['logger']
    guestname = params['guestname']

    conn = sharedmod.libvirtobj['conn']

    domobj = conn.lookupByName(guestname)

    # Check domain block status
    if check_guest_status(domobj):
        pass
    else:
        domobj.create()
        time.sleep(90)
    try:
        xml = domobj.XMLDesc(0)
        doc = libxml2.parseDoc(xml)
        cont = doc.xpathNewContext()
        devs = cont.xpathEval("/domain/devices/disk/target/@dev")
        path = devs[0].content
        blkstats = domobj.blockStats(path)
    except libvirtError as e:
        logger.error("API error message: %s, error code is %s"
                     % (e.message, e.get_error_code()))
        return 1

    if blkstats:
        # check_blkstats()
        logger.debug(blkstats)
        logger.info("%s rd_req %s" % (path, blkstats[0]))
        logger.info("%s rd_bytes %s" % (path, blkstats[1]))
        logger.info("%s wr_req %s" % (path, blkstats[2]))
        logger.info("%s wr_bytes %s" % (path, blkstats[3]))
    else:
        logger.error("fail to get domain block statistics\n")
        return 1

    return 0
예제 #59
0
def xmlmatch_from_mem(xmldata,
                      filename,
                      query="/",
                      stylesheet=None,
                      params=None):
    """Laat QUERY los op XMLDATA zoals bij xmlmatch

    FILENAME is de filenaam die we voor de uitvoer gebruiken.

    Beschrijving keyword argumenten:

      QUERY       - een XPath (string-)expressie met de query
      STYLESHEET  - een geparsed stylesheet (libxslt) (of None)
      PARAMS      - een dictionary met parameters voor het stylesheet

    """

    try:
        doc = libxml2.parseDoc(xmldata)
    except Exception, error:
        print >> sys.stderr, "Could not parse %s: %s" % (filename, error)
        return
예제 #60
0
 def demarshall(self, instance, data, **kwargs):
     doc = libxml2.parseDoc(data)
     p = instance.getPrimaryField()
     pname = p and p.getName() or None
     try:
         fields = [
             f for f in instance.Schema().fields() if f.getName() != pname
         ]
         for f in fields:
             items = doc.xpathEval('/*/%s' % f.getName())
             if not len(items):
                 continue
             # Note that we ignore all but the first element if
             # we get more than one
             value = items[0].children
             if not value:
                 continue
             mutator = f.getMutator(instance)
             if mutator is not None:
                 mutator(value.content.strip())
     finally:
         doc.freeDoc()