(no commit message)
authorSapan Bhatia <sapanb@cs.princeton.edu>
Thu, 13 Aug 2009 14:49:06 +0000 (14:49 +0000)
committerSapan Bhatia <sapanb@cs.princeton.edu>
Thu, 13 Aug 2009 14:49:06 +0000 (14:49 +0000)
sfatables/README
sfatables/command.py
sfatables/commands/Add.py
sfatables/commands/Delete.py
sfatables/commands/List.py
sfatables/commands/SetDefault.py
sfatables/sfatables [moved from sfatables/sfatables.py with 50% similarity]

index ed7b7ac..8c860cc 100644 (file)
@@ -8,10 +8,6 @@ e.g.
 
 or
 
-* sfatables -A INCOMING --requestor-hrn=plc.princeton.coblitz requested=plc.tp.*[tp_coblitz=true] -> result=true
-requester=plc.princeton.other_whitelisted_slice requested=plc.tp.*[tp_coblitz=true] -> result=true
-requester=* requested=plc.tp.*[tp_coblitz=true] -> result=false
-
 Default policy:
 
 * sfatables -P INCOMING REJECT
index 063caab..5a33928 100644 (file)
@@ -3,16 +3,17 @@ import os, time
 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)
index 79bb2d9..d8bd9a7 100644 (file)
@@ -1,19 +1,16 @@
 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)
index e59da62..cac5445 100644 (file)
@@ -1,7 +1,7 @@
 import os, time
-from sfa.sfatables.command import Add
+from sfatables.command import Command
 
-class Add(Command):
+class Delete(Command):
     options = [('-D','--delete')]
     help = 'Delete a rule from a chain'
     key='delete_rule'
index 38f5ebc..79d1849 100644 (file)
@@ -1,5 +1,5 @@
 import os, time
-from sfa.sfatables.command import Add
+from sfatables.command import Command
 
 class List(Command):
     options = [('-L','--list')]
index 8fac405..b840706 100644 (file)
@@ -1,10 +1,10 @@
 import os, time
-from sfa.sfatables.command import Add
+from sfatables.command import Command
 
 class SetDefault(Command):
     options = [('-P','--default')]
     help = 'Set the default rule for a chain'
-    key='add_rule'
+    key='set_default_rule'
     matches = False
     targets = False
 
similarity index 50%
rename from sfatables/sfatables.py
rename to sfatables/sfatables
index f5393f6..8762a50 100755 (executable)
@@ -16,14 +16,11 @@ from sfatables import commands, matches, targets
 
 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
 
@@ -34,34 +31,53 @@ def create_parser(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)