a little nicer wrt pep8
[sfa.git] / sfatables / runtime.py
index 8f5c323..f773b0d 100644 (file)
@@ -1,15 +1,14 @@
-#!/usr/bin/python
+#!/usr/bin/env python3
 
 import sys
 import os
-import pdb
+
 import libxml2
+import libxslt
 
-from optparse import OptionParser
-from sfatables import commands
-from sfatables.globals import *
-from sfatables.commands.List import *
-from sfatables.xmlrule import *
+from sfatables.globals import sfatables_config
+from sfatables.commands.List import List
+from sfatables.xmlrule import XMLRule
 
 class SFATablesRules:
     def __init__(self, chain_name):
@@ -45,18 +44,20 @@ class SFATablesRules:
 
     def create_xml_node(self, name, context_dict):
         node = libxml2.newNode(name)
-        for k in context_dict.keys():
+        for k in list(context_dict.keys()):
             if (type(context_dict[k])==dict):
                 childNode = self.create_xml_node(k, context_dict[k])
                 node.addChild(childNode)
             else:
-                node.addContent(context_dict[k])
+                childNode = libxml2.newNode(k)
+                childNode.addContent(context_dict[k])
+                node.addChild(childNode)
         return node
                 
     def add_request_context_to_rspec(self, doc):
         p = doc.xpathNewContext()
-        context = p.xpathEval("//rspec")
-        if (not context):
+        context = p.xpathEval("//*")
+        if not context or context[0].name not in ['RSpec', 'rspec']:
             raise Exception('Request is not an rspec')
         else:
             # Add the request context
@@ -65,60 +66,43 @@ class SFATablesRules:
         p.xpathFreeContext()
         return doc
 
-    def add_rule_context_to_rspec(self,arguments, doc):
-        p = doc.xpathNewContext()
-        context = p.xpathEval("//rspec")
-        if (not context):
-            raise Exception('Request is not an rspec')
-        else:
-            # Add the request context
-            ruleNode = libxml2.newNode('rule-context')
-            ac = self.active_context
-            for k in ac:
-                argumentNode = libxml2.newNode('argument')
-                nameNode = libxml2.newNode('name')
-                nameNode.addContent(k)
-                valueNode = libxml2.newNode('value')
-                valueNode.addContent(ac[k])
-                argumentNode.addChild(nameNode)
-                argumentNode.addChild(valueNode)
-                ruleNode.addChild(argumentNode)
-                context[0].addChild(ruleNode)
-        p.xpathFreeContext()
+    
+    def apply(self, rspec):
+        if (self.sorted_rule_list):
+            doc = libxml2.parseDoc(rspec)
+            doc = self.add_request_context_to_rspec(doc)
 
-        return doc
+            intermediate_rspec = doc
 
-    def apply(self, rspec):
-        doc = libxml2.parseDoc(rspec)
-        doc = self.add_request_context_to_rspec(doc)
+            for rule in self.sorted_rule_list:
+                (matched,intermediate_rspec) = rule.apply_interpreted(intermediate_rspec)
+                if (rule.terminal and matched):
+                    break
 
-        intermediate_rspec = doc
+            final_rspec = self.wrap_up(intermediate_rspec) 
+        else:
+            final_rspec = rspec
 
-        for rule in self.sorted_rule_list:
-            import pdb
-            pdb.set_trace()
-            intermediate_rspec  = rule.apply_interpreted(intermediate_rspec)
-            intermediate_rspec = XMLRule().wrap_up(intermediate_rspec) 
-            if (rule.terminal):
-                break
-
-        final_rspec = self.wrap_up(intermediate_rspec) 
         return final_rspec
 
+    def print_rules(self):
+        for rule in self.sorted_rule_list:
+            print(rule.processors)
+
 def main():
     incoming = SFATablesRules('INCOMING')
     incoming.set_context({'sfa':{'user':{'hrn':'plc.princeton.sapanb'}}})
 
     outgoing = SFATablesRules('OUTGOING')
-    print "%d rules loaded for INCOMING chain"%len(incoming.sorted_rule_list)
-    print incoming.sorted_rule_list[0].processors
+    print("%d rules loaded for INCOMING chain"%len(incoming.sorted_rule_list))
+    incoming.print_rules()
 
-    print "%d rules loaded for OUTGOING chain"%len(outgoing.sorted_rule_list)
-    print outgoing.sorted_rule_list[0].processors
+    print("%d rules loaded for OUTGOING chain"%len(outgoing.sorted_rule_list))
+    outgoing.print_rules()
 
     rspec = open(sys.argv[1]).read()
     newrspec = incoming.apply(rspec)
-    print newrspec
+    print(newrspec)
     return
 
 if __name__=="__main__":