From ca033bfcc5afa7b9633f9caba9e9b1691222cbf2 Mon Sep 17 00:00:00 2001
From: Sapan Bhatia <sapanb@cs.princeton.edu>
Date: Thu, 13 Aug 2009 14:49:06 +0000
Subject: [PATCH]

---
 sfatables/README                      |  4 --
 sfatables/command.py                  |  9 ++--
 sfatables/commands/Add.py             | 11 ++---
 sfatables/commands/Delete.py          |  4 +-
 sfatables/commands/List.py            |  2 +-
 sfatables/commands/SetDefault.py      |  4 +-
 sfatables/{sfatables.py => sfatables} | 62 +++++++++++++++++----------
 7 files changed, 53 insertions(+), 43 deletions(-)
 rename sfatables/{sfatables.py => sfatables} (50%)

diff --git a/sfatables/README b/sfatables/README
index ed7b7ac1..8c860cc9 100644
--- a/sfatables/README
+++ b/sfatables/README
@@ -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
diff --git a/sfatables/command.py b/sfatables/command.py
index 063caab9..5a339289 100644
--- a/sfatables/command.py
+++ b/sfatables/command.py
@@ -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)
diff --git a/sfatables/commands/Add.py b/sfatables/commands/Add.py
index 79bb2d97..d8bd9a76 100644
--- a/sfatables/commands/Add.py
+++ b/sfatables/commands/Add.py
@@ -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)
diff --git a/sfatables/commands/Delete.py b/sfatables/commands/Delete.py
index e59da62c..cac54454 100644
--- a/sfatables/commands/Delete.py
+++ b/sfatables/commands/Delete.py
@@ -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'
diff --git a/sfatables/commands/List.py b/sfatables/commands/List.py
index 38f5ebc1..79d1849b 100644
--- a/sfatables/commands/List.py
+++ b/sfatables/commands/List.py
@@ -1,5 +1,5 @@
 import os, time
-from sfa.sfatables.command import Add
+from sfatables.command import Command
 
 class List(Command):
     options = [('-L','--list')]
diff --git a/sfatables/commands/SetDefault.py b/sfatables/commands/SetDefault.py
index 8fac405f..b840706b 100644
--- a/sfatables/commands/SetDefault.py
+++ b/sfatables/commands/SetDefault.py
@@ -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
 
diff --git a/sfatables/sfatables.py b/sfatables/sfatables
similarity index 50%
rename from sfatables/sfatables.py
rename to sfatables/sfatables
index f5393f63..8762a507 100755
--- a/sfatables/sfatables.py
+++ b/sfatables/sfatables
@@ -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)
 
-- 
2.47.0