From c36efa8afed156cc5c25bc61dfd8987fed0010d1 Mon Sep 17 00:00:00 2001 From: Sapan Bhatia Date: Thu, 10 Sep 2009 18:58:19 +0000 Subject: [PATCH] These files bridge sfatables with sfa. They let sfa load rules and apply them to XML-formatted rspecs. --- sfatables/exec.py | 30 +++++++++++++++++++++++ sfatables/matches/hrn.xsl | 15 ++++-------- sfatables/xmlrule.py | 51 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 86 insertions(+), 10 deletions(-) create mode 100644 sfatables/exec.py create mode 100644 sfatables/xmlrule.py diff --git a/sfatables/exec.py b/sfatables/exec.py new file mode 100644 index 00000000..fcff3aa1 --- /dev/null +++ b/sfatables/exec.py @@ -0,0 +1,30 @@ +#!/usr/bin/python + +import sys +import os +import pdb +import libxml2 + +from optparse import OptionParser +from sfatables import commands, matches, targets +from sfatables.xmlextension import Xmlextension +from sfatables.globals import * +from sfatables.commands.List import * +from sfatables.xmlrule import * + +class SFATablesRules: + sorted_rule_list = None + + def __init__(self, chain_name): + chain_dir_path = "%s/%s"%(sfatables_config,chain_name) + rule_list = List().get_rule_list(chain_dir_path) + for rule_number in rule_list: + self.sorted_rule_list.append(XMLRule(rule_number)) + return + + def apply(self, rspec): + intermediate_rspec = rspec + for rule in self.sorted_rule_list: + intermediate_rspec = rule.apply(intermediate_rspec) + + return intermediate_rspec diff --git a/sfatables/matches/hrn.xsl b/sfatables/matches/hrn.xsl index 3781b430..29988a5c 100644 --- a/sfatables/matches/hrn.xsl +++ b/sfatables/matches/hrn.xsl @@ -1,22 +1,17 @@ - - - - - + + - True + True - False + False - - - + diff --git a/sfatables/xmlrule.py b/sfatables/xmlrule.py new file mode 100644 index 00000000..bce804a0 --- /dev/null +++ b/sfatables/xmlrule.py @@ -0,0 +1,51 @@ +import libxml2 +from sfatables.globals import * + +class Xmlrule: + rule_number = None + chain = None + xmldoc = None + + arguments = {'match':None,'target':None} + processors = {'match':None,'target':None} + context = {'match':None,'target':None} + + def load_xml_extension (self, type, chain, rule_number): + filename = sfatables_config+"/"+chain+"/"+"sfatables-%d-%s.xml"%(rule_number,type) + + self.xmldoc = libxml2.parseFile(filename) + p = self.xmldoc.xpathNewContext() + + context = p.xpathEval('//context/@select') + self.context[type] = context[0].content + + processor = p.xpathEval('//processor/@filename') + + self.processor[type] = processor[0].content + self.arguments[type] = p.xpathEval('//rule') + + p.xpathFreeContext() + + return + + def wrap_rspec (self, type, rspec): + argument = self.arguments[type] + p = rspec.xmldoc.xpathNewContext() + root_node = p.xpathEval('/RSpec') + if (!root_node or !len(root_node)): + raise Exception('An evil aggregate manager sent me a malformed RSpec. Please see the stack trace to identify it.') + + root_node.addChild(arguments[type]) + return rspec + + def __init__(self, chain, rule_number): + + self.load_xml_extension('match', chain, rule_number) + self.load_xml_extension('target',chain, rule_number) + self.rule_number = rule_number + self.chain = chain + + return + + def free(self): + self.xmldoc.freeDoc() -- 2.43.0