forked from Qalthos/linkybook.utils
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.py
104 lines (86 loc) · 3.38 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
from textwrap import dedent
data = dedent("""configure ethernet
#-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
echo "ethernet"
#-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
line 1/1/8/1
port-type uni
admin-up
mau 1
type 1000basebx10d
power up
exit
exit
line 1/1/8/2
port-type uni
admin-up
mau 1
type 1000basebx10d
power up
exit
exit
#-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
""")
from copy import deepcopy
from operator import contains
from anytree import Node, PreOrderIter, RenderTree
import debugpy
from ansible.module_utils.six import iteritems
# Counts the number of spaces at the beginning of a string
def _count_spaces(line):
spaces = 0
for char in line:
if char == ' ':
spaces += 1
else:
break
return spaces
# Parses the config into a tree structure and cleans up the config so that only relevant data is returned
# The tree structure is determined by the number of spaces at the beginning of each line
def _parse_config_to_tree( config):
if not config:
return None
chapter = 0
last_spaces = 0
root = None
parent_node = None
for line in config.splitlines():
# Check if line is valid
if line.startswith('echo') or line.startswith('#'):
continue
# Only here the actual content begins
# Check if parent node exists. Otherwise create it using first line
if parent_node is None:
root = Node(line.split('#',1)[0].strip())
parent_node = root
prev_node = root
# The "exit" string is not relevant for us but we need to keep track of the indentation. If the indent is smaller than the previous line, we need to go up the tree
elif "exit" in line:
if _count_spaces(line) < last_spaces:
parent_node = parent_node.parent
else:
continue
# Check if line is a child of the previous line as a greater indent means the line has to be a child of the previous line
elif _count_spaces(line) > last_spaces:
parent_node = prev_node
prev_node = Node(line.split('#',1)[0].strip(), parent=prev_node)
# In all other cases the current line is a sibling of the previous line
else:
prev_node = Node(line.split('#',1)[0].strip(), parent=parent_node)
# In any case, the indentation spaces of the current line become the last spaces to be used for the next line
last_spaces = _count_spaces(line)
print(RenderTree(root))
return root
def _flatten_config(config):
if not config:
return None
flat_config = []
root = _parse_config_to_tree(config)
for leave in root.leaves:
line = []
for node in leave.path:
line.append(node.name)
flat_config.append(" ".join(line))
return flat_config
out = _flatten_config( data)
print(out)