Added element support to the main sfatables file
[sfa.git] / sfatables / commands / Add.py
index 36b22c3..f5193eb 100644 (file)
@@ -14,11 +14,11 @@ class Add(Command):
 
     def getnextfilename(self,type,chain):
         dir = sfatables_config + "/"+chain;
-        last_rule_number = 1
+        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):
@@ -33,14 +33,19 @@ class Add(Command):
         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')