-
Notifications
You must be signed in to change notification settings - Fork 1
/
yamlify.py
executable file
·137 lines (117 loc) · 4.02 KB
/
yamlify.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/usr/bin/env python3
import sys
import re
if len(sys.argv) != 2:
print("Syntax: ./yamilfy.py <infile>")
raise
else:
try:
infile = open(sys.argv[1], 'r')
except(NameError, IOError):
print("Couldn't find %s" % infile)
raise
try:
outfile = open(sys.argv[2], 'w')
except:
#print("Couldn't open output file for writing")
## Not writing to file just yet. Just printing to stdout
pass
# Chars to strip from either end of an element
stripchars = '[](),\'\ \"'
# Used to strip whole elements from a list.
def strip(x):
unwanted = {'[', ']', '(', ')', 'qw', 'qw(', '[(', ')]', '),', ',', '\'', ' '}
if x in unwanted:
return True
else:
return False
def splitPortRange(item):
# find and replace port ranges using <, > or - notation
newItem =[]
if item[:1] == "<":
item = int(item[1:])
newItem.insert(0, 1)
newItem.insert(1, item)
elif item[:1] == ">":
item = int(item[1:])
newItem.insert(0, item)
newItem.insert(1, 65535)
elif re.search("-", item):
try: # Don't like port zero as a lower port in range
newItem = [int(n) for n in item.split("-")]
if newItem[0] == 0:
newItem[0] = 1
except:
return item
else:
return item
return newItem
def output(entry):
var = entry.pop(0) # Get the variable name
del entry[0] # Get rid of the '=''
# Check for any empty variables
if not entry or entry[0] == '':
print("WARNING: %s is empty" % var)
return
print("%s: " % var, end='') # Print without newlines
# Catch port ranges
if isinstance(entry, list) and len(entry) == 2 and all(isinstance(x, int) for x in entry):
print("\n startrange: \"%s\"" % entry[0])
print(" endrange: \"%s\"" % entry[1])
elif isinstance(entry, list) and len(entry) > 1:
print()
for value in entry:
print(" - \"%s\"" % value)
elif isinstance(entry, list):
try:
print("\"%s\"" % entry[0])
except:
print(entry)
raise
else:
print("\"%s\"" % entry)
def main():
print("---")
for line in infile:
# Skip comments and newlines
if re.match("^#", line) or line == "\n":
continue
entry = line.split()
if entry[1] != "=":
print("Skipped %s as it didn't follow format: 'variable = value'" % line)
continue
# Remove $ prefix from variable names
if entry[0][:1] == "$":
entry[0] = entry[0][1:]
# Iterate through values and s/$/{{}}/
# Always work on the outer entry
for item in entry:
idx = entry.index(item) # find index before we mangle the value
if isinstance(item, int):
continue # We don't need to parse integers
entry[idx] = item.strip(stripchars)
newItem = splitPortRange(entry[idx])
if isinstance(newItem, str):
entry[idx] = newItem
elif isinstance(newItem, list):
entry[idx] = newItem[0]
entry.append(newItem[1])
# Clean up any empty entries after we've stripped unwanted chars
if entry[idx] is "":
del entry[idx]
# Now check for variable after we've stripped quotes
for item in entry:
if not isinstance(item, str):
continue
else:
idx = entry.index(item)
if entry[idx][:1] == "$":
entry[idx] = entry[idx][1:] # strip leading $ and add curlies
entry[idx] = "{{ %s }}" % entry[idx].strip(stripchars)
# If line contains more than just "variable = value", remove unwanted entries
if len(entry) > 3:
entry[:] = [x for x in entry if not strip(x)]
output(entry)
print("...")
if __name__ == "__main__":
main()