def from_xml(cls, xml_document): """A factory method, that can be used to transform a xml document the matching Message instance. @param xml_document a etree.Element or etree.ElementTree or something parsable to it @return the matching Message instance or a failure.Failure instance """ # try to transform the given document into an etree instance if isinstance(xml_document, basestring): # got a string as an argument, so parse it try: etree.clearErrorLog() xml_document = etree.fromstring(xml_document) except etree.XMLSyntaxError, e: raise MessageParserError("syntax error", e)
def do_ConfirmReservation(self, elem, *args, **kw): log.debug("=============== XenBEEClient2BrokerProtocol::do_ConfirmReservation") try: confirm = message.MessageBuilder.from_xml(elem.getroottree()) except Exception, e: return message.BrokerError(confirm.uuid(), errcode.ILLEGAL_REQUEST, str(e)) ticket = TicketStore.getInstance().lookup(confirm.ticket()) if ticket is None: return message.BrokerError(confirm.uuid(), errcode.TICKET_INVALID, confirm.ticket()) log.debug("got confirmation with ticket %s" % confirm.ticket()) xbed = XBEDaemon.getInstance() jsdl_doc = jsdl.JsdlDocument(schema_map=xbed.schema_map) try: if hasattr(etree, 'clearErrorLog'): etree.clearErrorLog() if hasattr(etree, 'clear_error_log'): etree.clear_error_log() parsed_jsdl = jsdl_doc.parse(confirm.jsdl()) except etree.DocumentInvalid, e: log.info("got invalid document: %s" % str(e.error_log)) # TaskManager.getInstance().removeTask(ticket.task) # del ticket.task # TicketStore.getInstance().release(ticket) return message.BrokerError(confirm.uuid(), errcode.ILLEGAL_REQUEST, "JSDL document is invalid: %s" % (e.error_log,)) try: # does the job have our InstanceDescription element? # otherwise drop the job jsdl_doc.lookup_path( "JobDefinition/JobDescription/Resources/"+ "InstanceDefinition/InstanceDescription")
def main(): global LOCALES # parse command line parameters and check for sanity (opts, args) = loadOpts() if (getattr(opts, "podir", None) is None): print >> sys.stderr, "You must specify --podir." sys.exit(1) # load the catalogs and jurisdiction list LOCALES = loadCatalogs(opts.podir) # determine our output directory output_dir = getattr(opts, "outputDir", None) # set up our TAL context context = simpleTALES.Context(allowPythonPath=1) context.addGlobal("locales", LOCALES.keys()) context.addGlobal("jurisdictions", loadJurisdictions()) context.addGlobal("lookupString", lookupString) # iterate over the specified for in_fn in args: if output_dir is None: # just output to the same directory out_fn = in_fn[:-3] else: out_fn = os.path.join(output_dir, os.path.basename(in_fn)[:-3]) # generate a temporary intermediary file to validate the XML temp_fn = "{}.tmp".format(out_fn) # compile the template and write it to the temporary file template = simpleTAL.compileXMLTemplate(open(in_fn, "r")) output = file(temp_fn, "w") print "writing to {}..".format(temp_fn) template.expand(context, output, "utf-8") output.close() # try to clear the error log before checking validity try: et.clearErrorLog() except AttributeError: # lxml < 1.1 pass # re-read the temp file and parse it for well-formed-ness try: print "validating XML structure of {}...".format(temp_fn) tree = et.parse(temp_fn) # noqa: F841 except Exception, e: print print "An error exists in {}: ".format(temp_fn) print e sys.exit(1) # the file was either read correctly or elementtree is not available print "moving {} to {}...".format(temp_fn, out_fn) shutil.move(temp_fn, out_fn)
def main(): # get opts try: opts, args = getopt.getopt(sys.argv[1:], "l:t:r:k:hdu", [ "location=", "table=", "recordTag=", "keyTags=", "update", "debug", "help" ]) except getopt.GetoptError: usage() sys.exit(2) # set defaults location = None table = None recordTag = None keyTags = [] update = False debug = False # process opts for o, a in opts: # check if help if o in ("-h", "--help"): usage() sys.exit() # check if debug if o in ("-d", "--debug"): debug = True # check if update if o in ("-u", "--update"): update = True # set location if o in ("-l", "--location"): if location: print("""Multiple -l|--location specifications found.\ Only specify one location.""") usage() sys.exit(2) else: location = a # set table if o in ("-t", "--table"): if table: print("""Multiple -t|--table specifications found.\ Only specify one table.""") usage() sys.exit(2) else: table = a # set recordTag if o in ("-r", "--recordTag"): if recordTag: print("""Multiple -r|--recordTag specifications found.\ Only specify one recordTag.""") usage() sys.exit(2) else: recordTag = a # set keyTag if o in ("-k", "--keyTags"): if keyTags: print("""Multiple -k|--keyTags specifications found.\ Only specify one keyTag.""") usage() sys.exit(2) else: keyTags = [i.strip() for i in a.strip().split(',')] # make arg was specified if len(args) != 1: print("Please specify one xml document.") usage() sys.exit(2) # make sure table and location defined if table is None or location is None: print("Please specify both location and table.") usage() sys.exit(2) # xml doc doc = args[0] f = open(doc, 'r') xml = f.read() f.close() # insert kargs = { 'autoKey': False, 'createIfNeeded': True, 'iterateMode': False, 'forceDelete': False } if recordTag: kargs['recordTag'] = recordTag if keyTags: kargs['keyTags'] = keyTags if update: kargs['updateOnDup'] = True etree.clearErrorLog() try: ret = sciflo.db.insertXml(location, table, xml, **kargs) except etree.XMLSyntaxError as e: print("Got XMLSyntaxError:") print((e.error_log.filter_levels(etree.ErrorLevels.FATAL))) print("Please check xml.") except sciflo.db.NoIndexedFieldsInXmlError as e: print( "Couldn't find indexed/keyed fields. Specify them with -k|--keyTags option." ) usage() sys.exit(2) except Exception as e: if re.search(r'duplicate entry', str(e), re.IGNORECASE): print(e) print("Specify -u|--update option to force update.") usage() sys.exit(2) else: raise
def do_ConfirmReservation(self, elem, *args, **kw): log.debug("=============== XenBEEClient2BrokerProtocol::do_ConfirmReservation") try: confirm = message.MessageBuilder.from_xml(elem.getroottree()) except Exception, e: return message.BrokerError(confirm.uuid(), errcode.ILLEGAL_REQUEST, str(e)) ticket = TicketStore.getInstance().lookup(confirm.ticket()) if ticket is None: return message.BrokerError(confirm.uuid(), errcode.TICKET_INVALID, confirm.ticket()) log.debug("got confirmation with ticket %s" % confirm.ticket()) xbed = XBEDaemon.getInstance() jsdl_doc = jsdl.JsdlDocument(schema_map=xbed.schema_map) try: if hasattr(etree, "clearErrorLog"): etree.clearErrorLog() if hasattr(etree, "clear_error_log"): etree.clear_error_log() parsed_jsdl = jsdl_doc.parse(confirm.jsdl()) except etree.DocumentInvalid, e: log.info("got invalid document: %s" % str(e.error_log)) # TaskManager.getInstance().removeTask(ticket.task) # del ticket.task # TicketStore.getInstance().release(ticket) return message.BrokerError( confirm.uuid(), errcode.ILLEGAL_REQUEST, "JSDL document is invalid: %s" % (e.error_log,) ) try: # does the job have our InstanceDescription element? # otherwise drop the job
def main(): global LOCALES # parse command line parameters and check for sanity (opts, args) = loadOpts() if (getattr(opts, 'podir', None) is None): print >> sys.stderr, "You must specify --podir." sys.exit(1) # load the catalogs and jurisdiction list LOCALES = loadCatalogs(opts.podir) # determine our output directory output_dir = getattr(opts, 'outputDir', None) # set up our TAL context context = simpleTALES.Context(allowPythonPath=1) context.addGlobal ("locales", LOCALES.keys()) context.addGlobal ("jurisdictions", loadJurisdictions()) context.addGlobal ("lookupString", lookupString) # iterate over the specified for in_fn in args: if output_dir is None: # just output to the same directory out_fn = in_fn[:-3] else: out_fn = os.path.join(output_dir, os.path.basename(in_fn)[:-3]) # generate a temporary intermediary file to validate the XML temp_fn = "%s.tmp" % out_fn # compile the template and write it to the temporary file template = simpleTAL.compileXMLTemplate (open (in_fn, 'r')) output = file(temp_fn, 'w') print 'writing to %s..' % temp_fn template.expand (context, output, 'utf-8') output.close() # try to clear the error log before checking validity try: et.clearErrorLog() except AttributeError: # lxml < 1.1 pass # re-read the temp file and parse it for well-formed-ness try: print 'validating XML structure of %s...' % temp_fn tree = et.parse(temp_fn) except Exception, e: print print "An error exists in %s: " % temp_fn print e sys.exit(1) # the file was either read correctly or elementtree is not available print 'moving %s to %s...' % (temp_fn, out_fn) shutil.move(temp_fn, out_fn)
def transform(self, data, options=None): ''' Transforms data (OpenDocument file) to XHTML. It returns an TransformResult object. ''' if not self.available: log(DEBUG, "The LXML library is required to use the %s transform " % (self.name)) return None self._prepareTrans(data) if not self._dataFiles: return None; result = None #XSL tranformation try: try: etree.clearErrorLog() parser = etree.XMLParser(remove_comments=True,\ remove_blank_text=True) #concatenate all xml files contentXML = etree.parse(self._concatDataFiles(), parser) contentXML.xinclude() #adjust file paths root = contentXML.getroot() images = root.xpath("//draw:image", {'draw' :\ 'urn:oasis:names:tc:opendocument:xmlns:drawing:1.0' }) for i in images: imageName = i.get("{http://www.w3.org/1999/xlink}href") imageName = os.path.basename(imageName) if not self._imageNames.has_key(imageName): self.errors = self.errors + u''' Image file or OLE Object '%s' does not\ exist. Maybe it is\ not embedded in OpenDocument file? ''' % (imageName) i.set("{http://www.w3.org/1999/xlink}href", imageName) continue imageName = self._imageNames[imageName] i.set("{http://www.w3.org/1999/xlink}href", imageName) #extract meta data self._getMetaData(contentXML) #xslt transformation stylesheetXML = etree.parse(self.xsl_stylesheet, parser) xslt = etree.XSLT(stylesheetXML) resultXML = xslt(contentXML, **self.xsl_stylesheet_param) docinfo = u'<?xml version=\'1.0\' encoding=\'utf-8\'?>\ \n<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">\n' self._addCssSelectorPrefix(resultXML,'#odf_document ') self.data.write(docinfo.encode('utf-8')) resultXML.write(self.data, pretty_print=True) self.data.seek(0) #log non fatal errors and warnings if parser.error_log: self.errors = self.errors + u''' Parse errors which are not fatal: %s ''' % (parser.error_log) if xslt.error_log: self.errors = self.errors + u''' XSLT errors which are not fatal: %s ''' % (xslt.error_log) for f in self._dataFiles.values(): f.close() result = TransformResult(self.data, subobjects=self.subobjects or {}, metadata=self.metadata or {}, errors=self.errors or None ) except etree.LxmlError, e: log(DEBUG,\ str(e) + ('\nlibxml error_log:\n') + str(e.error_log)) return None except Exception, e: log(DEBUG, str(e)) return None