def searchFile2(event): label3.text = "Isi pesan terenkripsi" label4.text = "Isi pesan asli" myFileChooser = JFileChooser() rVal = int(myFileChooser.showOpenDialog(None)) print rVal if (rVal == JFileChooser.APPROVE_OPTION): encryptedTextFile.text = myFileChooser.getSelectedFile().getName() encryptedTextPath.text = myFileChooser.getCurrentDirectory().toString() try: myPath = encryptedTextPath.text + "/" + encryptedTextFile.text fileReaderX = FileReader(myPath) bufferedReaderX = BufferedReader(fileReaderX) inputFile = "" textFieldReadable = bufferedReaderX.readLine() while (textFieldReadable != None): inputFile += textFieldReadable inputFile += "\n" textFieldReadable = bufferedReaderX.readLine() pesanAsli.text = inputFile import myGUI.saveToFile as convertThis return convertThis.convertBackToInt(inputFile) except (RuntimeError, TypeError, NameError): print "eror gan"
def printToTextArea(event): try: myPath = plainTextPath.text + "/" + plainTextFile.text fileReader = FileReader(myPath) bufferedReader = BufferedReader(fileReader) inputFile = "" intToPrint = "" myString = "" textFieldReadable = bufferedReader.readLine() while (textFieldReadable != None): inputFile += textFieldReadable inputFile += "\n" textFieldReadable = bufferedReader.readLine() pesanAsli.text = inputFile thisInt = parseMyStringToInt(inputFile) elGamalPK = [int(alpha.text), int(beta.text), int(genPrima.text)] encryptedMessage = elGamal.Enkripsi(elGamalPK, thisInt) #thisInt = thisFoo(inputFile) for i in range(0, (len(thisInt) - 1)): intToPrint += encryptedMessage[i][0], myString += encryptedMessage[i][0] intToPrint += encryptedMessage[i][1] myString += encryptedMessage[i][1] if (i % 5 == 0): intToPrint += "\n" print "ini pesan:", intToPrint pesanTerenkripsi.setText("") pesanTerenkripsi.text = intToPrint except (RuntimeError, TypeError, NameError): print "eror gan"
def _read_value_from_file(file_path, model_context): """ Read a single text value from the first line in the specified file. :param file_path: the file from which to read the value :return: the text value :raises BundleAwareException if an error occurs while reading the value """ method_name = '_read_value_from_file' try: file_reader = BufferedReader(FileReader(file_path)) line = file_reader.readLine() file_reader.close() except IOException, e: if model_context.get_validation_method() == 'strict': _logger.severe('WLSDPLY-01733', file_path, e.getLocalizedMessage(), class_name=_class_name, method_name=method_name) ex = exception_helper.create_variable_exception( 'WLSDPLY-01733', file_path, e.getLocalizedMessage(), error=e) _logger.throwing(ex, class_name=_class_name, method_name=method_name) raise ex else: _logger.info('WLSDPLY-01733', file_path, e.getLocalizedMessage(), error=e, class_name=_class_name, method_name=method_name) line = ''
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 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 getPayloadContent(self, payload): if payload is None: return "" try: sb = StringBuilder() reader = BufferedReader(InputStreamReader(payload.open(), "UTF-8")) line = reader.readLine() p = re.compile('\s*<\?xml.*\?>\s*') firstLine = True while line is not None: if firstLine: if p.match(line) is not None: self.log.debug("Ignoring xml declaration") line = reader.readLine() else: sb.append(line).append("\n") line = reader.readLine() firstLine = False else: sb.append(line).append("\n") line = reader.readLine() payload.close() if sb: return sb return "" except Exception, e: return ""
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, 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 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 handle(self, httpExchange): try: method = httpExchange.getRequestMethod() requestHeaders = httpExchange.getRequestHeaders() contentType = requestHeaders.getFirst('Content-Type') if method == 'POST' and contentType == 'application/json': try: br = BufferedReader( InputStreamReader(httpExchange.getRequestBody())) if br: data = br.readLine() br.close() self.__manage(data) else: logger.error( '[contextCallback.handle] %s : No data in the Json request' % (self.__class__.__name__)) except Exception, e: logger.error('(handle) Exception- %s : stacktrace=%s' % (self.__class__.__name__, e)) logger.trace( '[contextCallback.handle] Answering 200 to the request') httpExchange.sendResponseHeaders(200, -1L) else:
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 _reader(self): stream = getResourceAsStream(self._path) reader = BufferedReader(InputStreamReader(stream, 'UTF-8')) try: yield reader finally: reader.close()
def __init__(self, reader, delimiter): ''' Constructs parser for stream @param reader: java.io.Reader @param delimiter: the separator of tokens @raise Exception: if reader of delimiter is None ''' if reader is None: raise Exception, "Reader can not be None" if delimiter is None: raise Exception, "Delimiter can not be None" #try to make reader as BufferedReader if isinstance(reader, BufferedReader): self.__reader = reader else: self.__reader = BufferedReader(reader) self.__escapeSeq = Parser.DEFAULT_ESCAPE_SYMB self.__delimiter = delimiter self.__rowToStartIndex = Parser.DEFAULT_ROW_TO_START_INDEX self.__quotesymb = Parser.DEFAULT_QUOTE_SYMB self.__isHeaderLineProcessed = 0
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 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 _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 run(self): reader = BufferedReader(InputStreamReader(self.getStream())) try: line = reader.readLine() while line: self.output += line line = reader.readLine() finally: reader.close()
def cluster(algorithm, filename, options=''): reader = BufferedReader(FileReader(filename)) data = Instances(reader) reader.close() cl = algorithm() cl.setOptions(options.split()) cl.buildClusterer(data) returnData = [] for instance in data.enumerateInstances(): returnData.append(cl.clusterInstance(instance)) print returnData
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 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 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 _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 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()