6 from pretty import Pretty
7 from sfatables.command import Command
10 options = [('-L','--list')]
19 def get_info(self, type, xmlextension_path):
20 xmldoc = libxml2.parseFile(xmlextension_path)
21 p = xmldoc.xpathNewContext()
23 ext_name_node = p.xpathEval("/%s/@name"%type)
24 ext_name = ext_name_node[0].content
26 name_nodes = p.xpathEval("//rule/argument[value!='']/name")
27 value_nodes = p.xpathEval("//rule/argument[value!='']/value")
29 names = [n.content for n in name_nodes]
30 values = [v.content for v in value_nodes]
32 name_values = zip(names,values)
33 name_value_pairs = map(lambda (n,v):n+'='+v, name_values)
35 argument_str = ",".join(name_value_pairs)
40 return {'name':ext_name, 'arguments':argument_str}
42 def get_rule_list(self, chain_dir_path):
43 broken_semantics = os.walk(chain_dir_path)
46 for (root, dirs, files) in broken_semantics:
48 if (file.startswith('sfatables')):
49 (magic,number,type) = file.split('-')
50 rule_numbers[int(number)]=1
52 rule_list = rule_numbers.keys()
56 def call(self, command_options, match_options, target_options):
57 if (len(command_options.args) < 1):
58 print "Please specify the name of the chain you would like to list, e.g. sfatables -L INCOMING."
61 chain = command_options.args[0]
62 chain_dir = sfatables_config + "/" + chain
64 rule_list = self.get_rule_list(chain_dir)
66 pretty = Pretty(['Rule','Match','Arguments','Target','Arguments'])
68 for number in rule_list:
69 match_file = "sfatables-%d-%s"%(number,'match')
70 target_file = "sfatables-%d-%s"%(number,'target')
72 match_path = sfatables_config + '/' + chain + '/' + match_file
73 target_path = sfatables_config + '/' + chain + '/' + target_file
75 match_info = self.get_info ('match',match_path)
76 target_info = self.get_info ('target',target_path)
78 pretty.push_row(["%d"%number, match_info['name'], match_info['arguments'], target_info['name'], target_info['arguments']])