]> git.llucax.com Git - software/pymin.git/blob - services/qos/dev.py
Add validation to DhcpHandler (refs #20)
[software/pymin.git] / services / qos / dev.py
1 # vim: set encoding=utf-8 et sw=4 sts=4 :
2
3 from os import path
4
5 from pymin.seqtools import Sequence
6 from pymin.dispatcher import handler
7 from pymin.service.util import SubHandler, ExecutionError, ItemNotFoundError, \
8                                call
9
10 __all__ = ('DeviceHandler',)
11
12
13 class Device(Sequence):
14
15     def __init__(self, name, mac):
16         self.name = name
17         self.mac = mac
18         self.classes = dict()
19
20     def as_tuple(self):
21         return (self.name, self.mac)
22
23
24 class DeviceHandler(SubHandler):
25
26     handler_help = u"Manage network devices"
27
28     def __init__(self, parent):
29         # FIXME remove templates to execute commands
30         from mako.template import Template
31         self.parent = parent
32         template_dir = path.join(path.dirname(__file__), 'templates')
33         dev_fn = path.join(template_dir, 'device')
34         self.device_template = Template(filename=dev_fn)
35
36     @handler(u'Bring the device up')
37     def up(self, name):
38         if name in self.parent.devices:
39             try:
40                 call(self.device_template.render(dev=name, action='add'), shell=True)
41             except ExecutionError:
42                 pass
43         else:
44             raise ItemNotFoundError(name)
45
46     @handler(u'Bring the device down')
47     def down(self, name):
48         if name in self.parent.devices:
49             try:
50                 call(self.device_template.render(dev=name, action='del'), shell=True)
51             except ExecutionError:
52                 pass
53         else:
54             raise ItemNotFoundError(name)
55
56     @handler(u'List all devices')
57     def list(self):
58         return self.parent.devices.keys()
59
60     @handler(u'Get information about a device')
61     def show(self):
62         return self.parent.devices.items()
63