def parse(xml, filename): """Parse the XML description of a VM image into a data structure. Returns an object of class Image. BASE should be the directory where the disk image files for this image can be found""" class ErrorHandler: def __init__(self): self.msg = "" def handler(self, ignore, s): self.msg += s error = ErrorHandler() libxml2.registerErrorHandler(error.handler, None) try: try: doc = libxml2.readMemory(xml, len(xml), None, None, libxml2.XML_PARSE_NOBLANKS) except (libxml2.parserError, libxml2.treeError), e: raise ParserException("%s\n%s" % (e, error.msg)) finally: libxml2.registerErrorHandler(None, None) try: root = doc.getRootElement() if root.name != "image": raise ParserException(_("Root element is not 'image'")) image = Image(root, filename=filename) finally: doc.freeDoc() return image
def parse_node_helper(xml, root_name, callback, exec_class=ValueError): """ Parse the passed XML, expecting root as root_name, and pass the root node to callback """ class ErrorHandler: def __init__(self): self.msg = "" def handler(self, ignore, s): self.msg += s error = ErrorHandler() libxml2.registerErrorHandler(error.handler, None) try: try: doc = libxml2.readMemory(xml, len(xml), None, None, libxml2.XML_PARSE_NOBLANKS) except (libxml2.parserError, libxml2.treeError), e: raise exec_class("%s\n%s" % (e, error.msg)) finally: libxml2.registerErrorHandler(None, None) ret = None try: root = doc.getRootElement() if root.name != root_name: raise ValueError("Root element is not '%s'" % root_name) ret = callback(root) finally: doc.freeDoc() return ret
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 apply_sheet_data(sheet, data): doc = libxml2.readMemory(data, len(data), '', '', options=libxml2.XML_PARSE_NONET) return _apply_sheet_doc(sheet, doc)
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 _apply_sheet_doc(sheet, doc): styledoc = libxml2.readMemory(sheet, len(sheet), '', '', options=libxml2.XML_PARSE_NONET) style = libxslt.parseStylesheetDoc(styledoc) result = style.applyStylesheet(doc, None) res = "" try: res = style.saveResultToString(result) except Exception as err: # print("saveResultToString got exception: %s"%err) pass style.freeStylesheet() doc.freeDoc() result.freeDoc() return res
def __read_metadata (self): if not self.metadata is None: return if not os.path.exists (self.path): dprint ("Profile file '%s' doesn't exist", self.path) self.metadata = self.__create_empty_metadata_doc () self.needs_saving = True return dprint ("Reading metadata from '%s'", self.path) try: self.zip = zipfile.ZipFile (self.path, "r") except: raise ProfileStorageException (_("Failed to read file '%s': %s") % (self.path, sys.exc_info()[1])) try: blob = self.zip.read ("metadata") doc = libxml2.readMemory (blob, len (blob), "metadata", None, libxml2.XML_PARSE_NOBLANKS) except: raise ProfileStorageException (_("Failed to read metadata from '%s': %s") % (self.path, sys.exc_info()[1])) root = doc.getRootElement () if not root or root.name != "metadata": doc.freeDoc () raise ProfileStorageException (_("Invalid metadata section in '%s': %s") % (self.path, sys.exc_info()[1])) self.metadata = doc if len (root.xpathEval ("directories")) == 0: root.newChild (None, "directories", None) if len (root.xpathEval ("files")) == 0: root.newChild (None, "files", None)