(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 def load_extensions(module):
16     command_dict={}
17     module_path = ".".join(module.split('.')[:-1])
18     pdb.set_trace()
19     commands = __import__(module,fromlist=[module_path])
20
21     for command_name in commands.all:
22         command_module = getattr(commands, command_name)
23         command = getattr(command_module, command_name)
24         command_dict[command.key]=command()
25
26     return command_dict
27
28 def create_parser(command_dict):
29     parser = OptionParser(usage="sfatables [command] [chain] [match] [target]",
30                              description='See "man sfatables" for more detail.')
31     
32     for k in command_dict.keys():
33         command = command_dict[k]
34         for (short_option,long_option) in command.options:
35             parser.add_option(short_option,long_option,dest=command.key,help=command.help,metavar=command.help.upper())
36
37     return parser
38
39
40 def main():
41     command_dict = load_extensions("sfa.sfatables.commands")
42     command_parser = create_parser(command_dict)
43     (options, args) = command_parser.parse_args()
44
45     if (len(options.keys()) != 1):
46         raise Exception("sfatables takes one command at a time.\n")
47
48     pdb.set_trace()
49     selected_command = command_dict[options.keys()[0]]
50
51     match_options = None
52     target_options = None
53
54     if (selected_command.matches):
55         match_dict = load_extensions("sfa.sfatables.matches")
56         match_parser = create_parser(match_dict)
57         (options, args) = match_parser.parse_args(args[2:]) 
58
59     if (selected_command.targets):
60         match_dict = load_extensions("sfa.sfatables.targets")
61         target_parser = create_parser(match_dict)
62         (options, args) = target_parser.parse_args(args[5:]) 
63
64     command(options, match_options, target_options)
65
66 if __name__=='__main__':
67     main()