1 # vim: set encoding=utf-8 et sw=4 sts=4 :
5 from pymin.service.util import Restorable, ConfigWriter, \
6 TransactionalHandler, ExecutionError, \
7 call, get_network_devices
9 from cls import ClassHandler
10 from dev import DeviceHandler, Device
11 from host import HostHandler
13 __all__ = ('QoSHandler',)
16 class QoSHandler(Restorable, ConfigWriter, TransactionalHandler):
18 handler_help = u"Manage QoS devices, classes and hosts"
20 _persistent_attrs = ('devices')
22 _restorable_defaults = dict(
23 devices=dict((dev, Device(dev, mac))
24 for (dev, mac) in get_network_devices().items())
27 _config_writer_files = ('device', 'class_add', 'class_del', 'host_add')
29 _config_writer_tpl_dir = path.join(path.dirname(__file__), 'templates')
31 def __init__(self, pickle_dir='.', config_dir='.'):
32 r"Initialize QoSHandler object, see class documentation for details."
33 self._persistent_dir = pickle_dir
34 self._config_writer_cfg_dir = config_dir
35 self._config_build_templates()
38 self.dev = DeviceHandler(self)
39 self.cls = ClassHandler(self)
40 self.host = HostHandler(self)
42 def _write_config(self):
43 r"_write_config() -> None :: Execute all commands."
44 for device in self.devices.values():
46 call(self._render_config('device', dict(dev=device.name, action='del')), shell=True)
47 except ExecutionError:
51 call(self._render_config('device', dict(dev=device.name, action='add')), shell=True)
52 except ExecutionError:
55 for cls in device.classes.values():
57 call(self._render_config('class_add', dict(
63 except ExecutionError:
66 for host in cls.hosts.values():
68 call(self._render_config('host_add', dict(
74 except ExecutionError:
77 def handle_timer(self):
78 self.refresh_devices()
80 def refresh_devices(self):
81 devices = get_network_devices()
82 #add not registered devices
83 for k, v in devices.items():
84 if k not in self.devices:
85 self.devices[k] = Device(k, v)
87 for k in self.devices.keys():
92 if __name__ == '__main__':
95 print '----------------------'