|
| 1 | +import re |
| 2 | +from collections import Mapping |
| 3 | + |
| 4 | +from utilities import ignored_term_in_line, process_key, convert_key_value_pairs_to_dictionary, read_from_clipboard, \ |
| 5 | + write_to_clipboard |
| 6 | + |
| 7 | + |
| 8 | +class EyConverter: |
| 9 | + |
| 10 | + def __init__(self, log=False): |
| 11 | + |
| 12 | + self.special_characters = re.compile('[@!#$%^&*()<>?\|}{~]') |
| 13 | + self.lines = [] |
| 14 | + self.ignored_terms = set() |
| 15 | + self.preserved_words_dict = {} |
| 16 | + self.d = {} |
| 17 | + self.output = [] |
| 18 | + self.log = log |
| 19 | + |
| 20 | + def add_line(self, line): |
| 21 | + |
| 22 | + self.lines.append(lines) |
| 23 | + return self |
| 24 | + |
| 25 | + def from_clipboard(self): |
| 26 | + |
| 27 | + self.lines = read_from_clipboard().split('\n') |
| 28 | + return self |
| 29 | + |
| 30 | + def to_clipboard(self): |
| 31 | + |
| 32 | + self.build_output(self.d, 0) |
| 33 | + write_to_clipboard('\n'.join(str(line) for line in self.output)) |
| 34 | + |
| 35 | + def load_file(self, filename): |
| 36 | + |
| 37 | + with open(filename) as file: |
| 38 | + self.lines = file.readlines() |
| 39 | + |
| 40 | + return self |
| 41 | + |
| 42 | + def ignore_lines_containing(self, *args): |
| 43 | + |
| 44 | + self.ignored_terms = set(args) |
| 45 | + |
| 46 | + return self |
| 47 | + |
| 48 | + def preserve_words(self, *args): |
| 49 | + |
| 50 | + for word in args: |
| 51 | + self.preserved_words_dict[word.lower()] = word |
| 52 | + |
| 53 | + return self |
| 54 | + |
| 55 | + def convert_to_dictionary(self): |
| 56 | + |
| 57 | + for line in self.lines: |
| 58 | + |
| 59 | + line.strip() |
| 60 | + |
| 61 | + self.print(f"PARSING: \t{line}", new_line=False) |
| 62 | + |
| 63 | + if ignored_term_in_line(line, self.ignored_terms): |
| 64 | + self.print("\tSKIPPING") |
| 65 | + continue |
| 66 | + |
| 67 | + key, value = line.split(':', 1) |
| 68 | + |
| 69 | + processed_key = process_key(key, self.preserved_words_dict) |
| 70 | + processed_value = value.strip() |
| 71 | + |
| 72 | + self.d = convert_key_value_pairs_to_dictionary(processed_key, processed_value, self.d) |
| 73 | + |
| 74 | + self.print("\tOK") |
| 75 | + |
| 76 | + return self |
| 77 | + |
| 78 | + def build_output(self, yml_dict, tab_count): |
| 79 | + |
| 80 | + for key, value in yml_dict.items(): |
| 81 | + self.output.append(' ' * tab_count + key + ':') |
| 82 | + |
| 83 | + if isinstance(value, Mapping): |
| 84 | + self.build_output(value, tab_count + 1) |
| 85 | + else: |
| 86 | + if self.special_characters.search(value): |
| 87 | + value = f'"{value}"' |
| 88 | + |
| 89 | + self.output[-1] += f' {value}' |
| 90 | + |
| 91 | + def show(self): |
| 92 | + |
| 93 | + self.build_output(self.d, 0) |
| 94 | + for line in self.output: |
| 95 | + print(line) |
| 96 | + |
| 97 | + def write_file(self, filename): |
| 98 | + |
| 99 | + self.build_output(self.d, 0) |
| 100 | + with open(filename, 'wb+') as file: |
| 101 | + for line in self.output: |
| 102 | + file.write(str.encode(line + "\n")) |
| 103 | + |
| 104 | + def print(self, message, new_line=True): |
| 105 | + if self.log: |
| 106 | + print(message.rstrip(), end='\n' if new_line else '') |
| 107 | + |
| 108 | + |
| 109 | +def load_file(filename, log=False): |
| 110 | + return EyConverter(log).load_file(filename) |
| 111 | + |
| 112 | + |
| 113 | +def from_clipboard(log=False): |
| 114 | + return EyConverter(log).from_clipboard() |
0 commit comments