Example #1
0
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()
Example #2
0
 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 = ''
Example #3
0
 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()
Example #6
0
    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
Example #7
0
def load_classpath_resource(resource):
    """
    Uploads the classpath resource to the session's working directory.
    :param resource: to find on the classpath to copy
    :return: string
    """
    url = Thread.currentThread().contextClassLoader.getResource(resource)
    if url is None:
        raise Exception("Resource [%s] not found on classpath." % resource)

    return Resources.toString(url, Charset.defaultCharset())
Example #8
0
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()
Example #9
0
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
Example #10
0
def load_session(req,session_dir):
    if req.params.has_key("mvcx.sessionID"):

        session_file=File("%s/%s.json" % (session_dir,req.params["mvcx.sessionID"]))
        print("session_path:"+str(session_file.getAbsolutePath()))
        if session_file.exists():
            session=JsonObject("\n".join(Files.readLines(session_file,Charset.defaultCharset())))

            return session
        else:
            return None
    else:
        return None
def decode_result(text):
    '''
        The output of ping command is encoded with OS's default encoding, it needs to be decoded
        when the encoding is not utf-8.
    '''
    result = None
    try:
        encoding = get_default_encoding()
        result = text.decode(encoding) if encoding else text
    except LookupError:
        # some encodings (eg cp936 for chinese) are missing in jython, try to decode with java
        # not using java in the first place because encodings like cp850 will fail (encoding windows-1252 is used in java)
        from java.nio.charset import Charset
        from java.lang import String
        result = String(text, Charset.defaultCharset())
    return result
def decode_result(text):
    '''
        The output of ping command is encoded with OS's default encoding, it needs to be decoded
        when the encoding is not utf-8.
    '''
    result = None
    try:
        encoding = get_default_encoding()
        result = text.decode(encoding) if encoding else text
    except LookupError:
        # some encodings (eg cp936 for chinese) are missing in jython, try to decode with java
        # not using java in the first place because encodings like cp850 will fail (encoding windows-1252 is used in java)
        from java.nio.charset import Charset
        from java.lang import String
        result = String(text, Charset.defaultCharset())
    return result
Example #13
0
    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()
Example #14
0
    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()
Example #15
0
    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)
Example #16
0
    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 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
Example #18
0
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()
Example #19
0
#!/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()
Example #21
0
    os = OperatingSystemFamily.WINDOWS
else:
    os = OperatingSystemFamily.UNIX

local_opts = LocalConnectionOptions(os=os)
local_host = OverthereHost(local_opts)
local_session = OverthereHostSession(local_host, stream_command_output=True)

local_yaml_file = local_session.work_dir_file(remote_yaml_file.getName())
print("local_yaml_file {0}".format(local_yaml_file))

print("copy....")
local_session.copy_to(remote_yaml_file, local_yaml_file)

print("---- YAML ")
print(OverthereUtils.read(local_yaml_file, Charset.defaultCharset().name()))
print("/----")

context = {
    'devopsAsCodePassword': ansible_controler.devopsAsCodePassword,
    'devopsAsCodeUsername': ansible_controler.devopsAsCodeUsername,
    'devopsAsCodeUrl': ansible_controler.devopsAsCodeUrl,
    'yaml_file': local_yaml_file.path,
    'xlPath': ansible_controler.xlPath
}

command_line = "{xlPath} --xl-deploy-password {devopsAsCodePassword} --xl-deploy-username {devopsAsCodeUsername} --xl-deploy-url {devopsAsCodeUrl} apply -f {yaml_file} ".format(
    **context)
print(command_line)

import subprocess
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;
Example #23
0
#!/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():
Example #24
0
     }
   }
   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);