11import re
22from 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
88class 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 ()
0 commit comments