Skip to content

Commit 98977aa

Browse files
committed
Treescriptify:
*u tree_reader.py *u tree_runner.py
1 parent 91cc6ad commit 98977aa

File tree

2 files changed

+25
-15
lines changed

2 files changed

+25
-15
lines changed

treescriptify/tree_reader.py

+16-7
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,21 @@
77

88

99
def generate_from_json(json_string: str) -> Generator[TreeNodeData, None, None]:
10+
"""Read the JSON string and generate TreeNodeData for all elements.
1011
"""
11-
"""
12-
tree_son, report = json.loads(json_string)
13-
tree_son = tree_son['contents']
14-
depth = 0
15-
# todo:
16-
for i in tree_son:
12+
full_json = json.loads(json_string)[0]
13+
for i in full_json['contents']:
14+
for node in _process_node(i, 0):
15+
yield node
16+
1717

18-
pass
18+
def _process_node(node: dict, depth: int) -> Generator[TreeNodeData, None, None]:
19+
"""
20+
"""
21+
if node['type'] == 'file':
22+
yield TreeNodeData(depth, False, node['name'])
23+
else:
24+
yield TreeNodeData(depth, True, node['name'])
25+
for nested_nodes in node.get('contents', []):
26+
for node in _process_node(nested_nodes, depth + 1):
27+
yield node

treescriptify/tree_runner.py

+9-8
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,27 @@ def get_tree_json(data: InputData) -> str:
99
"""Obtain the Tree Information as a JSON string.
1010
"""
1111
result = subprocess.run(
12-
args=['tree', '-J', '-i', '-x', '--noreport'] + _check_arguments(data),
12+
args='tree -Jix --noreport ' + _check_arguments(data),
1313
capture_output=True,
1414
text=True,
15-
shell=False,
15+
shell=True,
16+
timeout=3
1617
)
1718
output = result.stdout
1819
#error = result.stderr
1920
return output
2021

2122

22-
def _check_arguments(data: InputData) -> list[str]:
23+
def _check_arguments(data: InputData) -> str:
2324
"""Check the Input Data and map flags to tree command.
2425
"""
2526
extras = []
2627
if data.directories_only:
27-
extras += '-d'
28+
extras.append('-d ')
2829
if data.prune_dirs:
29-
extras += '--prune'
30+
extras += ['--prune ']
3031
if data.include_hidden:
31-
extras += '-a'
32+
extras += ['-a ']
3233
if data.git_ignore:
33-
extras += '--gitignore'
34-
return extras
34+
extras.append('--gitignore ')
35+
return ' '.join(extras)

0 commit comments

Comments
 (0)