X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=sfatables%2Fcommands%2FAdd.py;h=4f0e6067cd3406b6fdae7888edad08d58c4ac050;hb=7c7b4138dc8c6183f68e78e64eb616a1fdbafcbc;hp=c13c714a677473bbc04141ea28c5aa6b140ac733;hpb=b46f8ee1c69124fb2f99384ebb59c1f09fc459db;p=sfa.git diff --git a/sfatables/commands/Add.py b/sfatables/commands/Add.py index c13c714a..4f0e6067 100644 --- a/sfatables/commands/Add.py +++ b/sfatables/commands/Add.py @@ -13,33 +13,31 @@ class Add(Command): return def getnextfilename(self,type,chain): - dir = sfatables_config + chain; - last_rule_number = 1 + dir = sfatables_config + "/"+chain; + last_rule_number = 0 - 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-') and file.endswith(type)): + 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) + return "sfatables-%d-%s"%(last_rule_number+1,type) - - - def call(self, command_options, match_options, target_options): - import pdb - filename = match_dir + "/"+match_options.match_name+".xml" + def call_gen(self, chain, type, dir, options): + filename = dir + "/"+options.name+".xml" xmldoc = libxml2.parseFile(filename) p = xmldoc.xpathNewContext() - supplied_arguments = match_options.arguments + supplied_arguments = options.arguments for option in supplied_arguments: option_name = option['name'] - option_value = getattr(match_options,option_name) + option_value = getattr(options,option_name) - if (hasattr(match_options,option_name)): + if (hasattr(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'])) @@ -49,13 +47,18 @@ 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(filename) + filename = self.getnextfilename(type,chain) + file_path = sfatables_config + '/' + chain + '/' + filename + xmldoc.saveFile(file_path) p.xpathFreeContext() xmldoc.freeDoc() - return True + def call(self, command_options, match_options, target_options): + chain = command_options.args[0] + ret = self.call_gen(chain, 'match',match_dir, match_options) + if (ret): + ret = self.call_gen(chain, 'target',target_dir, target_options) + + return ret