]> git.llucax.com Git - software/pymin.git/blob - services/ip/route.py
Add GPL v3 license to the project
[software/pymin.git] / services / ip / route.py
1 # vim: set encoding=utf-8 et sw=4 sts=4 :
2
3 from pymin.seqtools import Sequence
4 from pymin.dispatcher import handler
5 from pymin.service.util import ListComposedSubHandler
6
7 __all__ = ('RouteHandler',)
8
9
10 class Route(Sequence):
11     def __init__(self, net_addr, prefix, gateway):
12         self.net_addr = net_addr
13         self.prefix = prefix
14         self.gateway = gateway
15
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
20
21     def as_tuple(self):
22         return(self.net_addr, self.prefix, self.gateway)
23
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:
28             return 0
29         return cmp(id(self), id(other))
30
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
36
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):
39         if dev is not None:
40             ListComposedSubHandler.add(self, dev, net_addr, prefix, gateway)
41         else:
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)
45
46     @handler("Deletes a route : ip route delete <route_number_in_show> [dev]")
47     def delete(self, index, dev=None):
48         if dev is not None:
49             ListComposedSubHandler.delete(self, dev, index)
50         else:
51             i = int(index)
52             del self.parent.no_device_routes[i]
53
54     @handler("Shows routes : ip route show [dev]")
55     def show(self, dev=None):
56         if dev is not None:
57             return ListComposedSubHandler.show(self, dev)
58         else:
59             return self.parent.no_device_routes
60