X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=sfatables%2Fcommands%2FAdd.py;h=f5193eb6d8a7fe5ca558c2521b710baa7358a9f3;hb=6c41d17ff2f98c362ad1db112d22bede960d8202;hp=a8a63dc61bcef5fb38d59e468a3479c5eadb5971;hpb=cf172a653c1d69c73fe5ff7cd93b4cee8187ab79;p=sfa.git diff --git a/sfatables/commands/Add.py b/sfatables/commands/Add.py index a8a63dc6..f5193eb6 100644 --- a/sfatables/commands/Add.py +++ b/sfatables/commands/Add.py @@ -13,12 +13,12 @@ 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 (root, dirs, files) in os.walk(dir): for file in files: - if (file.startswith('sfatables-')): + if (file.startswith('sfatables-') and file.endswith(type)): number_str = file.split('-')[1] number = int(number_str) if (number>last_rule_number): @@ -26,29 +26,33 @@ class Add(Command): return "sfatables-%d-%s"%(last_rule_number+1,type) - def call_gen(self, dir, options): + def call_gen(self, chain, type, dir, options): filename = dir + "/"+options.name+".xml" xmldoc = libxml2.parseFile(filename) p = xmldoc.xpathNewContext() supplied_arguments = options.arguments + if (hasattr(options,'element') and options.element): + element = options.element + else: + element='*' + for option in supplied_arguments: option_name = option['name'] option_value = getattr(options,option_name) if (hasattr(options,option_name)): - context = p.xpathEval("//rule/argument[name='%s']"%option_name) + context = p.xpathEval("//rule[@element='%s' or @element='*']/argument[name='%s']"%(element, option_name)) if (not context): - raise Exception('Unknown option %s for match %s'%(option,option['name'])) + raise Exception('Unknown option %s for match %s and element %s'%(option,option['name'], element)) else: # Add the value of option valueNode = libxml2.newNode('value') valueNode.addContent(option_value) context[0].addChild(valueNode) - chain = command_options.args[0] - filename = self.getnextfilename('match',chain) + filename = self.getnextfilename(type,chain) file_path = sfatables_config + '/' + chain + '/' + filename xmldoc.saveFile(file_path) p.xpathFreeContext() @@ -57,8 +61,9 @@ class Add(Command): return True def call(self, command_options, match_options, target_options): - ret = self.call_gen(match_dir, match_options) + chain = command_options.args[0] + ret = self.call_gen(chain, 'match',match_dir, match_options) if (ret): - ret = self.call_gen(target_dir, target_options) + ret = self.call_gen(chain, 'target',target_dir, target_options) return ret