def register_script(self): """ Registers a pig scripts with its variables substituted. raises: IOException If a temp file containing the pig script could not be created. raises: ParseException The pig script could not have all its variables substituted. todo: Refactor this processes that result in calling this method. This method gets called twice for a single assert as every method that needs the data assumes no one else has called it (even methods that call other methods that call it (assertOutput() calls get_alias() which both call this method). """ pigIStream = BufferedReader(StringReader(self.orig_pig_code)) pigOStream = StringWriter() ps = ParameterSubstitutionPreprocessor(50) # Where does 50 come from? ps.genSubstitutedFile(pigIStream, pigOStream, self.args, self.arg_files) substitutedPig = pigOStream.toString() f = File.createTempFile("tmp", "pigunit") pw = PrintWriter(f) pw.println(substitutedPig) pw.close() pigSubstitutedFile = f.getCanonicalPath() self._temp_pig_script = pigSubstitutedFile self.pig.registerScript(pigSubstitutedFile, self.alias_overrides)
def mainproc(): u'''Функция для склейки карточки с блоками правил''' try: rootPath = AppInfoSingleton.getAppInfo().getCurUserData().getPath( ) + '/xforms/' except: rootPath = 'E:/Projects/celesta/ssmmd/userscore/ssmmd/xforms/file/' templatePath = rootPath + add #путь к карточке, в которую необходимо вставить праила rulePath = rootPath + 'ruleTemplate.xml' #путь к блоку с правилами bindPath = rootPath + 'bindTemplate.xml' #путь к блоку с биндами для правил stringWriter = StringWriter() xmlWriter = XMLOutputFactory.newInstance().createXMLStreamWriter( stringWriter) parser = XMLReaderFactory.createXMLReader() handler = XformsAddRules(rulePath, bindPath, xmlWriter) parser.setContentHandler(handler) parser.setErrorHandler(handler) parser.setFeature("http://xml.org/sax/features/namespace-prefixes", True) parser.setProperty("http://xml.org/sax/properties/lexical-handler", handler) stream = FileInputStream(templatePath) parser.parse(InputSource(stream)) xmlWriter.close() stringWriter.close() return unicode(stringWriter)
def __toXMLString(self, document): domSource = DOMSource(document) writer = StringWriter() result = StreamResult(writer) tf = TransformerFactory.newInstance() transformer = tf.newTransformer() transformer.transform(domSource, result) return writer.toString()
def __init__(self, description): self.protocol = current_protocol self.test = current_test self.description = description sw = StringWriter() Exception('Stack trace').printStackTrace(PrintWriter(sw)) self.stack_trace = sw.toString() self.tb = '\n'.join( (line.strip() for line in traceback.format_stack()))
def handleStructs(): sio = StringWriter() sio.write("a") writer = ghidra.program.model.data.DataTypeWriter(dataTypeManager, sio) mon = ghidra.util.task.DummyCancellableTaskMonitor() writer.write( dataTypeManager.getCategory( ghidra.program.model.data.CategoryPath("/MH")), mon) json_dict["dataTypes"] = sio.toString()
def build_velocity_context(template_file): inputs = VelocityContext() fInputs = None try: fInputs = FileInputStream(template_file) Velocity.evaluate(inputs, StringWriter(), "inputs", fInputs) return inputs finally: if fInputs: fInputs.close()
def parseOutput(self, person): template = self.findBy("Template", "name", "'user-information'") ve = VelocityEngine() ve.init() context = VelocityContext() context.put("person", person) writer = StringWriter() ve.evaluate(context, writer, template.getName(), unicode(template.getSource())) evaluatedTemplate = writer.toString() return evaluatedTemplate
def render(context, logString, template): sWriter = StringWriter() fIS = None try: sWriter.append(MSGTEMPLATE_PREFIX) fIS = FileInputStream(File(MSGTEMPLATE_DIR, template)) Velocity.evaluate(context, sWriter, logString, fIS) sWriter.append(MSGTEMPLATE_SUFFIX) return sWriter.toString() finally: if fIS: fIS.close()
def _get_details(self): # OOME.printStackTrace seems to throw NullPointerException if self._is_out_of_memory_error(self._exc_type): return '' output = StringWriter() self._exc_value.printStackTrace(PrintWriter(output)) details = '\n'.join(line for line in output.toString().splitlines() if not self._is_ignored_stack_trace_line(line)) msg = unic(self._exc_value.getMessage() or '') if msg: details = details.replace(msg, '', 1) return details
def getPayloadContent(self): mimeType = self.__mimeType print " * single.py: payload content mimeType=%s" % mimeType contentStr = "" if mimeType.startswith("text/"): if mimeType == "text/html": contentStr = '<iframe class="iframe-preview" src="%s/download/%s"></iframe>' % \ (portalPath, self.__oid) else: pid = self.__oid[self.__oid.rfind("/") + 1:] payload = self.__storage.getPayload(self.__oid, pid) print " * single.py: pid=%s payload=%s" % (pid, payload) if payload is not None: sw = StringWriter() sw.write("<pre>") IOUtils.copy(payload.getInputStream(), sw) sw.write("</pre>") sw.flush() contentStr = sw.toString() elif mimeType == "application/pdf" or mimeType.find( "vnd.ms") > -1 or mimeType.find( "vnd.oasis.opendocument.") > -1: # get the html version if exist... pid = os.path.splitext(self.__pid)[0] + ".htm" print " * single.py: pid=%s" % pid #contentStr = '<iframe class="iframe-preview" src="%s/download/%s/%s"></iframe>' % \ # (portalPath, self.__oid, pid) payload = self.__storage.getPayload(self.__oid, pid) saxReader = SAXReader(Boolean.parseBoolean("false")) try: document = saxReader.read(payload.getInputStream()) slideNode = document.selectSingleNode( "//*[local-name()='body']") #linkNodes = slideNode.selectNodes("//img") #contentStr = slideNode.asXML(); # encode character entities correctly slideNode.setName("div") out = ByteArrayOutputStream() format = OutputFormat.createPrettyPrint() format.setSuppressDeclaration(True) format.setExpandEmptyElements(True) writer = XMLWriter(out, format) writer.write(slideNode) writer.close() contentStr = out.toString("UTF-8") except: traceback.print_exc() contentStr = "<p class=\"error\">No preview available</p>" elif mimeType.startswith("image/"): src = "%s/%s" % (self.__oid, self.__pid) contentStr = '<a class="image" href="%(src)s" style="max-width:98%%">' \ '<img src="%(src)s" style="max-width:100%%" /></a>' % { "src": self.__pid } return contentStr
def execute(self): connection = None try: connection = LocalConnection.getLocalConnection() exitCode = connection.execute(self.stdout, self.stderr, self.cmdLine) except Exception, e: stacktrace = StringWriter() writer = PrintWriter(stacktrace, True) e.printStackTrace(writer) self.stderr.handleLine(stacktrace.toString()) return 1
def getAboutPage(self, plugin, type): if type is None or plugin is None: return "<em>This plugin has provided no information about itself.</em>" pid = plugin.replace("-", "_") resource = "plugin/%s/%s/about.html" % (type, pid) stream = self.pageService.getResource(resource) if stream: writer = StringWriter() IOUtils.copy(stream, writer, "UTF-8") html = writer.toString() return html return "<em>This plugin has provided no information about itself.</em>"
def generateTemplate(self, calculator): self._template = self.findBy("Template", "name", "'possession-account-state'") self._context = VelocityContext() self._context.put('stateRent', calculator.calculateCurrentRentState()) self._context.put('stateRF', calculator.calculateCurrentRFState()) self._context.put('chargingRent', calculator.calculateRentCharging()) self._context.put('chargingRT', calculator.calculateRFCharging()) writer = StringWriter() self._ve = VelocityEngine() self._ve.init() self._ve.evaluate(self._context, writer, self._template.getName(), unicode(self._template.getSource())) evaluatedTemplate = writer.toString() self._svars.put('output', evaluatedTemplate)
def test_logger(self): # Setup writer stringWriter = StringWriter() pyFileWriter = PyFileWriter(stringWriter) oldWriter = sys.stdout sys.stdout = pyFileWriter # Run logger.log('123') output = stringWriter.toString() sys.stdout = oldWriter # Assert self.assertTrue(output.index('123') > 0)
def _get_java_details(exc_value): # OOME.printStackTrace seems to throw NullPointerException if isinstance(exc_value, OutOfMemoryError): return '' output = StringWriter() exc_value.printStackTrace(PrintWriter(output)) lines = [ line for line in output.toString().splitlines() if line and not _is_ignored_stacktrace_line(line) ] details = '\n'.join(lines) msg = unic(exc_value.getMessage() or '') if msg: details = details.replace(msg, '', 1) return details
def mainproc(): global xmlWriter, statusUid parser = XMLUtils.createSAXParser() stream = StringBufferInputStream(data.encode("utf-8")) stringWriter = StringWriter() xmlWriter = XMLOutputFactory.newInstance().createXMLStreamWriter( stringWriter) parser.parse(stream, formReader()) xmlWriter.close() if (statusUid != ''): if JythonPoolTest.getStorage().contains(statusUid): return UserMessage("test1", u"повтор!") JythonPoolTest.getStorage().add(statusUid) return None
def evaluate(self, template): ve = VelocityEngine() ve.init() context = VelocityContext() context.put('_formatter', SimpleDateFormat("dd-MM-yyyy")) self._logger.info('Date formatter stored as _formatter') self._logger.info('Template contains %d variables' % len(template.getTemplateVariableCollection())) for var in template.getTemplateVariableCollection(): self._logger.info('Preparing variable %s' % var.getName()) context.put(var.getName(), self.loadData(var.getData())) self._logger.info('Variable %s stored' % var.getName()) writer = StringWriter() ve.evaluate(context, writer, template.getName(), unicode(template.getSource())) evaluatedTemplate = writer.toString() return evaluatedTemplate
def exec_script(self, engine_name, script, post_script="", attr={}): r = "" l2sem = L2ScriptEngineManager.getInstance() context = SimpleScriptContext() sw = StringWriter() pw = PrintWriter(sw, True) context.setAttribute("out_writer", pw, ScriptContext.ENGINE_SCOPE) for k in attr: context.setAttribute(str(k), attr[k], ScriptContext.ENGINE_SCOPE) context.setWriter(pw) context.setErrorWriter(pw) try: l2sem.eval(engine_name, script, context) r += sw.toString() except ScriptException, e: r += sw.toString() r += e.getMessage()
def compileTemplate(self, template, restrictions): ve = VelocityEngine() ve.init() context = VelocityContext() context.put("restrictions", self._output) for restriction in restrictions: restriction.calculate() self._output[restriction.getTemplateName()] = [ restriction.getTemplateName(), restriction.getResult(), restriction.getMessage() ] writer = StringWriter() ve.evaluate(context, writer, template.getName(), unicode(template.getSource())) evaluatedTemplate = writer.toString() return evaluatedTemplate
def __getPayloadContent(self, oid, pid): print " * combined.py: oid='%s' pid='%s'" % (oid, pid) payload = self.__storage.getPayload(oid, pid) if payload is None: return "<div>Error: No content for '%s'</div>" % oid mimeType = payload.contentType contentStr = "" if mimeType.startswith("text/"): if mimeType == "text/html": contentStr = '<iframe class="iframe-preview" src="%s/download/%s"></iframe>' % \ (portalPath, oid) else: sw = StringWriter() sw.write("<pre>") IOUtils.copy(payload.getInputStream(), sw) sw.write("</pre>") sw.flush() contentStr = sw.toString() elif mimeType == "application/pdf" or mimeType.find("vnd.ms")>-1 or mimeType.find("vnd.oasis.opendocument.")>-1: # get the html version if exist... pid = os.path.splitext(pid)[0] + ".htm" print " * combined.py: pid=%s" % pid payload = self.__storage.getPayload(oid, pid) saxReader = SAXReader(False) try: document = saxReader.read(payload.getInputStream()) slideNode = document.selectSingleNode("//*[local-name()='body']") slideNode.setName("div") out = ByteArrayOutputStream() format = OutputFormat.createPrettyPrint() format.setSuppressDeclaration(True) format.setExpandEmptyElements(True) writer = XMLWriter(out, format) writer.write(slideNode) writer.close() contentStr = out.toString("UTF-8") except: traceback.print_exc() contentStr = "<p class=\"error\">No preview available</p>" elif mimeType.startswith("image/"): src = "%s/%s" % (oid, pid) contentStr = '<a class="image" href="%(src)s" style="max-width:98%%">' \ '<img src="%(src)s" style="max-width:100%%" /></a>' % { "src": pid } return contentStr
def __createPackage(self, outputFile=None): title = self.__manifest.getString(None, "title") manifest = self.__createManifest() context = JAXBContext.newInstance("com.googlecode.fascinator.ims") m = context.createMarshaller() m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, True) writer = StringWriter() jaxbElem = ObjectFactory.createManifest(ObjectFactory(), manifest) m.marshal(jaxbElem, writer) writer.close() if outputFile is not None: print "writing to %s..." % outputFile out = FileOutputStream(outputFile) else: print "writing to http output stream..." filename = urllib.quote(title.replace(" ", "_")) response.setHeader("Content-Disposition", "attachment; filename=%s.zip" % filename) out = response.getOutputStream("application/zip") zipOut = ZipOutputStream(out) zipOut.putNextEntry(ZipEntry("imsmanifest.xml")) IOUtils.write(writer.toString(), zipOut) zipOut.closeEntry() oidList = self.__manifest.search("id") for oid in oidList: obj = Services.getStorage().getObject(oid) for pid in obj.getPayloadIdList(): payload = obj.getPayload(pid) if not PayloadType.Annotation.equals(payload.getType()): zipOut.putNextEntry( ZipEntry("resources/%s/%s" % (oid, pid))) IOUtils.copy(payload.open(), zipOut) payload.close() zipOut.closeEntry() obj.close() zipOut.close() out.close()
def execute(self): self.customize(self.options) connection = None try: connection = Overthere.getConnection( CifsConnectionBuilder.CIFS_PROTOCOL, self.options) connection.setWorkingDirectory(connection.getFile(self.remotePath)) # upload the script and pass it to cscript.exe targetFile = connection.getTempFile('uploaded-script', '.vbs') OverthereUtils.write(String(self.script).getBytes(), targetFile) targetFile.setExecutable(True) # run cscript in batch mode scriptCommand = CmdLine.build(cscriptExecutable, '//B', '//nologo', targetFile.getPath()) return connection.execute(self.stdout, self.stderr, scriptCommand) except Exception, e: stacktrace = StringWriter() writer = PrintWriter(stacktrace, True) e.printStackTrace(writer) self.stderr.handleLine(stacktrace.toString()) return 1
def printXMLfile(self, doc): try: from javax.xml.transform import TransformerFactory from javax.xml.transform import OutputKeys from javax.xml.transform.stream import StreamSource from javax.xml.transform.stream import StreamResult from javax.xml.transform.dom import DOMSource from java.io import StringWriter xmlInput = DOMSource(doc) xmlOutput = StreamResult(StringWriter()) tranFactory = TransformerFactory.newInstance() aTransformer = tranFactory.newTransformer() aTransformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8") aTransformer.setOutputProperty(OutputKeys.INDENT, "yes") aTransformer.transform(xmlInput, xmlOutput) print xmlOutput.getWriter().toString() except: print "exception: %s" % traceback.format_exception(*sys.exc_info())
def ncdumpToString(path, output_format='cdl', show_values='c', vars=None): """Returns contents of a given netCDF file as a string. Please be aware that the resulting string CANNOT (currently) be used as input for ncgen due to netCDF-Java limitations. Args: path: Path to an existing netCDF file. output_format: Optional. Understands 'cdl' and 'ncml'. Defaults to 'cdl' show_values: Optional. Understands 'c' and 'vall'. Defaults to 'c'. vars: Optional. Allows you to dump specified variable(s) or variable section(s). Returns: String representation of "ncdump" output (or an empty string if there was a problem within netCDF). """ from jarray import array from java.io import IOException from java.io import StringWriter from ucar.nc2 import NCdumpW # build up commandline args to send off to netCDF-land (it wants 'em as a # string for some reason) args = '%s -%s -%s ' % (path, output_format, show_values) if vars: for var in vars: args = '%s -v %s' % (args, var) writer = StringWriter() try: NCdumpW.print(args, writer, None) except IOException, ioe: print 'Error attempting to list contents of', path print ioe
def handler(environ, start_response): reload(sys) sys.setdefaultencoding('UTF8') welcomeString = "<html><head><title>atom2rss converter • Atom to RSS2 converter</title><style>body { padding: 20px 40px; font-family: Verdana, Helvetica, Sans-Serif; font-size: medium; }</style></head><body><form method=\"post\"><h1>atom2rss converter written with Jython</h1><p>This tool will let you convert your atom 1.0 feed into an RSS2 feed that can be imported into WordPress.</p><p>Please read the full instructions before starting.</p><p><input type=\"text\" name=\"atom\" style=\"width: 400px;\"><input type=\"submit\" value=\"Convert\"></p><h2>Instructions</h2><p><strong>Step 1</strong>: Enter the url of an atom feed urls, e.g. <i>http://yoursite.com/atom.xml</i><br /></p><p><strong>Step 2</strong>: After clicking the \"Convert\" button, head to File / Save As... and save the file as rss.xml to your desktop.</p><p><strong>Step 3</strong>: Use this rss.xml file to import your data into WordPress under Import / RSS.</p><h2>About</h2><p>This tool is online as a convenience designed by <a href=\"http://kevin.9511.net/\">Kevin Li</a>.</p><p>Your can use it to import GoogleReader or Blogger data into Wordpress.</p></form></body></html>" response_parts = [] header_str = '' if environ['REQUEST_METHOD'] == 'POST': post = environ['j2ee.request'] if post.getParameterValues('atom') is None: bs = welcomeString else: try: atom = post.getParameterValues('atom')[0] atomUrl = URL(str(atom)) atomSource = StreamSource( InputStreamReader(atomUrl.openStream(), "utf-8")) file = open(os.path.join(os.getcwd(), 'webapp/atom2rss.xsl')) xsltString = file.read() file.close() xsltSource = StreamSource(StringReader(xsltString)) outputBuffer = StringWriter() transFactory = javax.xml.transform.TransformerFactory.newInstance( "org.apache.xalan.processor.TransformerFactoryImpl", Thread.currentThread().getContextClassLoader()) transformer = transFactory.newTransformer(xsltSource) transformer.transform(atomSource, StreamResult(outputBuffer)) bs = outputBuffer.buffer.toString() header_str = 'text/xml;charset=utf-8' except MalformedURLException, mue: bs = "url format error" except TransformerException, te: bs = "transforme failed" except IOException, ie: bs = ie.getMessage()
def startElement(self, namespaceURI, lname, qname, attrs): if self.parentTag == 'field': if self.flag == 0: self.stringWriter = StringWriter() self.xmlWriter = XMLOutputFactory.newInstance( ).createXMLStreamWriter(self.stringWriter, "UTF-8") self.flag += 1 self.xmlWriter.writeStartElement(qname) for i in range(0, attrs.getLength()): self.xmlWriter.writeAttribute(attrs.getQName(i), attrs.getValue(i)) elif qname == 'table' and self.flag == 0: if self.parentTag is not None: raise CelestaException(u"Неверный формат файла") self.parentTag = qname if not attrs.getValue('name'): raise CelestaException( u"Атрибут 'name' отсутствует в теге 'table'") elif attrs.getValue('name') != self.tableInstance.meta().getName(): raise CelestaException( u"Имя таблицы %s не соответствует значению атрибута 'name'" % self.tableInstance.meta().getName()) elif qname == 'row' and self.flag == 0: if self.parentTag != 'table': raise CelestaException(u"Неверный формат файла") self.parentTag = qname elif qname == 'field' and self.flag == 0: if self.parentTag != 'row': raise CelestaException(u"Неверный формат файла") self.currentEncoding = attrs.getValue('encoding') or u"utf8" self.currentCell = attrs.getValue('name') self.parentTag = qname self.currentString = u'' else: raise CelestaException(u"Неверный формат файла")
def executeFile(self): connection = None try: connection = LocalConnection.getLocalConnection() scriptFile = self.script print "using script %s" % (scriptFile) #scriptFile.setExecutable(True) self.cmdLine.addArgument('-source') self.cmdLine.addArgument(scriptFile) if (len(self.options) > 1): self.cmdLine.addArgument('--') optionsList = self.options.split(' ') for opt in optionsList: self.cmdLine.addArgument(opt) # End for # End if exitCode = connection.execute(self.stdout, self.stderr, self.cmdLine) except Exception, e: stacktrace = StringWriter() writer = PrintWriter(stacktrace, True) e.printStackTrace(writer) self.stderr.handleLine(stacktrace.toString()) return 1
def execute(self): connection = None try: connection = LocalConnection.getLocalConnection() scriptFile = connection.getTempFile('xlrScript', '.py') OverthereUtils.write(String(self.script).getBytes(), scriptFile) scriptFile.setExecutable(True) self.cmdLine.addArgument('-source') self.cmdLine.addArgument(scriptFile.getPath()) if len(self.options) > 1: self.cmdLine.addArgument('--') optionsList = self.options.split(' ') for opt in optionsList: self.cmdLine.addArgument(opt) # End for # End if exitCode = connection.execute(self.stdout, self.stderr, self.cmdLine) except Exception, e: stacktrace = StringWriter() writer = PrintWriter(stacktrace, True) e.printStackTrace(writer) self.stderr.handleLine(stacktrace.toString()) return 1
def startElement(self, namespaceURI, lname, qname, attrs): global stringEmployeesWriter, statusUid if (self.currentState != self.states[0] or qname == 'info'): if (self.currentState == self.states[0] and qname == 'info'): self.currentState = self.states[1] elif (self.currentState == self.states[1] and qname == 'status'): self.currentState = self.states[2] statusUid = attrs.getValue(attrs.getIndex("uid")) elif (self.currentState == self.states[1] and qname == 'employees'): self.currentState = self.states[3] stringEmployeesWriter = StringWriter() self.employeesWriter = XMLOutputFactory.newInstance( ).createXMLStreamWriter(stringEmployeesWriter) self.employeesWriter.writeStartElement(qname) elif (self.currentState == self.states[3]): self.employeesWriter.writeStartElement(qname) for i in range(0, attrs.getLength()): self.employeesWriter.writeAttribute( attrs.getQName(i), attrs.getValue(i)) else: if (attrs.getIndex("ftid") != -1): for i in range(0, attrs.getLength()): if (attrs.getQName(i) != 'ftid'): xmlWriter.writeStartElement('record') xmlWriter.writeAttribute( 'ftid', attrs.getValue(attrs.getIndex("ftid"))) xmlWriter.writeAttribute('name', attrs.getQName(i)) xmlWriter.writeAttribute('value', attrs.getValue(i)) xmlWriter.writeEndElement()
def generateReport(self): writer = StringWriter() self._ve.evaluate(self._context, writer, self._template.getName(), unicode(self._template.getSource())) evaluatedTemplate = writer.toString() return evaluatedTemplate