3 from sfatables.globals import *
11 arguments = {'match':None,'target':None}
12 processors = {'match':None,'target':None}
13 context = {'match':None,'target':None}
15 def apply_processor(self, type, rspec):
16 processor = self.processors[type]
19 filepath = 'processors/' + processor
22 styledoc = libxml2.parseFile(filepath)
23 style = libxslt.parseStylesheetDoc(styledoc)
24 doc = libxml2.parseDoc(rspec)
25 result = style.applyStylesheet(doc, None)
26 stylesheet_result = style.saveResultToString(result)
27 style.freeStylesheet()
31 return stylesheet_result
33 def match(self, rspec):
34 match_result = self.apply_processor('match',rspec)
35 return (match_result=='True')
37 def target(self, rspec):
38 target_result = self.apply_processor('target',rspec)
41 def apply_interpreted(self, rspec):
45 # if (match(match_args, rspec)
46 # then target(target_args, rspec)
49 if (self.match(rspec)):
50 return self.target(rspec)
55 def apply_compiled(rspec):
59 def load_xml_extension (self, type, chain, rule_number):
60 filename = sfatables_config+"/"+chain+"/"+"sfatables-%d-%s"%(rule_number,type)
62 self.xmldoc = libxml2.parseFile(filename)
63 p = self.xmldoc.xpathNewContext()
65 context = p.xpathEval('//context/@select')
66 self.context[type] = context[0].content
68 processor = p.xpathEval('//processor/@filename')
70 context = p.xpathEval('//attributes/attribute[@terminal="yes"]')
74 self.processors[type] = processor[0].content
75 self.arguments[type] = p.xpathEval('//rule')
80 def wrap_rspec (self, type, rspec):
81 argument = self.arguments[type]
82 p = rspec.xmldoc.xpathNewContext()
83 root_node = p.xpathEval('/RSpec')
84 if (not root_node or not root_node):
85 raise Exception('An evil aggregate manager sent me a malformed RSpec. Please see the stack trace to identify it.')
87 root_node.addChild(arguments[type])
90 def __init__(self, chain, rule_number):
92 self.load_xml_extension('match', chain, rule_number)
93 self.load_xml_extension('target',chain, rule_number)
94 self.rule_number = rule_number
100 self.xmldoc.freeDoc()