1 # vim: set encoding=utf-8 et sw=4 sts=4 :
5 from pymin.seqtools import Sequence
6 from pymin.dispatcher import handler
7 from pymin.service.util import SubHandler, ExecutionError, ItemNotFoundError, \
10 __all__ = ('DeviceHandler',)
13 class Device(Sequence):
15 def __init__(self, name, mac):
21 return (self.name, self.mac)
24 class DeviceHandler(SubHandler):
26 handler_help = u"Manage network devices"
28 def __init__(self, parent):
29 # FIXME remove templates to execute commands
30 from mako.template import Template
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)
36 @handler(u'Bring the device up')
38 if name in self.parent.devices:
40 call(self.device_template.render(dev=name, action='add'), shell=True)
41 except ExecutionError:
44 raise ItemNotFoundError(name)
46 @handler(u'Bring the device down')
48 if name in self.parent.devices:
50 call(self.device_template.render(dev=name, action='del'), shell=True)
51 except ExecutionError:
54 raise ItemNotFoundError(name)
56 @handler(u'List all devices')
58 return self.parent.devices.keys()
60 @handler(u'Get information about a device')
62 return self.parent.devices.items()