reviewed imports, tolerant on some that are hard to get on a mac
[sfa.git] / sfatables / xmlrule.py
1 import libxml2
2 # allow to run sfa2wsdl if this is missing (for mac)
3 import sys
4 try:import libxslt
5 except: print >>sys.stderr, "WARNING, could not import libxslt"
6
7 from sfatables.globals import *
8
9 class XMLRule:
10     def apply_processor(self, type, doc, output_xpath_filter=None):
11         processor = self.processors[type]
12
13         # XXX TO CLEAN UP
14         filepath = os.path.join(sfatables_config, 'processors', processor)
15         # XXX
16
17         styledoc = libxml2.parseFile(filepath)
18         style = libxslt.parseStylesheetDoc(styledoc)
19         result = style.applyStylesheet(doc, None)
20         if (output_xpath_filter):
21             p = result.xpathNewContext()
22             xpath_result = p.xpathEval(output_xpath_filter)
23             if (xpath_result == []):
24                 raise Exception("Could not apply processor %s."%processor)
25
26             stylesheet_result = xpath_result
27             p.xpathFreeContext()
28         else:
29             stylesheet_result = result #style.saveResultToString(result)
30
31         style.freeStylesheet()
32         #doc.freeDoc()
33         #result.freeDoc()
34
35         return stylesheet_result
36
37     def wrap_up(self, doc):
38         filepath = os.path.join(sfatables_config, 'processors', self.final_processor)
39
40         if not os.path.exists(filepath):
41             raise Exception('Could not find final rule filter')
42
43         styledoc = libxml2.parseFile(filepath)
44         style = libxslt.parseStylesheetDoc(styledoc)
45         result = style.applyStylesheet(doc, None)
46         stylesheet_result = result#style.saveResultToString(result)
47         style.freeStylesheet()
48         #doc.freeDoc()
49         #result.freeDoc()
50
51         return stylesheet_result
52
53     def match(self, rspec):
54         match_result = self.apply_processor('match',rspec,"//result/@verdict") 
55         return (match_result[0].content=='True')
56
57     def target(self, rspec):
58         target_result = self.apply_processor('target',rspec,None)
59         return target_result
60
61     def add_rule_context_to_rspec(self, doc):
62         p = doc.xpathNewContext()
63         context = p.xpathEval("//RSpec")
64         if (not context):
65             raise Exception('Request is not an rspec')
66         else:
67             # Add the request context
68             matchNode = libxml2.newNode('match-context')
69             for match_argument in self.arguments['match']:
70                 matchNode.addChild(match_argument)
71
72             targetNode = libxml2.newNode('target-context')
73             for target_argument in self.arguments['target']:
74                 targetNode.addChild(target_argument)
75
76             context[0].addChild(matchNode)
77             context[0].addChild(targetNode)
78         p.xpathFreeContext()
79
80         return doc
81
82     def apply_interpreted(self, rspec):
83         rspec = self.add_rule_context_to_rspec(rspec)
84         # Interpreted
85         #
86         # output =
87         #    if (match(match_args, rspec)
88         #       then target(target_args, rspec)
89         #       else rspec
90         
91         import pdb
92         if (self.match(rspec)):
93             return (True,self.wrap_up(self.target(rspec)))
94         else:
95             return (False,self.wrap_up(rspec))
96
97
98     def apply_compiled(rspec):
99         # Not supported yet
100         return None
101
102     def load_xml_extension (self, type, chain, rule_number):
103         filename = sfatables_config+"/"+chain+"/"+"sfatables-%d-%s"%(rule_number,type)
104
105         self.xmldoc = libxml2.parseFile(filename)
106         p = self.xmldoc.xpathNewContext()
107
108         context = p.xpathEval('//context/@select')
109         self.context[type] = context[0].content
110
111         processor = p.xpathEval('//processor/@filename')
112
113         context = p.xpathEval('//attributes/attribute[@terminal="yes"]')
114         if (context != []):
115             self.terminal = 1
116         
117         self.processors[type] = processor[0].content
118         self.arguments[type] = p.xpathEval('//rule//argument[value!=""]')
119
120         p.xpathFreeContext()
121
122
123     def __init__(self, chain=None, rule_number=None):
124         self.rule_number = None
125         self.chain = None
126         self.xmldoc = None
127         self.terminal = 0
128         self.final_processor = '__sfatables_rule_wrap_up__.xsl'
129
130         self.arguments = {'match':None,'target':None}
131         self.processors = {'match':None,'target':None}
132         self.context = {'match':None,'target':None}
133
134         if (chain and rule_number):
135             self.load_xml_extension('match', chain, rule_number)
136             self.load_xml_extension('target',chain, rule_number)
137             self.rule_number = rule_number
138             self.chain = chain
139         return
140         
141     def free(self):
142         self.xmldoc.freeDoc()