@@ -6,7 +6,7 @@ class Icinga2Parser(object):
6
6
7
7
def parse (self , attrs , constants , indent = 0 ):
8
8
def attribute_types (attr ):
9
- if re .search (r'^[a-zA-Z0-9_]+ $' , attr ):
9
+ if re .search (r'^[a-zA-Z_][a-zA- Z0-9_]* $' , attr ):
10
10
result = attr
11
11
else :
12
12
result = '"' + attr + '"'
@@ -16,7 +16,8 @@ def value_types(value, indent=2):
16
16
# Values without quotes
17
17
if ((re .search (r'^-?\d+\.?\d*[dhms]?$' , value )) or
18
18
(re .search (r'^(true|false|null)$' , value )) or
19
- (re .search (r'^!?(host|service|user)\.' , value ))):
19
+ (re .search (r'^!?(host|service|user)\.' , value )) or
20
+ any (value .startswith (constant + '.' ) for constant in constants )):
20
21
result = value
21
22
elif (re .search (r'^(True|False)$' , value )):
22
23
result = value .lower ()
@@ -90,9 +91,9 @@ def parser(row):
90
91
def process_array (items , indent = 2 ):
91
92
result = ''
92
93
for item in items :
93
- if type (item ) is dict :
94
+ if isinstance (item , dict ) :
94
95
result += "\n %s{\n %s%s}, " % (' ' * indent , process_hash (attrs = item , indent = indent + 2 ), ' ' * indent )
95
- elif type (item ) is list :
96
+ elif isinstance (item , list ) :
96
97
result += "[ %s], " % (process_array (item .split (',' ), indent = indent + 2 ))
97
98
else :
98
99
result += "%s, " % (parser (str (item )))
@@ -105,7 +106,7 @@ def process_hash(attrs, level=3, indent=2, prefix=' '):
105
106
op = ''
106
107
107
108
for attr , value in attrs .items ():
108
- if type (value ) is dict :
109
+ if isinstance (value , dict ) :
109
110
if "+" in value :
110
111
del value ['+' ]
111
112
op = "+"
@@ -133,7 +134,7 @@ def process_hash(attrs, level=3, indent=2, prefix=' '):
133
134
else :
134
135
result += "%s%s %s= {\n %s%s}\n " % (
135
136
prefix , attribute_types (attr ), op , process_hash (attrs = value , indent = indent + 2 ), ' ' * indent )
136
- elif type (value ) is list and value :
137
+ elif isinstance (value , list ) and value :
137
138
if value [0 ] == "+" :
138
139
op = "+"
139
140
value .pop (0 )
@@ -181,13 +182,13 @@ def divide_chunks(l, n):
181
182
for x in value :
182
183
config += "%s%s %s\n " % (' ' * indent , attr + ' where' , parser (x ))
183
184
elif attr == 'vars' :
184
- if type (value ) is dict :
185
+ if isinstance (value , dict ) :
185
186
if "+" in value :
186
187
del value ['+' ]
187
188
config += process_hash (attrs = value , indent = indent + 2 , level = 1 , prefix = ("%s%s." % (' ' * indent , attr )))
188
- elif type (value ) is list :
189
+ elif isinstance (value , list ) :
189
190
for item in value :
190
- if type (item ) is str :
191
+ if isinstance (item , str ) :
191
192
config += "%s%s += %s\n " % (indent * ' ' , attr , re .sub (r'^[\+,-]\s+/' , '' , item ))
192
193
else :
193
194
if "+" in item :
@@ -202,15 +203,15 @@ def divide_chunks(l, n):
202
203
value = re .sub (r'^\+\s+' , '' , str (value ))
203
204
config += "%s%s %s= %s\n " % (' ' * indent , attr , op , parser (value ))
204
205
else :
205
- if type (value ) is dict :
206
+ if isinstance (value , dict ) :
206
207
if "+" in value :
207
208
op = '+'
208
209
del value ['+' ]
209
210
if bool (value ):
210
211
config += "%s%s %s= {\n %s%s}\n " % (' ' * indent , attr , op , process_hash (attrs = value , indent = indent + 2 ), ' ' * indent )
211
212
else :
212
213
config += "%s%s %s= {}\n " % (' ' * indent , op , attr )
213
- elif type (value ) is list :
214
+ elif isinstance (value , list ) :
214
215
if value :
215
216
if value [0 ] == "+" :
216
217
op = "+"
0 commit comments