forked from door7302/transcode-sros
-
Notifications
You must be signed in to change notification settings - Fork 0
/
trancode-sros.py
executable file
·98 lines (88 loc) · 3.53 KB
/
trancode-sros.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
#!/usr/bin/env python3
# Allow to transcode a SROS config file collected by admin display-config in a flat config file
# The only limitation is do not have 4 space characters in your description as indentation is
# made of 4 space characters
#
# Vaersion 1.1
#
# Usage
# python transcode-sros.sh <your-config-file>
# or to save in file
# python transcode-sros.sh <your-config-file> > flat-config.txt
import fileinput
import re
import sys
# INIT file path
file_cfg = fileinput.input()
tab_LINE = []
index =0
last_indentation =0
indentation = 0
exit_found = False
for read in file_cfg :
# Read an config line
line = read
# Print lines already in the long format (beinging with "/") verbatim
if (re.search("^ *\/", line)):
print(line.strip())
continue
# Skip lines which are comments, echo statements of section headers (only "-" character)
isComment = re.search("^ *#", line)
isEcho = re.search("^ *echo", line)
isHeader = re.match("^-+$", line)
if isComment or isEcho or isHeader:
continue
if line.strip()!='' and line.strip() != 'exit all':
#SROS indentation is made of 4 space characters
indentation = line.count(' ')
#Manage line that are not made of an exit
if line.strip() != 'exit':
#If current line is more specific that previous one add word to the list
if ((indentation > last_indentation ) or indentation == 0 ):
#Add the / for configure statement
if line.strip() == 'configure':
line = '/'+ line
#manage line with create word differently
if 'create' in line.strip():
create_line = ' '.join(tab_LINE) + ' ' + line.strip()
print(create_line)
line = line.replace(' create','').strip()
if index>0 and 'create' in tab_LINE[index-1]:
print(' '.join(tab_LINE))
tab_LINE[index-1] = tab_LINE[index-1].replace(' create','')
tab_LINE.append(line.strip())
index+=1
exit_found = False
#If current line is less specific that previous one remove word to the list
if (indentation < last_indentation ):
del tab_LINE[index-1]
tab_LINE.append(line.strip())
#If current line is same specific that previous and depending on last exit found write config line in file
if ((last_indentation == indentation ) and indentation!=0 ):
if exit_found:
del tab_LINE[index-1]
tab_LINE.append(line.strip())
print(' '.join(tab_LINE))
else:
print(' '.join(tab_LINE))
del tab_LINE[index-1]
tab_LINE.append(line.strip())
last_indentation = indentation
#Manage line with an exit statement
else:
if exit_found != True:
if last_indentation != indentation:
print(' '.join(tab_LINE))
del tab_LINE[index-1]
index-=1
del tab_LINE[index-1]
index-=1
last_indentation = indentation - 1
exit_found = True
else:
exit_found = False
else:
del tab_LINE[index-1]
index-=1
last_indentation = indentation - 1
sys.stdout.close ()