Exemple #1
0
 def __init__(self, teryt_file_name):
     """
     Initiates teryt decoder using csv_file with teryt codes.
     
     Arguments:
     teryt_file_name - name of csv file containing TERYT codes
     """
     csv_file = CsvFile(teryt_file_name, delim=';', quote='"')
     data = CsvData(csv_file)
     data.build(rows_type='list')
     self.codes = {}
     for row in data.get_rows():
         if not self.is_important(row[4]):
             continue
         woj, pow, gm = row[1], row[2], row[3]
         name, type = row[5], row[6]
         if woj not in self.codes:
             self.codes[woj] = {'name': name, 'type': type, 'pows': {}}
         if pow != '00':
             powiats = self.codes[woj]['pows']
             if pow not in powiats:
                 powiats[pow] = {'name': name, 'type': type, 'gms': {}}
             if gm != '00':
                 gmins = powiats[pow]['gms']
                 gmins[gm] = {'name': name, 'type': type}
     csv_file.close()
Exemple #2
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()
Exemple #3
0
 def __init__(self, file_name):
     """Initiates decoder using file describing meaning of that figure.
     
     Arguments:
     file_name -- name of file with description of the fourth figure
     """
     
     csv_file = CsvFile(file_name, delim=';', quote='"')
     data = CsvData(csv_file)
     codes = {}
     for row in data.get_rows():
         codes[row[0]] = row[1]
Exemple #4
0
 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()
Exemple #5
0
 def __init__(self, file_name):
     """
     Initiates jst codes decoder using csv_file with jst codes.
     Omitts lines that does not describe jst.
     
     Arguments:
     file_name - name of csv file containing jst codes
     """
     
     csv_file = CsvFile(file_name, delim=';', quote='"', enc='cp1250')
     data = CsvData(csv_file)
     data.build(rows_type='list')
     self.codes = {}
     for row in data.get_rows():
         type = row[4]
         if type != 'z' and type != 'Z':
             continue
         
         code = row[9]
         name = row[0]
         self.codes[code] = name
Exemple #6
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