(no commit message)
[sfa.git] / sfatables / sfatables.py
1 #!/usr/bin/python 
2 # SFAtables is a tool for restricting access to an SFA aggregate in a generic
3 # and extensible way. 
4
5 # It is modeled using abstractions in iptables. Specifically, 'matches' specify
6 # criteria for matching certain requests, 'targets' specify actions that treat
7 # requests in a certain way, and 'chains' are used to group related
8 # match-action pairs.
9
10 import sys
11 import os
12 import pdb
13 from optparse import OptionParser
14
15 from sfatables import commands, matches, targets
16
17 def load_extensions(module, list):
18     command_dict={}
19     module_path = ".".join(module.split('.')[:-1])
20     pdb.set_trace()
21     commands = __import__(module,fromlist=[module_path])
22
23     for command_name in commands.all:
24         command_module = getattr(commands, command_name)
25         command = getattr(command_module, command_name)
26         command_dict[command.key]=command()
27
28     return command_dict
29
30 def create_parser(command_dict):
31     parser = OptionParser(usage="sfatables [command] [chain] [match] [target]",
32                              description='See "man sfatables" for more detail.')
33     
34     for k in command_dict.keys():
35         command = command_dict[k]
36         for (short_option,long_option) in command.options:
37             parser.add_option(short_option,long_option,dest=command.key,help=command.help,metavar=command.help.upper())
38
39     return parser
40
41
42 def main():
43     command_dict = load_extensions("sfatables.commands")
44     command_parser = create_parser(command_dict)
45     (options, args) = command_parser.parse_args()
46
47     if (len(options.keys()) != 1):
48         raise Exception("sfatables takes one command at a time.\n")
49
50     pdb.set_trace()
51     selected_command = command_dict[options.keys()[0]]
52
53     match_options = None
54     target_options = None
55
56     if (selected_command.matches):
57         match_dict = load_extensions("sfatables.matches")
58         match_parser = create_parser(match_dict)
59         (options, args) = match_parser.parse_args(args[2:]) 
60
61     if (selected_command.targets):
62         match_dict = load_extensions("sfatables.targets")
63         target_parser = create_parser(match_dict)
64         (options, args) = target_parser.parse_args(args[5:]) 
65
66     command(options, match_options, target_options)
67
68 if __name__=='__main__':
69     main()