Skip to content

Commit b5b7ce8

Browse files
committed
Added ability for paths to be formed of strings
1 parent 02e57ba commit b5b7ce8

File tree

4 files changed

+20
-12
lines changed

4 files changed

+20
-12
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,12 @@ nix run 'github:max-amb/nix-tree' <your filename>
6565
## Limitations
6666
This program obviously is not perfect, hence there are some limitations that should be taken into account by the user:
6767
* The program cannot currently parse the `let in` combination or any flake for that matter
68-
* The syntax "..." = ..., often found in home manager is not supported
68+
* ~~The syntax "..." = ..., often found in home manager is not supported~~ Now is after some more complex regex matching
6969
* One example that doesn't work is code that has groups inside of sections, like `[ { ... } ]`, this will break the program for now
7070
* ~~It may not be able to handle multiline strings often found in `extraConfig` options~~ The program can take them as input but has no way of displaying different lines or outputting different lines
7171
* Comments done with `/* */` aren't stored for re-attachment as they are often inside clauses
7272

7373
## Credits
7474
* The test configuration `./tests/example_configurations/pms_example_config.nix` comes from [here](https://perfectmediaserver.com/02-tech-stack/nixos/configuration.nix/)
7575
* The test configuration `./tests/example_configurations/yasu_example_config.nix` comes from [here](https://discourse.nixos.org/t/configuration-nix-home-nix-examples/8185)
76+
* The test configuration `./tests/example_configurations/shortened_default.nix` comes from the default nix configuration

nix_tree/decomposer.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ def __managing_headers(self) -> None:
191191
self.__tree.add_branch(contents=f"headers=[ {', '.join(headers)} ]")
192192

193193
def __connecting_spaced_lines(self, file: list[str], iterator: int) -> tuple[list[str], int]:
194-
if re.search(r"^''(?!.*''$).*", file[iterator]):
194+
if re.search(r"^''(?!.*'').*", file[iterator]):
195195
j = iterator + 1
196196
while j < len(file):
197197
file[iterator] += " " + file[j]
@@ -200,7 +200,7 @@ def __connecting_spaced_lines(self, file: list[str], iterator: int) -> tuple[lis
200200
del file[j]
201201
break
202202
del file[j]
203-
elif re.search(r'^"(?!.*"$).*', file[iterator]):
203+
elif re.search(r'^"(?!.*").*', file[iterator]):
204204
j = iterator + 1
205205
while j < len(file):
206206
file[iterator] += " " + file[j]
@@ -350,14 +350,14 @@ def __cleaning_the_configuration(self, file: str) -> str:
350350
Returns:
351351
file: str - the file all on one line now cleaned
352352
"""
353-
file = re.sub(r"[^\S\n]+", " ", file)
353+
file = re.sub(r"[^\S\n]+", " ", file) # The [^\S\n] is my form of .*, it just excludes new lines so comment attaching can work as expected
354354
file = re.sub("=", " = ", file)
355355
file = re.sub(r"[^\S\n]}", " } ", file)
356356
file = re.sub(r"[^\S\n]{", " { ", file)
357357
file = re.sub(";", " ; ", file) # For with clauses
358358
file = re.sub(r"}[^\S\n]*;", "}; ", file)
359359
file = re.sub(r"][^\S\n]*;", "]; ", file)
360-
file = re.sub(r'\[[^\S\n]*"', '[ "', file)
360+
file = re.sub(r'\[[^\S\n]*"', '[ "', file) # Looks scary because of escapes for square brackets, is simply removing and adding spaces in lists
361361
file = re.sub(r"\[[^\S\n]*'", "[ '", file)
362362
file = re.sub(r"\[[^\S\n]*''", "[ ''", file)
363363
file = re.sub(r"(\S*)(];)", r"\1 \2", file)

nix_tree/section_screens.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ def handle_return_from_variable_addition(data: tuple[str, Types | None] | None)
325325
else:
326326
self.app.pop_screen()
327327

328-
if re.search(r"[^a-zA-Z_.]", path.value):
328+
if re.search(r"[^a-zA-Z_.'\"]", path.value):
329329
self.notify("You have entered invalid character(s) for the path of your option, not adding", title="error adding option", severity="error")
330330
self.dismiss(None)
331331
else:

nix_tree/ui.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -202,19 +202,23 @@ def __extract_data_from_action(self, action: str) -> tuple[str, str, str]:
202202
variable = ""
203203
path = ""
204204
if "[" in action: # List types
205-
if match := re.search(r"(Added|Delete|Change) (.*)\[(.*)]", action):
205+
if match := re.search(r"(Added|Delete|Change) (.*)\[(.*)]( type: Types.(STRING|BOOL|UNIQUE|LIST|INT))?$", action):
206206
path = match.group(2)[:-1]
207207
variable = "[" + match.group(3) + "]"
208208
else:
209209
raise ErrorComposingFileFromTree(message="Was unable to parse actions to apply to tree")
210210
full_path = path + "=" + variable
211211
elif "'" in action: # Any of the string types
212-
if match := re.search(r"(Added|Delete|Change) (.*)''(.*)''", action):
212+
if match := re.search(r"(Added|Delete|Change) (.*)''(.*?)''( type: Types.(STRING|BOOL|UNIQUE|LIST|INT))?$", action):
213213
path = match.group(2)[:-1]
214214
variable = "''" + match.group(3) + "''"
215-
elif match := re.search(r"(Added|Delete|Change) (.*)'(.*)'", action):
215+
elif match := re.search(r"(Added|Delete|Change) (.*)'(.*?)'( type: Types.(STRING|BOOL|UNIQUE|LIST|INT))?$", action):
216+
# The 3rd group is non greedy to make a.'b'.c = 'x' paths work
216217
path = match.group(2)[:-1]
217218
variable = "'" + match.group(3) + "'"
219+
elif match := re.search(r"(Added|Delete|Change) (.*)=(.*)( type: Types.(STRING|BOOL|UNIQUE|LIST|INT))?$", action): # String path but not string var like a.'b'.c = true
220+
path = match.group(2)
221+
variable = match.group(3)
218222
else:
219223
raise ErrorComposingFileFromTree(message="Was unable to parse actions to apply to tree")
220224
full_path = path + "=" + variable
@@ -236,9 +240,9 @@ def action_undo(self, empty_command: bool = False) -> None:
236240
self.query_one("#operations_stack", ListView).pop(0)
237241
action: str | None = self.__stack.pop().name
238242
if action:
239-
path, variable, full_path = self.__extract_data_from_action(action)
240243
match action.split(" ")[0]:
241244
case "Delete":
245+
path, variable, full_path = self.__extract_data_from_action(action)
242246
var_type = None
243247
match action.split(" ")[-1]:
244248
case "Types.LIST":
@@ -262,6 +266,7 @@ def action_undo(self, empty_command: bool = False) -> None:
262266
else:
263267
raise TypeError("Cannot deduce node variable type")
264268
case "Added":
269+
path, variable, full_path = self.__extract_data_from_action(action)
265270
node_to_delete: UIVariableNode | None = self.recursive_searching_for_var(
266271
self.query_one(Tree).root,
267272
path.split("."),
@@ -272,6 +277,7 @@ def action_undo(self, empty_command: bool = False) -> None:
272277
else:
273278
raise NodeNotFound(node_name=full_path)
274279
case "Change":
280+
path, variable, full_path = self.__extract_data_from_action(action)
275281
change_command: str = action[7:] # Can't use space splits as it changes the lists spaces
276282
pre: list = change_command.split("->")[0].strip().split("=")
277283
post: list = change_command.split("->")[1].strip().split("=")
@@ -335,14 +341,14 @@ def __apply_changes(self) -> None:
335341
while self.__queue.get_len() > 0:
336342
action = self.__queue.dequeue().name
337343
if action:
338-
# This section is pulling all the data from the action such as the path of the option and the data
339-
_, _, full_path = self.__extract_data_from_action(action)
340344

341345
# Performing the operations on the tree
342346
match action.split(" ")[0]:
343347
case "Added":
348+
_, _, full_path = self.__extract_data_from_action(action)
344349
tree.add_branch(full_path)
345350
case "Delete":
351+
_, _, full_path = self.__extract_data_from_action(action)
346352
parent: Node | None = tree.find_node_parent(full_path, tree.get_root())
347353
if parent:
348354
if isinstance(parent, ConnectorNode):
@@ -352,6 +358,7 @@ def __apply_changes(self) -> None:
352358
else:
353359
raise NodeNotFound(node_name=full_path)
354360
case "Change":
361+
_, _, full_path = self.__extract_data_from_action(action)
355362
change_command: str = action[7:]
356363
pre: str = change_command.split("->")[0].strip()
357364
post: str = change_command.split("->")[1].strip()

0 commit comments

Comments
 (0)