def __init__ (self, db_file = None): """Create a UserDatabase object. @db_file: an (optional) path which specifes the location of the database file. If not specified, the default location of /etc/desktop-profiles/users.xml is used. """ if db_file is None: file = os.path.join (config.PROFILESDIR, "users.xml") elif db_file[0] != '/': file = os.path.join (config.PROFILESDIR, db_file) else: file = db_file self.file = file; self.modified = 0 dprint("New UserDatabase(%s) object\n" % self.file) try: self.doc = libxml2.readFile(file, None, libxml2.XML_PARSE_NOBLANKS) # Process XInclude statements self.doc.xincludeProcess() except: # TODO add fallback to last good database dprint("failed to parse %s falling back to default conf\n" % self.file) self.doc = None if self.doc == None: self.doc = libxml2.readMemory(defaultConf, len(defaultConf), None, None, libxml2.XML_PARSE_NOBLANKS);
def __init__ (self, db_file = None): """Create a SystemDatabase object. @db_file: a mandatory path which specifes the location of the database file. If only a file is specified, the directory /etc/sabayon is used. """ if db_file is None: raise SystemDatabaseException(_("No database file provided")) elif db_file[0] != '/': file = os.path.join (config.CONFIGDIR, db_file) else: file = db_file self.file = file self.xmlquery = None self.nodes = None # nodes from the XML file for LDAP usage. self.modified = 0 dprint("New UserDatabase(%s) object\n" % self.file) try: self.doc = libxml2.readFile(file, None, libxml2.XML_PARSE_NOBLANKS) # Process XInclude statements self.doc.xincludeProcess() except: # TODO add fallback to last good database dprint("failed to parse %s falling back to default conf\n" % self.file) self.doc = None if self.doc == None: self.doc = libxml2.readMemory(defaultConf, len(defaultConf), None, None, libxml2.XML_PARSE_NOBLANKS)
def load(conf,inputs,outputs): #print >> sys.stderr, conf # To define dbParams=['dbname','user','password','host','port'] values="{" try: parse_options = libxml2.XML_PARSE_DTDLOAD + libxml2.XML_PARSE_NOENT libxml2.initParser() xqf = libxml2.readFile(conf["main"]["dataPath"]+"/"+inputs["type"]["value"]+"/"+inputs["name"]["value"]+".xml", None, parse_options) cnt=0 for j in dbParams: print >> sys.stderr,j #print >> sys.stderr, j try: items = xqf.xpathEval("/connection/"+j) for i in items: #print >> sys.stderr, cnt if cnt>0: values+=', ' values+='"'+i.name+'": "'+str(i.children.get_content())+'"' cnt+=1 except: values+='"'+j+'": ""' cnt+=1 pass except Exception,e: print >> sys.stderr,e conf["lenv"]["message"]=zoo._("Unable to parse the file") return 4
def parse_GL_API(file_name, factory=None): doc = libxml2.readFile( file_name, None, libxml2.XML_PARSE_XINCLUDE + libxml2.XML_PARSE_NOBLANKS + libxml2.XML_PARSE_DTDVALID + libxml2.XML_PARSE_DTDATTR + libxml2.XML_PARSE_DTDLOAD + libxml2.XML_PARSE_NOENT) ret = doc.xincludeProcess() if not factory: factory = gl_item_factory() api = factory.create_item("api", None, None) api.process_element(doc) # After the XML has been processed, we need to go back and assign # dispatch offsets to the functions that request that their offsets # be assigned by the scripts. Typically this means all functions # that are not part of the ABI. for func in api.functionIterateByCategory(): if func.assign_offset: func.offset = api.next_offset api.next_offset += 1 doc.freeDoc() return api
def loadSchema(self, uri, targetNamespace = None): if uri in self.loadedSchemas: return print "Loading schema file: %s" % uri self.loadedSchemas.add(uri) doc = libxml2.readFile(uri, None, options = libxml2.XML_PARSE_NOBLANKS) root = doc.getRootElement() xpath = doc.xpathNewContext() if targetNamespace is None: targetNamespace = root.prop("targetNamespace") result = xpath.xpathEval("/*[local-name()='schema']/*[local-name()='include' or local-name()='import']") for node in result: url = urlparse.urlparse(node.prop("schemaLocation")) if bool(url.scheme): print " Ignoring non-local resource %s" % node.prop("schemaLocation") else: loc = os.path.normpath(os.path.join(os.path.dirname(uri), url.path)) self.loadSchema(loc, targetNamespace if node.name == "include" else None) result = xpath.xpathEval("/*[local-name()='schema']/*") for node in result: if node.name in self.declTypes: self.importDef(node, targetNamespace)
def validateXml(self, fpath, xmlschema_context): retval = True # Create validation context validation_context = xmlschema_context.schemaNewValidCtxt() # Set error/warning handlers eh = Libxml2ValidityHandler() validation_context.setValidityErrorHandler(eh.error, eh.warning, ARG) # Read file and resolve entities doc = libxml2.readFile(fpath, None, libxml2.XML_PARSE_NOENT) doc.xincludeProcessFlags(libxml2.XML_PARSE_NOENT) err = validation_context.schemaValidateDoc(doc) if err or eh.errors: for e in eh.errors: print(e.rstrip("\n")) # import pdb; pdb.set_trace() print("%s fails to validate" % fpath) retval = False # Cleanup doc.freeDoc() del validation_context return retval
def load(conf, inputs, outputs): #print >> sys.stderr, conf # To define dbParams = ['dbname', 'user', 'password', 'host', 'port'] values = "{" try: parse_options = libxml2.XML_PARSE_DTDLOAD + libxml2.XML_PARSE_NOENT libxml2.initParser() xqf = libxml2.readFile( conf["main"]["dataPath"] + "/" + inputs["type"]["value"] + "/" + inputs["name"]["value"] + ".xml", None, parse_options) cnt = 0 for j in dbParams: print >> sys.stderr, j #print >> sys.stderr, j try: items = xqf.xpathEval("/connection/" + j) for i in items: #print >> sys.stderr, cnt if cnt > 0: values += ', ' values += '"' + i.name + '": "' + str( i.children.get_content()) + '"' cnt += 1 except: values += '"' + j + '": ""' cnt += 1 pass except Exception, e: print >> sys.stderr, e conf["lenv"]["message"] = zoo._("Unable to parse the file") return 4
def loadSchema(self, uri, targetNamespace=None): if uri in self.loadedSchemas: return print "Loading schema file: %s" % uri self.loadedSchemas.add(uri) doc = libxml2.readFile(uri, None, options=libxml2.XML_PARSE_NOBLANKS) root = doc.getRootElement() xpath = doc.xpathNewContext() if targetNamespace is None: targetNamespace = root.prop("targetNamespace") result = xpath.xpathEval( "/*[local-name()='schema']/*[local-name()='include' or local-name()='import']" ) for node in result: url = urlparse.urlparse(node.prop("schemaLocation")) if bool(url.scheme): print " Ignoring non-local resource %s" % node.prop( "schemaLocation") else: loc = os.path.normpath( os.path.join(os.path.dirname(uri), url.path)) self.loadSchema( loc, targetNamespace if node.name == "include" else None) result = xpath.xpathEval("/*[local-name()='schema']/*") for node in result: if node.name in self.declTypes: self.importDef(node, targetNamespace)
def _ParseXML(filename, apiname): conversions = { # from to 'GLfloat': [ 'GLdouble' ], 'GLclampf': [ 'GLclampd' ], 'GLubyte': [ 'GLfloat', 'GLdouble' ], 'GLint': [ 'GLfloat', 'GLdouble' ], 'GLfixed': [ 'GLfloat', 'GLdouble' ], 'GLclampx': [ 'GLclampf', 'GLclampd' ], } doc = libxml2.readFile(filename, None, libxml2.XML_PARSE_DTDLOAD + libxml2.XML_PARSE_DTDVALID + libxml2.XML_PARSE_NOBLANKS) spec = APIspec.Spec(doc) impl = spec.get_impl() api = spec.get_api(apiname) doc.freeDoc() __spec["impl"] = impl __spec["api"] = api for func in api.functions: alias, need_conv = impl.match(func, conversions) if not alias: # external functions are manually dispatched if not func.is_external: print >>sys.stderr, "Error: unable to dispatch %s" % func.name alias = func need_conv = False __functions[func.name] = func __aliases[func.name] = (alias, need_conv)
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.readFile(str(node), None, libxml2.XML_PARSE_NOENT) 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
def flushTitleToTemplate(options, buttonText, titles, titleindex, titleText, dvdindex): buttonText = buttonText + "<button>jump vmgm menu entry title; </button>" text = readFile(options.common + "/template-titles.xml") first = text.replace("@a", buttonText) second = first.replace("@x", str(titleindex)) third = second.replace("@y", str(dvdindex)) titles = titles + third.replace("@b", titleText) return titles
def prettyPrintFile(self, fpath): # Read file and resolve entities doc = libxml2.readFile(fpath, None, libxml2d.XML_PARSE_NOENT) with open(fpath, 'w') as fp: # Prettyprint fp.write(doc.serialize("UTF-8", 1)) # Cleanup doc.freeDoc()
def flushTitleToTemplate(options,buttonText, titles,titleindex,titleText,dvdindex): buttonText=buttonText+"<button>jump vmgm menu entry title; </button>" text=readFile(options.common+"/template-titles.xml") first= text.replace('@a',buttonText); second= first.replace('@x',str(titleindex)) third= second.replace('@y',str(dvdindex)) titles=titles+ third.replace('@b',titleText); return titles;
def prettyPrintFile(self, fpath): # Read file and resolve entities doc = libxml2.readFile(fpath, None, libxml2d.XML_PARSE_NOENT) fp = open(fpath, 'w') # Prettyprint fp.write(doc.serialize("UTF-8", 1)) fp.close() # Cleanup doc.freeDoc()
def __xinclude_libxml2(target, source, env): """ Resolving XIncludes, using the libxml2 module. """ doc = libxml2.readFile(str(source[0]), None, libxml2.XML_PARSE_NOENT) doc.xincludeProcessFlags(libxml2.XML_PARSE_NOENT) doc.saveFile(str(target[0])) doc.freeDoc() return None
def __check_directory(self): directory = self.orig_directory if directory != None: info = self.__ensure_directory (directory, False) if info == None: directory = None if directory == None: parent_info = self.__ensure_directory (get_home_dir() + "/.sabayon", True) if parent_info: directory = get_home_dir() + "/.sabayon/profile_cache" info = self.__ensure_directory (directory, True) if info == None: dprint("Running with cache deactivated") self.directory = None return else: self.directory = directory if stat.S_IMODE(info[0]) != stat.S_IRUSR + stat.S_IWUSR + stat.S_IXUSR: dprint("Wrong mode for %s", directory) try: os.chmod(directory, stat.S_IRUSR + stat.S_IWUSR + stat.S_IXUSR) except: dprint("Failed to chmod %s, ignored", directory) self.directory = None return if self.directory == None: dprint("Running with cache deactivated") return catalogfile = self.directory + "/catalog.xml" try: self.catalog = libxml2.readFile(catalogfile, None, libxml2.XML_PARSE_NOBLANKS) except: dprint("Failed to load catalog from %s" %(catalogfile)) self.catalog = None if self.catalog != None: root = self.catalog.getRootElement() if not root or root.name != "catalog": dprint("Discarding corrupted catalog") self.catalog.freeDoc () self.catalog = None else: self.root = root # remove empty catalogs if self.catalog == None or self.root == None or \ self.root.children == None: try: os.unlink(self.directory + "/catalog.xml") except: pass
def add_resources(target, source, env): """Add missing resources to the OEBPS directory Ensure all the resources in the manifest are present in the OEBPS directory. """ hrefs = [] content_file = os.path.join(source[0].get_abspath(), 'content.opf') if not os.path.isfile(content_file): return hrefs = [] if has_libxml2: nsmap = {'opf': 'http://www.idpf.org/2007/opf'} # Read file and resolve entities doc = libxml2.readFile(content_file, None, 0) opf = doc.getRootElement() # Create xpath context xpath_context = doc.xpathNewContext() # Register namespaces for key, val in nsmap.iteritems(): xpath_context.xpathRegisterNs(key, val) if hasattr(opf, 'xpathEval') and xpath_context: # Use the xpath context xpath_context.setContextNode(opf) items = xpath_context.xpathEval(".//opf:item") else: items = opf.findall(".//{'http://www.idpf.org/2007/opf'}item") for item in items: if hasattr(item, 'prop'): hrefs.append(item.prop('href')) else: hrefs.append(item.attrib['href']) doc.freeDoc() xpath_context.xpathFreeContext() elif has_lxml: from lxml import etree opf = etree.parse(content_file) # All the opf:item elements are resources for item in opf.xpath( '//opf:item', namespaces={'opf': 'http://www.idpf.org/2007/opf'}): hrefs.append(item.attrib['href']) for href in hrefs: # If the resource was not already created by DocBook XSL itself, # copy it into the OEBPS folder referenced_file = os.path.join(source[0].get_abspath(), href) if not os.path.exists(referenced_file): shutil.copy(href, os.path.join(source[0].get_abspath(), href))
def add_resources(target, source, env): """Add missing resources to the OEBPS directory Ensure all the resources in the manifest are present in the OEBPS directory. """ hrefs = [] content_file = os.path.join(source[0].get_abspath(), 'content.opf') if not os.path.isfile(content_file): return hrefs = [] if has_libxml2: nsmap = {'opf' : 'http://www.idpf.org/2007/opf'} # Read file and resolve entities doc = libxml2.readFile(content_file, None, 0) opf = doc.getRootElement() # Create xpath context xpath_context = doc.xpathNewContext() # Register namespaces for key, val in nsmap.iteritems(): xpath_context.xpathRegisterNs(key, val) if hasattr(opf, 'xpathEval') and xpath_context: # Use the xpath context xpath_context.setContextNode(opf) items = xpath_context.xpathEval(".//opf:item") else: items = opf.findall(".//{'http://www.idpf.org/2007/opf'}item") for item in items: if hasattr(item, 'prop'): hrefs.append(item.prop('href')) else: hrefs.append(item.attrib['href']) doc.freeDoc() xpath_context.xpathFreeContext() elif has_lxml: from lxml import etree opf = etree.parse(content_file) # All the opf:item elements are resources for item in opf.xpath('//opf:item', namespaces= { 'opf': 'http://www.idpf.org/2007/opf' }): hrefs.append(item.attrib['href']) for href in hrefs: # If the resource was not already created by DocBook XSL itself, # copy it into the OEBPS folder referenced_file = os.path.join(source[0].get_abspath(), href) if not os.path.exists(referenced_file): shutil.copy(href, os.path.join(source[0].get_abspath(), href))
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.readFile(str(node), None, libxml2.XML_PARSE_NOENT) 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
def get_file(filename): doc = libxml2.readFile(filename, None, 0) def remove_ns(c): c.setNs(None) c = c.get_children() while c != None: remove_ns(c) c = c.next remove_ns(doc) return doc
def parse_GL_API( file_name, factory = None ): doc = libxml2.readFile( file_name, None, libxml2.XML_PARSE_XINCLUDE + libxml2.XML_PARSE_NOBLANKS + libxml2.XML_PARSE_DTDVALID + libxml2.XML_PARSE_DTDATTR + libxml2.XML_PARSE_DTDLOAD + libxml2.XML_PARSE_NOENT ) ret = doc.xincludeProcess() if not factory: factory = gl_item_factory() api = factory.create_item( "api", None, None ) api.process_element( doc ) doc.freeDoc() return api
def parseXmlFile(self, fpath): if not has_libxml2: # Create domtree from file domtree = etree.parse(fpath) self.root = domtree.getroot() else: # Read file and resolve entities self.doc = libxml2.readFile(fpath, None, libxml2.XML_PARSE_NOENT) self.root = self.doc.getRootElement() # Create xpath context self.xpath_context = self.doc.xpathNewContext() # Register namespaces for key, val in self.nsmap.items(): self.xpath_context.xpathRegisterNs(key, val)
def performTransform(self, manifest, package_type): """ Transform the manifest if necessary. """ if package_type: xform = os.path.join(INSTANCE_HOME, 'Products', 'IMSTransport', 'IMS', package_type[1]) styledoc = readFile(xform, None, 0) style = parseStylesheetDoc(styledoc) doc = parseDoc(manifest) manifest = '' result = style.applyStylesheet(doc, None) if result and result.get_content(): manifest = style.saveResultToString(result) style.freeStylesheet() doc.freeDoc() result.freeDoc() return manifest
def parseXmlFile(self, fpath): nsmap = {'dbx' : dbxsd} if not has_libxml2: # Create domtree from file domtree = etree.parse(fpath) self.root = domtree.getroot() else: # Read file and resolve entities self.doc = libxml2.readFile(fpath, None, libxml2.XML_PARSE_NOENT) self.root = self.doc.getRootElement() # Create xpath context self.xpath_context = self.doc.xpathNewContext() # Register namespaces for key, val in self.nsmap.iteritems(): self.xpath_context.xpathRegisterNs(key, val)
def parse_GL_API(file_name, factory=None): doc = libxml2.readFile( file_name, None, libxml2.XML_PARSE_XINCLUDE + libxml2.XML_PARSE_NOBLANKS + libxml2.XML_PARSE_DTDVALID + libxml2.XML_PARSE_DTDATTR + libxml2.XML_PARSE_DTDLOAD + libxml2.XML_PARSE_NOENT) ret = doc.xincludeProcess() if not factory: factory = gl_item_factory() api = factory.create_item("api", None, None) api.process_element(doc) doc.freeDoc() return api
def createTitlesets(options,start,end,dvdindex): buttonText="";#the text for the template control file titleplus=1; titleindex=0; for title in range(start,end): if(titleindex%14==0 and titleindex!=0): buttonText=buttonText+"<button>jump titleset "+str(titleplus)+" menu;</button>"+"\n"; titleplus=titleplus+1; titleindex=titleindex+1; #write the final one buttonText=buttonText+"<button>jump titleset "+str(titleplus)+" menu;</button>"+"\n"; text = readFile(options.common+"/template.xml") text = text.replace('@a',buttonText); outputDVDXML=options.output + '/dvd'+str(dvdindex)+'.xml'; writeToFile(outputDVDXML, text)
def __init__(self, xmlFile, addComments = False): '''Class constructor. Collects some information needed for config generation, loads XML. Keyword arguments: addComments -- add comments to result XML (default False) xmlFile -- file that contains XML to parse ''' # get path to script self.scriptPath = os.path.dirname(sys.argv[0]) # default options self.addComments = addComments self.xmlFile = xmlFile # suppress all error messages from libxml2 libxml2.registerErrorHandler(self.noErr, None) # load xml self.xml = libxml2.readFile(xmlFile, None, 0)
def __init__(self, xmlFile, addComments=False): '''Class constructor. Collects some information needed for config generation, loads XML. Keyword arguments: addComments -- add comments to result XML (default False) xmlFile -- file that contains XML to parse ''' # get path to script self.scriptPath = os.path.dirname(os.path.realpath(sys.argv[0])) # default options self.addComments = addComments self.xmlFile = xmlFile # suppress all error messages from libxml2 libxml2.registerErrorHandler(self.noErr, None) # load xml self.xml = libxml2.readFile(xmlFile, None, 0)
def main(): import libxml2 filename = "APIspec.xml" apinames = ["GLES1.1", "GLES2.0"] doc = libxml2.readFile( filename, None, libxml2.XML_PARSE_DTDLOAD + libxml2.XML_PARSE_DTDVALID + libxml2.XML_PARSE_NOBLANKS) spec = Spec(doc) impl = spec.get_impl() for apiname in apinames: spec.get_api(apiname) doc.freeDoc() print "%s is successfully parsed" % filename
def createTitlesets(options, start, end, dvdindex): buttonText = "" # the text for the template control file titleplus = 1 titleindex = 0 for title in range(start, end): if titleindex % 14 == 0 and titleindex != 0: buttonText = buttonText + "<button>jump titleset " + str(titleplus) + " menu;</button>" + "\n" titleplus = titleplus + 1 titleindex = titleindex + 1 # write the final one buttonText = buttonText + "<button>jump titleset " + str(titleplus) + " menu;</button>" + "\n" text = readFile(options.common + "/template.xml") text = text.replace("@a", buttonText) outputDVDXML = options.output + "/dvd" + str(dvdindex) + ".xml" writeToFile(outputDVDXML, text)
def main(): import libxml2 filename = "APIspec.xml" apinames = ["GLES1.1", "GLES2.0"] doc = libxml2.readFile(filename, None, libxml2.XML_PARSE_DTDLOAD + libxml2.XML_PARSE_DTDVALID + libxml2.XML_PARSE_NOBLANKS) spec = Spec(doc) impl = spec.get_impl() for apiname in apinames: spec.get_api(apiname) doc.freeDoc() print "%s is successfully parsed" % filename
def __build_libxml2(target, source, env): """ General XSLT builder (HTML/FO), using the libxml2 module. """ xsl_style = env.subst('$DOCBOOK_XSL') styledoc = libxml2.parseFile(xsl_style) style = libxslt.parseStylesheetDoc(styledoc) doc = libxml2.readFile(str(source[0]),None,libxml2.XML_PARSE_NOENT) # Support for additional parameters parampass = {} if parampass: result = style.applyStylesheet(doc, parampass) else: result = style.applyStylesheet(doc, None) style.saveResultToFilename(str(target[0]), result, 0) style.freeStylesheet() doc.freeDoc() result.freeDoc() return None
def __build_libxml2(target, source, env): """ General XSLT builder (HTML/FO), using the libxml2 module. """ xsl_style = env.subst('$DOCBOOK_XSL') styledoc = libxml2.parseFile(xsl_style) style = libxslt.parseStylesheetDoc(styledoc) doc = libxml2.readFile(str(source[0]), None, libxml2.XML_PARSE_NOENT) # Support for additional parameters parampass = {} if parampass: result = style.applyStylesheet(doc, parampass) else: result = style.applyStylesheet(doc, None) style.saveResultToFilename(str(target[0]), result, 0) style.freeStylesheet() doc.freeDoc() result.freeDoc() return None
def getPlayListFromAPI(options): outputMessage("Reading Playlist from API") httpServ = httplib.HTTPConnection("www.khanacademy.org", 80) httpServ.connect() httpServ.request('GET', "/api/v1/playlists/library/list") response = httpServ.getresponse() if response.status == httplib.OK: if(options.sample=='y'): result = readFile('./playlist'); # just read a local copy else: result = response.read(); playlistitems = json.loads(result); filename = ''; count = 0; names=[]; if(len(playlistitems)==0):# in case the API is down raise IOError('API Playlist is empty'); for topic in playlistitems: if (topic['title']==options.playlist): for item in topic['videos']: playlistfile = item['download_urls']['mp4']; name = item['readable_id']; readabletitles.append(name); globtitles.append(item['title']); filename = options.output + "/" + name + ".mp4"; downloadLinks.append([playlistfile,filename]); names.append(name); #TESTING count = count + 1; if(count > 1 and options.sample=='y'): return; else: raise IOError("Playlist API unavailable");
def getPlayListFromAPI(options): outputMessage("Reading Playlist from API") httpServ = httplib.HTTPConnection("www.khanacademy.org", 80) httpServ.connect() httpServ.request("GET", "/api/v1/playlists/library/list") response = httpServ.getresponse() if response.status == httplib.OK: if options.sample == "y": result = readFile("./playlist") # just read a local copy else: result = response.read() playlistitems = json.loads(result) filename = "" count = 0 names = [] if len(playlistitems) == 0: # in case the API is down raise IOError("API Playlist is empty") for topic in playlistitems: if topic["title"] == options.playlist: for item in topic["videos"]: playlistfile = item["download_urls"]["mp4"] name = item["readable_id"] readabletitles.append(name) globtitles.append(item["title"]) filename = options.output + "/" + name + ".mp4" downloadLinks.append([playlistfile, filename]) names.append(name) # TESTING count = count + 1 if count > 1 and options.sample == "y": return else: raise IOError("Playlist API unavailable")
def details(conf,inputs,outputs): dbParams=['dbname','user','password','host','port'] res={} values="{" try: parse_options = libxml2.XML_PARSE_DTDLOAD + libxml2.XML_PARSE_NOENT libxml2.initParser() xqf = libxml2.readFile(conf["main"]["dataPath"]+"/"+inputs["type"]["value"]+"/"+inputs["name"]["value"]+".xml", None, parse_options) cnt=0 for j in dbParams: print >> sys.stderr,j #print >> sys.stderr, j try: items = xqf.xpathEval("/connection/"+j) for i in items: res[i.name]=str(i.children.get_content()) except: res[j]="" pass except Exception,e: conf["lenv"]["message"]=zoo._("Unable to parse the file: ")+str(e) return zoo.SERVICE_FAILED
def parse_GL_API( file_name, factory = None ): doc = libxml2.readFile( file_name, None, libxml2.XML_PARSE_XINCLUDE + libxml2.XML_PARSE_NOBLANKS + libxml2.XML_PARSE_DTDVALID + libxml2.XML_PARSE_DTDATTR + libxml2.XML_PARSE_DTDLOAD + libxml2.XML_PARSE_NOENT ) ret = doc.xincludeProcess() if not factory: factory = gl_item_factory() api = factory.create_item( "api", None, None ) api.process_element( doc ) # After the XML has been processed, we need to go back and assign # dispatch offsets to the functions that request that their offsets # be assigned by the scripts. Typically this means all functions # that are not part of the ABI. for func in api.functionIterateByCategory(): if func.assign_offset: func.offset = api.next_offset; api.next_offset += 1 doc.freeDoc() return api
def createTitles(options,start,end,dvdindex): titleText=""; titles=""; outputDVDXML=options.output+'/dvd'+str(dvdindex)+'.xml'; buttonText="" buttonindex=1; # keep track of how many buttons are in this title titleindex=0; for title in range(start,end): if(buttonindex%14==0 and buttonindex!=0): titles=flushTitleToTemplate(options, buttonText, titles, titleindex,titleText, dvdindex) titleText=""; buttonText=""; buttonindex=1; titleindex=titleindex+1; titleText=titleText+"<vob file=\""+readabletitles[title]+".mpeg\" pause=\"3\" />\n"; buttonText=buttonText+"<button>jump title 1 chapter "+str(buttonindex)+";</button>" buttonindex=buttonindex+1; #write the final title titles=flushTitleToTemplate(options, buttonText,titles, titleindex,titleText, dvdindex) text=readFile(outputDVDXML) text = text.replace('@b',titles); writeToFile(outputDVDXML, text)
def createTitles(options, start, end, dvdindex): titleText = "" titles = "" outputDVDXML = options.output + "/dvd" + str(dvdindex) + ".xml" buttonText = "" buttonindex = 1 # keep track of how many buttons are in this title titleindex = 0 for title in range(start, end): if buttonindex % 14 == 0 and buttonindex != 0: titles = flushTitleToTemplate(options, buttonText, titles, titleindex, titleText, dvdindex) titleText = "" buttonText = "" buttonindex = 1 titleindex = titleindex + 1 titleText = titleText + '<vob file="' + readabletitles[title] + '.mpeg" pause="3" />\n' buttonText = buttonText + "<button>jump title 1 chapter " + str(buttonindex) + ";</button>" buttonindex = buttonindex + 1 # write the final title titles = flushTitleToTemplate(options, buttonText, titles, titleindex, titleText, dvdindex) text = readFile(outputDVDXML) text = text.replace("@b", titles) writeToFile(outputDVDXML, text)
def details(conf, inputs, outputs): dbParams = ['dbname', 'user', 'password', 'host', 'port'] res = {} values = "{" try: parse_options = libxml2.XML_PARSE_DTDLOAD + libxml2.XML_PARSE_NOENT libxml2.initParser() xqf = libxml2.readFile( conf["main"]["dataPath"] + "/" + inputs["type"]["value"] + "/" + inputs["name"]["value"] + ".xml", None, parse_options) cnt = 0 for j in dbParams: print >> sys.stderr, j #print >> sys.stderr, j try: items = xqf.xpathEval("/connection/" + j) for i in items: res[i.name] = str(i.children.get_content()) except: res[j] = "" pass except Exception, e: conf["lenv"]["message"] = zoo._("Unable to parse the file: ") + str(e) return zoo.SERVICE_FAILED
line = input.readline() input.close() if line == "": print "Could not find the CUT marker in testapi.c skipping generation" test.close() sys.exit(0) print("Scanned testapi.c: found %d parameters types and %d return types\n" % (len(known_param_types), len(known_return_types))) test.write("/* CUT HERE: everything below that line is generated */\n") # # Open the input API description # doc = libxml2.readFile(srcPref + 'doc/libxml2-api.xml', None, 0) if doc == None: print "Failed to load doc/libxml2-api.xml" sys.exit(1) ctxt = doc.xpathNewContext() # # Generate a list of all function parameters and select only # those used in the api tests # argtypes = {} args = ctxt.xpathEval("/api/symbols/function/arg") for arg in args: mod = arg.xpathEval('string(../@file)') func = arg.xpathEval('string(../@name)') if (mod not in skipped_modules) and (func not in skipped_functions):
input.close() if line == "": print "Could not find the CUT marker in testapi.c skipping generation" test.close() sys.exit(0) print("Scanned testapi.c: found %d parameters types and %d return types\n" % ( len(known_param_types), len(known_return_types))) test.write("/* CUT HERE: everything below that line is generated */\n") # # Open the input API description # doc = libxml2.readFile(srcPref + 'doc/libxml2-api.xml', None, 0) if doc == None: print "Failed to load doc/libxml2-api.xml" sys.exit(1) ctxt = doc.xpathNewContext() # # Generate a list of all function parameters and select only # those used in the api tests # argtypes = {} args = ctxt.xpathEval("/api/symbols/function/arg") for arg in args: mod = arg.xpathEval('string(../@file)') func = arg.xpathEval('string(../@name)') if (mod not in skipped_modules) and (func not in skipped_functions):
def apply_sheet_file(sheet, fn): doc = libxml2.readFile(fn, '', options=libxml2.XML_PARSE_NONET) return _apply_sheet_doc(sheet, doc)