Exemplo n.º 1
0
    def run(self):
        '''
        starting thread

        @return void
        '''

        try:
            with open(self.file, 'rb') as file:
                boool = (len(self.__info[2]) == 2 and self.__info[2][0] == 'E')
                if (boool):  # compress
                    gzip = NamedTemporaryFile(prefix='comp_', delete=False)
                    with GzipFile(gzip.name, 'wb') as comp:
                        copyfileobj(file, comp)
                    file = gzip  # override
                # not_dots fx
                putt = self.que.put_nowait
                count = FIRST
                fread = file.read

                if (self.__info[2][0] == 'E'):  # encrypt
                    while True:
                        tmp = fread(BUFFER)
                        if (not tmp):
                            break
                        putt((tmp, count))
                        count += 1

                else:  # decrypt
                    try:
                        buffer = int(fread(BUFFER_PICK))  # pickle buffer
                        header = fread(BUFFER_HEAD).decode()  # header
                        if (header == self.__info[5][0:BUFFER_HEAD]):
                            while True:
                                tmp = fread(buffer)
                                if (not tmp):
                                    break
                                putt((tmp, count))
                                count += 1
                    except ValueError:
                        pass

            for i in range(self.__info[4]):  # end of queue
                putt((False, i))
            if (boool):  # remove compress
                gzip.close()
                u_file_del(gzip.name)

            self._error = False
        except KeyboardInterrupt:  # Ctrl + C
            pass

        if (self._error):  # error
            self.__thread.put_nowait(0)
        else:  # for write
            self.__thread.put_nowait(count)

        return
Exemplo n.º 2
0
    def run(self):
        '''
        starting thread

        @return void
        '''

        try:
            with open(self.file, 'rb') as file:
                boool = (len(self.__info[2]) == 2 and self.__info[2][0] == 'E')
                if(boool):    # compress
                    gzip = NamedTemporaryFile(prefix='comp_', delete=False)
                    with GzipFile(gzip.name, 'wb') as comp:
                        copyfileobj(file, comp)
                    file = gzip    # override
                # not_dots fx
                putt = self.que.put_nowait
                count = FIRST
                fread = file.read

                if(self.__info[2][0] == 'E'):    # encrypt
                    while True:
                        tmp = fread(BUFFER)
                        if(not tmp):
                            break
                        putt((tmp, count))
                        count += 1

                else:    # decrypt
                    try:
                        buffer = int(fread(BUFFER_PICK))    # pickle buffer
                        header = fread(BUFFER_HEAD).decode()    # header
                        if(header == self.__info[5][0:BUFFER_HEAD]):
                            while True:
                                tmp = fread(buffer)
                                if(not tmp):
                                    break
                                putt((tmp, count))
                                count += 1
                    except ValueError:
                        pass

            for i in range(self.__info[4]):    # end of queue
                putt((False, i))
            if(boool):    # remove compress
                gzip.close()
                u_file_del(gzip.name)

            self._error = False
        except KeyboardInterrupt:    # Ctrl + C
            pass

        if(self._error):    # error
            self.__thread.put_nowait(0)
        else:    # for write
            self.__thread.put_nowait(count)

        return
Exemplo n.º 3
0
    def __delete(self):
        '''
        delete original file

        @return bool
        '''

        return u_file_del(self.file[0])
Exemplo n.º 4
0
    def end(self):
        '''
        check created file and print elapsed time
        only if isn't a hash function

        @return void
        '''

        tot = self.others[3]
        for iii in range(0, tot - 1):    # remove temporary file
            u_file_del(self._tmp_r[iii])
            u_file_del(self._tmp_w[iii])

        if(self.others[4]):    # stats
            print()
            print('Reading average time: %.3f sec' % (self.__stat[0] / tot))
            print('Working average time: %.3f sec' % (self.__stat[1] / tot))
            print('Total time: %.3f sec' % (self.__stat[0] + self.__stat[1]))

        if(not self.others[2][self._counter - 1]):    # end info
            if(not u_file_exists(self.file[1])):
                print('File error!\a')
        return
Exemplo n.º 5
0
    def run(self):
        '''
        starting thread

        @return void
        '''

        try:
            cyc = self.__thread.get()
            if (self._ash):  # header part
                pass
            elif (cyc > 0):
                with open(self.file, 'wb') as file:
                    boool = (len(self.__info[2]) == 2 and \
                            self.__info[2][0] == 'D')
                    if (boool):  # decompress
                        gzip = NamedTemporaryFile(prefix='comp_', delete=False)
                        backup = file
                        file = gzip  # override
                    # not_dots fx
                    gett = self.que.get
                    count = FIRST
                    self._fwrite = fwrite = file.write

                    if (self.__info[2][0] == 'E'):  # encrypt
                        raw, seq = gett()
                        self.buffer[seq] = raw
                        first = str(len(raw)).zfill(BUFFER_PICK)
                        fwrite(first.encode())
                        fwrite(self.__info[5][0:BUFFER_HEAD].encode())
                        cyc -= 1
                        del first

                        while cyc > 0:
                            raw, seq = gett()
                            if (not raw):
                                break
                            elif (count == seq):
                                fwrite(raw)
                                count += 1
                            else:
                                self.buffer[seq] = raw
                                count = self.controll(count)
                            cyc -= 1

                    else:  # decrypt
                        while cyc > 0:
                            raw, seq = gett()
                            if (not raw):
                                break
                            elif (count == seq):
                                fwrite(raw)
                                count += 1
                            else:
                                self.buffer[seq] = raw
                                count = self.controll(count)
                            cyc -= 1

                    self.last(count)
                    if (boool):
                        gzip.close()
                        with GzipFile(gzip.name, 'rb') as comp:
                            copyfileobj(comp, backup)
                        u_file_del(gzip.name)
        except KeyboardInterrupt:  # Ctrl + C
            pass

        return
Exemplo n.º 6
0
    def run(self):
        '''
        starting thread

        @return void
        '''

        try:
            cyc = self.__thread.get()
            if(self._ash):    # header part
                pass
            elif(cyc > 0):
                with open(self.file, 'wb') as file:
                    boool = (len(self.__info[2]) == 2 and \
                            self.__info[2][0] == 'D')
                    if(boool):    # decompress
                        gzip = NamedTemporaryFile(prefix='comp_', delete=False)
                        backup = file
                        file = gzip    # override
                    # not_dots fx
                    gett = self.que.get
                    count = FIRST
                    self._fwrite = fwrite = file.write

                    if(self.__info[2][0] == 'E'):    # encrypt
                        raw, seq = gett()
                        self.buffer[seq] = raw
                        first = str(len(raw)).zfill(BUFFER_PICK)
                        fwrite(first.encode())
                        fwrite(self.__info[5][0:BUFFER_HEAD].encode())
                        cyc -= 1
                        del first

                        while cyc > 0:
                            raw, seq = gett()
                            if(not raw):
                                break
                            elif(count == seq):
                                fwrite(raw)
                                count += 1
                            else:
                                self.buffer[seq] = raw
                                count = self.controll(count)
                            cyc -= 1

                    else:    # decrypt
                        while cyc > 0:
                            raw, seq = gett()
                            if(not raw):
                                break
                            elif(count == seq):
                                fwrite(raw)
                                count += 1
                            else:
                                self.buffer[seq] = raw
                                count = self.controll(count)
                            cyc -= 1

                    self.last(count)
                    if(boool):
                        gzip.close()
                        with GzipFile(gzip.name, 'rb') as comp:
                            copyfileobj(comp, backup)
                        u_file_del(gzip.name)
        except KeyboardInterrupt:    # Ctrl + C
            pass

        return