X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=sfatables%2Fruntime.py;h=0ed38e0628d5abf4697deb0a93d7202cb397f8b6;hb=9a053166cdb0187d0514403e92e8719d9a3aa8ee;hp=1aac7731ef2ebe632bd196ccdf12a2390e33fb05;hpb=c451060afdf4ef60cdfea767ba6f00d6ffce66f8;p=sfa.git diff --git a/sfatables/runtime.py b/sfatables/runtime.py index 1aac7731..0ed38e06 100644 --- a/sfatables/runtime.py +++ b/sfatables/runtime.py @@ -16,12 +16,28 @@ class SFATablesRules: self.active_context = {} self.contexts = None # placeholder for rspec_manger self.sorted_rule_list = [] + self.final_processor = '__sfatables_wrap_up__.xsl' chain_dir_path = os.path.join(sfatables_config,chain_name) rule_list = List().get_rule_list(chain_dir_path) for rule_number in rule_list: self.sorted_rule_list = self.sorted_rule_list + [XMLRule(chain_name, rule_number)] return + def wrap_up(self, doc): + filepath = os.path.join(sfatables_config, 'processors', self.final_processor) + + if not os.path.exists(filepath): + raise Exception('Could not find final rule filter') + + styledoc = libxml2.parseFile(filepath) + style = libxslt.parseStylesheetDoc(styledoc) + result = style.applyStylesheet(doc, None) + stylesheet_result = style.saveResultToString(result) + style.freeStylesheet() + doc.freeDoc() + result.freeDoc() + + return stylesheet_result def set_context(self, request_context): self.active_context = request_context @@ -39,7 +55,7 @@ class SFATablesRules: def add_request_context_to_rspec(self, doc): p = doc.xpathNewContext() - context = p.xpathEval("//rspec") + context = p.xpathEval("//Rspec") if (not context): raise Exception('Request is not an rspec') else: @@ -49,41 +65,23 @@ class SFATablesRules: p.xpathFreeContext() return doc - def add_rule_context_to_rspec(self,arguments, doc): - p = doc.xpathNewContext() - context = p.xpathEval("//rspec") - if (not context): - raise Exception('Request is not an rspec') - else: - # Add the request context - ruleNode = libxml2.newNode('rule-context') - ac = self.active_context - for k in ac: - argumentNode = libxml2.newNode('argument') - nameNode = libxml2.newNode('name') - nameNode.addContent(k) - valueNode = libxml2.newNode('value') - valueNode.addContent(ac[k]) - argumentNode.addChild(nameNode) - argumentNode.addChild(valueNode) - ruleNode.addChild(argumentNode) - context[0].addChild(ruleNode) - p.xpathFreeContext() - - return doc - + def apply(self, rspec): - doc = libxml2.parseDoc(rspec) - doc = self.add_request_context_to_rspec(doc) + if (self.sorted_rule_list): + doc = libxml2.parseDoc(rspec) + doc = self.add_request_context_to_rspec(doc) + + intermediate_rspec = doc - intermediate_rspec = doc + for rule in self.sorted_rule_list: + intermediate_rspec = rule.apply_interpreted(intermediate_rspec) + if (rule.terminal): + break - for rule in self.sorted_rule_list: - intermediate_rspec = rule.apply_interpreted(intermediate_rspec) - if (rule.terminal): - break + final_rspec = self.wrap_up(intermediate_rspec) + else: + final_rspec = rspec - final_rspec = XMLRule().wrap_up(intermediate_rspec) return final_rspec def main():