Skip to content

Commit b023e45

Browse files
committed
eek
1 parent a4751e4 commit b023e45

File tree

2 files changed

+73
-21
lines changed

2 files changed

+73
-21
lines changed

nix_tree/composer.py

Lines changed: 48 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414
class ComposerIterator:
1515
"""An iterator that composer uses to build the file"""
1616

17-
prepend: str = ""
18-
previous_prepend: str = ""
19-
lines: str = ""
20-
previous_addition: str = ""
17+
prepend: str = "" # To store tabs
18+
previous_prepend: str = "" # To store prev tabs
19+
lines: str = "" # To store current progress
20+
previous_addition: str = "" # To store what data was previously added (for comments)
2121

2222

2323
class Composer:
@@ -65,12 +65,17 @@ def __work_out_lines_comments(self, node: Node) -> None:
6565
6666
Args:
6767
node: Node - the starting node
68+
69+
Note:
70+
The only difference between this function and the ordinary work out lines function
71+
is adding of comments to the end of lines - which requires use of previous_addition
72+
so that there is no confusing of comments for data
6873
"""
6974

7075
comment_for_after = ""
7176
if node.get_comments():
7277
for comment in node.get_comments():
73-
if comment[1]: # Need to insert above current line
78+
if comment[1]: # Need to insert above current line (the true false variable in the comment tuple)
7479
before_comment = self.__composer_iterator.lines.split("\n")[:-1]
7580
post_comment = self.__composer_iterator.lines.split("\n")[-1]
7681
before_comment_str = '\n'.join(before_comment) + "\n" + self.__composer_iterator.prepend + comment[0]
@@ -190,32 +195,50 @@ def __work_out_lines_no_comments(self, node: Node) -> None:
190195
Args:
191196
node: Node - the starting node
192197
"""
193-
if isinstance(node, ConnectorNode):
194-
if len(node.get_connected_nodes()) > 1:
198+
199+
if isinstance(node, ConnectorNode): # Most will be connector nodes
200+
if len(node.get_connected_nodes()) > 1: # To check if we should split it with curly braecs or not
201+
202+
# Managing the placing of the start of the group
195203
if self.__composer_iterator.lines[-1] != ":":
196-
if self.__composer_iterator.lines[-1] == ".":
204+
if self.__composer_iterator.lines[-1] == ".": # if this is already part of a path like x. already exists
197205
self.__composer_iterator.lines += node.get_name() + " = {\n"
198-
elif self.__composer_iterator.lines[-1] == "\n":
206+
elif self.__composer_iterator.lines[-1] == "\n": # new line so not part of path - means that the for tabs needs to be added
199207
self.__composer_iterator.lines += self.__composer_iterator.prepend + node.get_name() + " = {\n"
200208
else:
201209
raise ErrorComposingFileFromTree(
202210
f"There was an error composing the file from the tree, here is what has been generated already {self.__composer_iterator.lines}"
203211
)
204212
else:
205-
self.__composer_iterator.lines += "\n\n{\n"
206-
self.__composer_iterator.previous_prepend = self.__composer_iterator.prepend
207-
self.__composer_iterator.prepend += " "
213+
self.__composer_iterator.lines += "\n\n{\n" # If it is a new file! (only run on first iteration)
214+
self.__composer_iterator.previous_prepend = self.__composer_iterator.prepend # need to indent in
215+
self.__composer_iterator.prepend += " "
216+
217+
# indent managing
218+
self.__composer_iterator.previous_prepend = self.__composer_iterator.prepend # Updating the prepend (to go down indent later)
219+
self.__composer_iterator.prepend += " " # indenting a bit more - we just opened curly braces!
220+
208221
for singular_node in node.get_connected_nodes():
222+
# Recursive calling!
209223
self.__work_out_lines_no_comments(singular_node)
224+
225+
210226
if self.__composer_iterator.previous_prepend != "":
211-
self.__composer_iterator.lines += self.__composer_iterator.previous_prepend + "};\n"
212-
else: # Then it is the end of the file
227+
self.__composer_iterator.lines += self.__composer_iterator.previous_prepend + "};\n" # Need to shut the group
228+
else: # Then it is the end of the file as we have shut the final group (the large {})
213229
pass
230+
231+
# Updating prepends again to go back
214232
self.__composer_iterator.prepend = self.__composer_iterator.previous_prepend
215233
self.__composer_iterator.previous_prepend = self.__composer_iterator.previous_prepend[2:]
234+
235+
# This is to make the base indent level more spaced out
216236
if len(self.__composer_iterator.prepend) == 2:
217237
self.__composer_iterator.lines += "\n"
218-
elif len(node.get_connected_nodes()) == 1:
238+
239+
elif len(node.get_connected_nodes()) == 1: # If there is only one child
240+
241+
# Slightly different to above - no curly brace
219242
if self.__composer_iterator.lines[-1] != ":":
220243
if self.__composer_iterator.lines[-1] == ".":
221244
self.__composer_iterator.lines += node.get_name() + "."
@@ -227,27 +250,30 @@ def __work_out_lines_no_comments(self, node: Node) -> None:
227250
)
228251
else:
229252
self.__composer_iterator.lines += "\n\n{\n"
230-
self.__composer_iterator.previous_prepend = self.__composer_iterator.prepend
253+
self.__composer_iterator.previous_prepend = self.__composer_iterator.prepend # need to indent in
231254
self.__composer_iterator.prepend += " "
255+
256+
# call it for the singular node
232257
self.__work_out_lines_no_comments(node.get_connected_nodes()[0])
233258
else:
234259
pass
235260
elif isinstance(node, VariableNode):
261+
# getting "x =" from "y.z.x = gosh" node
236262
data = node.get_name().split(".")[-1] + " = "
237263

238264
if node.get_type() == Types.LIST:
239-
if "'" not in node.get_data():
265+
if "'" not in node.get_data(): # If it isnt a list of strings
240266
data_as_list = node.get_data().split(" ")
241267
data_as_list = data_as_list[1:-1]
242-
if len(data_as_list) >= 3:
268+
if len(data_as_list) >= 3: # Splitting the list to multiple lines if longer than 2 elements
243269
data += "[\n"
244270
for list_item in data_as_list:
245271
data += self.__composer_iterator.prepend + " " + list_item + "\n"
246272
data += self.__composer_iterator.prepend + "]"
247273
else:
248274
data += node.get_data()
249275
else:
250-
data_as_list = node.get_data().split("' '")
276+
data_as_list = node.get_data().split("' '") # note the different splitting required
251277
data_as_list = data_as_list[1:-1]
252278
if len(data_as_list) >= 3:
253279
data += "[\n"
@@ -268,10 +294,11 @@ def __work_out_lines_no_comments(self, node: Node) -> None:
268294
data = re.sub(rf"\({with_clause}\)\.", "", data)
269295
data = data.split("=")[0] + "= with " + with_clause + ";" + data.split("=")[1]
270296

271-
if self.__composer_iterator.lines[-1] == ".":
297+
# handling adding it in
298+
if self.__composer_iterator.lines[-1] == ".": # end of a connector like x.y now adding z = enable
272299
self.__composer_iterator.lines += data + ";\n"
273300
elif self.__composer_iterator.lines[-1] == "\n":
274-
self.__composer_iterator.lines += self.__composer_iterator.prepend + data + ";\n"
301+
self.__composer_iterator.lines += self.__composer_iterator.prepend + data + ";\n" # indenting!!!!
275302
else:
276303
raise ErrorComposingFileFromTree(
277304
f"There was an error composing the file from the tree, the previous character was unexpected, here is what there is currently: {self.__composer_iterator.lines}"

nix_tree/decomposer.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,24 @@ def __managing_headers(self) -> None:
194194
self.__tree.add_branch(contents=f"headers=[ {', '.join(headers)} ]")
195195

196196
def __connecting_spaced_lines(self, file: list[str], iterator: int) -> tuple[list[str], int]:
197+
"""Connects spaced lines - lines with spaces in "x y"
198+
199+
Args:
200+
file: list[str] - the file split on spaces
201+
iterator: int - where the prepare the file function is currently looking
202+
203+
Returns:
204+
tuple[list[str], int] - the updated file and updated position of the iterator
205+
206+
Note:
207+
This function needs to update both the file and the iterator as it modifies both while
208+
connecting strings with spaces in.
209+
210+
The regex searches are negative lookaheads they are checking for a bit of the file list that
211+
starts with a string sign and then does not end with one - which means it must have been split
212+
on a space in the middle.
213+
"""
214+
197215
if re.search(r"^''(?!.*'').*", file[iterator]):
198216
j = iterator + 1
199217
while j < len(file):
@@ -441,12 +459,19 @@ def __find_equal_locations_lines(self, equals_locations: list, file_with_lines:
441459
442460
Returns:
443461
list - the equals locations list with line numbers
462+
463+
Note:
464+
This needs to be done to map to the comments dictionary which attaches
465+
comments to line numbers
466+
The replace functions are so the program can avoid = in strings which obviously
467+
should be avoided
444468
"""
445469
def replace_single_speech(match):
446470
return "'" + 'A' * (len(match.group(0))-2) + "'"
447471

448472
def replace_double_speech(match):
449473
# As double quote strings can have new lines in them
474+
# e.g. ''abcd\n=gosh'' would go to ''AAAA\nAAAAA''
450475
string = "''"
451476
content = match.group(0)
452477
for char in content:

0 commit comments

Comments
 (0)