Exemplo n.º 1
0
class PackingSystem:
    DEFAULT_PACKET_DATA_SIZE = 4096
    PACKET_HEADER_SIZE = 4

    def __init__(self):
        self.__data_size_in_bytes = PackingSystem.DEFAULT_PACKET_DATA_SIZE
        self.__packet_size_in_bytes = PackingSystem.DEFAULT_PACKET_DATA_SIZE + PackingSystem.PACKET_HEADER_SIZE

        self.__packet_number = 1

        self.__header_size_in_bytes = PackingSystem.PACKET_HEADER_SIZE

    def pack_data(self):
        new_packet = SWPacket(self.__packet_size_in_bytes,
                              self.__data_size_in_bytes,
                              self.__header_size_in_bytes,
                              packet_type=PacketType.DATA)
        new_packet.set_packet_number(self.__packet_number)
        self.__packet_number += 1
        new_packet.store_data(self.__file_reader.read())
        return new_packet

    def open_file(self, source_file):
        self.__file_reader = FileReader(source_file, self.__data_size_in_bytes)
        self.__file_reader.open()

    def close_file(self):
        self.__file_reader.close()

    def get_file_size(self):
        return self.__file_reader.get_file_size_in_bytes()

    def get_current_packet_number(self):
        return self.__packet_number

    def get_data_size_in_bytes(self):
        return self.__data_size_in_bytes

    def get_end_file_packet(self):
        end_packet = SWPacket(self.__packet_size_in_bytes,
                              self.__data_size_in_bytes,
                              self.__header_size_in_bytes,
                              packet_type=PacketType.DATA)

        end_packet.make_end_packet()
        end_packet.set_packet_number(self.__packet_number)

        return end_packet

    def reset(self):
        self.__packet_number = 1

    def set_data_size(self, new_size):
        self.__data_size_in_bytes = new_size
        self.__packet_size_in_bytes = new_size + PackingSystem.PACKET_HEADER_SIZE
class SpecReader_2():
    def __init__(self, path = None):
        self._reader = FileReader(path)

    def setPath(self, path):
        self._reader.setPath(path)

    def close(self):
        self._reader.close()

    def open(self):
        '''Státuszjelzés:
        False, ha bármely okból nem sikerült a művelet
        True, ha sikerült
        '''

        return self._reader.open()

    def readLine(self):
        '''Státuszjelzés:
        -1, ha bármely okból nem sikerült a művelet
        +1, ha sikerült adatot olvasni
        0,  ha már nincs több adat
        '''

        while True:
            ret_dic = self._reader.readLine()
            if ret_dic['status'] <= 0:
                return ret_dic

            data = ret_dic['data'].rstrip()
            if len(data) == 0: continue

            if data.lstrip()[0] == '#': continue

            return ret_dic
Exemplo n.º 3
0
from file_reader import FileReader
from os.path import exists


strings = ['Hello', 'goodbye', 'you_suck']
fr1 = FileReader(strings, 'test_file1.txt')
fr2 = FileReader([''], 'test_file2.txt')

def check_file_exists():
    return fr1.exists() and fr2.exists()

def print_information():
    print fr1.contents
    print fr1.cfile
    print fr2.cfile

'''
    Check the files existstance
'''
if check_file_exists():
    fr1.open()
    fr1.read()
    fr2.open()
    print_information()
    fr2.write(fr1.contents)
else:
    print 'One of the files were not found'