def parse_mapping(data): ''' Takes a list of strings and returns the (character,configuration) mapping in it The 'data' passed is actually the metadata at ths tartting of the file, which specifies characters which have to be replaced with configurations. For example, 'u sp 5d' indicates that u is to be replaced with 'sp 5d' The function uses the fact that different mappings are separated by a gap of at least two spaces. So, data is parsed to separate out individual mappings. Then, the part after the character is joined to create a mapping. ''' mapping = {} # Defined two grammars, as Optional() wasn't working as expected: map_parse1 = CharsNotIn(' ') + White() + Word(alphanums) + White(exact=1) + Word(alphanums) + White(min=2) + restOfLine map_parse2 = CharsNotIn(' ') + White() + Word(alphanums) + White(min=2) + restOfLine for row in data: row_copy = row while len(row_copy) > 0: # If it's a two-level confiugration: try: temp = map_parse1.parseString(row_copy) mapping[temp[0]] = ' '.join([temp[2],temp[4]]) row_copy = temp[-1] except: # If it's a single-level configuration: try: temp = map_parse2.parseString(row_copy) mapping[temp[0]] = temp[2] row_copy = temp[-1] except: # Parsing error, so mappings must be complete return mapping
def parse_final(): ''' Takes the parsed data( list of lists), and performs substitutions according to the mapping specified as the start of the file. Applies first mapping for first half of the document, and second mapping for the second half. The substitutions are performed by trying to match every field with the grammar below, and making substitutions whenever the grammar's rule s satisfied. ''' first_mapping = [] second_mapping = [] parsed_data = [] if not parse_data(first_mapping, second_mapping,parsed_data): return False SUBS = CharsNotIn('(') + "(" + Word(alphanums) + ")" + restOfLine for row in parsed_data: for i in range(len(row)): # First half of data ; first mapping will apply: if len(row)>6: try: parsed = SUBS.parseString(row[i]) parsed[0] = first_mapping[parsed[0]] row[i] = ''.join(parsed) # print parsed except: pass # Second half of data ; second mapping will apply: else: try: parsed = SUBS.parseString(row[i]) parsed[0] = second_mapping[parsed[0]] row[i] = ''.join(parsed) except: pass return parsed_data