This repository has been archived by the owner on Mar 3, 2021. It is now read-only.
forked from ckishimo/juniper_display_set
-
Notifications
You must be signed in to change notification settings - Fork 0
/
junos-converter-30.py
63 lines (57 loc) · 1.74 KB
/
junos-converter-30.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
#!/usr/bin/python
#
#
# Copyright (c) 2017 [email protected]
#
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.
import sys
def print_set_command (lcommands, leaf):
c = ' '
c = c.join(lcommands)
print(("%s %s" % (c, leaf)))
return
def get_set_config (filein):
try:
f = open(filein, 'r')
except IOError:
print("Error: could not read input file:", filein)
sys.exit()
data = f.read()
f.close()
l = data.split('\n')
lres = ['set']
for i in range(len(l)):
elem = l[i]
if (not elem.startswith('##')) and (not elem.startswith('/*')):
clean_elem = elem.strip('\t\n\r{ ')
if ("inactive" in clean_elem):
clean_elem = clean_elem.replace("inactive: ","")
linactive = list(lres)
linactive[0] = "deactivate"
print_set_command(linactive, clean_elem)
if ("protect" in clean_elem):
clean_elem = clean_elem.replace("protect: ","")
lprotect = list(lres)
lprotect[0] = "protect"
print_set_command(lprotect, clean_elem)
if ';' in clean_elem: # this is a leaf
print_set_command(lres, clean_elem.split(";")[0])
elif clean_elem == '}': # Up one level remove parent
lres.pop()
else:
lres.append(clean_elem)
if len(sys.argv) != 2:
print("Usage: %s FILEIN\n" % sys.argv[0])
else:
get_set_config(sys.argv[1])