Ejemplo n.º 1
0
def _transformFilesXslt(transformFile, srcDir, destDir, outFile, 
 fileList):
    """ Run the list of files through an XSLT transform
    """
    
    print "Running XSLT processor using ", transformFile
    if not os.path.exists(destDir):
        os.mkdir(destDir)

    for file in fileList:
        srcFile = srcDir + os.sep + file
        destFile = destDir + os.sep + file
        destFile = os.path.join(os.path.dirname(destFile), outFile)
        try:
            os.makedirs(os.path.dirname(destFile))
        except Exception, e:
            pass
        if os.path.exists(srcFile):
            print ("Transforming " + srcFile)
            styledoc = libxml2.parseFile(transformFile)
            style = libxslt.parseStylesheetDoc(styledoc)
            doc = libxml2.parseFile(srcFile)
            result = style.applyStylesheet(doc, None)
            style.saveResultToFilename(destFile, result, 0)
            style.freeStylesheet()
            doc.freeDoc()
            result.freeDoc()
Ejemplo n.º 2
0
def main():
 
  libxml2.lineNumbersDefault(1)
  libxml2.substituteEntitiesDefault(1)
 
  # URL to fetch the OSM data from
  map_source_data_url="http://xapi.openstreetmap.org/api/0.6/*[power=generator][bbox=-0.51,51.20,0.35,51.80]"
 
  # Filename for OSM map data
  xml_filename = "generators.xml"
 
  # Filename for XSLT to extract POIs
  xsl_filename = "trans_csv_generators.xsl"
 
  # Download the map.osm file from the net, if we don't already have one.
  #if os.path.isfile(xml_filename):
    #print "Not downloading map data.  '%s' already exists."%xml_filename
  #else:
  #print "Downloading OSM data."
  #print "'%s' -> '%s'"%(map_source_data_url,xml_filename)
  #urllib.urlretrieve(map_source_data_url,xml_filename)
 
  # Read the XML into memory.  We will use it many times.
  osmdoc = libxml2.parseFile(xml_filename)
 
  # Read the XSLT
  styledoc = libxml2.parseFile(xsl_filename)
  style = libxslt.parseStylesheetDoc(styledoc)
 
  # Extract POIs to layer text files
  result = style.applyStylesheet(osmdoc, {"key":"power", "value":"generator"})
  style.saveResultToFilename("temp.csv", result, 0)
Ejemplo n.º 3
0
def processRawData(xsl_uri, features, bbox):
  """
    Downloads the data from XAPI and turns it into a Python object.
  """
  # Download data to temporary file
  xapi_uri = "http://open.mapquestapi.com/xapi/api/0.6/*[%s][bbox=%s]" % (features, bbox)
  if ('-v' in sys.argv):
    print " : Downloading %s" % (xapi_uri)
  urllib.urlretrieve(xapi_uri,'temp.xml')
  
  # Translate XML to CSV (easier to then read into py object)
  if ('-v' in sys.argv):
    print " : Processing data..."
  osmdoc = libxml2.parseFile('temp.xml')
  styledoc = libxml2.parseFile(xsl_uri)
  style = libxslt.parseStylesheetDoc(styledoc)
  result = style.applyStylesheet(osmdoc, None)
  style.saveResultToFilename('temp.csv', result, 0)

  # Encode HTML elements
  f = open('temp.csv', 'r')
  safe_content = escape(f.read())
  f = open('temp.csv', 'w')
  f.write(safe_content)
  
  # Read CSV file into dict
  pdata = csv.DictReader(open('temp.csv', 'rb'), delimiter='	')
  return pdata
Ejemplo n.º 4
0
	def applyXSLT(self, fileName):
		sourceXMLFile = fileName
		sourceDoc = libxml2.parseFile(sourceXMLFile)
		styleDoc = libxml2.parseFile("base.xsl")
		style = libxslt.parseStylesheetDoc(styleDoc)
		result = style.applyStylesheet(sourceDoc, None)
		print result
Ejemplo n.º 5
0
 def scan(self):
     """Read in and index old repo data"""
     self.basenodes = {}
     self.filesnodes = {}
     self.othernodes = {}
     self.pkg_ids = {}
     if self.opts.get('verbose'):
         print _("Scanning old repo data")
     for fn in self.files.values():
         if not os.path.exists(fn):
             #cannot scan
             errorprint(_("Warning: Old repodata file missing: %s") % fn)
             return
     root = libxml2.parseFile(self.files['base']).getRootElement()
     self._scanPackageNodes(root, self._handleBase)
     if self.opts.get('verbose'):
         print _("Indexed %i base nodes" % len(self.basenodes))
     root = libxml2.parseFile(self.files['filelist']).getRootElement()
     self._scanPackageNodes(root, self._handleFiles)
     if self.opts.get('verbose'):
         print _("Indexed %i filelist nodes" % len(self.filesnodes))
     root = libxml2.parseFile(self.files['other']).getRootElement()
     self._scanPackageNodes(root, self._handleOther)
     if self.opts.get('verbose'):
         print _("Indexed %i other nodes" % len(self.othernodes))
     #reverse index pkg ids to track references
     self.pkgrefs = {}
     for relpath, pkgid in self.pkg_ids.iteritems():
         self.pkgrefs.setdefault(pkgid,[]).append(relpath)
Ejemplo n.º 6
0
def _translate_file_to(xslFilename, inFile, outFile, params=None):
    
    # parse the stylesheet file
    styledoc = libxml2.parseFile(xslFilename)

    # setup the stylesheet instance
    style = libxslt.parseStylesheetDoc(styledoc)

    try:
        # parse the inline generated XML file
        doc = libxml2.parseFile(inFile)

        # apply the stylesheet instance to the document instance
        result = style.applyStylesheet(doc, params)
    
        style.saveResultToFilename(outFile, result, 0)

        success = 1
    
    
        # free instances
        result.freeDoc()
        style.freeStylesheet()
        doc.freeDoc()

    except:
        print "Can't parse: %s, skipping" % inFile
        success = 0

    return success
def processRawData(xapi_uri, xsl_uri, features):
  """
    Downloads the data from XAPI and turns it into a Python object.
  """
  # Download data to temporary file and read the XML/XSL into memory
  if ('-v' in sys.argv):
    print "Downloading %s" % (xapi_uri)
  urllib.urlretrieve(xapi_uri,'temp.xml')
  osmdoc = libxml2.parseFile('temp.xml')
  styledoc = libxml2.parseFile(xsl_uri)
  style = libxslt.parseStylesheetDoc(styledoc)

  # Translate XML to CSV (easier to then read into py object)
  if ('-v' in sys.argv):
    print "Processing data..."
  for key,value in features.iteritems():
    result = style.applyStylesheet(osmdoc,\
      { "key":"'%s'"%key, "value":"'%s'"%value })
    style.saveResultToFilename('temp.csv', result, 0)

  # Encode HTML elements
  f = open('temp.csv', 'r')
  safe_content = escape(f.read())
  f = open('temp.csv', 'w')
  f.write(safe_content.encode('utf-8'))
  
  # Read CSV file into dict
  csv_file = open('temp.csv', 'rb')
  temp_data = csv_file.read()
  temp_data_unicoded = unicode(temp_data, 'utf-8')
  pdata = csv.DictReader(csv_file, delimiter='	')
  for row in pdata:
    print row
  exit(42)
  return pdata
Ejemplo n.º 8
0
def xslt_convert( xml, xsl ):
	if not _have_xslt:
		Error( _err_msg )

	libxml2.lineNumbersDefault( 1 )
	libxml2.substituteEntitiesDefault( 1 )
	
	try:
		styledoc = libxml2.parseFile( xsl )
	except libxml2.parserError:
		Error("Cannot parse XSL stylesheet: '%s'" % xsl )

	style = libxslt.parseStylesheetDoc( styledoc )

	try:
		doc = libxml2.parseFile( xml )
	except libxml2.parserError:
		Error("Unable to parse XML document: '%s'" % xml )

	result = style.applyStylesheet( doc, None )
	s = style.saveResultToString( result )
	style.freeStylesheet()
	doc.freeDoc()
	result.freeDoc()
	return s
Ejemplo n.º 9
0
def run_xslt(xml_file, xslt_file, out_file=None):
    """
    Transforms the specified XML file using the specified XSLT stylesheet. If
    optional out_file specified, writes output to file; otherwise, returns
    styled metadata as a string.

    Examples:
    >>> run_xslt('/usr/local/apache/htdocs/pacioos/metadata/iso/NS01agg.xml', '/usr/local/apache/htdocs/pacioos/metadata/xslt/iso2fgdc.xsl', '/usr/local/apache/htdocs/pacioos/metadata/fgdc/NS01agg.xml')
    >>> metadata_styled_string = run_xslt('/usr/local/apache/htdocs/pacioos/metadata/iso/NS01agg.xml', '/usr/local/apache/htdocs/pacioos/metadata/xslt/iso2fgdc.xsl')
    """
    import libxml2
    import libxslt

    # Render the XML into the specified style using its XSLT stylesheet:
    # NOTE: The return object from the parser is an XML DOM object data 
    # structure (DOM = Document Object Model):

    metadata_xml_dom       = libxml2.parseFile(xml_file)
    xsl_dom                = libxml2.parseFile(xslt_file)
    stylesheet             = libxslt.parseStylesheetDoc(xsl_dom)
    metadata_styled_dom    = stylesheet.applyStylesheet(metadata_xml_dom, None)

    if out_file:
        stylesheet.saveResultToFilename(out_file, metadata_styled_dom, 0)
    else:
        metadata_styled_string = stylesheet.saveResultToString(metadata_styled_dom)
        return metadata_styled_string
Ejemplo n.º 10
0
    def __transform_xmllint(self, file, xsl_file, output, params = {}):
        import libxml2
        import libxslt

        new_params = {}
        keys = params.keys()
        for key in keys:
            new_params[key] = '"%s"' % params[key]
        params = new_params
            


        try:
            xml_doc = file
            # parse stylesheet
            styledoc = libxml2.parseFile(xsl_file)
            style = libxslt.parseStylesheetDoc(styledoc)
            # parse doc
            doc = libxml2.parseFile(xml_doc)
            result = style.applyStylesheet(doc, params)
            style.saveResultToFilename(output, result, 0)
            style.freeStylesheet()
            doc.freeDoc()
            result.freeDoc()
        except libxml2.parserError:
            return 1, ''
        return 0, ''
Ejemplo n.º 11
0
 def __load_xslt(self, fname):
     if os.path.exists(fname):
         return libxml2.parseFile(fname)
     elif os.path.exists(self.sharedir + '/' + fname):
         return libxml2.parseFile(self.sharedir + '/' + fname)
     else:
         raise RuntimeError, 'Could not locate XSLT template for DMI data (%s)' % fname
Ejemplo n.º 12
0
def get_kbt_items(taxonomyfilename, templatefilename, searchwith=""):
    """
    Get items from taxonomy file using a templatefile. If searchwith is defined,
    return only items that match with it.
    @param taxonomyfilename: full path+name of the RDF file
    @param templatefile: full path+name of the XSLT file
    @param searchwith: a term to search with
    """
    styledoc = libxml2.parseFile(templatefilename)
    style = libxslt.parseStylesheetDoc(styledoc)
    doc = libxml2.parseFile(taxonomyfilename)
    result = style.applyStylesheet(doc, None)
    strres = style.saveResultToString(result)
    style.freeStylesheet()
    doc.freeDoc()
    result.freeDoc()
    ritems = []
    if len(strres) == 0:
        return []
    else:
        lines = strres.split("\n")
        for line in lines:
            if searchwith:
                if line.count(searchwith) > 0:
                    ritems.append(line)
            else:
                if len(line) > 0:
                    ritems.append(line)
    return ritems
Ejemplo n.º 13
0
 def parseConf(self):
     libxml2.initParser()
     doc = libxml2.parseFile(self.conf["main"]["dataPath"] + "/PostGIS/" + self.dbfile + ".xml")
     styledoc = libxml2.parseFile(self.conf["main"]["dataPath"] + "/PostGIS/conn.xsl")
     style = libxslt.parseStylesheetDoc(styledoc)
     res = style.applyStylesheet(doc, None)
     self.db_string = res.content.replace("PG: ", "")
Ejemplo n.º 14
0
def fixup():      
    for mainXSD in dictXSD:
        debugMsg("fixing '%s'..." % mainXSD)	
	schemaDoc = None
	xpmainCtx = None	
        # Load the schema document.
        schemaFile = os.path.join(baseDir, mainXSD)
        schemaDoc = libxml2.parseFile(schemaFile)
	if (schemaDoc is None):
	    print "ERROR: doc '%s' not found" % mainXSD
            sys.exit(1)
	try:    
	    xpmainCtx = schemaDoc.xpathNewContext()
            xpmainCtx.xpathRegisterNs("xs", "http://www.w3.org/2001/XMLSchema");		
            xpres = xpmainCtx.xpathEval("/xs:schema")
            if len(xpres) == 0:
                print "ERROR: doc '%s' has no <schema> element" % mainXSD
                sys.exit(1)
	    schemaElem = xpres[0]  
	    schemaNs = schemaElem.ns()
	    # Select all <import>s.
	    xpres = xpmainCtx.xpathEval("/xs:schema/xs:import")	
	    if len(xpres) != 0:
	        for elem in xpres:
	            loc = elem.noNsProp("schemaLocation")
	            if (loc is not None):
	                debugMsg("  imports '%s'" % loc)
	                if loc in dictXSD[mainXSD]:
	                    dictXSD[mainXSD].remove(loc)			
	    for loc in dictXSD[mainXSD]:	    
	        # Read out the targetNamespace.
	        impTargetNs = None
	        impFile = os.path.join(baseDir, loc)
	        impDoc = libxml2.parseFile(impFile)
		try:
                    xpimpCtx = impDoc.xpathNewContext()
		    try:
                        xpimpCtx.setContextDoc(impDoc)
	                xpimpCtx.xpathRegisterNs("xs", "http://www.w3.org/2001/XMLSchema");  
	                xpres = xpimpCtx.xpathEval("/xs:schema")
	                impTargetNs = xpres[0].noNsProp("targetNamespace")
	            finally:
                        xpimpCtx.xpathFreeContext()
	        finally:
                    impDoc.freeDoc()
	                
	        # Add the <import>.
	        debugMsg("  adding <import namespace='%s' schemaLocation='%s'/>" % (impTargetNs, loc))
	        newElem = schemaDoc.newDocNode(schemaNs, "import", None)
	        if (impTargetNs is not None):
                    newElem.newProp("namespace", impTargetNs)
	        newElem.newProp("schemaLocation", loc)
	        if schemaElem.children is not None:
                    schemaElem.children.addPrevSibling(newElem)
                schemaDoc.saveFile(schemaFile)
	finally:
            xpmainCtx.xpathFreeContext()
            schemaDoc.freeDoc()
Ejemplo n.º 15
0
def parse(filename, filename_settings):
    """Returns a list of parsed packets
    """
    document = request.Document()
    xml_doc = libxml2.parseFile(filename)

    settings_doc = libxml2.parseFile(filename_settings)

    # Parse allowed packets
    for packet_node in settings_doc.xpathEval("/allowedPackets/packet"):
        allowed_packet = request.AllowedPacket(Node(packet_node))
        document.allowed_packets[allowed_packet.name] = allowed_packet.version

    # parse global params
    for param_node in xml_doc.xpathEval("/serverFormat/globalParams/param"):
        param = request.Param(Node(param_node))
        document.params[ param.id ] = param

    # parse global server version
    for ver_node in xml_doc.xpathEval("/serverFormat/version"):
        ver = request.ServerVersion(Node(ver_node))
        document.server_version = ver

    # Global param structs, like version etc
    for struct_node in xml_doc.xpathEval("/serverFormat/globalParams/struct"):
        struct = request.Struct(Node(struct_node))
        document.structs.append( struct )

    # Common enums
    for enum_node in xml_doc.xpathEval("/serverFormat/globalEnums/enum"):
        enum = request.Enum(Node(enum_node))
        document.common_enums.append( enum )

    # Common structs
    for struct_node in xml_doc.xpathEval("/serverFormat/globalStructs/struct"):
        struct = request.Struct(Node(struct_node))
        document.common_structs.append( struct )
#        document.common_structs = {}

    # parse requests
    for request_node in xml_doc.xpathEval("/serverFormat/reqPacket"):
        req = request.Packet(Node(request_node))
        document.requests[ ( req.name, req.version ) ] = req

    # parse replies
    for reply_node in xml_doc.xpathEval("/serverFormat/replyPacket"):
        # Yes, we use request class as reply
        reply = request.Packet(Node(reply_node))
        document.replies[ ( reply.name, reply.version ) ] = reply

    # parser enums (in serverformat, skipping the other enums for now)
    for enum_node in xml_doc.xpathEval("/serverFormat/enum"):
        enum = request.Enum(Node(enum_node))
        document.enums[ enum.name ] = enum
     
    # XXX: Add also for replyPacketBase and requestPacketBase

    return document;
Ejemplo n.º 16
0
def transform(xml_file, xsl_file):
  xml_doc = libxml2.parseFile(xml_file)
  xsl_doc = libxml2.parseFile(xsl_file)
  xsl = libxslt.parseStylesheetDoc(xsl_doc)
  out_doc = xsl.applyStylesheet(xml_doc ,None)
  print out_doc
  xsl.freeStylesheet()
  out_doc.freeDoc()
  xml_doc.freeDoc()
Ejemplo n.º 17
0
def begin_conv():
	styledoc = libxml2.parseFile(xslttransform)
	style = libxslt.parseStylesheetDoc(styledoc)
	doc = libxml2.parseFile(inputf)
	result = style.applyStylesheet(doc, None)
	style.saveResultToFilename(output, result, 0)
	style.freeStylesheet()
	doc.freeDoc()
	result.freeDoc()
Ejemplo n.º 18
0
def performXSLTransform():
    styledoc = libxml2.parseFile(xsl_file)
    style = libxslt.parseStylesheetDoc(styledoc)
    doc = libxml2.parseFile(xml_file)
    result = style.applyStylesheet(doc, None)
    style.saveResultToFilename(out_file, result, 0)
    style.freeStylesheet()
    doc.freeDoc()
    result.freeDoc()
Ejemplo n.º 19
0
def xsltProcess(styleFile, inputFile, outputFile):
    """Transform an xml inputFile to an outputFile using the given styleFile"""
    styledoc = libxml2.parseFile(styleFile)
    style = libxslt.parseStylesheetDoc(styledoc)
    doc = libxml2.parseFile(inputFile)
    result = style.applyStylesheet(doc, None)
    style.saveResultToFilename(outputFile, result, 0)
    style.freeStylesheet()
    doc.freeDoc()
    result.freeDoc()
Ejemplo n.º 20
0
def transform(xslt, source, target):
    xslt = 'uml2dj/%s' % xslt
    styledoc = libxml2.parseFile(xslt)
    style = libxslt.parseStylesheetDoc(styledoc)
    doc = libxml2.parseFile(source)
    result = style.applyStylesheet(doc, None)
    style.saveResultToFilename(target, result, 0)
    style.freeStylesheet()
    doc.freeDoc()
    result.freeDoc()
Ejemplo n.º 21
0
def xmlToRoadmap(filePath):
    libxml2.registerErrorHandler(lambda ctx,str: None, None)
    libxslt.registerErrorHandler(lambda ctx,str: None, None)
    pmlStyleDoc=libxml2.parseFile(settings.PML_PATH + "/xpml/pmldoc.xsl")
    style = libxslt.parseStylesheetDoc(pmlStyleDoc)
    try:
        doc = libxml2.parseFile(filePath)
        result = style.applyStylesheet(doc, None)
        return style.saveResultToString(result)
    except (libxml2.parserError, TypeError):
        return None
Ejemplo n.º 22
0
 def defineGDMLVariables(self):
     """
     setup gdml file, parse and verify it
     """
     # The survey information is in the CAD geometry
     # file which should have one of these names
     fnames = ['fastradModel.gdml', 'FastradModel.gdml', 'Step_IV.gdml']
     for name in fnames:
         tempfile = os.path.join(self.dl_dir, name)
         if os.path.exists(tempfile):
             self.GDMLFile = tempfile
             # break on the first qualifing instance.
             break
     # An override method --- if necessary
     if self.DataFile != "":
         self.GDMLFile = self.DataFile
     # The default source of reference information for the fit.
     self.XMLFile  = os.path.join(self.dl_dir,'Maus_Information.gdml')
     if not os.path.exists(self.XMLFile):
         self.XMLFile = ''
     # An override method --- if necessary
     if self.RefFile != "":
         self.XMLFile  = self.RefFile
     # print 
     print self.GDMLFile, self.XMLFile
     self.datafile = libxml2.parseFile(self.XMLFile)
     self.gdmlfile = libxml2.parseFile(self.GDMLFile)
     # get the survey target detectors
     # The list of active detectors should appear in
     # the Maus_Information file under the following path
     basepath = 'MICE_Information/Detector_Information/'
     # This is a list of all possible detectors (which
     # have GDML geometries).
     detectors = ['TOF0', 'TOF1', 'TOF2', \
                  'KL', 'Ckov1', 'Ckov2', 'EMR', \
                  'Tracker0', 'Tracker1', 'LH2', \
                  'Disk_LiH', 'Disk_PE', 'Wedge_LiH_45', \
                  'Wedge_LiH_90']
     # Evaluate the set of entries consistant with the path
     detector_search = self.datafile.xpathEval(basepath+'/*')
     # initialize the container for the detector target
     self.sfTarget = []
     # loop over the path elements
     for elem in detector_search:
         # loop over the potential detector names
         for det in detectors:
             # check for matches between the list
             if det == elem.name:
                 # add the detector name to the list
                 self.sfTarget.append(det)
     if not len(self.sfTarget):
         # check for an alternate list if no detectors match. 
         self.sfTarget = self.tempList
Ejemplo n.º 23
0
def transform(xml_file, xsl_file, out_file):
  """ xslt трансформация xml-документа"""
  print "Открываем документ " + xml_file
  xml_doc = libxml2.parseFile(xml_file)
  xsl_doc = libxml2.parseFile(xsl_file)
  xsl = libxslt.parseStylesheetDoc(xsl_doc)
  out_doc = xsl.applyStylesheet(xml_doc, None)
  print out_doc
  xsl.saveResultToFilename(out_file, out_doc, 0)
  xsl.freeStylesheet()
  out_doc.freeDoc()
  xml_doc.freeDoc()
Ejemplo n.º 24
0
def __xml_scan(node, env, path, arg):
    """ Simple XML file scanner, detecting local images and XIncludes as implicit dependencies. """
    # Does the node exist yet?
    if not os.path.isfile(str(node)):
        return []
    
    if env.get('DOCBOOK_SCANENT',''):
        # Use simple pattern matching for system entities..., no support 
        # for recursion yet.
        contents = node.get_text_contents()
        return sentity_re.findall(contents)

    xsl_file = os.path.join(scriptpath,'utils','xmldepend.xsl')
    if not has_libxml2 or prefer_xsltproc:
        if has_lxml and not prefer_xsltproc:
            
            from lxml import etree
            
            xsl_tree = etree.parse(xsl_file)
            doc = etree.parse(str(node))
            result = doc.xslt(xsl_tree)

            depfiles = [x.strip() for x in str(result).splitlines() if x.strip() != "" and not x.startswith("<?xml ")]
            return depfiles
        else:
            # Try to call xsltproc
            xsltproc = env.subst("$DOCBOOK_XSLTPROC")
            if xsltproc and xsltproc.endswith('xsltproc'):
                result = env.backtick(' '.join([xsltproc, xsl_file, str(node)]))
                depfiles = [x.strip() for x in str(result).splitlines() if x.strip() != "" and not x.startswith("<?xml ")]
                return depfiles
            else:
                # Use simple pattern matching, there is currently no support
                # for xi:includes...
                contents = node.get_text_contents()
                return include_re.findall(contents)

    styledoc = libxml2.parseFile(xsl_file)
    style = libxslt.parseStylesheetDoc(styledoc)
    doc = libxml2.parseFile(str(node))
    result = style.applyStylesheet(doc, None)

    depfiles = []
    for x in str(result).splitlines():
        if x.strip() != "" and not x.startswith("<?xml "):
            depfiles.extend(x.strip().split())
    
    style.freeStylesheet()
    doc.freeDoc()
    result.freeDoc()

    return depfiles
Ejemplo n.º 25
0
def xmlToPml(filePath):
    #Redirect XML errors away from stdout so that it doesn't cause a
    #mod_wsgi exception.
    libxml2.registerErrorHandler(lambda ctx,str: None, None)
    libxslt.registerErrorHandler(lambda ctx,str: None, None)
    pmlStyleDoc=libxml2.parseFile(settings.PML_PATH + "/xpml/xpml.xsl")
    style = libxslt.parseStylesheetDoc(pmlStyleDoc)
    try:
        doc = libxml2.parseFile(filePath)
        result = style.applyStylesheet(doc, None)
        return style.saveResultToString(result)
    except (libxml2.parserError, TypeError):
        return None
Ejemplo n.º 26
0
def get_kbt_items(taxonomyfilename, templatefilename, searchwith=""):
    """
    Get items from taxonomy file using a templatefile.

    If searchwith is defined, return only items that match with it.
    :param taxonomyfilename: full path+name of the RDF file
    :param templatefile: full path+name of the XSLT file
    :param searchwith: a term to search with
    """
    if processor_type == 1:
        # lxml
        doc = etree.XML(taxonomyfilename)
        styledoc = etree.XML(templatefilename)
        style = etree.XSLT(styledoc)
        result = style(doc)
        strres = str(result)
        del result
        del style
        del styledoc
        del doc
    elif processor_type == 2:
        # libxml2 & libxslt
        styledoc = libxml2.parseFile(templatefilename)
        style = libxslt.parseStylesheetDoc(styledoc)
        doc = libxml2.parseFile(taxonomyfilename)
        result = style.applyStylesheet(doc, None)
        strres = style.saveResultToString(result)
        style.freeStylesheet()
        doc.freeDoc()
        result.freeDoc()
    else:
        # no xml parser found
        strres = ""

    ritems = []
    if len(strres) == 0:
        return []
    else:
        lines = strres.split("\n")
        for line in lines:
            if searchwith:
                if line.count(searchwith) > 0:
                    ritems.append(line)
            else:
                if len(line) > 0:
                    ritems.append(line)

    return ritems
Ejemplo n.º 27
0
    def create(self):

        iso = None
        if self.name == 'F16':
            iso = '/var/lib/libvirt/images/Fedora-16-x86_64-DVD.iso'
        if self.name == 'F15':
            iso = '/var/lib/libvirt/images/Fedora-15-x86_64-DVD.iso'
        if self.name == 'F14':
            iso = '/var/lib/libvirt/images/Fedora-14-x86_64-DVD.iso'
        if iso:
            if not os.access(iso, os.R_OK):
                print '*** %s does not exist.' % (iso)
                return False

        res = os.system("oz-install -t 50000 -u -d3 -x %s %s" % (self.xml_path, self.tdl_path))
        if res == 256:
            return False

        os.system("qemu-img convert -O qcow2 %s %s" % (self.dsk_filename, self.qcow2_filename))

        libvirt_xml = libxml2.parseFile(self.xml_path)
        source_xml = libvirt_xml.xpathEval('/domain/devices/disk')
        driver = source_xml[0].newChild (None, "driver", None)
        driver.newProp ("type", "qcow2")
        source_xml = libvirt_xml.xpathEval('/domain/devices/disk/source')
        source_xml[0].setProp('file', self.qcow2_filename)
        source_xml = libvirt_xml.xpathEval('/domain/devices/serial')
        root_node = source_xml[0]
        root_node.unlinkNode()
        libvirt_xml.saveFormatFile(self.xml_path, format=1)
        return True
Ejemplo n.º 28
0
  def __init__(self, sitePath):        
      self.sitePath = sitePath
      print "Creating site at path "  + sitePath
      # TODO: make sure the config file actually exists
      self.config = XMLFragment(libxml2.parseFile(sitePath + "/config/config.xml"))
 
      self.db = Database(self.config)
Ejemplo n.º 29
0
 def loadById(self, id):
     try:
         self.doc = libxml2.parseFile('http://api.erpk.org/citizen/profile/' + str(id) + '.xml?key=nIKh0F7U')
     except:
         return None
     self.ctxt = self.doc.xpathNewContext()
     return self
Ejemplo n.º 30
0
 def loadByName(self, name):
     try:
         self.doc2 = libxml2.parseFile('http://api.erepublik.com/v2/feeds/exchange/' + name + '/gold')
     except:
         return None
     self.ctxt2 = self.doc2.xpathNewContext()
     return self
Ejemplo n.º 31
0
    def applyStyleSheet(self, styleSheetName, xmlToProcess):

        try:
            styledoc = libxml2.parseFile(styleSheetName)
        except libxml2.parserError, err:
            self.logger.error("Unable to parse XSLT: " + styleSheetName + " " +
                              str(err))
            sys.exit(RET_CRITICAL)
Ejemplo n.º 32
0
 def parser(self, filename):
     try:
         self.document = libxml2.parseFile(filename)
         self.context = self.document.xpathNewContext()
     except (libxml2.parserError, TypeError):
         raise Exception('Arquivo XML invalido ou inexistente')
     finally:
         return self
Ejemplo n.º 33
0
def query(xml_file, xpath_query):
    doc = libxml2.parseFile(xml_file)
    ctxt = doc.xpathNewContext()
    results = ctxt.xpathEval(xpath_query)
    for res in results:
        print res.content
    ctxt.xpathFreeContext()
    doc.freeDoc()
Ejemplo n.º 34
0
    def add_target_content(self):
        """Merge in target specific package and repo content.
        TDL object must already exist as self.tdlobj"""
        doc = None
        if isfile("/etc/imagefactory/target_content.xml"):
            doc = libxml2.parseFile("/etc/imagefactory/target_content.xml")
        else:
            self.log.debug(
                "Found neither a call-time config nor a config file - doing nothing"
            )
            return

        # Purely to make the xpath statements below a tiny bit shorter
        target = self.target
        os = self.tdlobj.distro
        version = self.tdlobj.update
        arch = self.tdlobj.arch

        # We go from most to least specific in this order:
        #   arch -> version -> os-> target
        # Note that at the moment we even allow an include statment that covers absolutely everything.
        # That is, one that doesn't even specify a target - this is to support a very simple call-time syntax
        include = doc.xpathEval(
            "/template_includes/include[@target='%s' and @os='%s' and @version='%s' and @arch='%s']"
            % (target, os, version, arch))
        if len(include) == 0:
            include = doc.xpathEval(
                "/template_includes/include[@target='%s' and @os='%s' and @version='%s' and not(@arch)]"
                % (target, os, version))
        if len(include) == 0:
            include = doc.xpathEval(
                "/template_includes/include[@target='%s' and @os='%s' and not(@version) and not(@arch)]"
                % (target, os))
        if len(include) == 0:
            include = doc.xpathEval(
                "/template_includes/include[@target='%s' and not(@os) and not(@version) and not(@arch)]"
                % (target))
        if len(include) == 0:
            include = doc.xpathEval(
                "/template_includes/include[not(@target) and not(@os) and not(@version) and not(@arch)]"
            )
        if len(include) == 0:
            self.log.debug(
                "cannot find a config section that matches our build details - doing nothing"
            )
            return

        # OK - We have at least one config block that matches our build - take the first one, merge it and be done
        # TODO: Merge all of them?  Err out if there is more than one?  Warn?
        include = include[0]

        packages = include.xpathEval("packages")
        if len(packages) > 0:
            self.tdlobj.merge_packages(str(packages[0]))

        repositories = include.xpathEval("repositories")
        if len(repositories) > 0:
            self.tdlobj.merge_repositories(str(repositories[0]))
Ejemplo n.º 35
0
def transform_doc(doc, stylesheet, params={}):
    if stylesheet not in xsl_cache:
        xsl_doc = libxml2.parseFile(stylesheet)
        xsl = libxslt.parseStylesheetDoc(xsl_doc)
        if xsl is None:
            raise Exception("Failed to parse XML file into a stylesheet: %s" % stylesheet)
        xsl_cache[stylesheet] = xsl
    result = xsl_cache[stylesheet].applyStylesheet(doc, params)
    return result
Ejemplo n.º 36
0
def task1(xml_file):
    doc = libxml2.parseFile(xml_file)
    ctxt = doc.xpathNewContext()
    department = ctxt.xpathEval(
        " faculty[@name='Кибернетика и Безопасность']/sector/department ")
    print department
    print "\n"
    ctxt.xpathFreeContext()
    doc.freeDoc()
Ejemplo n.º 37
0
def delete_attr(xml_file, tag, prop, data):
    doc = libxml2.parseFile(xml_file)
    root = doc.children
    for children in root.children:
        if children.name == tag and children.prop(prop) == data:
            children.unsetProp(prop)
            print 'Success delete'
    doc.saveFile('save.xml')
    doc.freeDoc()
Ejemplo n.º 38
0
def parse_path(path, dict_constructor=OrderedDict, omit_namespaces=False):
    if not os.path.exists(path):
        return False

    if ET:
        return _parse_lxml_fork(path, dict_constructor, omit_namespaces)
    elif libxml2:
        data = libxml2.parseFile(path)
        return _parse_libxml2(data, dict_constructor, omit_namespaces)
Ejemplo n.º 39
0
def load_quiz_log(log_file):
    bob = libxml2.parseFile(log_file)
    context = bob.xpathNewContext()
    root = bob.getRootElement()
    item = root.children
    filed = []

    while item is not None:
        if item.name == "answer":
            types = item.children
            while types is not None:
                if types.name == "index":
                    index1 = types.content
                    if types.content == "":
                        index1 = None
                elif types.name == "path":
                    path1 = types.content
                    if types.content == "":
                        path1 = None
                elif types.name == "result":
                    name1 = types.content
                    if types.content == "":
                        name1 = None
                elif types.name == "answer":
                    answer1 = types.content
                    if types.content == "":
                        answer1 = None
                elif types.name == "time":
                    time1 = types.content
                    if types.content == "":
                        time1 = None
                #print 'bro'
                types = types.next
            file1 = Answer(index1, path1, name1, answer1, time1)
            filed.append(file1)

        elif item.name == "display":
            flyes = item.children
            while flyes is not None:
                if flyes.name == "index":
                    index2 = flyes.content
                    if flyes.content == "":
                        index1 = None
                elif flyes.name == "path":
                    path2 = flyes.content
                    if flyes.content == "":
                        path1 = None
                elif flyes.name == "time":
                    time2 = flyes.content
                    if flyes.content == "":
                        time1 = None
                #print 'sup'
                flyes = flyes.next
            file2 = Display(index2, path2, time2)
            filed.append(file2)
        item = item.next
    return filed
Ejemplo n.º 40
0
def print_xml(xml_file):
  """Открытие xml файла"""
  print "Открываем документ " + xml_file
  doc = libxml2.parseFile(xml_file)
  print root.name
  root = doc.chidlren
  from chaildrt in root.chidlren:
    if chailren.type == "element":
      print chidlren.name + ": "
Ejemplo n.º 41
0
    def load(self, filename=None):
        if not self._filename and not filename:
            raise errors.RadioError("Need a location to load from")

        if filename:
            self._filename = filename

        self.doc = libxml2.parseFile(self._filename)
        validate_doc(self.doc)
Ejemplo n.º 42
0
def family(xml_file, family):
    print "Открываем документ " + xml_file
    doc = libxml2.parseFile(xml_file)
    ctxt = doc.xpathNewContext()
    students = ctxt.xpathEval("//group[/student[@name==" + family + "]]")
    for student in students:
        print student.prop('name')
    doc.freeDoc()
    ctxt.xpathFreeContext()
Ejemplo n.º 43
0
def updatesysval(infile, outfile, name, value):
    doc = libxml2.parseFile(infile)
    for node in doc.xpathEval("spec/sys"):
        if node.prop("name").find(name) >= 0:
            node.setProp("value", str(value))
    f = open(outfile,"w")
    doc.saveTo(f)
    f.close()
    doc.freeDoc()
Ejemplo n.º 44
0
def updatefilename(infile, outfile, newname):
    doc = libxml2.parseFile(infile)
    for node in doc.xpathEval("spec/file"):
        if node.prop("id").find("outfile") >= 0:
            node.setProp("name", newname)
    f = open(outfile,"w")
    doc.saveTo(f)
    f.close()
    doc.freeDoc()
Ejemplo n.º 45
0
def xsltProcess(doc, cur, filename):
    global timing
    global xinclude
    global params
    global html

    if xinclude:
        if timing:
            startTimer()
        doc.XIncludeProcess()
        if timing:
            endTimer("XInclude processing %s" % (filename))

    if timing:
        startTimer()
    if output == None:
        if repeat != 0:
            for j in range(1, repeat):
                res = cur.applyStylesheet(doc, params)
                res.freeDoc()
                doc.freeDoc()
                if html == 1:
                    doc = libxml2.htmlParseFile(filename, None)
                else:
                    doc = libxml2.parseFile(filename, None)


#        ctxt = libxslt.newTransformContext(doc)
#        if ctxt == None:
#            return
        if profile:
            print "TODO: Profiling not yet supported"
        else:
            res = cur.applyStylesheet(doc, params)
        if timing:
            if repeat != 0:
                endTimer("Applying stylesheet %d times" % (repeat))
            else:
                endTimer("Applying stylesheet")
        doc.freeDoc()
        if res == None:
            print "no result for %s" % (filename)
            return
        if noout != 0:
            res.freeDoc()
            return
        if debug == 1:
            res.debugDumpDocument(None)
        else:
            if timing:
                startTimer()
            cur.saveResultToFilename("-", res, 0)
            if timing:
                endTimer("Saving result")
        res.freeDoc()
    else:
        print "TODO: xsltRunStylesheet not yet mapped"
Ejemplo n.º 46
0
def process_merge(filename1, filename2):
    tempfile = tempname('.xml')
    othertempfile = tempname('.xml')
    
    doc = libxml2.parseFile(filename2)
    f = file(othertempfile, 'wt')
    f.write(str(doc))
    f.close()
    
    doc = libxml2.parseFile(filename1)
    stylesheet = get_stylesheet_path(MERGE_XSL)
    doc2 = transform_doc(doc, stylesheet, {'other': "'%s'" % othertempfile.replace('\\', '/')})
        
    f = file(tempfile, 'wt')
    f.write(str(doc2))
    f.close()
    
    return tempfile
Ejemplo n.º 47
0
def generate_protocol(xmlInput, output, lang):
    print "[i] generating new protocol"
    print "[i] protocol xml file is " + xmlInput
    print "[i] selected language is " + lang
    print "[i] result will be writed to " + output
    print "[i] parsing files..."
    generator = "./generators/" + lang + ".xslt"
    xmldoc = libxml2.parseFile(xmlInput)
    styledoc = libxml2.parseFile(generator)
    style = libxslt.parseStylesheetDoc(styledoc)
    print "[i] applying stylesheet..."
    result = style.applyStylesheet(xmldoc, None)
    print "[i] writing the result..."
    style.saveResultToFilename(output, result, 0)
    print "[i] cleanup..."
    xmldoc.freeDoc()
    style.freeStylesheet()
    result.freeDoc()
Ejemplo n.º 48
0
def year(xml_file, year):
    print "Открываем документ " + xml_file
    doc = libxml2.parseFile(xml_file)
    ctxt = doc.xpathNewContext()
    students = ctxt.xpathEval("//student[../@entry_year>" + year + "]")
    for student in students:
        print student.prop('name')
    doc.freeDoc()
    ctxt.xpathFreeContext()
Ejemplo n.º 49
0
def xpath(document, search):

    doc = libxml2.parseFile(document)
    for url, name in zip(doc.xpathEval('//category//categoryPageURL//text()'),
                         doc.xpathEval('//category//name//text()')):

        if url.content == search:
            name_url = "filename::" + document + " Category Name::" + name.content + "       Category url::" + url.content
            print name_url
Ejemplo n.º 50
0
 def getUsd(self):
     list = ''
     idx = 0
 
     
     self.doc = libxml2.parseFile('http://api.erepublik.com/v2/feeds/exchange/usd/gold')
     self.ctxt = self.doc.xpathNewContext()
     points = float(self.ctxt.xpathEval('//offers/offer/exchange-rate/text()')[1].getContent())
     return points
Ejemplo n.º 51
0
 def __init__(self):
     self.xml_file = '/var/lib/pacemaker-cloud/db_jeos.xml'
     try:
         self.doc = libxml2.parseFile(self.xml_file)
         self.doc_images = self.doc.getRootElement()
     except:
         self.doc = libxml2.newDoc("1.0")
         self.doc.newChild(None, "images", None)
         self.doc_images = self.doc.getRootElement()
Ejemplo n.º 52
0
def open(xml_file):
    print "Открываем документ " + xml_file
    doc = libxml2.parseFile(xml_file)
    ctxt = doc.xpathNewContext()
    students = ctxt.xpathEval("//students")
    for student in students:
        print student.prop('name')
    doc.freeDoc()
    ctxt.xpathFreeContext()
Ejemplo n.º 53
0
    def xslt(self, xslfile, xmldoc):
        styledoc = libxml2.parseFile(xslfile)
        style = libxslt.parseStylesheetDoc(styledoc)

        result = style.applyStylesheet(xmldoc, None)
        out = result.serialize()
        style.freeStylesheet()
        result.freeDoc()
        return out
Ejemplo n.º 54
0
def departments(xml_file):
    doc = libxml2.parseFile(xml_file)
    ctxt = doc.xpathNewContext()
    departments = ctxt.xpathEval(
        "//faculty[@name='Кибернетика и Безопасность']/*/department ")
    for el in departments:
        print el.prop('name')
    ctxt.xpathFreeContext()
    doc.freeDoc()
Ejemplo n.º 55
0
def faculty_name(xml_file, number):
    doc = libxml2.parseFile(xml_file)
    ctxt = doc.xpathNewContext()
    departments = ctxt.xpathEval("//faculty[sector[department[@number=" +
                                 number + "]]]")
    for el in departments:
        print el.prop('name')
    ctxt.xpathFreeContext()
    doc.freeDoc()
Ejemplo n.º 56
0
def student_count_in_department(xml_file, number):
    doc = libxml2.parseFile(xml_file)
    ctxt = doc.xpathNewContext()
    departments = ctxt.xpathEval("//department[@number=" + number +
                                 "]/*/student")
    count = len(departments)
    print count
    ctxt.xpathFreeContext()
    doc.freeDoc()
Ejemplo n.º 57
0
    def _target_content(self, tdl, target):
        target_xml = '/etc/imagefactory/target_content.xml'
        if os.path.isfile(target_xml):
            doc = libxml2.parseFile(target_xml)
        else:
            self.log.debug(
                "Found neither a call-time config nor a config file - doing nothing"
            )
            return None, None

        # We go from most to least specific in this order:
        #   arch -> version -> os-> target
        # Note that at the moment we even allow an include statment that covers absolutely everything.
        # That is, one that doesn't even specify a target - this is to support a very simple call-time syntax
        include = doc.xpathEval(
            "/template_includes/include[@target='%s' and @os='%s' and @version='%s' and @arch='%s']"
            % (target, tdl.distro, tdl.update, tdl.arch))
        if len(include) == 0:
            include = doc.xpathEval(
                "/template_includes/include[@target='%s' and @os='%s' and @version='%s' and \
            not(@arch)]" % (target, tdl.distro, tdl.update))
        if len(include) == 0:
            include = doc.xpathEval(
                "/template_includes/include[@target='%s' and @os='%s' and not(@version) and \
            not(@arch)]" % (target, tdl.distro))
        if len(include) == 0:
            include = doc.xpathEval(
                "/template_includes/include[@target='%s' and not(@os) and not(@version) and \
            not(@arch)]" % target)
        if len(include) == 0:
            include = doc.xpathEval(
                "/template_includes/include[not(@target) and not(@os) and not(@version) and \
            not(@arch)]")
        if len(include) == 0:
            self.log.debug(
                "cannot find a config section that matches our build details - doing nothing"
            )
            return None, None

        # OK - We have at least one config block that matches our build - take the first one, merge it and be done
        # TODO: Merge all of them?  Err out if there is more than one?  Warn?
        include = include[0]

        packages = include.xpathEval("packages")
        if len(packages) > 0:
            ret_pkgs = packages[0]
        else:
            ret_pkgs = None

        repositories = include.xpathEval("repositories")
        if len(repositories) > 0:
            ret_repos = repositories[0]
        else:
            ret_repos = None

        return ret_repos, ret_pkgs
Ejemplo n.º 58
0
def getPackageList(libraryName=None):
    """
    Get a list of associated packages associated with the library 
    defined in the modules configuration file.
    """
    if libraryName == None or type(libraryName) != type(''):
        return []

    configFile = sbProps.SB_CONFIG_FILE
    if not os.path.isfile(configFile):
        return []

    try:
        logger = TCSLogger.TCSLogger.getInstance(6)
    except TCSLogger.SingletonException:
        logger = TCSLogger.TCSLogger.getInstance()

    sysCpeName = sb_utils.os.info.getCpeName()

    package_list = []
    working_dict = {}
    xmldoc = libxml2.parseFile(configFile)
    for module_node in xmldoc.xpathEval("//security_module"):
        cpe_nodes = None
        try:
            libname = module_node.xpathEval("library")[0].getContent().strip(
                "'")
            if libname == libraryName:
                cpe_nodes = module_node.xpathEval("platforms/cpe-item")
        except:
            continue

        if not cpe_nodes:
            continue

        exact_match = False
        for test_cpe in cpe_nodes:
            if sysCpeName == test_cpe.prop("name"):
                exact_match = True
                for package_node in test_cpe.xpathEval("packages/package"):
                    package_list.append(package_node.prop("name"))
                break

        if exact_match == False:
            for test_cpe in cpe_nodes:
                if sysCpeName.startswith(test_cpe.prop("name")):
                    for package_node in test_cpe.xpathEval("packages/package"):
                        package_list.append(package_node.prop("name"))

        xmldoc.freeDoc()

        msg = "Identified %s packages for module library '%s'" % (
            str(package_list), libraryName)
        logger.debug(MODULE_NAME, msg)

        return package_list
Ejemplo n.º 59
0
 def apply_sheet_file(sheet, fn):
     styledoc = libxml2.parseMemory(sheet, len(sheet))
     style = libxslt.parseStylesheetDoc(styledoc)
     doc = libxml2.parseFile(fn)
     result = style.applyStylesheet(doc, None)
     res = style.saveResultToString(result)
     style.freeStylesheet()
     doc.freeDoc()
     result.freeDoc()
     return res
Ejemplo n.º 60
0
def kaf(xml_file):
    print "Открываем документ " + xml_file
    doc = libxml2.parseFile(xml_file)
    ctxt = doc.xpathNewContext()
    students = ctxt.xpathEval(
        "//department[../../@name='Кибернетика и Безопасность']")
    for student in students:
        print student.prop('name')
    doc.freeDoc()
    ctxt.xpathFreeContext()