Skip to content

Commit 503dea8

Browse files
committed
Cleanup
1 parent 51490a2 commit 503dea8

File tree

11 files changed

+60
-41
lines changed

11 files changed

+60
-41
lines changed

nix_tree/__main__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
1-
"""Starts the UI"""
1+
"""Handles user command line interaction"""
22

33
import argparse
44
from pathlib import Path
55

66
from nix_tree.ui import start_ui
77
from nix_tree.errors import ConfigurationFileNotFound
88

9+
910
def main():
1011
"""Parses the arguments to the tool and passes it on to the rest of the code"""
1112

1213
parser = argparse.ArgumentParser(prog="nix-tree",
1314
description="A tool for viewing and editing your nix configuration as a tree")
14-
parser.add_argument("file_location", type=str, default="/home/max/nea/NEA/configuration.nix",
15+
parser.add_argument("file_location", type=str,
1516
help="The location of your nix configuration file")
1617
parser.add_argument("-w", "--writeover", default=False, action="store_true",
1718
help="Write over the file that you are editing")

nix_tree/composer.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
"""contains the composer which builds the file"""
1+
"""The composer builds the output file if the user applies their changes"""
22

33
import re
44
import os
@@ -9,6 +9,7 @@
99
from nix_tree.tree import VariableNode, ConnectorNode, Node
1010
from nix_tree.parsing import Types
1111

12+
1213
@dataclass
1314
class ComposerIterator:
1415
"""An iterator that composer uses to build the file"""
@@ -18,6 +19,7 @@ class ComposerIterator:
1819
lines: str = ""
1920
previous_addition: str = ""
2021

22+
2123
class Composer:
2224
"""The class which contains the functionality to output the edited tree"""
2325

@@ -68,7 +70,7 @@ def __work_out_lines_comments(self, node: Node) -> None:
6870
comment_for_after = ""
6971
if node.get_comments():
7072
for comment in node.get_comments():
71-
if comment[1]: # Need to insert above current line
73+
if comment[1]: # Need to insert above current line
7274
before_comment = self.__composer_iterator.lines.split("\n")[:-1]
7375
post_comment = self.__composer_iterator.lines.split("\n")[-1]
7476
before_comment_str = '\n'.join(before_comment) + "\n" + self.__composer_iterator.prepend + comment[0]

nix_tree/custom_types.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
"""Defines two custom types for use in the ui mostly"""
1+
"""Defines two custom types for use in the ui"""
22

33
# Need to import Tree to import _tree
44
from textual.widgets import Tree, _tree

nix_tree/decomposer.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
"""The module containing the decomposer"""
1+
"""The decomposer decomposes the file into tokes and passes them into the tree"""
2+
23
from dataclasses import dataclass
34
from pathlib import Path
45
import re
@@ -15,6 +16,7 @@ class Iterator:
1516
prepend: str = ""
1617
previous_prepend: str = ""
1718

19+
1820
class CommentHandling:
1921
"""Class to handle the comments in a Nix file"""
2022

@@ -116,12 +118,13 @@ def get_comments_for_attaching(self) -> dict[int, list[tuple[str, bool]]]:
116118

117119
return self.__comments
118120

121+
119122
class Decomposer:
120123
"""Class to handle the decomposition of the Nix file and addition of tokens to the tree"""
121124

122125
def __init__(self, file_path: Path, tree: DecomposerTree) -> None:
123126
"""Takes in file path and stores it for the main decomposition function
124-
127+
125128
Args:
126129
file_path: Path - The file path for the Nix configuration file
127130
tree: DecomposerTree - The tree that decomposer should add to
@@ -224,7 +227,7 @@ def __prepare_the_file(self, file: str) -> list[str]:
224227
rest_of_file_split: list = file.split(" ")
225228
i = 0
226229
while True:
227-
if i < len(rest_of_file_split): # Doing it like this because the length changes every time
230+
if i < len(rest_of_file_split): # Doing it like this because the length changes every time
228231
rest_of_file_split, i = self.__connecting_spaced_lines(rest_of_file_split, i)
229232
i += 1
230233
else:
@@ -268,13 +271,13 @@ def __managing_the_rest_of_the_file(self) -> None:
268271
comment_list: list[tuple[str, bool]] = comments.pop(equals_locations[iterator.equals_number][2])
269272
comments_attached_to_id.update({
270273
self.__checking_group(groups, equals_locations[iterator.equals_number][0]) +
271-
rest_of_file_split[equals_locations[iterator.equals_number][1] - 1] :
274+
rest_of_file_split[equals_locations[iterator.equals_number][1] - 1]:
272275
comment_list
273276
})
274277
except KeyError: # If there isn't a comment attached
275278
pass
276279
place_to_check = 1
277-
for i in range(1, len(rest_of_file_split)): # To stop empty portions like [""] being an issue and meaning something was skipped
280+
for i in range(1, len(rest_of_file_split)): # To stop empty portions like [""] being an issue and meaning something was skipped
278281
if rest_of_file_split[equals_locations[iterator.equals_number][1] + i] != "":
279282
place_to_check = i
280283
break
@@ -350,14 +353,14 @@ def __cleaning_the_configuration(self, file: str) -> str:
350353
Returns:
351354
file: str - the file all on one line now cleaned
352355
"""
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
356+
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
354357
file = re.sub("=", " = ", file)
355358
file = re.sub(r"[^\S\n]}", " } ", file)
356359
file = re.sub(r"[^\S\n]{", " { ", file)
357360
file = re.sub(";", " ; ", file) # For with clauses
358361
file = re.sub(r"}[^\S\n]*;", "}; ", file)
359362
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
363+
file = re.sub(r'\[[^\S\n]*"', '[ "', file) # Looks scary because of escapes for square brackets, is simply removing and adding spaces in lists
361364
file = re.sub(r"\[[^\S\n]*'", "[ '", file)
362365
file = re.sub(r"\[[^\S\n]*''", "[ ''", file)
363366
file = re.sub(r"(\S*)(];)", r"\1 \2", file)
@@ -478,7 +481,7 @@ def replace_normal_speech(match):
478481
char_count += len(line)
479482
return equals_locations
480483

481-
def __add_comments_to_nodes(self, node: Node, prepend: str, comments: dict[str, list[tuple[str,bool]]]):
484+
def __add_comments_to_nodes(self, node: Node, prepend: str, comments: dict[str, list[tuple[str, bool]]]):
482485
"""Adds the comment lists to their respective nodes
483486
484487
Args:

nix_tree/errors.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class NodeNotFound(Exception):
1212
def __init__(self, node_name: str, message: str = "Node {NODE} does not exist in the tree"):
1313
super().__init__(message.format(NODE=node_name))
1414

15+
1516
class NoValidHeadersNode(Exception):
1617
"""Raised if no valid headers are found by the composer
1718
@@ -21,6 +22,7 @@ class NoValidHeadersNode(Exception):
2122
def __init__(self, message: str = "Could not find any valid headers nodes to start outputting the tree") -> None:
2223
super().__init__(message)
2324

25+
2426
class ErrorComposingFileFromTree(Exception):
2527
"""Raised if there is an error outputting/composing the tree
2628
@@ -30,9 +32,11 @@ class ErrorComposingFileFromTree(Exception):
3032
def __init__(self, message: str = "An error was encountered composing the file from the tree") -> None:
3133
super().__init__(message)
3234

35+
3336
class ConfigurationFileNotFound(Exception):
3437
"""Raised if no configuration file is found to edit/read"""
3538

39+
3640
class ErrorHandlingComments(Exception):
3741
"""Raised if there was an error with comment management
3842

nix_tree/help_screens.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
- Enter: To modify a variable or fold an indent
1313
- q/Esc : To close this help dialog
1414
15+
Note: if in a list there it looks like (xy).z,
16+
it means that xy is used in a with clause!
1517
"""
1618

1719
# The help text for the options menu
@@ -33,11 +35,12 @@
3335
- Tab: To switch between buttons
3436
- Enter: To select one of the buttons
3537
- q/Esc: To close this help dialog or the options dialog
36-
- Delete: To delete that section
37-
- Add: To add a variable/section
38+
- Delete: To delete that section
39+
- Add: To add a variable/section
3840
- Exit: To close the options dialog
3941
"""
4042

43+
4144
class HelpScreen(ModalScreen):
4245
"""The outline for a help screen in the app"""
4346

nix_tree/parsing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
"""Stores the parsing class for options checking"""
1+
"""Stores the parsing class to operate upon the options.json"""
22

33
import json
44
from enum import Enum

nix_tree/section_screens.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ def handle_return_from_variable_addition(data: tuple[str, Types | None] | None)
313313
path_as_list = work_out_full_path(self.__node.node, [])
314314
if data[1]:
315315
node_added = self.recursive_addition(self.__node.node, path.value.split("."), data[0], path_as_list,
316-
data[1])
316+
data[1])
317317
else:
318318
raise TypeError("The nodes type could not be determined")
319319
if node_added:

nix_tree/stacks.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
"""Contains the stacks and queue implementations used in throughout the program
22
33
Note:
4-
Inheritance is useless due to all the stacks being of different data types
4+
Inheritance is useless due to all the stacks being of different data types by design, to avoid confusion
55
"""
6+
67
from textual.widgets import ListItem
78

89

nix_tree/tree.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
"""Contains the tree implementation"""
1+
"""Contains the tree used to store the decomposed file"""
2+
23
import re
34

45
from nix_tree.custom_types import UIConnectorNode
@@ -36,7 +37,7 @@ class Node:
3637

3738
def __init__(self, name: str) -> None:
3839
"""Sets the name of the node
39-
40+
4041
Args:
4142
name: str - the name to be set
4243
"""
@@ -70,7 +71,7 @@ def get_connected_nodes(self) -> list:
7071
"""
7172
return []
7273

73-
def set_comments(self, comments: list[tuple[str,bool]]) -> None:
74+
def set_comments(self, comments: list[tuple[str, bool]]) -> None:
7475
"""To set the comments of the current node
7576
7677
Args:
@@ -79,7 +80,7 @@ def set_comments(self, comments: list[tuple[str,bool]]) -> None:
7980

8081
self.__comments = comments
8182

82-
def get_comments(self) -> list[tuple[str,bool]]:
83+
def get_comments(self) -> list[tuple[str, bool]]:
8384
"""To get the comments of the current node
8485
8586
Returns:
@@ -90,6 +91,7 @@ def get_comments(self) -> list[tuple[str,bool]]:
9091
return self.__comments
9192
return []
9293

94+
9395
class ConnectorNode(Node):
9496
"""The connector node, it is a part of the path
9597
@@ -142,7 +144,7 @@ def remove_child_variable_node(self, full_path: str) -> None:
142144
raise NodeNotFound(full_path)
143145

144146
def remove_child_section_node(self, name: str) -> None:
145-
"""Given a section nodes name, this method removes the section node from
147+
"""Given a section nodes name, this method removes the section node from
146148
the current nodes children
147149
148150
Args:
@@ -179,7 +181,7 @@ def __init__(self, name: str, data, data_type: Types) -> None:
179181
self.__data = data
180182

181183
def get_type(self) -> Types:
182-
"""Returns the data type of the variable
184+
"""Returns the data type of the variable
183185
184186
Returns:
185187
Types - the data type of the variable
@@ -236,7 +238,7 @@ def add_branch(self, contents: str) -> None:
236238
contents: str - the variables full path
237239
"""
238240

239-
if contents.count("=") >= 2: # To account for having equals in strings
241+
if contents.count("=") >= 2: # To account for having equals in strings
240242
equals_splits = contents.split("=")
241243
string_path = equals_splits[0]
242244
del equals_splits[0]

0 commit comments

Comments
 (0)