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 = self.processors[type]
18 filepath = 'processors/' + processor
21 styledoc = libxml2.parseFile(processor)
22 style = libxslt.parseStylesheetDoc(styledoc)
23 doc = libxml2.parseDoc(rspec)
24 result = style.applyStylesheet(doc, None)
25 processed_result = style.saveResultToString(result, 0)
26 style.freeStylesheet()
30 return stylesheet_result
32 def match(self, rspec):
33 match_result = self.apply_processor('match',rspec)
34 return (match_result=='True')
36 def target(self, rspec):
37 target_result = self.apply_processor('target',rspec)
40 def apply_interpreted(self, rspec):
44 # if (match(match_args, rspec)
45 # then target(target_args, rspec)
48 if (self.match(rspec)):
49 return self.target(rspec)
54 def apply_compiled(rspec):
58 def load_xml_extension (self, type, chain, rule_number):
59 filename = sfatables_config+"/"+chain+"/"+"sfatables-%d-%s"%(rule_number,type)
61 self.xmldoc = libxml2.parseFile(filename)
62 p = self.xmldoc.xpathNewContext()
64 context = p.xpathEval('//context/@select')
65 self.context[type] = context[0].content
67 processor = p.xpathEval('//processor/@filename')
69 context = p.xpathEval('//attributes/attribute[@terminal="yes"]')
73 self.processors[type] = processor[0].content
74 self.arguments[type] = p.xpathEval('//rule')
79 def wrap_rspec (self, type, rspec):
80 argument = self.arguments[type]
81 p = rspec.xmldoc.xpathNewContext()
82 root_node = p.xpathEval('/RSpec')
83 if (not root_node or not root_node):
84 raise Exception('An evil aggregate manager sent me a malformed RSpec. Please see the stack trace to identify it.')
86 root_node.addChild(arguments[type])
89 def __init__(self, chain, rule_number):
91 self.load_xml_extension('match', chain, rule_number)
92 self.load_xml_extension('target',chain, rule_number)
93 self.rule_number = rule_number