These files bridge sfatables with sfa. They let sfa load rules and apply them to...
[sfa.git] / sfatables / xmlrule.py
1 import libxml2
2 from sfatables.globals import *
3
4 class Xmlrule:
5     rule_number = None
6     chain = None
7     xmldoc = None
8
9     arguments = {'match':None,'target':None}
10     processors = {'match':None,'target':None}
11     context = {'match':None,'target':None}
12
13     def load_xml_extension (self, type, chain, rule_number):
14         filename = sfatables_config+"/"+chain+"/"+"sfatables-%d-%s.xml"%(rule_number,type)
15
16         self.xmldoc = libxml2.parseFile(filename)
17         p = self.xmldoc.xpathNewContext()
18
19         context = p.xpathEval('//context/@select')
20         self.context[type] = context[0].content
21
22         processor = p.xpathEval('//processor/@filename')
23
24         self.processor[type] = processor[0].content
25         self.arguments[type] = p.xpathEval('//rule')
26
27         p.xpathFreeContext()
28
29         return 
30
31     def wrap_rspec (self, type, rspec):
32         argument = self.arguments[type]
33         p = rspec.xmldoc.xpathNewContext()
34         root_node = p.xpathEval('/RSpec')
35         if (!root_node or !len(root_node)):
36             raise Exception('An evil aggregate manager sent me a malformed RSpec. Please see the stack trace to identify it.')
37
38         root_node.addChild(arguments[type])
39         return rspec
40
41     def __init__(self, chain, rule_number):
42
43         self.load_xml_extension('match', chain, rule_number)
44         self.load_xml_extension('target',chain, rule_number)
45         self.rule_number = rule_number
46         self.chain = chain
47
48         return
49         
50     def free(self):
51         self.xmldoc.freeDoc()