From b46f8ee1c69124fb2f99384ebb59c1f09fc459db Mon Sep 17 00:00:00 2001 From: Sapan Bhatia Date: Wed, 2 Sep 2009 03:14:00 +0000 Subject: [PATCH] --- sfatables/commands/Add.py | 46 ++++++++++++++++++++++++++++++++++----- sfatables/sfatables | 24 ++++++++++++++++++-- sfatables/xmlextension.py | 3 +-- 3 files changed, 63 insertions(+), 10 deletions(-) diff --git a/sfatables/commands/Add.py b/sfatables/commands/Add.py index ef2b26e2..c13c714a 100644 --- a/sfatables/commands/Add.py +++ b/sfatables/commands/Add.py @@ -1,7 +1,7 @@ import os, time +import libxml2 from sfatables.command import Command - -import sfatables.globals +from sfatables.globals import * class Add(Command): options = [('-A','--add')] @@ -12,15 +12,49 @@ class Add(Command): def __init__(self): return + def getnextfilename(self,type,chain): + 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 + + return "sfatables-%d-%s"%(last_rule_number,type) + + + def call(self, command_options, match_options, target_options): - filename = match_dir + "/"+match_options.name+".xml" + import pdb + filename = match_dir + "/"+match_options.match_name+".xml" xmldoc = libxml2.parseFile(filename) - p = self.xmldoc.xpathNewContext() + p = xmldoc.xpathNewContext() + + supplied_arguments = match_options.arguments + for option in supplied_arguments: + option_name = option['name'] + option_value = getattr(match_options,option_name) + + if (hasattr(match_options,option_name)): + context = p.xpathEval("//rule/argument[name='%s']"%option_name) + if (not context): + raise Exception('Unknown option %s for match %s'%(option,option['name'])) + else: + # Add the value of option + valueNode = libxml2.newNode('value') + valueNode.addContent(option_value) + context[0].addChild(valueNode) - context = p.xpathEval("//rule/argument[name='user-hrn']") pdb.set_trace() - + chain = command_options.args[0] + filename = self.getnextfilename('match',chain) + xmldoc.saveFile(filename) + p.xpathFreeContext() + xmldoc.freeDoc() return True diff --git a/sfatables/sfatables b/sfatables/sfatables index 59628e75..f4bb8a81 100755 --- a/sfatables/sfatables +++ b/sfatables/sfatables @@ -78,7 +78,8 @@ def main(): command_dict = load_commands("sfatables.commands",commands.all) command_parser = create_parser(command_dict) - (options, args) = command_parser.parse_args() + (options, args) = command_parser.parse_args(pargs[0]) + setattr(options, 'args', args) command = command_dict[options.command] @@ -89,8 +90,17 @@ def main(): match_parser = create_parser_xml_ext(match_dict) matches_str = ",".join(match_dict.keys()) match_parser.add_option('-m','--match',dest='match_name',help='Match name (one of %s)'%matches_str, metavar = 'MATCH') - (match_options, args) = match_parser.parse_args(pargs[1]) + try: + match_name = match_options.match_name + except Exception: + print "Must specify match name with -m" + + if (match_dict.has_key(match_name)): + setattr(match_options, 'arguments', match_dict[match_name].arguments) + else: + raise Exception('Match %s not found'%match_name) + else: match_options=None @@ -102,6 +112,16 @@ def main(): targets_str = ",".join(target_dict.keys()) target_parser.add_option('-j','--jump',dest='target_name',help='Target name (one of %s)'%targets, metavar = 'TARGET') (target_options, args) = target_parser.parse_args(pargs[2]) + try: + target_name = target_options.target_name + except Exception: + print "Must specify target name with -m" + +# if (target_dict.has_key(target_name)): +# setattr(target_options, 'arguments', target_dict[target_name].arguments) +# else: +# raise Exception('Target %s not found'%target_name) + else: target_options = None diff --git a/sfatables/xmlextension.py b/sfatables/xmlextension.py index c852a268..84e336fa 100644 --- a/sfatables/xmlextension.py +++ b/sfatables/xmlextension.py @@ -5,8 +5,7 @@ # - The parameters that the processor needs to evaluate the context import libxml2 - -match_dir = 'matches' +from sfatables.globals import * class Xmlextension: context = "" -- 2.43.0