From aab206b1b50c439c1697c2bbcfd9e540a6c416cd Mon Sep 17 00:00:00 2001 From: Sapan Bhatia Date: Sat, 15 Aug 2009 00:33:02 +0000 Subject: [PATCH 1/1] --- sfatables/README | 7 +++++++ sfatables/matches/hrn.xml | 6 +++--- sfatables/sfatables | 35 +++++++++++++++++++++++++------ sfatables/targets/filternodes.xsl | 26 +++++++++++++++++++++++ sfatables/xmlextension.py | 30 ++++++++++++++++++++++++++ 5 files changed, 95 insertions(+), 9 deletions(-) create mode 100644 sfatables/targets/filternodes.xsl create mode 100644 sfatables/xmlextension.py diff --git a/sfatables/README b/sfatables/README index 21519225..050e54ad 100644 --- a/sfatables/README +++ b/sfatables/README @@ -30,3 +30,10 @@ E.g. if the policy is that your site is limited to < 1000 slivers (e.g. sfatable only requires the user's HRN. A site's slice count match may require the number of slices/site. The config file consists of a set of - When sfa needs to invoke sfatables, it passes a 'rule context' (i.e. the parameter to the sfatables command line) along with the input from the current context to the appropriate match. If it returns True, then it goes on to invoke target specified by the rule, and uses the filtered version of the rspec that it produces. + + +Specifics: + +- On startup sfa calls sfatables.load(), which loads available commands, matches and targets + +- When invoking sfatables diff --git a/sfatables/matches/hrn.xml b/sfatables/matches/hrn.xml index 12001603..ff6bbde4 100644 --- a/sfatables/matches/hrn.xml +++ b/sfatables/matches/hrn.xml @@ -7,8 +7,8 @@ as the one matched by this rule. --> - - - + + + diff --git a/sfatables/sfatables b/sfatables/sfatables index 8762a507..977153ec 100755 --- a/sfatables/sfatables +++ b/sfatables/sfatables @@ -10,19 +10,31 @@ import sys import os import pdb +import libxml2 from optparse import OptionParser from sfatables import commands, matches, targets +from sfatables import Xmlextension -def load_extensions(module, list): - command_dict={} +def load_commands(module, list): + ext_dict={} for command_name in list: command_module = __import__(".".join([module,command_name]),fromlist=[module]) command = getattr(command_module, command_name) command_dict[command_name]=command() - return command_dict + return ext_dict + +def load_xml_extensions(module, list): + ext_dict={} + + for ext_name in list: + module = Xmlextension(ext_name) + ext_dict[ext_name]=module + + return ext_dict + def create_parser(command_dict): parser = OptionParser(usage="sfatables [command] [chain] [match] [target]", @@ -35,6 +47,17 @@ def create_parser(command_dict): return parser +def xml_ext_create_parser(ext_dict): + parser = OptionParser(usage="sfatables [command] [chain] [match] [target]", + description='See "man sfatables" for more detail.') + + for k in command_dict.keys(): + command = command_dict[k] + for arg in command.arguments: + parser.add_option(None,"--"+arg,dest=arg,help=command.help,metavar=command.operand) + + return parser + def partition(sep, lst): ret = [] @@ -55,7 +78,7 @@ def main(): pargs = partition('--', sys.argv[1:]) - command_dict = load_extensions("sfatables.commands",commands.all) + command_dict = load_commands("sfatables.commands",commands.all) command_parser = create_parser(command_dict) (options, args) = command_parser.parse_args() @@ -64,7 +87,7 @@ def main(): if (command.matches): if (len(pargs)<2): raise Exception("Must specify match for this command") - match_dict = load_extensions("sfatables.matches",matches.all) + match_dict = load_xml_extensions("sfatables.matches",matches.all) match_parser = create_parser(match_dict) (match_options, args) = match_parser.parse_args(pargs[1]) else: @@ -73,7 +96,7 @@ def main(): if (command.targets): if (len(pargs)<3): raise Exception("Must specify a target for this command") - match_dict = load_extensions("sfatables.targets",targets.all) + match_dict = load_xml_extensions("sfatables.targets",targets.all) target_parser = create_parser(match_dict) (target_options, args) = target_parser.parse_args(pargs[2]) else: diff --git a/sfatables/targets/filternodes.xsl b/sfatables/targets/filternodes.xsl new file mode 100644 index 00000000..d65a2ae6 --- /dev/null +++ b/sfatables/targets/filternodes.xsl @@ -0,0 +1,26 @@ + + + + + + + + + + + + True + + + False + + + + + + + + diff --git a/sfatables/xmlextension.py b/sfatables/xmlextension.py new file mode 100644 index 00000000..b4b52cd7 --- /dev/null +++ b/sfatables/xmlextension.py @@ -0,0 +1,30 @@ +import libxml2 + +class Xmlextension: + context = "" + processor = "" + operand = "VALUE" + arguments = [] + + def __init__(filename): + self.xmldoc = libxml2.parseFile(filename) + # TODO: Check xmldoc against a schema + + p = self.xmldoc.XPathNewContext() + + # + # + # + + context = p.xpathEval('//context/@select') + self.context = context[0].value + + processor = p.xpathEval('//processor@name') + self.context = processor[0].value + + params = p.xpathEval('//rule/argument/@param') + self.arguments = [node.value for node in params] + + + return + -- 2.43.0