def process(self): #We'll return a list with 1 JsonSimple object jsonList = [] data = None reader = None inStream = None document = None # Run the XML through our parser try: inStream = FileInputStream(File(self.file)) reader = InputStreamReader(inStream, "UTF-8") document = self.saxReader.read(reader) # Parse fails except: raise # Close our file access objects finally: if reader is not None: reader.close() if inStream is not None: inStream.close() # Now go looking for all our data data = JsonObject() data.put("workflow_source", "XML Alert") # Default self.__mapXpathToFields(document, self.map, data) if data is None: return None jsonList.append(JsonSimple(data)) return jsonList
def process(self): '''Read the XML file and map xpath items to metadata Return a list with 1 JsonSimple object (at most) ''' jsonList = [] data = None reader = None inStream = None document = None # Run the XML through our parser try: inStream = FileInputStream(File(self.file)) reader = InputStreamReader(inStream, "UTF-8") document = self.saxReader.read(reader) # Parse fails except: raise # Close our file access objects finally: if reader is not None: reader.close() if inStream is not None: inStream.close() # Now go looking for all our data data = self.getNewJsonObject() self.__mapXpathToFields(document, self.map, data) if data is None: return None jsonList.append(JsonSimple(data)) return jsonList
def __readWorkflowMetadata(self, oid): object = self.services.getStorage().getObject(oid) sourceId = object.getSourceId() payload = object.getPayload("workflow.metadata") payloadReader = InputStreamReader(payload.open(), "UTF-8") metadata = JsonConfigHelper(payloadReader) payloadReader.close() payload.close() object.close() return metadata
def __readManifest(self, oid): object = Services.getStorage().getObject(oid) sourceId = object.getSourceId() payload = object.getPayload(sourceId) payloadReader = InputStreamReader(payload.open(), "UTF-8") manifest = JsonConfigHelper(payloadReader) payloadReader.close() payload.close() object.close() return manifest
def _create_file_from_stream(template_stream, template_hash, output_file): template_reader = BufferedReader(InputStreamReader(template_stream)) file_writer = open(output_file.getPath(), "w") block_key = None block_lines = [] line = '' while line is not None: line = template_reader.readLine() if line is not None: block_start_key = _get_block_start_key(line) # if inside a block, collect lines until end key is found, then process the block. if block_key is not None: block_end_key = _get_block_end_key(line) if block_end_key == block_key: _process_block(block_key, block_lines, template_hash, file_writer) block_key = None else: block_lines.append(line) # if this is a block start, begin collecting block lines elif block_start_key is not None: block_key = block_start_key block_lines = [] # otherwise, substitute and write the line else: line = _substitute_line(line, template_hash) file_writer.write(line + "\n") file_writer.close()
def perform_request(url, method='GET', data=None, timeout=None): try: connection = URL(url).openConnection() connection.setRequestProperty("Connection", "close") connection.setRequestProperty("User-Agent", "PyScanClient") connection.setRequestProperty("Accept", "text/xml") connection.setDoOutput(True) connection.setRequestMethod(method) if data is not None: data = java.lang.String(data).getBytes() connection.setRequestProperty("Content-Type", "text/xml") connection.setRequestProperty("Content-Length", str(len(data))) out = connection.getOutputStream() out.write(data) out.close() inp = BufferedReader(InputStreamReader( connection.getInputStream())) result = java.lang.StringBuilder() while True: line = inp.readLine() if line is None: break result.append(line).append('\n') inp.close() result = result.toString() connection.disconnect() return result except java.lang.Exception as e: raise Exception("%s: %s" % (url, str(e)))
def getRequestBody(self): """ Return the body of the request message. Note that this is not very suitable for streaming or large message bodies at this point, since the entire message is read into a single string before it is returned to the client. @return: Body of the request. @rtype: string """ if self.__native_req: buffered_reader = BufferedReader( InputStreamReader(self.__native_req.getRequestBody())) lines = [] while True: line = buffered_reader.readLine() if not line: break lines.append(line) return '\n'.join(lines) else: return None
def process(self, input) : try : reader = InputStreamReader(input) bufferedReader = BufferedReader(reader) self.__line = bufferedReader.readLine() except : print "Exception in Reader:" print '-' * 60 traceback.print_exc(file=sys.stdout) print '-' * 60 raise finally : if bufferedReader is not None : bufferedReader.close() if reader is not None : reader.close()
def readWebPage( webpageURL ): webpageURL = webpageURL.strip() log(VERBOSE_, "readWebpage webpageURL=" + webpageURL) url = URL(webpageURL) conn = url.openConnection() conn.setConnectTimeout(30000) conn.setReadTimeout(10000) conn.connect() responseCode = conn.getResponseCode() cookie = conn.getHeaderField("Set-Cookie") cookiePath = None pathDiscr = " Path=" if cookie and cookie.find(pathDiscr) > 0: cookiePath = cookie[cookie.index(pathDiscr) + len(pathDiscr):] respLines = [] if responseCode >= 400: log(ERROR_, "HTTP ERROR " + `responseCode` + ": " + `conn.getResponseMessage()`) else: log(VERBOSE_, "WebPageResponse status=" + `responseCode` + " reason=" + `conn.getResponseMessage()`) #log(DEBUG_,"WebPageResponse resp="+`resp` ) reader = BufferedReader(InputStreamReader(conn.getInputStream())) inputLine = reader.readLine() while inputLine is not None: respLines.append(inputLine) inputLine = reader.readLine() reader.close() return respLines, cookiePath
def _reader(self): stream = getResourceAsStream(self._path) reader = BufferedReader(InputStreamReader(stream, 'UTF-8')) try: yield reader finally: reader.close()
def http_get(self, url): '''Return java.lang.String JSON Input: java.lang.String URL ''' start_timer = System.currentTimeMillis() try: url = URL(url) urlconnect = url.openConnection() br = BufferedReader( InputStreamReader( urlconnect.getInputStream(), "UTF-8" ) ) s = br.readLine() br.close() except MalformedURLException() as ex: cumulus_logger.error(str(ex)) MessageBox.showError(str(ex), "Exception") raise except IOException as ex: cumulus_logger.error(str(ex)) MessageBox.showError(str(ex), "Exception") raise end_timer = System.currentTimeMillis() cumulus_logger.debug( "HTTP GET (milliseconds): {}".format( (end_timer - start_timer) ) ) return s
def actionPerformed(self,actionEvent): accl = self.linac_setup_controller.linac_wizard_document.getAccl() cav_name_node_dict = self.linac_setup_controller.getCavNameNodeDict(accl) fc = JFileChooser(constants_lib.const_path_dict["LINAC_WIZARD_FILES_DIR_PATH"]) fc.setDialogTitle("Read Cavities Amp. and Phases from external file ...") fc.setApproveButtonText("Open") returnVal = fc.showOpenDialog(self.linac_setup_controller.linac_wizard_document.linac_wizard_window.frame) if(returnVal == JFileChooser.APPROVE_OPTION): fl_in = fc.getSelectedFile() file_name = fl_in.getName() buff_in = BufferedReader(InputStreamReader(FileInputStream(fl_in))) line = buff_in.readLine() while( line != null): #print "debug line=",line res_arr = line.split() if(len(res_arr) == 3): cav_name = res_arr[0] amp = float(res_arr[1]) phase = float(res_arr[2]) if(cav_name_node_dict.has_key(cav_name)): #print "debug cav=",cav_name," amp=",amp," phase",phase cav = cav_name_node_dict[cav_name] cav.setDfltCavAmp(amp) cav.setDfltCavPhase(phase) line = buff_in.readLine()
def __getTableOfContents(self, package, oid): try: # get the package manifest object = Services.getStorage().getObject(package) sourceId = object.getSourceId() payload = object.getPayload(sourceId) payloadReader = InputStreamReader(payload.open(), "UTF-8") manifest = JsonConfigHelper(payloadReader) payloadReader.close() payload.close() object.close() # generate toc result = self.__toc(manifest.getJsonMap("manifest/" + oid.replace("blank-", "node-") + "/children")) except Exception, e: print "Failed to load manifest '%s': '%s'" % (package, str(e)) result = '<div class="error">Failed to generate table of contents!</div><pre>%s</pre>' % str(e)
def test_calc_blob(self): cursor = tBlobCursor(self.context) cursor.deleteAll() cursor.insert() cursor.get(1) self.assertEquals(1, cursor.id) self.assertEquals(None, cursor.dat) cursor.calcdat() self.assertTrue(cursor.dat and cursor.dat.isNull()) os = cursor.dat.getOutStream() osw = OutputStreamWriter(os, 'utf-8') try: osw.append('blob field') finally: osw.close() cursor.update() cursor.clear() cursor.get(1) cursor.calcdat() bf = BufferedReader( InputStreamReader(cursor.dat.getInStream(), 'utf-8')) self.assertEquals('blob field', bf.readLine()) bf.close() cursor.clear() cursor.calcdat() os = cursor.dat.getOutStream() osw = OutputStreamWriter(os, 'utf-8') try: osw.append('blob field 2!') finally: osw.close() cursor.insert() cursor.clear() cursor.get(2) cursor.calcdat() bf = BufferedReader( InputStreamReader(cursor.dat.getInStream(), 'utf-8')) self.assertEquals('blob field 2!', bf.readLine()) bf.close()
def create_file(resource_path, template_hash, output_file, exception_type): """ Read the template from the resource stream, perform any substitutions, and write it to the output file. :param resource_path: the resource path of the source template :param template_hash: a dictionary of substitution values :param output_file: the file to write :param exception_type: the type of exception to throw if needed """ _method_name = 'create_file' file_writer = open(output_file.getPath(), "w") template_stream = FileUtils.getResourceAsStream(resource_path) if template_stream is None: ex = exception_helper.create_exception(exception_type, 'WLSDPLY-01661', resource_path) __logger.throwing(ex, class_name=__class_name, method_name=_method_name) raise ex template_reader = BufferedReader( InputStreamReader(FileUtils.getResourceAsStream(resource_path))) current_block_key = None block_lines = [] more = True while more: line = template_reader.readLine() if line is not None: block_start_key = _get_block_start_key(line) block_end_key = _get_block_end_key(line) # if this is a nested block start, continue and add without substitution if (block_start_key is not None) and (current_block_key is None): current_block_key = block_start_key block_lines = [] # if this is a nested block end, continue and add without substitution elif (block_end_key == current_block_key) and (current_block_key is not None): _write_block(current_block_key, block_lines, template_hash, file_writer) current_block_key = None else: line = _substitute_line(line, template_hash) if current_block_key is not None: block_lines.append(line) else: file_writer.write(line + "\n") else: more = False file_writer.close()
def archive_read(archive, path): from java.lang import ClassLoader from java.io import InputStreamReader, BufferedReader # --- make sure this works from within .jar files and such stream = ClassLoader.getSystemResourceAsStream(path) reader = BufferedReader(InputStreamReader(stream)) archive.addAll(reader)
def __modify(self): print "Set active package..." oid = self.vc("formData").get("oid") try: object = Services.getStorage().getObject(oid) sourceId = object.getSourceId() payload = object.getPayload(sourceId) payloadReader = InputStreamReader(payload.open(), "UTF-8") manifest = JsonConfigHelper(payloadReader) payloadReader.close() payload.close() object.close() self.vc("sessionState").set("package/active", manifest) self.vc("sessionState").set("package/active/id", oid) self.vc("sessionState").set("package/active/pid", sourceId) except StorageException, e: self.vc("response").setStatus(500) return '{ error: %s }' % str(e)
def run(self): reader = BufferedReader(InputStreamReader(self.getStream())) try: line = reader.readLine() while line: self.output += line line = reader.readLine() finally: reader.close()
def process(self, input) : try: reader = InputStreamReader(input) bufferedReader = BufferedReader(reader) line = bufferedReader.readLine() self.__line = line except : print("Exception in Reader:") print('-' * 60) #traceback.print_exc(file=sys.stdout) print('-' * 60) raise finally : if bufferedReader is not None : bufferedReader.close() if reader is not None : reader.close()
def _read_from_stream(self, stream): reader = BufferedReader( InputStreamReader(StreamGobbler(stream), self._encoding)) result = '' line = reader.readLine() while line is not None: result += line + '\n' line = reader.readLine() return result
def parseFileContent(self, bytes): byteInputStream = ByteArrayInputStream(bytes) reader = InputStreamReader(byteInputStream, self.encoding) parser = csvParser.Parser(reader, self.delimiter) parser.setQuoteSymbol(self.quoteSymbol) parser.setRowToStartIndex(self.rowToStartIndex) return csvParser.Processor().processByParser(parser).getRows()
def readFileSAX(filename): try: FIS = FileInputStream(File(filename)) ISR = InputStreamReader(FIS, ENCODING) src = InputSource(ISR, encoding=ENCODING) factory = SAXParserFactory.newInstance() factory.setValidating(1) parser = factory.newSAXParser() parser.parse(src, SAXhandler()) except: print '\nError: %s\nvalue: %s' % sys.exc_info()[:2]
def readInputStream(inputStream): reader = BufferedReader(InputStreamReader(inputStream)) builder = StringBuilder() line = None while True: line = reader.readLine() if line is None: break builder.append(line) builder.append(System.getProperty("line.separator")) return builder.toString()
def readInputStreamToString(inStream): if (inStream == None): return "" rd = BufferedReader(InputStreamReader(inStream)) response = "" line = rd.readLine() while line != None: response += line + "\r" line = rd.readLine() rd.close() return response
def Exec(): from java.lang import Runtime from java.io import BufferedReader from java.io import InputStreamReader process = Runtime.getRuntime().exec(cmd); inp = BufferedReader(InputStreamReader(process.getInputStream(),"euc-kr")) out = "" line = inp.readLine() while line: out = out + line line = inp.readLine()
def __init__(self): print "formData: %s" % formData self.__oid = formData.get("oid") result = None try: # get the package manifest object = Services.getStorage().getObject(self.__oid) sourceId = object.getSourceId() payload = object.getPayload(sourceId) payloadReader = InputStreamReader(payload.open(), "UTF-8") self.__manifest = JsonConfigHelper(payloadReader) payloadReader.close() payload.close() object.close() # check if we need to do processing func = formData.get("func") if func == "get-rvt-manifest": result = self.__getRvtManifest(self.getManifest()) except Exception, e: log.error("Failed to load manifest", e); result = '{ status: "error", message: "%s" }' % str(e)
def process(self, inputStream, outputStream): try: self.total = 0 reader = InputStreamReader(inputStream, "UTF-8") bufferedReader = BufferedReader(reader) writer = OutputStreamWriter(outputStream, "UTF-8") line = bufferedReader.readLine() while line != None: ChangedRec = line.upper() writer.write(ChangedRec) writer.write('\n') a = line.split(",") for valu in a: b = valu.strip() self.total += int(b) line = bufferedReader.readLine() print("Summation of Records are %s ", self.total) writer.flush() writer.close() reader.close() bufferedReader.close() except: print "Exception in Reader:" print '-' * 60 traceback.print_exc(file=sys.stdout) print '-' * 60 raise session.transfer(flowFile, ExecuteScript.REL_FAILURE) finally: if bufferedReader is not None: bufferedReader.close() if reader is not None: reader.close()
def _readLines( self, stream, func=None ): """Read lines of stream, and either append them to return array of lines, or call func on each line. """ lines = [] func = func or lines.append # should read both stderror and stdout in separate threads... bufStream = BufferedReader( InputStreamReader( stream )) while 1: line = bufStream.readLine() if line is None: break func( line ) return lines or None
def stringify(stream): reader = BufferedReader(InputStreamReader(stream)) out = StringBuilder() while True: line = reader.readLine() if line is None: break out.append(line) out.append("\n") reader.close() return out.toString()
def run_CPython(path, script): """ Run CPython script from command prompt Parameters ---------- path : str Path to python script script : str Name of python script to run Returns ------- list Output from stdout list Output from stderr """ # Command to run cmd_str = "cmd /k cd {} & python {} & exit()".format(path, script) # Start a new process, run python script from cmd run = Runtime.getRuntime() proc = run.exec(cmd_str) # Collect output from stdout and stderr, print to console stdout_reader = BufferedReader(InputStreamReader(proc.getInputStream())) stderr_reader = BufferedReader(InputStreamReader(proc.getErrorStream())) print("stdout: \n") stdout = print_output(stdout_reader) print("stderr: \n") stderr = print_output(stderr_reader) return stdout, stderr
def http_post(self, json_string, url): '''Return java.lang.String JSON Input: java.lang.String JSON, java.lang.String URL ''' start_timer = System.currentTimeMillis() try: # Get a connection and set the request properties url = URL(url) urlconnect = url.openConnection() urlconnect.setDoOutput(True) urlconnect.setRequestMethod("POST") urlconnect.setRequestProperty("Content-Type", "application/json; UTF-8") urlconnect.setRequestProperty("Accept", "application/json") # Write to the body bw = BufferedWriter( OutputStreamWriter( urlconnect.getOutputStream() ) ) bw.write(json_string) bw.flush() bw.close() # Read the result from the POST br = BufferedReader( InputStreamReader( urlconnect.getInputStream(), "UTF-8" ) ) s = br.readLine() br.close() except MalformedURLException() as ex: cumulus_logger.error(str(ex)) MessageBox.showError(str(ex), "Exception") raise Exception(ex) except IOException as ex: cumulus_logger.error(str(ex)) MessageBox.showError(str(ex), "Exception") raise Exception(ex) end_timer = System.currentTimeMillis() cumulus_logger.debug( "HTTP GET (milliseconds): {}".format( (end_timer - start_timer) ) ) return s
def execute(cmd): runtime = Runtime.getRuntime() p = runtime.exec(cmd) p.outputStream.close() result = "" reader = BufferedReader(InputStreamReader(p.inputStream)) errorReader = BufferedReader(InputStreamReader(p.errorStream)) while True: if p.errorStream.available() > 0: print errorReader.readLine() line=reader.readLine() if line == None: break result+=line + "\n" while True: line = errorReader.readLine() if line == None: break print line p.waitFor() if p.exitValue() != 0: print result raise RuntimeError, 'execution failure' return result
def _handleConnection(self, socket): reader = BufferedReader(InputStreamReader(socket.getInputStream())) try: line = reader.readLine(); while(line is not None and not self._serverSocket.closed): self._handleMessage(line) line = reader.readLine(); except SocketException: if self._isListening: raise SocketException else: log("Ignoring socket exception during shutdown") socket.close()
def execOnLambda(nefBucket, pdbBucket, rootName, seed): stime = time.time() inKey = rootName+'.nef' outKey = rootName+'_'+str(seed)+'.pdb' energyKey = rootName+'_'+str(seed)+'.txt' inputStream = LambdaIO.getInputStream(nefBucket,inKey) inputStreamReader = InputStreamReader(inputStream) bfR = BufferedReader(inputStreamReader) star = STAR3(bfR,'star3') star.scanFile() reader = NMRStarReader(star) dihedral = reader.processNEF() energyList = dihedral.energyList mol = energyList.getMolecule() molName = mol.getName() refiner = refine.refine() refiner.molecule = mol refiner.seed = seed refiner.outDir = None refiner.eFileRoot = None refiner.eTimeStart = time.time() refiner.useDegrees = False refiner.setupEnergy(molName,eList=energyList) refiner.setForces({'repel':0.5,'dis':1.0,'dih':5}) refiner.setPars({'coarse':False,'useh':False,'dislim':refiner.disLim,'end':500,'hardSphere':0.15,'shrinkValue':0.20}) dOpt = refine.dynOptions(highFrac=0.4) refiner.anneal(dOpt) outputStr = mol.writeXYZToPDBString(0) energy = refiner.energy() ftime = time.time() etime = ftime-stime energyStr = 'REMARK ENERGY %7.2f\n' % (energy) timeStr = 'REMARK TIME START %26s END %26s ELAPSED %7.2f\n' % (time.ctime(stime), time.ctime(ftime),etime) outputStr = energyStr + timeStr + outputStr LambdaIO.putObject(pdbBucket, outKey, outputStr) energyString = refiner.getEnergyDump(0.1) LambdaIO.putObject(pdbBucket, energyKey, energyString)
def __xmlToJson(self, fileName, xmlMappings, xmlExceptions): self.log.info("Converting '{}' to JSON...", fileName) filePath = self.pBase(fileName) timestamp = time.gmtime(os.path.getmtime(filePath)) # Run the XML through our parser try: inStream = FileInputStream(File(filePath)) reader = InputStreamReader(inStream, "UTF-8") document = self.saxReader.read(reader) # Parse fails except Exception, e: ## Move the XML to the 'failed' directory shutil.move(filePath, self.pFail(fileName)) ## And write our error data to disk beside it self.writeError(fileName, e) return None
def executeExternalApplication(command): try: pb = ProcessBuilder(command) pb.redirectErrorStream(true) process = pb.start() inputStream = process.getInputStream() bufferedReader = BufferedReader(InputStreamReader(inputStream)) s = bufferedReader.readLine() while s != None: print(s) s = bufferedReader.readLine() if (bufferedReader != None): bufferedReader.close() except Exception, e: print(e) if (bufferedReader != None): bufferedReader.close()
def view_source(context, web_view, filename): from base64 import b64encode from os.path import join from pygments import highlight from pygments.formatters import HtmlFormatter from pygments.lexers import get_lexer_for_filename from java.io import BufferedReader, InputStreamReader stream = context.getAssets().open(join(ASSET_SOURCE_DIR, filename)) reader = BufferedReader(InputStreamReader(stream)) text = "\n".join(iter(reader.readLine, None)) formatter = HtmlFormatter() body = highlight(text, get_lexer_for_filename(filename), formatter) html = ("<html><head><style>{}\n{}</style></head><body>{}</body></html>". format(formatter.get_style_defs(), EXTRA_CSS, body)).encode() web_view.loadData(b64encode(html).decode(), "text/html", "base64")
def loadResource(resourceName): cl = ClassLoader.getSystemClassLoader() istream = cl.getResourceAsStream(resourceName) lines = "" if istream == None: raise Exception("Cannot find '" + resourceName + "' on classpath") else: reader = InputStreamReader(istream) breader = BufferedReader(reader) while True: line = breader.readLine() if line == None: break if lines != '': lines += '\n' lines += line breader.close() return lines
trace = "".join([str(item) + "\r\n" for item in entry.getValue()]) thread_element.setTextContent(trace + " " + str(dir(thread))) thread_element.setAttribute("id", str(thread.getId())) thread_element.setAttribute("name", thread.getName()) thread_element.setAttribute("is-interrupted", str(thread.isInterrupted())) thread_element.setAttribute("status", thread.getState().toString()) thread_element.setAttribute("request", req_map.get(thread.getId())) con = Hiber.session().connection() db_metadata = con.getMetaData() source.setAttribute("db-product-name", db_metadata.getDatabaseProductName()) source.setAttribute("db-product-version", db_metadata.getDatabaseProductVersion()) source.setAttribute("db-driver-name", db_metadata.getDriverName()) source.setAttribute("db-driver-version", db_metadata.getDriverVersion()) istream = InputStreamReader(Monad.getContext().getResource("/WEB-INF/VERSION").openStream(), "UTF-8") c = istream.read() sr = StringWriter() while c != -1: sr.write(c) c = istream.read() source.setAttribute("chellow-version", sr.toString()) source.setAttribute("transaction-isolation", str(con.getTransactionIsolation())) pstmt = con.prepareStatement("select * from pg_stat_activity") rs = pstmt.executeQuery() rs_meta = rs.getMetaData() pg_stat_activity_el = doc.createElement("pg-stat-activity") source.appendChild(pg_stat_activity_el) for i in range(1, rs_meta.getColumnCount() + 1):
elif method == 'POST': file_item = inv.getFileItem("file") file_path = file_item.getName() file_head, file_name = os.path.split(file_path) file_title, file_ext = os.path.splitext(file_name) if not file_ext == '.csv': raise UserException( "The file name should have the extension .csv, but in fact it " "has the extension '" + file_ext + "'.") idx = file_title.rfind('_') table = file_title[:idx] version = file_title[idx+1:] f = StringIO.StringIO() if sys.platform.startswith('java'): from java.io import InputStreamReader stream = InputStreamReader(file_item.getInputStream(), 'utf-8') bt = stream.read() while bt != -1: f.write(chr(bt)) bt = stream.read() else: f.writelines(file_item.f.stream) f.seek(0) def content(): sess = None try: sess = db.session() reader = iter(csv.reader(f)) reader.next()