Exemple #1
0
 def _reader(self):
     stream = getResourceAsStream(self._path)
     reader = BufferedReader(InputStreamReader(stream, 'UTF-8'))
     try:
         yield reader
     finally:
         reader.close()
Exemple #2
0
    def _excmd(self, sshcmd):
        """
        return (connected_ok, response_array)
        """
        connected_ok = True
        resp = []
        try:
            conn = Connection(self.hostname)
            conn.connect()
            self.logger.info("ssh connection created.")
            isAuthenticated = conn.authenticateWithPassword(self.user, self.password)
            if not isAuthenticated:
                connected_ok = False
                self.logger.error("ssh failed to authenticatd.")
            else:
                self.logger.info("ssh authenticated.")
                sess = conn.openSession()

                self.logger.info("ssh session created.")
                sess.execCommand(sshcmd)
                self.logger.info("ssh command issued. cmd is %s" % sshcmd)

                stdout = StreamGobbler(sess.getStdout())
                br = BufferedReader(InputStreamReader(stdout))
                while True:
                    line = br.readLine()
                    if line is None:
                        break
                    else:
                        resp.append(line)
                self.logger.warning("ssh command output: " % resp)
        except IOException, ex:
            connected_ok = False
            # print "oops..error,", ex
            self.logger.error("ssh exception: %s" % ex)
	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()
Exemple #4
0
    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
Exemple #5
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 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 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 ""
Exemple #9
0
    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)))
Exemple #10
0
    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 _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 = ''
Exemple #12
0
    def open(self, url_or_req):
        try:
            if isinstance(url_or_req, Request):
                connection = URL(url_or_req.get_full_url()).openConnection()
                for header in self.addheaders:
                    connection.setRequestProperty(header[0], header[1])
                for key, value in url_or_req.header_items():
                    connection.setRequestProperty(key, value)
                
                connection.setRequestMethod(url_or_req.get_method())
                
                if url_or_req.get_data() != None:
                    connection.setDoOutput(True)
                    outstream = connection.getOutputStream()
                    outstream.write(url_or_req.get_data())
            else:
                connection = URL(url_or_req).openConnection()
                for header in self.addheaders:
                    connection.setRequestProperty(header[0], header[1])

            instream = connection.getInputStream()
            doc = ""
            reader = BufferedReader(InputStreamReader(instream))
            line = reader.readLine()
            while line:
                doc += line
                line = reader.readLine()
            return Response(doc)
        except:
            traceback.print_exc()
            raise
Exemple #13
0
 def readAsServer_0(cls, socket):
     """ generated source for method readAsServer_0 """
     br = BufferedReader(InputStreamReader(socket.getInputStream()))
     #  The first line of the HTTP request is the request line.
     requestLine = br.readLine()
     if requestLine == None:
         raise IOException("The HTTP request was empty.")
     message = str()
     if requestLine.toUpperCase().startsWith("GET "):
         message = requestLine.substring(5, requestLine.lastIndexOf(' '))
         message = URLDecoder.decode(message, "UTF-8")
         message = message.replace(str(13), ' ')
     elif requestLine.toUpperCase().startsWith("POST "):
         message = readContentFromPOST(br)
     elif requestLine.toUpperCase().startsWith("OPTIONS "):
         #  Web browsers can send an OPTIONS request in advance of sending
         #  real XHR requests, to discover whether they should have permission
         #  to send those XHR requests. We want to handle this at the network
         #  layer rather than sending it up to the actual player, so we write
         #  a blank response (which will include the headers that the browser
         #  is interested in) and throw an exception so the player ignores the
         #  rest of this request.
         HttpWriter.writeAsServer(socket, "")
         raise IOException("Drop this message at the network layer.")
     else:
         HttpWriter.writeAsServer(socket, "")
         raise IOException("Unexpected request type: " + requestLine)
     return message
Exemple #14
0
 def _reader(self):
     stream = getResourceAsStream(self._path)
     reader = BufferedReader(InputStreamReader(stream, 'UTF-8'))
     try:
         yield reader
     finally:
         reader.close()
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 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
Exemple #18
0
    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:
Exemple #19
0
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()
Exemple #20
0
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()
Exemple #21
0
 def run(self):
     while (rhost.keepAlive == 1):
         lang.Thread.sleep(interval)
         sess = rhost.connection.openSession()
         sess.execCommand(command) # execute command
         inputreader = BufferedReader(InputStreamReader(sess.getStdout()))
         inputreader.readLine()
         sess.close()
Exemple #22
0
 def read_jar_stream(self, template_path):
     reader = BufferedReader(InputStreamReader(Thread.currentThread().getContextClassLoader().getResourceAsStream(template_path)))
     line = reader.readLine()
     sb = StringBuilder()
     while line is not None:
         sb.append(line).append("\n")
         line = reader.readLine()
     return sb.toString()
 def _read_from_stream(self, stream):
     reader = BufferedReader(InputStreamReader(StreamGobbler(stream)))
     result = ""
     line = reader.readLine()
     while line is not None:
         result += line + "\n"
         line = reader.readLine()
     return result
Exemple #24
0
 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 _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
Exemple #26
0
 def readFile(cls, rootFile):
     """ generated source for method readFile """
     #  Show contents of the file.
     fr = FileReader(rootFile)
     br = BufferedReader(fr)
     try:
         while (line = br.readLine()) != None:
             response += line + "\n"
         return response
Exemple #27
0
 def run(self):
     reader = BufferedReader(InputStreamReader(self.getStream()))
     try:
         line = reader.readLine()
         while line:
             self.output += line
             line = reader.readLine()
     finally:
         reader.close()
Exemple #28
0
    def __metadata(self):
        ISOCode = self.params.getProperty("ISOcode")
        print "*** Processing: ", ISOCode
        #Index the country metadata
        metadataPayload = self.object.getPayload("%s.json" % ISOCode)
        json = JsonConfigHelper(metadataPayload.open())
        allMeta = json.getMap(".")
        self.utils.add(self.index, "recordType", "country")
        for key in allMeta.keySet():
            self.utils.add(self.index, key, allMeta.get(key));
        metadataPayload.close()

        #Index the country detail
        geoNamePayload = self.object.getPayload("%s.txt" % ISOCode)
        countryName = self.params.getProperty("countryName")
    
        countryAreaStream = geoNamePayload.open()
        reader = BufferedReader(InputStreamReader(countryAreaStream, "UTF-8"));
        line = reader.readLine()
        
        headerList = ["geonameid", "name", "asciiname", "alternatenames", "latitude", "longitude", \
                      "feature class", "feature code", "country code", "cc2", "admin1 code", "admin2 code", \
                      "admin3 code", "admin4 code", "population", "elevation", "gtopo30", "timezone", "modification date"]
        
        while (line != None):
            arraySplit = line.split("\t")
            geonamesId = arraySplit[0]
            oid = DigestUtils.md5Hex("http://geonames.org/" + geonamesId)

            if oid == self.oid:
                extraIndex = self.index
            else:
                extraIndex = HashMap()
                self.utils.add(extraIndex, "recordType", "area")
                self.utils.add(extraIndex, "item_type", self.itemType)
                self.utils.add(extraIndex, "dc_identifier", oid)
                self.utils.add(extraIndex, "id", oid)
                self.utils.add(extraIndex, "storage_id", self.oid)   #Use parent object
                self.utils.add(extraIndex, "last_modified", time.strftime("%Y-%m-%dT%H:%M:%SZ"))
                self.utils.add(extraIndex, "display_type", "geonames")
                self.utils.add(extraIndex, "countryName", countryName)
                self.utils.add(extraIndex, "repository_name", self.params["repository.name"])
                self.utils.add(extraIndex, "repository_type", self.params["repository.type"])
                self.utils.add(extraIndex, "security_filter", "guest")
            self.utils.add(extraIndex, "dc_title", arraySplit[1])
            # The rest of the metadata
            count = 0
            for array in arraySplit:
                if headerList[count] !="alternatenames" and array:
                    self.utils.add(extraIndex, headerList[count], array)
                count +=1
                
            self.indexer.sendIndexToBuffer(oid, extraIndex)
            line = reader.readLine()
        geoNamePayload.close()
        
Exemple #29
0
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 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()
Exemple #31
0
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
Exemple #33
0
 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()
Exemple #34
0
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()
Exemple #35
0
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()
Exemple #36
0
 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 _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
Exemple #38
0
 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()
Exemple #39
0
    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
Exemple #40
0
    def openRead(self, fPath):
        fpHdfs = Path(fPath)
        fsInput = self.fsHd.open(fpHdfs)

        reader = None
        pat = r'.*\.gz'
        match = re.search(pat, hst)
        if match == None:
            reader = BufferedReader(InputStreamReader(fsInput))
        else:
            # The file stream is in GZip format...
            reader = BufferedReader(InputStreamReader(
                GZIPInputStream(fsInput)))

        return reader
Exemple #41
0
 def __getJson(self):
     data = {}
     basePath = portalId + "/" + pageName
     uri = URLDecoder.decode(request.getAttribute("RequestURI"))
     oid = uri[len(basePath)+1:]
     payload = Services.storage.getPayload(oid, "imsmanifest.xml")
     if payload is not None:
         try:
             from xml.etree import ElementTree as ElementTree
             #xml = ElementTree()
             #IOUtils.copy(payload.inputStream, out)
             sb = StringBuilder()
             inputStream = payload.inputStream
             reader = BufferedReader(InputStreamReader(inputStream,
                         "UTF-8"))
             while True:
                 line=reader.readLine()
                 if line is None:
                     break
                 sb.append(line).append("\n")
             inputStream.close()
             xmlStr =  sb.toString()
             xml = ElementTree.XML(xmlStr)
             ns = xml.tag[:xml.tag.find("}")+1]
             resources = {}
             for res in xml.findall(ns+"resources/"+ns+"resource"):
                 resources[res.attrib.get("identifier")] = res.attrib.get("href")
             #print resources
             organizations = xml.find(ns+"organizations")
             defaultName = organizations.attrib.get("default")
             organizations = organizations.findall(ns+"organization")
             organizations = [o for o in organizations if o.attrib.get("identifier")==defaultName]
             organization = organizations[0]
             title = organization.find(ns+"title").text
             data["title"] = title
             items = []
             for item in organization.findall(ns+"item"):
                 a = item.attrib
                 isVisible = a.get("isvisible")
                 idr = a.get("identifierref")
                 id = resources.get(idr)
                 iTitle = item.find(ns+"title").text
                 if isVisible and id and id.endswith(".htm"):
                     items.append({"attributes":{"id":id}, "data":iTitle})
             data["nodes"] = items
         except Exception, e:
              data["error"] = "Error - %s" % str(e)
              print data["error"]
Exemple #42
0
    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 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
Exemple #44
0
def getLinks(page):
    url = URI(page).toURL()
    conn = url.openConnection()
    isr = InputStreamReader(conn.getInputStream())
    br = BufferedReader(isr)

    kit = HTMLEditorKit()
    doc = kit.createDefaultDocument()
    parser = ParserDelegator()
    callback = doc.getReader(0)
    parser.parse(br, callback, 1)

    iterator = doc.getIterator(HTML.Tag.A)
    while iterator.isValid():
        try:
            attr = iterator.getAttributes()
            src = attr.getAttribute(HTML.Attribute.HREF)
            start = iterator.getStartOffset()
            fini = iterator.getEndOffset()
            length = fini - start
            text = doc.getText(start, length)
            print '%40s -> %s' % (text, src)
        except:
            pass
        iterator.next()
    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()
Exemple #46
0
 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()
Exemple #47
0
 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()
Exemple #48
0
    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 _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 = ''
Exemple #50
0
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 getOsType():

    NOT_WINDOWS = 'NOT WINDOWS'

    try:
        osNameTemp = os.getenv('OS',NOT_WINDOWS)
        if not osNameTemp == NOT_WINDOWS and string.find(osNameTemp, 'Windows') == 0:
            return OS_TYPE_WINDOWS

        # non-Windows process
        process = Runtime.getRuntime().exec("uname")
        output = process.getInputStream() # process' output is our input
        br = BufferedReader(InputStreamReader(output))
        osName = br.readLine()

    except Exception, error:
        log.error("Could not determine operating system type: " + str(error))
Exemple #52
0
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()
Exemple #53
0
    def __init__(self, hdfsCluster, fpath):
        self.hdfs = Hdfs(hdfsCluster)
        self.fsHd = self.hdfs.fileSystem

        fpHdfs = Path(fpath)
        fsInput = self.fsHd.open(fpHdfs)
        # The file has text so we want to use read the input stream via the BufferedReader.
        reader = BufferedReader(InputStreamReader(fsInput))
        self.lineCount = 0
        self.lines = []
        line = reader.readLine()
        while line is not None:
            # print line
            self.lines.append(line)
            self.lineCount = self.lineCount + 1
            if (self.lineCount % 1000) == 0:
                print self.lineCount
            line = reader.readLine()
    def buildImagesCSVTable(fileName, logger):

        # Initialize the table
        csvTable = LinkedHashMap()

        # Header
        isHeader = True

        # Read the CSV file
        br = BufferedReader(FileReader(fileName))

        # Read the first line from the text file
        line = br.readLine()

        # loop until all lines are read 
        while line is not None:

            if isHeader:

                # We are past the header
                isHeader = False

                # Read next line 
                line = br.readLine()

                continue

            # Get all values for current row
            row = line.split(";")

            # Remove '"' and '\' characters if needed
            for i in range(len(row)):
                row[i] = row[i].replace("\"", "")
                row[i] = row[i].replace("\\\\", "\\")
                row[i] = row[i].replace("\\", "/")

            # Add the row with the file name as key
            csvTable.put(row[6], row)

            # Read next line 
            line = br.readLine()

        return csvTable
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
Exemple #56
0
def processJobResults(inStream, interimResult):
    failures = []
    finished = []
    skip = False
    rdr = BufferedReader(InputStreamReader(inStream))
    line = rdr.readLine()
    while line is not None:
        skip = False
        parts = line.split()
        if len(parts) == 4 and re.search("[0-9][0-9]:[0-9][0-9]:[0-5][0-9]", parts[0]):
            rc = int(parts[-1])
            if interimResult is not None and rc == interimResult:
                skip = True
            if not skip:
                if rc != 0:
                    failures.append(parts[1])
                finished.append(parts[1])
        print line
        line = rdr.readLine()
    return (finished, failures)
Exemple #57
0
    def getPayloadContent(self, payload):
        if payload is None:
            return ""

        try:
            sb = StringBuilder()
            reader = BufferedReader(InputStreamReader(payload.open(), "UTF-8"))
            line = reader.readLine()

            while line is not None:
                sb.append(line).append("\n")
                line = reader.readLine()
            payload.close()

            if sb:
                return sb
            return ""

        except Exception, e:
            return ""
Exemple #58
0
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