X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=sfatables%2Fsfatables;h=0693020d380c5d64a0533cd5cb52e55a3b7558aa;hb=87121ca63d2b95bb155ea2adc5fd7c61d5cddcbe;hp=1428dc06bb25f26f6209e30486015ade820392ce;hpb=c18d4a0349f3e8bbfbe4b7efcf1ce031a1c06fd7;p=sfa.git diff --git a/sfatables/sfatables b/sfatables/sfatables index 1428dc06..0693020d 100755 --- a/sfatables/sfatables +++ b/sfatables/sfatables @@ -9,11 +9,13 @@ import sys import os import pdb +import glob import libxml2 from optparse import OptionParser -from sfatables import commands, matches, targets +from sfatables import commands from sfatables.xmlextension import Xmlextension +from sfatables.globals import * def load_commands(module, list): command_dict={} @@ -25,12 +27,16 @@ def load_commands(module, list): return command_dict -def load_xml_extensions(module, list): +def load_xml_extensions(module, ext_dir): ext_dict={} - for ext_name in list: - module = Xmlextension(ext_name) + exts = glob.glob(ext_dir + os.path.sep + "*") + for ext in exts: + module = Xmlextension(ext) + # get the filename and get rid of the ".xml" extension + ext_name = os.path.extsep.join(os.path.splitext(os.path.basename(ext))[:-1]) ext_dict[ext_name]=module + print "Loaded:", ext_name, ext return ext_dict @@ -52,9 +58,8 @@ def create_parser_xml_ext(ext_dict): for k in ext_dict.keys(): command = ext_dict[k] - pdb.set_trace() for arg in command.arguments: - parser.add_option(None,"--"+arg,dest=arg,help=command.help,metavar=command.operand) + parser.add_option('',"--"+arg['name'],dest=arg['name'],help=arg['help'],metavar=arg['target']) return parser @@ -79,30 +84,52 @@ def main(): command_dict = load_commands("sfatables.commands",commands.all) command_parser = create_parser(command_dict) - (options, args) = command_parser.parse_args() + (options, args) = command_parser.parse_args(pargs[0]) + setattr(options, 'args', args) command = command_dict[options.command] if (command.matches): if (len(pargs)<2): raise Exception("Must specify match for this command") - match_dict = load_xml_extensions("sfatables.matches",matches.all) + match_dict = load_xml_extensions("sfatables.matches",match_dir) match_parser = create_parser_xml_ext(match_dict) matches_str = ",".join(match_dict.keys()) - match_parser.add_option('-m','--match',dest='match_name',help='Match name (one of %s)'%matches_str, metavar = 'MATCH') - + match_parser.add_option('-m','--match',dest='name',help='Match name (one of %s)'%matches_str, metavar = 'MATCH') + match_parser.add_option('-n','--negate',dest='negate',help='Negate result',action='store_true') (match_options, args) = match_parser.parse_args(pargs[1]) + try: + name = match_options.name + except Exception: + print "Must specify match name with -m" + + if (match_dict.has_key(name)): + setattr(match_options, 'arguments', match_dict[name].arguments) + else: + raise Exception('Match %s not found'%name) + else: match_options=None if (command.targets): if (len(pargs)<3): raise Exception("Must specify a target for this command") - target_dict = load_xml_extensions("sfatables.targets",targets.all) + target_dict = load_xml_extensions("sfatables.targets",target_dir) target_parser = create_parser_xml_ext(target_dict) - targets = ",".join(target_dict.keys()) + targets_str = ",".join(target_dict.keys()) + target_parser.add_option('-j','--jump',dest='name',help='Target name (one of %s)'%targets_str, metavar = 'TARGET') + target_parser.add_option('-e','--element',dest='element',help='Element name', metavar = 'ELEMENT') (target_options, args) = target_parser.parse_args(pargs[2]) - target_parser.add_option('-j','--jump',dest='target_name',help='Target name (one of %s)'%targets, metavar = 'TARGET') + try: + name = target_options.name + except Exception: + print "Must specify target name with -j" + + if (target_dict.has_key(name)): + setattr(target_options, 'arguments', target_dict[name].arguments) + else: + raise Exception('Target %s not found'%name) + else: target_options = None