]> git.llucax.com Git - software/pymin.git/commitdiff
Convert HopHandler to a ListSubHandler (fixes #15, fixes #12).
authorLeandro Lucarella <llucax@gmail.com>
Sat, 10 May 2008 03:55:59 +0000 (00:55 -0300)
committerLeandro Lucarella <llucax@gmail.com>
Sat, 10 May 2008 03:57:31 +0000 (00:57 -0300)
HopHandler was doing all the dirty job in the old fashion. Now it inherits
from ListSubHandler, adding code only for the custom checks.

pymin/services/ip/__init__.py

index 98690985575f0f8b7bcd7e5d55e879b97946543d..12b53e518e2a1d5f45898806b52e0dcc20f17c6c 100644 (file)
@@ -9,28 +9,11 @@ from pymin.dispatcher import handler, HandlerError, Handler
 from pymin.services.util import Restorable, ConfigWriter, InitdHandler, \
                                 TransactionalHandler, SubHandler, call, \
                                 get_network_devices, ListComposedSubHandler, \
 from pymin.services.util import Restorable, ConfigWriter, InitdHandler, \
                                 TransactionalHandler, SubHandler, call, \
                                 get_network_devices, ListComposedSubHandler, \
-                                DictComposedSubHandler, Device, Address, \
-                                ExecutionError
+                                DictComposedSubHandler, ListSubHandler, \
+                                Device, Address, ExecutionError
 
 __all__ = ('IpHandler',)
 
 
 __all__ = ('IpHandler',)
 
-# TODO: convertir HopHandler a ComposedSubHandler
-
-class HopError(HandlerError):
-
-    def __init__(self, hop):
-        self.message = u'Hop error : "%s"' % hop
-
-class HopNotFoundError(HopError):
-
-    def __init__(self, hop):
-        self.message = u'Hop not found : "%s"' % hop
-
-class HopAlreadyExistsError(HopError):
-
-    def __init__(self, hop):
-        self.message = u'Hop already exists : "%s"' % hop
-
 
 class Hop(Sequence):
 
 
 class Hop(Sequence):
 
@@ -47,47 +30,23 @@ class Hop(Sequence):
             return 0
         return cmp(id(self), id(other))
 
             return 0
         return cmp(id(self), id(other))
 
-class HopHandler(Handler):
+class HopHandler(ListSubHandler):
+    handler_help = u"Manage IP hops"
+    _cont_subhandler_attr = 'hops'
+    _cont_subhandler_class = Hop
 
 
-    def __init__(self, parent):
-        self.parent = parent
-
-    @handler('Adds a hop : add <gateway> <device>')
-    def add(self, gw, dev):
+    @handler('Add a hop: add <device> <gateway>')
+    def add(self, dev, gw):
         if not dev in self.parent.devices:
             raise DeviceNotFoundError(device)
         if not dev in self.parent.devices:
             raise DeviceNotFoundError(device)
-        h = Hop(gw, dev)
-        try:
-            self.parent.hops.index(h)
-            raise HopAlreadyExistsError(gw  + '->' + dev)
-        except ValueError:
-            self.parent.hops.append(h)
+        return ListSubHandler.add(self, dev, gw)
 
 
-    @handler(u'Deletes a hop : delete <gateway> <device>')
-    def delete(self, gw, dev):
+    @handler(u'Delete a hop: delete <device> <gateway>')
+    def delete(self, dev, gw):
         if not dev in self.parent.devices:
             raise DeviceNotFoundError(device)
         if not dev in self.parent.devices:
             raise DeviceNotFoundError(device)
-        h = Hop(gw, dev)
-        try:
-            self.parent.hops.remove(h)
-        except ValueError:
-            raise HopNotFoundError(gw + '->' + dev)
+        return ListSubHandler.delete(self, dev, gw)
 
 
-    @handler(u'Lists hops : list <dev>')
-    def list(self, device):
-        try:
-            k = self.parent.hops.keys()
-        except ValueError:
-            k = list()
-        return k
-
-    @handler(u'Get information about all hops: show <dev>')
-    def show(self, device):
-        try:
-            k = self.parent.hops.values()
-        except ValueError:
-            k = list()
-        return k
 
 class Route(Sequence):
     def __init__(self, net_addr, prefix, gateway):
 
 class Route(Sequence):
     def __init__(self, net_addr, prefix, gateway):