Skip to content

Commit 5309bf1

Browse files
committed
Quick clean up with pycharm and pylint helping out
1 parent 9ff5f79 commit 5309bf1

File tree

8 files changed

+42
-44
lines changed

8 files changed

+42
-44
lines changed

nix_tree/decomposer.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ def __managing_the_rest_of_the_file(self) -> None:
274274
except KeyError: # If there isn't a comment attached
275275
pass
276276
place_to_check = 1
277-
for i in range(1, len(rest_of_file_split)): # To stop empty portions lile [""] being an issue and meaning something was skipped
277+
for i in range(1, len(rest_of_file_split)): # To stop empty portions like [""] being an issue and meaning something was skipped
278278
if rest_of_file_split[equals_locations[iterator.equals_number][1] + i] != "":
279279
place_to_check = i
280280
break
@@ -354,7 +354,7 @@ def __cleaning_the_configuration(self, file: str) -> str:
354354
file = re.sub(r"[^\S\n]{", " { ", file)
355355
file = re.sub(";", " ; ", file) # For with clauses
356356
file = re.sub(r"}[^\S\n]*;", "}; ", file)
357-
file = re.sub(r"\][^\S\n]*;", "]; ", file)
357+
file = re.sub(r"][^\S\n]*;", "]; ", file)
358358
file = re.sub(r'\[[^\S\n]*"', '[ "', file)
359359
file = re.sub(r"\[[^\S\n]*'", "[ '", file)
360360
file = re.sub(r"\[[^\S\n]*''", "[ ''", file)

nix_tree/section_screens.py

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,9 @@ def on_input_submitted(self, user_input: Input.Submitted):
8080
user_input: Input.Submitted - the choice of the user
8181
"""
8282

83-
clean_input: str = re.sub(r"\"", "'", user_input.value)
83+
clean_input: str = re.sub(r'"', "'", user_input.value)
8484
clean_input = re.sub(r"(\[)(\s*)", "[ ", clean_input)
85-
clean_input = re.sub(r"(\s*)(\])", " ]", clean_input)
85+
clean_input = re.sub(r"(\s*)(])", " ]", clean_input)
8686
self.dismiss(clean_input)
8787

8888

@@ -292,14 +292,17 @@ def handle_return_from_variable_addition(data: tuple[str, Types | None] | None)
292292
"""
293293

294294
if data:
295-
if data[1] == Types.LIST and (not re.search(r"^\s*\[[^\]]*\]\s*$", data[0])):
295+
if data[1] == Types.LIST and (not re.search(r"^\s*\[[^]]*]\s*$", data[0])):
296296
self.notify("You lost (or gained) a bracket! Not updating", title="error adding option", severity="error")
297297
if type_as_defined:
298298
self.app.push_screen(RecommendedTypeOrChooseType(type_as_defined),
299299
handle_return_from_variable_addition)
300300
else:
301301
self.app.push_screen(AddScreenVariableSelection(), handle_return_from_variable_addition)
302-
elif data[1] == Types.STRING and (not re.search(r"^\s*'[^']*'\s*$", data[0])):
302+
elif data[1] == Types.STRING and (
303+
(not re.search(r"^\s*'[^']*'\s*$", data[0])) and
304+
(not re.search(r"^\s*''[^'']*''\s*$", data[0]))
305+
):
303306
self.notify("You lost (or gained) a speech mark! Not updating", title="error adding option", severity="error")
304307
if type_as_defined:
305308
self.app.push_screen(RecommendedTypeOrChooseType(type_as_defined),
@@ -322,7 +325,7 @@ def handle_return_from_variable_addition(data: tuple[str, Types | None] | None)
322325
else:
323326
self.app.pop_screen()
324327

325-
if re.search(r"[^a-zA-Z_]", path.value):
328+
if re.search(r"[^a-zA-Z_.]", path.value):
326329
self.notify("You have entered invalid character(s) for the path of your option, not adding", title="error adding option", severity="error")
327330
self.dismiss(None)
328331
else:
@@ -365,36 +368,29 @@ def recursive_addition(self, node: UIConnectorNode, path: list, data: str, path_
365368
bool - true if the function added the variable and false if otherwise
366369
"""
367370
if len(path) > 1:
368-
path_bit_already_exists = False
369371
for child in node.children:
370372
if child.label.plain == path[0]:
371-
path_bit_already_exists = True
372373
del path[0]
373374
return self.recursive_addition(child, path, data, path_as_list, data_type)
374-
if not path_bit_already_exists:
375-
new_node = node.add(path[0])
376-
if new_node.parent.is_root:
377-
self.__operations.append(f"Section {path[0]} added")
378-
else:
379-
self.__operations.append(f"Section {'.'.join(work_out_full_path(new_node, []))} added")
380-
del path[0]
381-
return self.recursive_addition(new_node, path, data, path_as_list, data_type)
375+
new_node = node.add(path[0])
376+
if new_node.parent.is_root:
377+
self.__operations.append(f"Section {path[0]} added")
378+
else:
379+
self.__operations.append(f"Section {'.'.join(work_out_full_path(new_node, []))} added")
380+
del path[0]
381+
return self.recursive_addition(new_node, path, data, path_as_list, data_type)
382+
383+
for child in node.children:
384+
if child.label.plain.split("=")[0] == self.__path.split(".")[-1]:
385+
self.notify("variable already exists", severity="error")
386+
return False
387+
if not path_as_list:
388+
node.add_leaf(self.__path.split(".")[-1] + "=" + data,
389+
data={self.__path: data, "type": data_type})
382390
else:
383-
var_already_exists = False
384-
for child in node.children:
385-
if child.label.plain.split("=")[0] == self.__path.split(".")[-1]:
386-
var_already_exists = True
387-
self.notify("variable already exists", severity="error")
388-
return False
389-
if not var_already_exists:
390-
if not path_as_list:
391-
node.add_leaf(self.__path.split(".")[-1] + "=" + data,
392-
data={self.__path: data, "type": data_type})
393-
else:
394-
node.add_leaf(self.__path.split(".")[-1] + "=" + data,
395-
data={'.'.join(path_as_list) + "." + self.__path: data, "type": data_type})
396-
return True
397-
return None
391+
node.add_leaf(self.__path.split(".")[-1] + "=" + data,
392+
data={'.'.join(path_as_list) + "." + self.__path: data, "type": data_type})
393+
return True
398394

399395
def action_quit_pressed(self) -> None:
400396
"""Quits the screen when one of the quit buttons are pressed"""

nix_tree/tree.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def find_type(variable: str) -> Types:
2424
return Types.INT
2525
if variable in ("true", "false"):
2626
return Types.BOOL
27-
if re.search(r"^\s*\[.*\]\s*$", variable):
27+
if re.search(r"^\s*\[.*]\s*$", variable):
2828
return Types.LIST
2929
if re.search(r"^\s*(\"|'|'').*(\"|'|'')\s*$", variable):
3030
return Types.STRING

nix_tree/ui.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -197,10 +197,9 @@ def __extract_data_from_action(self, action: str) -> tuple[str, str, str]:
197197
ErrorComposingFileFromTree - If the regex doesn't match meaning the action is corrupted
198198
"""
199199

200-
path = ""
201200
variable = ""
202201
if "[" in action: # List types
203-
if match := re.search(r"(Added|Delete|Change) (.*)\[(.*)\]", action):
202+
if match := re.search(r"(Added|Delete|Change) (.*)\[(.*)]", action):
204203
path = match.group(2)
205204
variable = "[" + match.group(3) + "]"
206205
else:
@@ -216,10 +215,10 @@ def __extract_data_from_action(self, action: str) -> tuple[str, str, str]:
216215
else:
217216
raise ErrorComposingFileFromTree(message="Was unable to parse actions to apply to tree")
218217
full_path = path + variable
219-
else: # True falses and uniques
218+
else: # True, falses and uniques
220219
full_path = action.split(" ")[1]
221220
path = full_path.split("=")[0]
222-
return (path, variable, full_path)
221+
return path, variable, full_path
223222

224223
def action_undo(self, empty_command: bool = False) -> None:
225224
"""Performs the undo commands by popping the change from the stack and reverse engineering it
@@ -334,7 +333,7 @@ def __apply_changes(self) -> None:
334333
while self.__queue.get_len() > 0:
335334
action = self.__queue.dequeue().name
336335
if action:
337-
# This section is pulling all of the data from the action such as the path of the option and the data
336+
# This section is pulling all the data from the action such as the path of the option and the data
338337
_, _, full_path = self.__extract_data_from_action(action)
339338

340339
# Performing the operations on the tree

nix_tree/variable_screens.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,19 @@ def on_input_submitted(self, new_data: Input.Submitted) -> None:
6868
"""
6969

7070
if self.__value != new_data.value:
71-
if self.__type == Types.LIST and (not re.search(r"^\s*\[[^\]]*\]\s*$", new_data.value)):
71+
if self.__type == Types.LIST and (not re.search(r"^\s*\[[^]]*]\s*$", new_data.value)):
7272
self.notify("You lost (or gained) a bracket! Not updating", title="error changing option", severity="error")
7373
self.app.pop_screen()
74-
elif self.__type == Types.STRING and (not re.search(r"^\s*'[^']*'\s*$", new_data.value)):
74+
elif self.__type == Types.STRING and (
75+
(not re.search(r"^\s*'[^']*'\s*$", new_data.value)) and
76+
(not re.search(r"^\s*''[^'']*''\s*$", new_data.value))
77+
):
7578
self.notify("You lost (or gained) a speech mark! Not updating", title="error changing option", severity="error")
7679
self.app.pop_screen()
7780
else:
7881
clean_input: str = re.sub(r"\"", "'", new_data.value)
7982
clean_input = re.sub(r"(\[)(\s*)", "[ ", clean_input)
80-
clean_input = re.sub(r"(\s*)(\])", " ]", clean_input)
83+
clean_input = re.sub(r"(\s*)(])", " ]", clean_input)
8184
self.__node.node.label = self.__path.split(".")[-1] + "=" + clean_input
8285
if self.__node.node.data:
8386
self.__node.node.data[self.__path] = clean_input

tests/test_comment_collecting.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
"""Tests the comment collecting functions for accuracy"""
12
from pathlib import Path
23

34
from nix_tree.decomposer import CommentHandling
@@ -14,7 +15,7 @@ def test_yasu_example_config():
1415
"""
1516
Checks if all comments are collected correctly in yasu_example_config.nix configuration
1617
"""
17-
18+
1819
comment_handler = CommentHandling(Path("./tests/example_configurations/yasu_example_config.nix"))
1920
assert "{6: [('# Include the results of the hardware scan.\\n', False)], 13: [('# Define your hostname.\\n', False)], 23: [('#audio', True)], 38: [('# Steam\\n', False)]}".replace("\"", '"') == str(comment_handler.get_comments_for_attaching())
2021

tests/test_exceptions.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
"""Tests that the exceptions are called at the right times in the program"""
12
from pathlib import Path
23
import pytest
34

tests/test_individual_functions.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,5 +114,3 @@ def test_work_out_full_path_example():
114114
at_bottom_of_tree = True
115115

116116
assert path == work_out_full_path(current_node, [])
117-
118-

0 commit comments

Comments
 (0)