Skip to content

Commit b31b396

Browse files
author
Procopio
committed
adds ability to separate to display cleanly
1 parent 87c0c33 commit b31b396

File tree

2 files changed

+24
-15
lines changed

2 files changed

+24
-15
lines changed

E2Yaml/E2Yaml.py

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import re
22
from collections import Mapping
33

4-
from E2Yaml.utilities import read_from_clipboard, write_to_clipboard, ignored_term_in_line, \
4+
from utilities import read_from_clipboard, write_to_clipboard, ignored_term_in_line, \
55
convert_key_value_pairs_to_dictionary, process_key
66

77

88
class EyConverter:
99

10-
def __init__(self, log=False):
10+
def __init__(self, log=False, separate=False):
1111

1212
self.special_characters = re.compile('[@!#$%^&*()<>?\|}{~]')
1313
self.lines = []
@@ -16,6 +16,7 @@ def __init__(self, log=False):
1616
self.d = {}
1717
self.output = []
1818
self.log = log
19+
self.separate = separate
1920

2021
def add_line(self, line):
2122

@@ -24,18 +25,19 @@ def add_line(self, line):
2425

2526
def from_clipboard(self):
2627

27-
self.lines = read_from_clipboard().split('\n')
28+
self.lines = filter(None, read_from_clipboard().split('\n'))
2829
return self
2930

3031
def to_clipboard(self):
3132

32-
self.build_output(self.d, 0)
33+
self.build_output(self.d, 0, 0)
3334
write_to_clipboard('\n'.join(str(line) for line in self.output))
3435

3536
def load_file(self, filename):
3637

3738
with open(filename) as file:
38-
self.lines = file.readlines()
39+
self.lines = filter(lambda x: x != '\n', file.readlines())
40+
print(self.lines)
3941

4042
return self
4143

@@ -75,13 +77,20 @@ def convert_to_dictionary(self):
7577

7678
return self
7779

78-
def build_output(self, yml_dict, tab_count):
80+
def build_output(self, yml_dict, tab_count, level):
7981

82+
index = 0
8083
for key, value in yml_dict.items():
84+
85+
if level == 0 and self.separate and index != 0:
86+
self.output.append('')
87+
88+
index += 1
89+
8190
self.output.append(' ' * tab_count + key + ':')
8291

8392
if isinstance(value, Mapping):
84-
self.build_output(value, tab_count + 1)
93+
self.build_output(value, tab_count + 1, level + 1)
8594
else:
8695
if self.special_characters.search(value):
8796
value = f'"{value}"'
@@ -90,13 +99,13 @@ def build_output(self, yml_dict, tab_count):
9099

91100
def show(self):
92101

93-
self.build_output(self.d, 0)
102+
self.build_output(self.d, 0, 0)
94103
for line in self.output:
95104
print(line)
96105

97106
def write_file(self, filename):
98107

99-
self.build_output(self.d, 0)
108+
self.build_output(self.d, 0, 0)
100109
with open(filename, 'wb+') as file:
101110
for line in self.output:
102111
file.write(str.encode(line + "\n"))
@@ -106,9 +115,9 @@ def print(self, message, new_line=True):
106115
print(message.rstrip(), end='\n' if new_line else '')
107116

108117

109-
def load_file(filename, log=False):
110-
return EyConverter(log).load_file(filename)
118+
def load_file(filename, log=False, separate=False):
119+
return EyConverter(log, separate).load_file(filename)
111120

112121

113-
def from_clipboard(log=False):
114-
return EyConverter(log).from_clipboard()
122+
def from_clipboard(log=False, separate=False):
123+
return EyConverter(log, separate).from_clipboard()

examples/driver.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
# example program loading from clipboard and outputting to clipboard
66
import E2Yaml
77

8-
yml = E2Yaml.from_clipboard(log=True)
8+
yml = E2Yaml.from_clipboard(log=True, separate=True)
99

1010
# uncomment this if you would like to convert a file
11-
# yml = ey.load_file('test_input.env', log=True)
11+
# yml = E2Yaml.load_file('test_input.env', log=True)
1212

1313
# optional argument to ignore lines that contain a value. Accepts unlimited parameters
1414
yml.ignore_lines_containing('JAVA_OPTS', 'CONVEYOR', 'JBP')

0 commit comments

Comments
 (0)