2020
2121simple = False
2222show_attributes = True
23-
2423gformat = '''
2524 graph [
2625 size="8.27";
2726 ratio="1";
2827 nodesep="0.15";
29- ranksep="0.5";
30- #splines="false";
28+ ranksep="0.5";
29+ #splines="false";
3130 rankdir=LR;
3231 bgcolor="transparent";
3332 ];
4241 ]
4342 '''
4443
45- def format_attr (policy ,attr ):
4644
47- if not show_attributes :
48- return '<<B>{0}</B>>' .format (policy )
45+ def format_attr (policy , attr ):
46+
47+ if not show_attributes :
48+ return '<<B>{0}</B>>' .format (policy )
4949
50- attr_table = '<<TABLE BORDER="0" CELLBORDER="0" CELLSPACING="1" CELLPADDING="1">'
51- attr_table = attr_table + '<TR><TD ALIGN="LEFT" COLSPAN="2"><B>{0}</B></TD><TD></TD></TR>' .format (policy )
52- attr_table = attr_table + '<TR><TD></TD><TD></TD></TR>'
50+ attr_table = '<<TABLE BORDER="0" CELLBORDER="0" CELLSPACING="1" CELLPADDING="1">'
51+ attr_table = attr_table + \
52+ '<TR><TD ALIGN="LEFT" COLSPAN="2"><B>{0}</B></TD><TD></TD></TR>' .format (
53+ policy )
54+ attr_table = attr_table + '<TR><TD></TD><TD></TD></TR>'
5355
54- for k , v in attr .items ():
55- if k not in ['status' ] and v :
56- attr_table = attr_table + '<TR><TD ALIGN="LEFT">{0}</TD><TD ALIGN="LEFT">: {1}</TD></TR>' .format (k ,(v [:20 ] + '..' ) if len (v ) > 20 else v )
56+ for k , v in attr .items ():
57+ if k not in ['status' ] and v :
58+ attr_table = attr_table + \
59+ '<TR><TD ALIGN="LEFT">{0}</TD><TD ALIGN="LEFT">: {1}</TD></TR>' .format (
60+ k , (v [:20 ] + '..' ) if len (v ) > 20 else v )
5761
58- attr_table = attr_table + '</TABLE>>'
62+ attr_table = attr_table + '</TABLE>>'
63+
64+ return attr_table
5965
60- return attr_table
6166
6267def iterd (d , i ):
6368
64- #print('# Index: {0}, {1}'.format(i, d))
65- for k , v in d .items ():
69+ for k , v in d .items ():
70+
71+ if isinstance (v , dict ):
72+ attr = v .get ("attributes" )
6673
67- if isinstance (v , dict ):
68- attr = v .get ("attributes" )
69-
70- # Write Node Properties
71- if attr :
72- print ('{0} [label={1};]' .format (k + str (i ), format_attr (k , attr )))
73- else :
74- print ('{0} [label="{0}";]' .format (k + str (i )))
74+ if attr :
75+ print ('{0} [label={1};]' .format (
76+ k + str (i ), format_attr (k , attr )))
77+ else :
78+ print ('{0} [label="{0}";]' .format (k + str (i )))
7579
76- children = v .get ("children" )
80+ children = v .get ("children" )
81+
82+ if children :
83+ childi = 0
84+ for child in children :
85+ if not simple :
86+ childi = childi + 1
87+
88+ for childk , childv in child .items ():
89+ print ('{0} -> {1}' .format (k +
90+ str (i ), childk + str (childi )))
91+ iterd (child , childi )
7792
78- if children :
79- i2 = 0
80- for child in children :
81- if not simple : i2 = i2 + 1
82- for childk , childv in child .items ():
83- print ('{0} -> {1}' .format (k + str (i ), childk + str (i2 )))
84- iterd (child , i2 )
8593
8694def write_gformat ():
87- with open (".aci2dot" , "wt" ) as text_file :
88- text_file .write (gformat )
95+ with open (".aci2dot" , "wt" ) as text_file :
96+ text_file .write (gformat )
97+
8998
9099def main ():
91100
92- global simple
93- global show_attributes
94- global gformat
95- parser = argparse .ArgumentParser (description = 'Create DOT formatted Graph from JSON formatted ACI policy export.' )
96- parser .add_argument ('policy_file' , type = argparse .FileType ('r' ), help = 'JSON ACI Policy Filename' )
97- parser .add_argument ('--nr' , action = 'store_true' , help = 'Suppress redundant children' )
98- parser .add_argument ('--na' , action = 'store_true' , help = "Don't show attributes" )
99- parser .add_argument ('--write' , action = 'store_true' , help = "Write config template to .aci2dot and exit" )
100- group = parser .add_mutually_exclusive_group ()
101- group .add_argument ('--stdout' , action = 'store_true' , help = "Write to STDOUT instead of to file" )
102- choices = ["svg" , "png" , "pdf" ]
103- group .add_argument ('--dot' , choices = choices , help = "Also write SVG/PNG/PDF. 'dot' needs to be installed." )
104-
105- args = parser .parse_args ()
106-
107- #print(args.dot)
108- #sys.exit()
109-
110- if args .write :
111- write_gformat ()
112- sys .exit ('Config template written to .aci2dot' )
113-
114- simple = args .nr
115- show_attributes = not args .na if not args .nr else False
116- base_name = (os .path .splitext (args .policy_file .name )[0 ])
117-
118- try :
119- data = json .load (args .policy_file )
120- except :
121- sys .exit ('JSON File can not be read.' )
122-
123- try :
124- with open (".aci2dot" , 'r' ) as config_file :
125- gformat = config_file .read ()
126- print ("Graph config read from .aci2dot" , file = sys .stderr )
127- except IOError :
128- pass
129-
130- if not args .stdout :
131- sys .stdout = open ('{0}.dot' .format (base_name ), 'w' )
132-
133- print ('strict digraph Policy {' )
134- print (gformat )
135- iterd (data , 0 )
136- print ('}' , flush = True )
137-
138- if args .dot :
139- os .system ("dot -T{0} -o{1}.{0} {1}.dot" .format (args .dot , base_name ))
140- #print(("dot -T{0} -o{1}.svg {1}.dot".format(args.dot, base_name)))
141- print ("{0} exported to {1}.{0}" .format (args .dot , base_name ), file = sys .stderr )
101+ global simple
102+ global show_attributes
103+ global gformat
104+
105+ parser = argparse .ArgumentParser (
106+ description = 'Create DOT formatted Graph from JSON formatted ACI policy export.' )
107+ parser .add_argument ('policy_file' , type = argparse .FileType (
108+ 'r' ), help = 'JSON ACI Policy Filename' )
109+ parser .add_argument ('--nr' , action = 'store_true' ,
110+ help = 'Suppress redundant children' )
111+ parser .add_argument ('--na' , action = 'store_true' ,
112+ help = "Don't show attributes" )
113+ parser .add_argument ('--write' , action = 'store_true' ,
114+ help = "Write config template to .aci2dot and exit" )
115+ group = parser .add_mutually_exclusive_group ()
116+ group .add_argument ('--stdout' , action = 'store_true' ,
117+ help = "Write to STDOUT instead of to file" )
118+ choices = ["svg" , "png" , "pdf" ]
119+ group .add_argument ('--dot' , choices = choices ,
120+ help = "Also write SVG/PNG/PDF. 'dot' needs to be installed." )
121+
122+ args = parser .parse_args ()
123+
124+ if args .write :
125+ write_gformat ()
126+ sys .exit ('Config template written to .aci2dot' )
127+
128+ simple = args .nr
129+ show_attributes = not args .na if not args .nr else False
130+
131+ try :
132+ data = json .load (args .policy_file )
133+ except json .decoder .JSONDecodeError :
134+ sys .exit ('JSON File can not be read.' )
135+
136+ base_name = (os .path .splitext (args .policy_file .name )[0 ])
137+
138+ try :
139+ with open (".aci2dot" , 'r' ) as config_file :
140+ gformat = config_file .read ()
141+ print ("Graph config read from .aci2dot" , file = sys .stderr )
142+ except IOError :
143+ pass
144+
145+ if not args .stdout :
146+ sys .stdout = open ('{0}.dot' .format (base_name ), 'w' )
147+
148+ print ('strict digraph Policy {' )
149+ print (gformat )
150+ iterd (data , 0 )
151+ print ('}' , flush = True )
152+
153+ if args .dot :
154+ os .system ("dot -T{0} -o{1}.{0} {1}.dot" .format (args .dot , base_name ))
155+ print ("{0} exported to {1}.{0}" .format (
156+ args .dot , base_name ), file = sys .stderr )
157+
142158
143159if __name__ == '__main__' :
144- main ()
160+ main ()
0 commit comments