2 from sfatables.globals import *
10 arguments = {'match':None,'target':None}
11 processors = {'match':None,'target':None}
12 context = {'match':None,'target':None}
14 def apply_processor(self, type, rspec):
15 processor = processors[type]
16 styledoc = libxml2.parseFile(processor)
17 style = libxslt.parseStylesheetDoc(styledoc)
18 doc = libxml2.parseDoc(rspec)
19 result = style.applyStylesheet(doc, None)
20 processed_result = style.saveResultToString(result, 0)
21 style.freeStylesheet()
25 return stylesheet_result
27 def match(self, rspec):
28 match_result = self.apply_processor('match',rspec)
29 return (match_result=='True')
31 def target(self, rspec):
32 target_result = self.apply_processor('target',rspec)
35 def apply_interpreted(self, rspec):
39 # if (match(match_args, rspec)
40 # then target(target_args, rspec)
43 if (self.match(rspec)):
44 return self.target(rspec)
49 def apply_compiled(rspec):
53 def load_xml_extension (self, type, chain, rule_number):
54 filename = sfatables_config+"/"+chain+"/"+"sfatables-%d-%s"%(rule_number,type)
56 self.xmldoc = libxml2.parseFile(filename)
57 p = self.xmldoc.xpathNewContext()
59 context = p.xpathEval('//context/@select')
60 self.context[type] = context[0].content
62 processor = p.xpathEval('//processor/@filename')
64 context = p.xpathEval('//attributes/attribute[@terminal="yes"]')
68 self.processors[type] = processor[0].content
69 self.arguments[type] = p.xpathEval('//rule')
74 def wrap_rspec (self, type, rspec):
75 argument = self.arguments[type]
76 p = rspec.xmldoc.xpathNewContext()
77 root_node = p.xpathEval('/RSpec')
78 if (not root_node or not root_node):
79 raise Exception('An evil aggregate manager sent me a malformed RSpec. Please see the stack trace to identify it.')
81 root_node.addChild(arguments[type])
84 def __init__(self, chain, rule_number):
86 self.load_xml_extension('match', chain, rule_number)
87 self.load_xml_extension('target',chain, rule_number)
88 self.rule_number = rule_number