def parse_flag_chunk(chunk: list, colors_dict: dict, names: dict) -> Flag: name = re.match(r'[A-Z]*_*[A-Z]*', chunk[0])[0] chunk.pop(0) try: name = names[name] except Exception: pass # scan for the background elements background = re.split( r'(colored_emblem|texture_emblem|textured_emblem)\s*=\s*{', ''.join(chunk))[0] background = '\n'.join(background.split('\t')) # parsing pattern pattern = re.findall(r'pattern\s*=\s*"?.*"?', background) pattern = re.findall(r'[A-z0-9]*[.]tga', pattern[0])[0] # parsing background colors colors = re.findall(r'color[0-9]\s*=\s*"?.*"?(?=\t)?', background) colors = [ colors_dict[color.split('=')[-1].replace('"', '').replace(' ', '')] for color in colors ] # make flag object flag = Flag(name, pattern, colors, size=SIZE) # scan for emblems and add them to the flag object defaults = {'colors': colors} emblems = [] chunk = '\n'.join(chunk).replace('\t', '\n').replace(' ', '\n') chunk = re.split( r'(colored_emblem|texture_emblem|textured_emblem)\s*=\s*{', ''.join(chunk))[1:] while not chunk == []: chunk = ''.join(chunk) emblems = parse_emblem( re.split( r'(colored_emblem|texture_emblem|textured_emblem)\s*=\s*{', chunk)[0], colors_dict, defaults) chunk = re.split(r'(colored_emblem|texture_emblem)\s*=\s*{', chunk)[1:] flag.set_emblems(emblems) return flag