1 # vim: set encoding=utf-8 et sw=4 sts=4 :
3 from pymin.seqtools import Sequence
4 from pymin.dispatcher import handler
5 from pymin.service.util import ListComposedSubHandler
7 __all__ = ('RouteHandler',)
10 class Route(Sequence):
11 def __init__(self, net_addr, prefix, gateway):
12 self.net_addr = net_addr
14 self.gateway = gateway
16 def update(self, net_addr=None, prefix=None, gateway=None):
17 if net_addr is not None: self.net_addr = net_addr
18 if prefix is not None: self.prefix = prefix
19 if gateway is not None: self.gateway = gateway
22 return(self.net_addr, self.prefix, self.gateway)
24 def __cmp__(self, other):
25 if self.net_addr == other.net_addr \
26 and self.prefix == other.prefix \
27 and self.gateway == other.gateway:
29 return cmp(id(self), id(other))
31 class RouteHandler(ListComposedSubHandler):
32 handler_help = u"Manage IP routes"
33 _comp_subhandler_cont = 'devices'
34 _comp_subhandler_attr = 'routes'
35 _comp_subhandler_class = Route
37 @handler(u'Adds a route to : ip route add <net_addr> <prefix> <gateway> [device]')
38 def add(self, net_addr, prefix, gateway, dev=None):
40 ListComposedSubHandler.add(self, dev, net_addr, prefix, gateway)
42 r = Route(net_addr, prefix, gateway)
43 if not r in self.parent.no_device_routes:
44 self.parent.no_device_routes.append(r)
46 @handler("Deletes a route : ip route delete <route_number_in_show> [dev]")
47 def delete(self, index, dev=None):
49 ListComposedSubHandler.delete(self, dev, index)
52 del self.parent.no_device_routes[i]
54 @handler("Shows routes : ip route show [dev]")
55 def show(self, dev=None):
57 return ListComposedSubHandler.show(self, dev)
59 return self.parent.no_device_routes