Skip to content

Commit ea429d2

Browse files
author
Joseph Procopio and Rene Sanchez
committed
restructures for pip
1 parent 93f25d3 commit ea429d2

File tree

7 files changed

+117
-2
lines changed

7 files changed

+117
-2
lines changed

E2Yaml/E2Yaml.py

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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()

E2Yaml/__init__.py

Whitespace-only changes.
File renamed without changes.

driver.py renamed to examples/driver.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
#!/usr/bin/env python3
22

33
# first import the tool
4-
import eyconverter as ey
54

65
# example program loading from clipboard and outputting to clipboard
7-
yml = ey.from_clipboard(log=True)
6+
from E2Yaml import E2Yaml
7+
8+
yml = E2Yaml.from_clipboard(log=True)
89

910
# uncomment this if you would like to convert a file
1011
# yml = ey.load_file('test_input.env', log=True)
File renamed without changes.
File renamed without changes.

setup.cfg

Whitespace-only changes.

0 commit comments

Comments
 (0)