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 _reader(self): stream = getResourceAsStream(self._path) reader = BufferedReader(InputStreamReader(stream, 'UTF-8')) try: yield reader finally: reader.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 get(targetURL, params, username=None): paramStr = "" for aKey in params.keys(): paramStr+=aKey+"="+URLEncoder.encode(params[aKey], "UTF-8")+"&" paramStr=paramStr[:-1] url = URL(targetURL+"?"+paramStr) print url connection = url.openConnection() if username!=None: userpass = username basicAuth = "Basic " + base64.b64encode(userpass); #print basicAuth connection.setRequestProperty ("Authorization", basicAuth); connection.setRequestMethod("GET") connection.setRequestProperty("Content-Language", "en-GB") connection.setUseCaches(0) connection.setDoOutput(2) inStream= connection.getInputStream() rd= BufferedReader(InputStreamReader(inStream)) response = "" line = rd.readLine() while line != None: response +=line+"\r" line = rd.readLine() rd.close() return response
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 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 process(self, input_stream, output_stream): try: writer = OutputStreamWriter(output_stream, "UTF-8") reader = BufferedReader(InputStreamReader(input_stream, "UTF-8")) line = reader.readLine() found_quote_flag = False while line != None: # Write the line always writer.write(line) # If there is an odd number of double quotes (free text field) # then there is a newline somewhere in one of the column fields. # Raise the flag to not add any newlines until we've found the matching # line that contains an odd number of quotes if line.count('"') % 2 != 0: found_quote_flag = not found_quote_flag if not found_quote_flag: writer.write('\n') line = reader.readLine() writer.flush() writer.close() reader.close() except: traceback.print_exc(file=sys.stdout) raise
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 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 getDoc(self, url): # print "call doc on %s" % url # doc=None; # try: # newurl = URL( url ); # dbf = DocumentBuilderFactory.newInstance(); # dbf.setNamespaceAware(True) # db = dbf.newDocumentBuilder(); # doc = db.parse( newurl.openStream() ); # print doc # except Exception, e: # print 'Failed to reach a server.' # print 'Details', str(e); # # return doc; # print "getDoc called with %s" % url # Java method above no longer works. doc=None; try: responseBuilder = StringBuilder(); newurl = URL( url ); conn = newurl.openConnection(); rd = BufferedReader( InputStreamReader(conn.getInputStream()) ); line = rd.readLine(); while line != None: responseBuilder.append(line + '\n'); line = rd.readLine(); rd.close(); except Exception, e: print 'Failed to reach a server.' print 'Details', str(e); return doc;
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 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 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 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 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 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 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 delete(targetURL, params): url = URL(targetURL) connection = url.openConnection() connection.setRequestMethod("DELETE") connection.setRequestProperty("Content-Language", "en-GB") connection.setUseCaches(0) connection.setDoOutput(2) inStream= connection.getInputStream() rd= BufferedReader(InputStreamReader(inStream)) response = "" line = rd.readLine() while line != None: response +=line+"\r" line = rd.readLine() rd.close() return response
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 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 _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: _report_token_issue('WLSDPLY-01733', method_name, model_context, file_path, e.getLocalizedMessage()) line = ''
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 _parse_and_rewrite_svg_file(svg_input_path, svg_output_path): write_str = "" file_reader = FileReader(svg_input_path) buffered_reader = BufferedReader(file_reader) read_line = "" check = False while True: read_line = buffered_reader.readLine() if read_line is None: break if "viewBox" in read_line: view_box_content = _get_viewbox_content(read_line) view_box_values = _get_viewbox_values(view_box_content) if view_box_values[0] != 0: view_box_values[2] = abs(view_box_values[2]) + abs( view_box_values[0]) view_box_values[0] = 0 if view_box_values[1] != 0: view_box_values[3] = abs(view_box_values[3]) + abs( view_box_values[1]) view_box_values[1] = 0 read_line = re.sub(r"viewBox=\"[\-|0-9| ]+\"", "", read_line, 1) read_line = re.sub(r"width=\"[0-9]+\"", "width=\"" + str(view_box_values[2]) + "\"", read_line, 1) read_line = re.sub(r"height=\"[0-9]+\"", "height=\"" + str(view_box_values[3]) + "\"", read_line, 1) check = True if "g id=\"ID" in read_line and not check: if "transform=" in read_line: _log.info(read_line) read_line = read_line[0:read_line.find("transform")] + ">" check = True write_str += read_line + "\n" buffered_reader.close() file_reader.close() file_writer = PrintWriter(svg_output_path) file_writer.print(write_str) file_writer.close()
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
def run(): print base = raw_input('Base file, eg. config/templates/wl_as_template.properties? ') env = raw_input('Environment, eg. local? ') print new_cfg = 'config/' + env + '/new_config.properties' input = BufferedReader(FileReader(base)) output = BufferedWriter(FileWriter(new_cfg)) output.write('base=' + base + '\n\n') line = input.readLine() while line is not None: if re.match('.*\?', line): output.write(line) output.newLine() line = input.readLine() input.close() output.close() log.info('new configuration file written to: ' + new_cfg)
def getDoc(self, url): doc=None; try: responseBuilder = StringBuilder(); newurl = URL( url ); conn = newurl.openConnection(); rd = BufferedReader( InputStreamReader(conn.getInputStream()) ); line = rd.readLine(); while line != None: responseBuilder.append(line + '\n'); line = rd.readLine(); rd.close(); except Exception, e: print 'Failed to reach a server.' print 'Details', str(e); return doc;
def post(targetURL, params, contentType="text/xml", username=None): #paramStr = params["data"] paramStr = "" for aKey in params.keys(): paramStr+=aKey+"="+URLEncoder.encode(params[aKey], "UTF-8")+"&" paramStr=paramStr[:-1] url = URL(targetURL) print targetURL print paramStr print contentType url = URL(targetURL+"?"+paramStr) print url connection = url.openConnection() if username!=None: userpass = username basicAuth = "Basic " + base64.b64encode(userpass); connection.setRequestProperty ("Authorization", basicAuth); connection.setRequestMethod("POST") if contentType != None: connection.setRequestProperty("Content-Type", contentType) connection.setRequestProperty("Content-Length", str(len(paramStr))) connection.setRequestProperty("Content-Language", "en-GB") connection.setUseCaches(0) connection.setDoInput(1) connection.setDoOutput(2) #wr= DataOutputStream(connection.getOutputStream()) #wr.writeBytes(paramStr) #wr.flush() #wr.close() inStream= connection.getInputStream() rd= BufferedReader(InputStreamReader(inStream)) response = "" line = rd.readLine() while line != None: response +=line+"\r" line = rd.readLine() rd.close() return response
def __init__(self): sessionDuration = grinder.properties.getInt( "player.sessionDurationInSeconds", 0) self.password = grinder.properties.getProperty("player.password") players_url = grinder.properties.getProperty("player.source-url") passwordHash = URLEncoder.encode( grinder.properties.getProperty("player.passwordHash"), "UTF-8") workers = grinder.properties.getInt("grinder.processes", 0) threads = grinder.properties.getInt("grinder.threads", 0) workerIndex = grinder.processNumber - grinder.firstProcessNumber threadsPerAgent = workers * threads agentIndex = grinder.agentNumber start = agentIndex * threadsPerAgent + workerIndex * threads if isLogEnabled: log("Worker will handle %s players starting from %d" % (threads, start)) playerRange = (start, threads) self.workerPlayers = [] params = "passwordHash=%s&minimumBalance=%s&startRow=%s&limit=%s" % ( passwordHash, str(sessionDuration / 5), str( playerRange[0]), str(threads)) urlStr = players_url + "?" + params try: data = StringBuffer() url = URL(urlStr) conn = url.openConnection() rd = BufferedReader(InputStreamReader(conn.getInputStream())) line = rd.readLine() while line is not None: data.append(line) line = rd.readLine() rd.close() if isLogEnabled: log(data.toString()) message = JSONValue.parse(str(String(data.toString(), "UTF-8"))) for entry in message: self.workerPlayers.append((entry.get(0), entry.get(1))) except Exception: raise Exception("Couldn't fetch players from %s: %s" % (urlStr, traceback.format_exc())) if isLogEnabled: log(str(self.workerPlayers))
def read_from_jar(relative_path): ''' reads a file from within a jar. returns the file content ''' from java.lang import ClassLoader from java.io import InputStreamReader, BufferedReader loader = ClassLoader.getSystemClassLoader() stream = loader.getResourceAsStream(relative_path) reader = BufferedReader(InputStreamReader(stream)) line = reader.readLine() buf = '' while line is not None: buf += line line = reader.readLine() reader.close() stream.close() return buf
def prepare_security_token_request(params): """Construct the request body to acquire security token from STS endpoint""" #logger = SamlTokenProvider.logger(SamlTokenProvider.prepare_security_token_request.__name__) #logger.debug_secrets('params: %s', params) ### logger.info("params: %s" % params) # Modified for reading SAML.xml from JAR file loader = ClassLoader.getSystemClassLoader() stream = loader.getResourceAsStream("office365/runtime/auth/SAML.xml") reader = InputStreamReader(stream) bufferedReader = BufferedReader(reader) data = "" line = bufferedReader.readLine() while (line): data += line line = bufferedReader.readLine() bufferedReader.close() for key in params: data = data.replace('[' + key + ']', params[key]) return data
def _parse_and_rewrite_svg_file(svg_input_path, svg_output_path): write_str = "" file_reader = FileReader(svg_input_path) buffered_reader = BufferedReader(file_reader) read_line = "" check = False while True: read_line = buffered_reader.readLine() if read_line is None: break if "viewBox" in read_line: view_box_content = _get_viewbox_content(read_line) view_box_values = _get_viewbox_values(view_box_content) if view_box_values[0] != 0: view_box_values[2] = abs(view_box_values[2]) + abs(view_box_values[0]) view_box_values[0] = 0 if view_box_values[1] != 0: view_box_values[3] = abs(view_box_values[3]) + abs(view_box_values[1]) view_box_values[1] = 0 read_line = re.sub(r"viewBox=\"[\-|0-9| ]+\"", "", read_line, 1) read_line = re.sub(r"width=\"[0-9]+\"", "width=\""+ str(view_box_values[2]) + "\"", read_line, 1) read_line = re.sub(r"height=\"[0-9]+\"", "height=\""+ str(view_box_values[3]) + "\"", read_line, 1) check = True if "g id=\"ID" in read_line and not check: if "transform=" in read_line: _log.info(read_line) read_line = read_line[0:read_line.find("transform")] + ">" check = True write_str += read_line + "\n" buffered_reader.close() file_reader.close() file_writer = PrintWriter(svg_output_path) file_writer.print(write_str) file_writer.close()
def __init__(self): sessionDuration = grinder.properties.getInt("player.sessionDurationInSeconds", 0) self.password = grinder.properties.getProperty("player.password") players_url = grinder.properties.getProperty("player.source-url") passwordHash = URLEncoder.encode(grinder.properties.getProperty("player.passwordHash"), "UTF-8") workers = grinder.properties.getInt("grinder.processes", 0) threads = grinder.properties.getInt("grinder.threads", 0) workerIndex = grinder.processNumber - grinder.firstProcessNumber threadsPerAgent = workers * threads agentIndex = grinder.agentNumber start = agentIndex * threadsPerAgent + workerIndex * threads if isLogEnabled: log("Worker will handle %s players starting from %d" % (threads, start)) playerRange = (start, threads) self.workerPlayers = [] params = "passwordHash=%s&minimumBalance=%s&startRow=%s&limit=%s" % ( passwordHash, str(sessionDuration / 5), str(playerRange[0]), str(threads), ) urlStr = players_url + "?" + params try: data = StringBuffer() url = URL(urlStr) conn = url.openConnection() rd = BufferedReader(InputStreamReader(conn.getInputStream())) line = rd.readLine() while line is not None: data.append(line) line = rd.readLine() rd.close() if isLogEnabled: log(data.toString()) message = JSONValue.parse(str(String(data.toString(), "UTF-8"))) for entry in message: self.workerPlayers.append((entry.get(0), entry.get(1))) except Exception: raise Exception("Couldn't fetch players from %s: %s" % (urlStr, traceback.format_exc())) if isLogEnabled: log(str(self.workerPlayers))
def _parse_and_rewrite_svg_file(svg_input_path, svg_output_path): write_str = "" file_reader = FileReader(svg_input_path) buffered_reader = BufferedReader(file_reader) read_line = "" while True: read_line = buffered_reader.readLine() if read_line is None: break if "viewBox" in read_line: view_box_content = _get_viewbox_content(read_line) view_box_values = _get_viewbox_values(view_box_content) if view_box_values[0] != 0: view_box_values[2] += view_box_values[0] view_box_values[0] = 0 if view_box_values[1] != 0: view_box_values[3] += view_box_values[1] view_box_values[1] = 0 new_view_box = str(view_box_values[0]) + " " + str(view_box_values[1]) + " " + \ str(view_box_values[2]) + " " + str(view_box_values[3]) read_line = re.sub(r"viewBox=\"[\-|0-9| ]+\"", "viewBox=\"" + new_view_box + "\"", read_line, 1) read_line = re.sub(r"width=\"[0-9]+\"", "width=\""+ str(view_box_values[2]) + "\"", read_line, 1) read_line = re.sub(r"height=\"[0-9]+\"", "height=\""+ str(view_box_values[3]) + "\"", read_line, 1) write_str += read_line + "\n" buffered_reader.close() file_reader.close() file_writer = PrintWriter(svg_output_path) file_writer.print(write_str) file_writer.close()
def readLocations(self): self.locations = {} self.order = [] if Environment.getExternalStorageState() != Environment.MEDIA_MOUNTED: return storageDir = Environment.getExternalStoragePublicDirectory( Environment.DIRECTORY_DOWNLOADS) subdir = File(storageDir, "WeatherForecast") if subdir.exists(): f = File(subdir, "locations.txt") try: stream = BufferedReader(FileReader(f)) while True: line = stream.readLine() if line == None: break spec = line.trim() pieces = spec.split("/") if len(pieces) < 3: continue place = pieces[len(pieces) - 1] self.locations[place] = spec self.order.add(place) stream.close() except FileNotFoundException: pass
def _get_response_msg(self, url_conn): """Функция, возвращающая тело ответа""" get_input_stream = url_conn.responseCode < HttpURLConnection.HTTP_BAD_REQUEST if get_input_stream: in_stream = url_conn.getInputStream() else: in_stream = url_conn.getErrorStream() response = "" if in_stream: in_stream = BufferedReader(InputStreamReader(in_stream, 'utf-8')) response = StringBuffer() while True: inputLine = in_stream.readLine() if not inputLine: break response.append(inputLine) in_stream.close() return unicode(response)
## read in csv csvReader = BufferedReader(FileReader(input_path)) header = csvReader.readLine().split(",") row = csvReader.readLine() spots = SpotCollection() while row is not None: data = {col: val for col, val in zip(header, row.split(","))} spot = Spot(float(data['j']), float(data['i']), 0, math.sqrt(float(data['area']) / math.pi), 1, data['cell']) spots.add(spot, int(data['frame'])) row = csvReader.readLine() csvReader.close() model.setSpots(spots, False) # Set up dummy detector settings.detectorFactory = ManualDetectorFactory() settings.detectorSettings = {} settings.detectorSettings['RADIUS'] = 1. # Configure tracker - We want to allow merges and fusions settings.trackerFactory = SparseLAPTrackerFactory() settings.trackerSettings = LAPUtils.getDefaultLAPSettingsMap() # format for json conversion tracker_settings = tracker_settings.replace('{', '{"').replace(':', '":').replace(
def doInBackground(self): """Download errors data """ #Download and parse errors self.tool = self.app.downloadingTool self.checks = self.app.downloadingChecks #Initialize progress property. progress = 0 self.super__setProgress(progress) progress = 1 self.super__setProgress(progress) #debug offline = False writeToFile = False if offline or self.tool.isLocal: if offline and not self.tool.isLocal: fileName = File.separator.join([self.app.SCRIPTDIR, "%s_errors.gfs" % self.tool.name]) else: fileName = self.tool.fileName print "\n read errors from file: %s" % fileName inFile = open(fileName, "r") self.app.errorsData = inFile.read() inFile.close() else: try: print "\n url: ", self.app.downloadingUrl url = URL(self.app.downloadingUrl) uc = url.openConnection() ins = uc.getInputStream() inb = BufferedReader(InputStreamReader(ins, StandardCharsets.UTF_8)) builder = StringBuilder() line = inb.readLine() while line is not None and not self.isCancelled(): builder.append(line) builder.append(System.getProperty("line.separator")) line = inb.readLine() if self.isCancelled(): if inb is not None: inb.close() if uc is not None: uc.disconnect() return inb.close() uc.disconnect() self.app.errorsData = builder.toString() except (UnknownHostException, FileNotFoundException, IOException, SocketException): msg = self.app.strings.getString("connection_not_working") self.app.downloadAndReadDlg.progressBar.setIndeterminate(False) self.app.downloadAndReadDlg.dispose() JOptionPane.showMessageDialog(Main.parent, msg) self.cancel(True) return if writeToFile: f = open(File.separator.join([self.app.SCRIPTDIR, "%s_errors.gfs" % self.tool.name]), "w") f.write(self.app.errorsData.encode("utf-8")) f.close() print "errors file saved", File.separator.join([self.app.SCRIPTDIR, "%s_errors.gfs" % self.tool.name])
def doInBackground(self): """Download errors data """ #Download and parse errors self.tool = self.app.downloadingTool self.checks = self.app.downloadingChecks #Initialize progress property. progress = 0 self.super__setProgress(progress) progress = 1 self.super__setProgress(progress) #debug offline = False writeToFile = False if offline or self.tool.isLocal: if offline and not self.tool.isLocal: fileName = File.separator.join([self.app.SCRIPTDIR, "%s_errors.gfs" % self.tool.name]) else: fileName = self.tool.fileName print "\n read errors from file: %s" % fileName inFile = open(fileName, "r") self.app.errorsData = inFile.read() inFile.close() else: try: print "\n url: ", self.app.downloadingUrl url = URL(self.app.downloadingUrl) uc = url.openConnection() ins = uc.getInputStream() inb = BufferedReader(InputStreamReader(ins, Utils.UTF_8)) builder = StringBuilder() line = inb.readLine() while line is not None and not self.isCancelled(): builder.append(line) builder.append(System.getProperty("line.separator")) line = inb.readLine() if self.isCancelled(): if inb is not None: inb.close() if uc is not None: uc.disconnect() return inb.close() uc.disconnect() self.app.errorsData = builder.toString() except (UnknownHostException, FileNotFoundException, IOException, SocketException): msg = self.app.strings.getString("connection_not_working") self.app.downloadAndReadDlg.progressBar.setIndeterminate(False) self.app.downloadAndReadDlg.dispose() JOptionPane.showMessageDialog(Main.parent, msg) self.cancel(True) return if writeToFile: f = open(File.separator.join([self.app.SCRIPTDIR, "%s_errors.gfs" % self.tool.name]), "w") f.write(self.app.errorsData.encode("utf-8")) f.close() print "errors file saved", File.separator.join([self.app.SCRIPTDIR, "%s_errors.gfs" % self.tool.name])
class Parser: ''' CSV content parser ''' DEFAULT_QUOTE_SYMB = '"' DEFAULT_ESCAPE_SYMB = "\\" DEFAULT_ROW_TO_START_INDEX = 0 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 setQuoteSymbol(self, symbol): ''' Set the quote symbol @param symbol: the quote symbol ''' self.__quotesymb = symbol or self.DEFAULT_QUOTE_SYMB def getQuoteSymbol(self): return self.__quotesymb def setRowToStartIndex(self, index): ''' Set the index, that indicates row number from witch parsing will be started. Fist row has index 0 and so on. @param index: index of row to start ''' self.__rowToStartIndex = index or Parser.DEFAULT_ROW_TO_START_INDEX def getRowToStartIndex(self): return self.__rowToStartIndex def parseNext(self): ''' Parse next line @return: tokens of parsed line ''' line = self.__readNextLine() tokens = None if line is not None: tokens = line.strip() and self.__parseLine(line) or [] return tokens def __readNextLine(self): if not self.__isHeaderLineProcessed and self.__rowToStartIndex: for i in range(0, self.__rowToStartIndex): self.__reader.readLine() self.__isHeaderLineProcessed = 1 return self.__reader.readLine() def __isEscaped(self, charIndex, line): 'int, str -> bool' escapeSymbIdx = charIndex - len(self.__escapeSeq) return (escapeSymbIdx > -1 and line[escapeSymbIdx: charIndex] == self.__escapeSeq) def __parseLine(self, line): tokensOnThisLine = [] sb = '' inQuotes = 0 while 1: if inQuotes: sb += "\n" line = self.__readNextLine() if line is None: break i = 0 while i < len(line): c = line[i] if c == self.__quotesymb: # escape quote if self.__isEscaped(i, line): sb = sb[:-len(self.__escapeSeq)] sb += c # treat two quote symbols as one elif (len(line) > (i+1) and line[i+1] == self.__quotesymb): sb += c i+=1 else: inQuotes = not inQuotes elif c == self.__delimiter and not inQuotes: # escape delimiter if self.__isEscaped(i, line): sb = sb[:-len(self.__escapeSeq)] sb += c else: #save token and process next tokensOnThisLine.append(sb) sb = '' else: sb += c i+=1 #do-while condition if not inQuotes: break tokensOnThisLine.append(sb) return tokensOnThisLine def close(self): ''' Close reader ''' self.__reader.close()
from java.io import BufferedReader as BR from java.io import FileReader as FR from java.io import BufferedWriter as BW from java.io import FileWriter as FW from java.io import File as F file_r = F("file3.txt") file_w = F("file4.txt") br = BR(FR(file_r)) bw = BW(FW(file_w)) while True: line = br.readLine() if line != None: bw.write(line + "\n") else: break br.close() bw.close()