X-Git-Url: https://git.llucax.com/software/pymin.git/blobdiff_plain/296d853cc95fd5bef262248cfe21b507abd26a4f..3660782f64b0e3dfc2db4b1bfce4c59b4b46e3fc:/pymin/services/dhcp/__init__.py?ds=sidebyside diff --git a/pymin/services/dhcp/__init__.py b/pymin/services/dhcp/__init__.py index 8f42e1a..6e6f198 100644 --- a/pymin/services/dhcp/__init__.py +++ b/pymin/services/dhcp/__init__.py @@ -5,10 +5,10 @@ from os import path from pymin.seqtools import Sequence from pymin.dispatcher import Handler, handler, HandlerError from pymin.services.util import Restorable, ConfigWriter, InitdHandler, \ - TransactionalHandler, ParametersHandler + TransactionalHandler, ParametersHandler, \ + DictSubHandler -__ALL__ = ('DhcpHandler', 'Error', 'HostError', 'HostAlreadyExistsError', - 'HostNotFoundError') +__ALL__ = ('DhcpHandler', 'Error') class Error(HandlerError): r""" @@ -19,48 +19,7 @@ class Error(HandlerError): message - A descriptive error message. """ - - def __init__(self, message): - r"Initialize the Error object. See class documentation for more info." - self.message = message - - def __str__(self): - return self.message - -class HostError(Error, KeyError): - r""" - HostError(hostname) -> HostError instance - - This is the base exception for all host related errors. - """ - - def __init__(self, hostname): - r"Initialize the object. See class documentation for more info." - self.message = 'Host error: "%s"' % hostname - -class HostAlreadyExistsError(HostError): - r""" - HostAlreadyExistsError(hostname) -> HostAlreadyExistsError instance - - This exception is raised when trying to add a hostname that already exists. - """ - - def __init__(self, hostname): - r"Initialize the object. See class documentation for more info." - self.message = 'Host already exists: "%s"' % hostname - -class HostNotFoundError(HostError): - r""" - HostNotFoundError(hostname) -> HostNotFoundError instance - - This exception is raised when trying to operate on a hostname that doesn't - exists. - """ - - def __init__(self, hostname): - r"Initialize the object. See class documentation for more info." - self.message = 'Host not found: "%s"' % hostname - + pass class Host(Sequence): r"""Host(name, ip, mac) -> Host instance :: Class representing a host. @@ -80,60 +39,23 @@ class Host(Sequence): r"Return a tuple representing the host." return (self.name, self.ip, self.mac) -class HostHandler(Handler): - r"""HostHandler(hosts) -> HostHandler instance :: Handle a list of hosts. - - This class is a helper for DhcpHandler to do all the work related to hosts - administration. - - hosts - A dictionary with string keys (hostnames) and Host instances values. - """ - - def __init__(self, hosts): - r"Initialize HostHandler object, see class documentation for details." - self.hosts = hosts - - @handler(u'Add a new host.') - def add(self, name, ip, mac): - r"add(name, ip, mac) -> None :: Add a host to the hosts list." - if name in self.hosts: - raise HostAlreadyExistsError(name) - self.hosts[name] = Host(name, ip, mac) - - @handler(u'Update a host.') - def update(self, name, ip=None, mac=None): - r"update(name[, ip[, mac]]) -> None :: Update a host of the hosts list." - if not name in self.hosts: - raise HostNotFoundError(name) + def update(self, ip=None, mac=None): if ip is not None: - self.hosts[name].ip = ip + self.ip = ip if mac is not None: - self.hosts[name].mac = mac + self.mac = mac - @handler(u'Delete a host.') - def delete(self, name): - r"delete(name) -> None :: Delete a host of the hosts list." - if not name in self.hosts: - raise HostNotFoundError(name) - del self.hosts[name] +class HostHandler(DictSubHandler): + r"""HostHandler(parent) -> HostHandler instance :: Handle a list of hosts. - @handler(u'Get information about a host.') - def get(self, name): - r"get(name) -> Host :: List all the information of a host." - if not name in self.hosts: - raise HostNotFoundError(name) - return self.hosts[name] - - @handler(u'List hosts.') - def list(self): - r"list() -> tuple :: List all the hostnames." - return self.hosts.keys() + This class is a helper for DhcpHandler to do all the work related to hosts + administration. + """ - @handler(u'Get information about all hosts.') - def show(self): - r"show() -> list of Hosts :: List all the complete hosts information." - return self.hosts.values() + handler_help = u"Manage DHCP hosts" + _cont_subhandler_attr = 'hosts' + _cont_subhandler_class = Host class DhcpHandler(Restorable, ConfigWriter, InitdHandler, TransactionalHandler, ParametersHandler): @@ -148,6 +70,8 @@ class DhcpHandler(Restorable, ConfigWriter, InitdHandler, TransactionalHandler, Both defaults to the current working directory. """ + handler_help = u"Manage DHCP service" + _initd_name = 'dhcpd' _persistent_attrs = ('params', 'hosts') @@ -175,7 +99,7 @@ class DhcpHandler(Restorable, ConfigWriter, InitdHandler, TransactionalHandler, self._config_writer_cfg_dir = config_dir self._config_build_templates() self._restore() - self.host = HostHandler(self.hosts) + self.host = HostHandler(self) def _get_config_vars(self, config_file): return dict(hosts=self.hosts.values(), **self.params)