def __init__(self, errors='strict', encoding=None,): assert encoding self.encoding = encoding self.errors = errors self.decoder = Charset.forName(self.encoding).newDecoder() self.output_buffer = CharBuffer.allocate(1024) self.buffer = ''
def getAccessToken(self, bcid, forTask): httpService = CdiUtil.bean(HttpService) http_client = httpService.getHttpsClient() http_client_params = http_client.getParams() bioID_service_url = self.ENDPOINT + "token?id=" + self.APP_IDENTIFIER + "&bcid=" + bcid + "&task=" + forTask + "&livedetection=true" encodedString = base64.b64encode( (self.APP_IDENTIFIER + ":" + self.APP_SECRET).encode('utf-8')) bioID_service_headers = {"Authorization": "Basic " + encodedString} try: http_service_response = httpService.executeGet( http_client, bioID_service_url, bioID_service_headers) http_response = http_service_response.getHttpResponse() except: print "BioID. Unable to obtain access token. Exception: ", sys.exc_info( )[1] return None try: if not httpService.isResponseStastusCodeOk(http_response): print "BioID. Unable to obtain access token. Get non 200 OK response from server:", str( http_response.getStatusLine().getStatusCode()) httpService.consume(http_response) return None response_bytes = httpService.getResponseContent(http_response) response_string = httpService.convertEntityToString( response_bytes, Charset.forName("UTF-8")) httpService.consume(http_response) return response_string finally: http_service_response.closeConnection()
def performBiometricOperation(self, token, task): httpService = CdiUtil.bean(HttpService) http_client = httpService.getHttpsClient() http_client_params = http_client.getParams() bioID_service_url = self.ENDPOINT + task + "?livedetection=true" bioID_service_headers = {"Authorization": "Bearer " + token} try: http_service_response = httpService.executeGet( http_client, bioID_service_url, bioID_service_headers) http_response = http_service_response.getHttpResponse() response_bytes = httpService.getResponseContent(http_response) response_string = httpService.convertEntityToString( response_bytes, Charset.forName("UTF-8")) json_response = JSONObject(response_string) httpService.consume(http_response) if json_response.get("Success") == True: return True else: print "BioID. Reason for failure : %s " % json_response.get( "Error") return False except: print "BioID. failed to invoke %s API: %s" % (task, sys.exc_info()[1]) return None finally: http_service_response.closeConnection()
def _java_factory(encoding): encoding = python_to_java.get(encoding, encoding) supported = False try: supported = Charset.isSupported(encoding) except IllegalCharsetNameException: pass if not supported: return None, set() charset = Charset.forName( encoding ) # FIXME should we return this canonical name? could be best... TBD entry = codecs.CodecInfo(name=encoding, encode=Codec(encoding).encode, decode=Codec(encoding).decode, incrementalencoder=partial(IncrementalEncoder, encoding=encoding), incrementaldecoder=partial(IncrementalDecoder, encoding=encoding), streamreader=partial(StreamReader, encoding=encoding), streamwriter=partial(StreamWriter, encoding=encoding)) return entry, charset.aliases()
def getGeolocation(self, identity): session_attributes = identity.getSessionId().getSessionAttributes() if session_attributes.containsKey("remote_ip"): remote_ip = session_attributes.get("remote_ip") if StringHelper.isNotEmpty(remote_ip): httpService = CdiUtil.bean(HttpService) http_client = httpService.getHttpsClient() http_client_params = http_client.getParams() http_client_params.setIntParameter( CoreConnectionPNames.CONNECTION_TIMEOUT, 4 * 1000) geolocation_service_url = "http://ip-api.com/json/%s?fields=country,city,status,message" % remote_ip geolocation_service_headers = {"Accept": "application/json"} try: http_service_response = httpService.executeGet( http_client, geolocation_service_url, geolocation_service_headers) http_response = http_service_response.getHttpResponse() except: print "Casa. Determine remote location. Exception: ", sys.exc_info( )[1] return None try: if not httpService.isResponseStastusCodeOk(http_response): print "Casa. Determine remote location. Get non 200 OK response from server:", str( http_response.getStatusLine().getStatusCode()) httpService.consume(http_response) return None response_bytes = httpService.getResponseContent( http_response) response_string = httpService.convertEntityToString( response_bytes, Charset.forName("UTF-8")) httpService.consume(http_response) finally: http_service_response.closeConnection() if response_string == None: print "Casa. Determine remote location. Get empty response from location server" return None response = json.loads(response_string) if not StringHelper.equalsIgnoreCase(response['status'], "success"): print "Casa. Determine remote location. Get response with status: '%s'" % response[ 'status'] return None return response return None
def build_dbf_datasource(filepath, charset='UTF-8'): ''' -> org.saig.core.model.data.dao.dbf.DBFRecordDataSource''' from java.nio.charset import Charset from org.saig.core.model.data.dao.dbf import DBFRecordDataSource from org.saig.core.model.data import TableFactory import os selectedCharset = Charset.forName(charset) datasource = DBFRecordDataSource(filepath, None, selectedCharset) name = os.path.basename(filepath) # el nombre del archivo, sin la ruta name = os.path.splitext(name)[0] # el nombre del archivo, sin la extension datasource.setName(name) return datasource
def decode(self, input, errors='strict', final=True): error_function = codecs.lookup_error(errors) input_buffer = ByteBuffer.wrap(array('b', input)) decoder = Charset.forName(self.encoding).newDecoder() output_buffer = CharBuffer.allocate(min(max(int(len(input) / 2), 256), 1024)) builder = StringBuilder(int(decoder.averageCharsPerByte() * len(input))) while True: result = decoder.decode(input_buffer, output_buffer, False) pos = output_buffer.position() output_buffer.rewind() builder.append(output_buffer.subSequence(0, pos)) if result.isUnderflow(): if final: _process_incomplete_decode(self.encoding, input, error_function, input_buffer, builder) break _process_decode_errors(self.encoding, input, result, error_function, input_buffer, builder) return builder.toString(), input_buffer.position()
def encode(self, input, errors='strict'): error_function = codecs.lookup_error(errors) # workaround non-BMP issues - need to get the exact count of chars, not codepoints input_buffer = CharBuffer.allocate(StringBuilder(input).length()) input_buffer.put(input) input_buffer.rewind() encoder = Charset.forName(self.encoding).newEncoder() output_buffer = ByteBuffer.allocate(min(max(len(input) * 2, 256), 1024)) builder = StringIO() while True: result = encoder.encode(input_buffer, output_buffer, True) pos = output_buffer.position() output_buffer.rewind() builder.write(output_buffer.array()[0:pos].tostring()) if result.isUnderflow(): break _process_encode_errors(self.encoding, input, result, error_function, input_buffer, builder) return builder.getvalue(), len(input)
def _java_factory(encoding): encoding = python_to_java.get(encoding, encoding) supported = False try: supported = Charset.isSupported(encoding) except IllegalCharsetNameException: pass if not supported: return None, set() charset = Charset.forName(encoding) # FIXME should we return this canonical name? could be best... TBD entry = codecs.CodecInfo( name=encoding, encode=Codec(encoding).encode, decode=Codec(encoding).decode, incrementalencoder=partial(IncrementalEncoder, encoding=encoding), incrementaldecoder=partial(IncrementalDecoder, encoding=encoding), streamreader=partial(StreamReader, encoding=encoding), streamwriter=partial(StreamWriter, encoding=encoding) ) return entry, charset.aliases()
def getGeolocation(self, identity): session_attributes = identity.getSessionId().getSessionAttributes() if session_attributes.containsKey("remote_ip"): remote_ip = session_attributes.get("remote_ip") if StringHelper.isNotEmpty(remote_ip): httpService = CdiUtil.bean(HttpService) http_client = httpService.getHttpsClient() http_client_params = http_client.getParams() http_client_params.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 4 * 1000) geolocation_service_url = "http://ip-api.com/json/%s?fields=country,city,status,message" % remote_ip geolocation_service_headers = { "Accept" : "application/json" } try: http_service_response = httpService.executeGet(http_client, geolocation_service_url, geolocation_service_headers) http_response = http_service_response.getHttpResponse() except: print "Casa. Determine remote location. Exception: ", sys.exc_info()[1] return None try: if not httpService.isResponseStastusCodeOk(http_response): print "Casa. Determine remote location. Get non 200 OK response from server:", str(http_response.getStatusLine().getStatusCode()) httpService.consume(http_response) return None response_bytes = httpService.getResponseContent(http_response) response_string = httpService.convertEntityToString(response_bytes, Charset.forName("UTF-8")) httpService.consume(http_response) finally: http_service_response.closeConnection() if response_string == None: print "Casa. Determine remote location. Get empty response from location server" return None response = json.loads(response_string) if not StringHelper.equalsIgnoreCase(response['status'], "success"): print "Casa. Determine remote location. Get response with status: '%s'" % response['status'] return None return response return None
#!/bin/sh ''''exec "$(dirname "$0")"/ImageJ.sh --jython "$0" "$@" # (call again with fiji)''' from java.io import File, FileInputStream, FileOutputStream from java.nio import ByteBuffer from java.nio.charset import Charset import sys if sys.argv[1] == '-f': decoder = Charset.forName(sys.argv[2]).newDecoder() sys.argv[1:] = sys.argv[3:] elif sys.argv[1] == '-l': for name in Charset.availableCharsets().keySet(): print name sys.exit(0) else: decoder = Charset.forName("ISO-8859-1").newDecoder() encoder = Charset.forName("UTF-8").newEncoder() def iconv(file): print 'Converting', file f = File(file) if not f.exists(): print file, 'does not exist' sys.exit(1) buffer = ByteBuffer.allocate(f.length() * 2) input = FileInputStream(f) input.getChannel().read(buffer) buffer.limit(buffer.position()) buffer.position(0)
infile = "/Users/mac/Downloads/im" outfile = "/Users/mac/Downloads/dialogues.html" textfile = "/Users/mac/Downloads/dialogues.txt" else: infile = argv[1] outfile = argv[2] textfile = argv[3] with iopen(outfile, "w", encoding="utf-8", errors="ignore") as output: input = File(infile) soup = Jsoup.parse(input, "UTF-8", "") # First, create a new document new_doc = Jsoup.parse("<body></body>") new_doc.updateMetaCharsetElement(True) new_doc.charset(Charset.forName("UTF-8")) new_body = new_doc.select("body").first() for element in soup.select("*"): if (element.tag().toString() == "ul" and element.className() == "ui_clean_list im-mess-stack--mess _im_stack_messages") or ( element.tag().toString() == "div" and element.className() == "im-mess-stack--pname"): new_body.appendChild(element) # Then remove empty tags from it and transform the labels visitor = MyVisitor() NodeTraversor(visitor).traverse(new_doc) visitor.strip_empty_tags() visitor.replace_label_nodes()
def getUrlQueryParamValue(url, paramNameToLookFor): urlParams = URLEncodedUtils.parse(url, Charset.forName("UTF-8")); for param in urlParams: if param.getName() == paramNameToLookFor: return param.getValue(); return None;
#!/bin/sh ''''exec "$(dirname "$0")"/../ImageJ --jython "$0" "$@" # (call again with fiji)''' from java.io import File, FileInputStream, FileOutputStream from java.nio import ByteBuffer from java.nio.charset import Charset import sys if sys.argv[1] == '-f': decoder = Charset.forName(sys.argv[2]).newDecoder() sys.argv[1:] = sys.argv[3:] elif sys.argv[1] == '-l': for name in Charset.availableCharsets().keySet(): print name sys.exit(0) else: decoder = Charset.forName("ISO-8859-1").newDecoder() encoder = Charset.forName("UTF-8").newEncoder() def iconv(file): print 'Converting', file f = File(file) if not f.exists(): print file, 'does not exist' sys.exit(1) buffer = ByteBuffer.allocate(f.length() * 2) input = FileInputStream(f) input.getChannel().read(buffer) buffer.limit(buffer.position()) buffer.position(0) if buffer.limit() != f.length():
from java.io import File, FileInputStream, FileOutputStream from java.nio import ByteBuffer from java.nio.charset import Charset import sys decoder = Charset.forName("ISO-8859-1").newDecoder() encoder = Charset.forName("UTF-8").newEncoder() def iconv(file): print 'Converting', file f = File(file) if not f.exists(): print file, 'does not exist' sys.exit(1) buffer = ByteBuffer.allocate(f.length() * 2) input = FileInputStream(f) input.getChannel().read(buffer) buffer.limit(buffer.position()) buffer.position(0) if buffer.limit() != f.length(): print file, 'could not be read completely' sys.exit(1) input.close() buffer = encoder.encode(decoder.decode(buffer)) buffer.position(0) output = FileOutputStream(file + '.cnv') if output.getChannel().write(buffer) != buffer.limit(): print file, 'could not be reencoded' sys.exit(1) output.close() f.delete()
} } return best; } private static byte[] readAllBytesOrExit(Path path) { try { return Files.readAllBytes(path); } catch (IOException e) { System.err.println("Failed to read [" + path + "]: " + e.getMessage()); System.exit(1); } return null; } private static List<String> readAllLinesOrExit(Path path) { try { return Files.readAllLines(path, Charset.forName("UTF-8")); } catch (IOException e) { System.err.println("Failed to read [" + path + "]: " + e.getMessage()); System.exit(0); } return null; } // In the fullness of time, equivalents of the methods of this class should be auto-generated from // the OpDefs linked into libtensorflow_jni.so. That would match what is done in other languages // like Python, C++ and Go. static class GraphBuilder { GraphBuilder(Graph g) { this.g = g; } Output<Float> div(Output<Float> x, Output<Float> y) { return binaryOp("Div", x, y);