Exemplo n.º 1
0
 def decode_file(self, source_filename, result_filename):
     """Decodes given file.
     
     Arguments:
     source_filename -- name of file to decode
     result_filename -- name of file to save decoded data
     """
     csv_file = CsvFile(source_filename, delim=';', quote='"')
     csv_data = CsvData(csv_file)
     
     new_header = self.decode_header(csv_data.get_header())
     
     new_rows = []
     row = csv_data.get_next_row(row_type='list')
     i = 0
     while row:
         i += 1
         changed_row = row[:]
         is_jst = row[4] in ['z', 'Z']
         
         changed_row[1] = self.teryt_decoder.get_name(row[1])
         if changed_row[1] is None:
             print i
         if is_jst:
             changed_row[2] = self.teryt_decoder.get_name(row[1] + row[2])
             if changed_row[2] is None:
                 print i
             changed_row[3] = self.jst_decoder.get_name(row[6][1:]) # decoder has xyz, file has 0xyz
             if changed_row[3] is None:
                 print i
             changed_row[4] = u'Związek JST'
         else:
             type = self.teryt_decoder.get_type(row[1])
             if row[2] != '00':
                 changed_row[2] = self.teryt_decoder.get_name(row[1] + row[2])
                 if changed_row[2] is None:
                     print i
                 type = self.teryt_decoder.get_type(row[1] + row[2])
             else:
                 changed_row[2] = ''
                 
             if row[3] != '00':
                 changed_row[3] = self.teryt_decoder.get_name(row[1] + row[2] + row[3])
                 if changed_row[3] is None:
                     print i
                 type = self.teryt_decoder.get_type(row[1] + row[2] + row[3])
             else:
                 changed_row[3] = ''
             changed_row[4] = type
         
         self.clean_row(changed_row)
         new_rows.append(changed_row)
         row = csv_data.get_next_row(row_type='list')
     
     csv_file.close()
     
     
     new_data = Data([new_header] + new_rows, result_filename)
     new_data.save()
Exemplo n.º 2
0
class OldToNewFormat:
    
    """Transformes file in the old format to the new one:
    - removes IDD, IDSPRAW, UWAGI, STATUS(for Rb27s and Rb28s)
    - moves ID_JST after PT, renames to KODMSWIA and changes its value
    """
    
    def __init__(self, filename):
        """Reads data from specified file.
        
        Arguments:
        filename -- name of file to change
        """
        csv_file = CsvFile(filename, delim=';')
        self.data = CsvData(csv_file)
        self.name = filename
        self.status_exists = 'STATUS' in self.data.get_header()
    
    def change_file(self, fname=None):
        """Transforms file to new format and saves it.
        
        Arguments:
        fname -- name of transformed file, if None, then generated from
                 input file's name
        """
        new_header = self.change_header(self.data.get_header())
        new_rows = []
        
        row = self.data.get_next_row(row_type='list')
        while row:
            new_row = self.change_row(row)
            new_rows.append(new_row)
            row = self.data.get_next_row(row_type='list')
        
        if fname is None:
            fname = self.name[:-4] + '_mod.csv'
        
        save_data = Data([new_header] + new_rows, fname)
        save_data.save(quoting=csv.QUOTE_NONE)
    
    def change_header(self, header):
        """Returns new header with unnecessary fields removed: IDD, IDSPRAW,
        UWAGI[, STATUS]; moves ID_JST after PT and renames it to KODMSWIA.
        
        Arguments:
        header -- header that should be changed
        """
        new_header = header[:]
                
        if self.status_exists:
            to_remove = [15, 12, 2, 1, 0]
        else:
            to_remove = [12, 2, 1, 0]
        
        for i in to_remove:
            del new_header[i]
        
        new_header.insert(6, 'KODMSWIA')
        return new_header
    
    def change_row(self, row):
        """Returns changed row.
        Removes unnecessary fields(the same as change_header) and
        moves value from ID_JST to KODMSWIA and changes it:
        if it's związek JST, copies it without the first digit,
        otherwise changes it to ''
        
        Arguments:
        row -- data row to change
        """
        new_row = row[:]
        mswia_code = new_row[2]
        
        if self.status_exists:
            to_remove = [15, 12, 2, 1, 0]
        else:
            to_remove = [12, 2, 1, 0]
        
        for i in to_remove:
            del new_row[i]
        
        if mswia_code.startswith('4'):
            mswia_code = mswia_code[1:]
        else:
            mswia_code = ''
        
        new_row.insert(6, mswia_code)
            
        return new_row