import os import itertools def get_valid_filename(question, extension = ''): filename = input(question) filename = os.path.expanduser(filename) if not (filename.endswith(extension) and os.path.exists(filename)): print('Please, enter valid filename') return get_valid_filename(question, extension) return filename def parse_color_from_integers(a, b, c): return f'#{str(hex(a))[2:]}{str(hex(b))[2:]}{str(hex(c))[2:]}' def parse_color_from_section(cell): # Parsing string format Color=R,G,B ints = cell.split(',') ints[0] = ints[0][6:] return parse_color_from_integers(*map(int, ints)) def main(): f = open(get_valid_filename('Enter path to Konsole filescheme: ', '.colorscheme'), 'r') data = [line.strip() for line in f.readlines()] sections = [list(y) for x, y in itertools.groupby(data, lambda z: z == '') if not x] out = open('scheme.conf', 'w') for section in sections: desc = section[0][1:-1] out.write(f'# importing {desc}\n') if desc == 'Background': out.write(f'background {parse_color_from_section(section[1])}\n') elif desc == 'Foreground': out.write(f'foreground {parse_color_from_section(section[1])}\n') elif desc.startswith('Color'): mod = 0 if desc.endswith('Intense'): mod = 1 if desc.endswith('Faint'): mod = 2 color = int(desc[5]) out.write(f'color{color + 8 * mod} {parse_color_from_section(section[1])}\n') out.close() f.close() if __name__ == '__main__': main()