From 2e0fbfbdfd45bf70b92d8fa3866bcbbd2f49fe64 Mon Sep 17 00:00:00 2001 From: Sapan Bhatia Date: Wed, 2 Sep 2009 21:59:00 +0000 Subject: [PATCH] --- sfatables/commands/Add.py | 17 +++++----- sfatables/commands/List.py | 69 +++++++++++++++++++++++++++++++++++--- sfatables/matches/hrn.xml | 2 +- sfatables/pretty.py | 40 ++++++++++++++++++++++ 4 files changed, 113 insertions(+), 15 deletions(-) create mode 100644 sfatables/pretty.py diff --git a/sfatables/commands/Add.py b/sfatables/commands/Add.py index d67b54f6..b62ad6c4 100644 --- a/sfatables/commands/Add.py +++ b/sfatables/commands/Add.py @@ -16,19 +16,19 @@ class Add(Command): dir = sfatables_config + chain; last_rule_number = 1 - for file in os.walk(dir): - if (file.startswith('sfatables-')): - number_str = file.split('-')[1] - number = int(number_str) - if (number>last_rule_number): - last_rule_number = number + for (root, dirs, files) in os.walk(dir): + for file in files: + if (file.startswith('sfatables-')): + number_str = file.split('-')[1] + number = int(number_str) + if (number>last_rule_number): + last_rule_number = number return "sfatables-%d-%s"%(last_rule_number,type) def call(self, command_options, match_options, target_options): - import pdb filename = match_dir + "/"+match_options.match_name+".xml" xmldoc = libxml2.parseFile(filename) @@ -49,10 +49,9 @@ class Add(Command): valueNode.addContent(option_value) context[0].addChild(valueNode) - pdb.set_trace() chain = command_options.args[0] filename = self.getnextfilename('match',chain) - xmldoc.saveFile(match_dir + '/' + chain + '/' + filename) + xmldoc.saveFile(filename) p.xpathFreeContext() xmldoc.freeDoc() diff --git a/sfatables/commands/List.py b/sfatables/commands/List.py index b3385e4c..3a0bb396 100644 --- a/sfatables/commands/List.py +++ b/sfatables/commands/List.py @@ -1,4 +1,9 @@ import os, time +import libxml2 +import pdb + +from globals import * +from pretty import Pretty from sfatables.command import Command class List(Command): @@ -11,10 +16,64 @@ class List(Command): def __init__(self): return - def call(self): - # Override this function - return True - def __call__(self, option, opt_str, value, parser, *args, **kwargs): + def get_info(self, xmlextension_path): + xmldoc = libxml2.parseFile(xmlextension_path) + p = xmldoc.xpathNewContext() + + ext_name_node = p.xpathEval("/match/@name") + ext_name = ext_name_node[0].content + + name_nodes = p.xpathEval("//rule/argument[value!='']/name") + value_nodes = p.xpathEval("//rule/argument[value!='']/value") + + names = [n.content for n in name_nodes] + values = [v.content for v in value_nodes] + + name_values = zip(names,values) + name_value_pairs = map(lambda (n,v):n+'='+v, name_values) + + argument_str = ",".join(name_value_pairs) + + p.xpathFreeContext() + xmldoc.freeDoc() + + return {'name':ext_name, 'arguments':'argument_str'} + + def call(self, command_options, match_options, target_options): + chain = command_options.args[0] + chain_dir = sfatables_config + "/" + chain + rule_list = [] + broken_semantics = os.walk(chain_dir) + for (root, dirs, files) in broken_semantics: + for file in files: + if (file.startswith('sfatables')): + (magic,number,type) = file.split('-') + rule_list.append(int(number)) + + rule_list.sort() + + pretty = Pretty(['Rule','Match','Target','Arguments']) + + for number in rule_list: + match_file = "sfatables-%d-%s"%(number,'match') + target_file = "sfatables-%d-%s"%(number,'target') + + match_path = sfatables_config + '/' + chain + '/' + match_file + target_path = sfatables_config + '/' + chain + '/' + target_file + + match_info = self.get_info (match_path) + target_info = self.get_info (target_path) + + pretty.push_row(["%d"%number, match_info['name'], match_info['arguments'], target_info['arguments']]) + + + pretty.pprint() + + + + + + + - return self.call(option) diff --git a/sfatables/matches/hrn.xml b/sfatables/matches/hrn.xml index 5a7121c3..e6444e89 100644 --- a/sfatables/matches/hrn.xml +++ b/sfatables/matches/hrn.xml @@ -7,7 +7,7 @@ as the one matched by this rule. --> - + diff --git a/sfatables/pretty.py b/sfatables/pretty.py new file mode 100644 index 00000000..9a896ee3 --- /dev/null +++ b/sfatables/pretty.py @@ -0,0 +1,40 @@ +#!/usr/bin/python + +class Pretty: + rows = [] + column_width = [] + + def __init__(self, header): + self.rows.append(header) + for c in header: + self.column_width.append(len(header)) + + def push_row (self, row): + self.rows.append(row) + num = 0 + for c in row: + if (self.column_width[num] < len(c)): + self.column_width[num] = len(c) + num = num + 1 + return + + def pprint (self): + print '\n' + + for rule in self.rows: + cur_line = "" + num = 0 + + import pdb + pdb.set_trace() + for r in rule: + cur_line = cur_line + "%s "%r + if (self.column_width[num] > len(r)): + padding0 = ''.zfill(self.column_width[num] - len(r)) + padding = padding0.replace('0',' ') + cur_line = cur_line + padding + num = num + 1 + + print cur_line + + -- 2.43.0