class Command:
options = []
help = ''
- key=''
+ type='command'
matches = False
targets = False
+ action = 'store_const'
def __init__(self):
return
- def call(self):
+ def call(self, coptions, moptions, toptions):
# Override this function
return True
- def __call__(self, option, opt_str, value, parser, *args, **kwargs):
- return self.call(option)
+ def __call__(self, coption, moptions, toptions):
+ return self.call(coptions,moptions,toptions)
import os, time
-from sfa.sfatables.command import Add
+from sfatables.command import Command
class Add(Command):
options = [('-A','--add')]
help = 'Add a rule to a chain'
- key='add_rule'
- matches = False
- targets = False
+ matches = True
+ targets = True
def __init__(self):
return
- def call(self):
+ def call(self, command_options, match_options, target_options):
# Override this function
return True
- def __call__(self, option, opt_str, value, parser, *args, **kwargs):
- return self.call(option)
def load_extensions(module, list):
command_dict={}
- module_path = ".".join(module.split('.')[:-1])
- pdb.set_trace()
- commands = __import__(module,fromlist=[module_path])
- for command_name in commands.all:
- command_module = getattr(commands, command_name)
+ for command_name in list:
+ command_module = __import__(".".join([module,command_name]),fromlist=[module])
command = getattr(command_module, command_name)
- command_dict[command.key]=command()
+ command_dict[command_name]=command()
return command_dict
for k in command_dict.keys():
command = command_dict[k]
for (short_option,long_option) in command.options:
- parser.add_option(short_option,long_option,dest=command.key,help=command.help,metavar=command.help.upper())
+ parser.add_option(short_option,long_option,dest=command.type,action=command.action,const=k,help=command.help,metavar="CHAIN")
return parser
+def partition(sep, lst):
+ ret = []
+ curpart = []
+ for item in lst:
+ if (item==sep):
+ ret.append(curpart)
+ curpart=[]
+ else:
+ curpart.append(item)
+ ret.append(curpart)
+
+ return ret
+
+
def main():
- command_dict = load_extensions("sfatables.commands")
+ # Segment command line into three blobs, one each for the command, match and target respectively.
+
+ pargs = partition('--', sys.argv[1:])
+
+ command_dict = load_extensions("sfatables.commands",commands.all)
command_parser = create_parser(command_dict)
(options, args) = command_parser.parse_args()
- if (len(options.keys()) != 1):
- raise Exception("sfatables takes one command at a time.\n")
-
- pdb.set_trace()
- selected_command = command_dict[options.keys()[0]]
+ command = command_dict[options.command]
- match_options = None
- target_options = None
-
- if (selected_command.matches):
- match_dict = load_extensions("sfatables.matches")
+ if (command.matches):
+ if (len(pargs)<2):
+ raise Exception("Must specify match for this command")
+ match_dict = load_extensions("sfatables.matches",matches.all)
match_parser = create_parser(match_dict)
- (options, args) = match_parser.parse_args(args[2:])
-
- if (selected_command.targets):
- match_dict = load_extensions("sfatables.targets")
+ (match_options, args) = match_parser.parse_args(pargs[1])
+ else:
+ match_options=None
+
+ if (command.targets):
+ if (len(pargs)<3):
+ raise Exception("Must specify a target for this command")
+ match_dict = load_extensions("sfatables.targets",targets.all)
target_parser = create_parser(match_dict)
- (options, args) = target_parser.parse_args(args[5:])
+ (target_options, args) = target_parser.parse_args(pargs[2])
+ else:
+ target_options = None
command(options, match_options, target_options)