X-Git-Url: https://git.llucax.com/software/pymin.git/blobdiff_plain/2269d334bc9d3ae1c0de8229875b068e4cb29ac9..031a757fb7b600a9015d018011cc7b135d7c8627:/services/dhcp/__init__.py?ds=inline diff --git a/services/dhcp/__init__.py b/services/dhcp/__init__.py index 618f702..08bb8b5 100644 --- a/services/dhcp/__init__.py +++ b/services/dhcp/__init__.py @@ -7,12 +7,25 @@ try: import cPickle as pickle except ImportError: import pickle + +try: + from seqtools import Sequence +except ImportError: + # NOP for testing + class Sequence: pass try: - from dispatcher import handler + from dispatcher import Handler, handler, HandlerError except ImportError: - def handler(f): return f # NOP for testing + # NOP for testing + class HandlerError(RuntimeError): pass + class Handler: pass + def handler(help): + def wrapper(f): + return f + return wrapper -__ALL__ = ('DhcpHandler',) +__ALL__ = ('DhcpHandler', 'Error', 'HostError', 'HostAlreadyExistsError', + 'HostNotFoundError', 'ParameterError', 'ParameterNotFoundError') pickle_ext = '.pkl' pickle_vars = 'vars' @@ -22,9 +35,9 @@ config_filename = 'dhcpd.conf' template_dir = path.join(path.dirname(__file__), 'templates') -class Error(RuntimeError): +class Error(HandlerError): r""" - Error(command) -> Error instance :: Base DhcpHandler exception class. + Error(message) -> Error instance :: Base DhcpHandler exception class. All exceptions raised by the DhcpHandler inherits from this one, so you can easily catch any DhcpHandler exception. @@ -97,7 +110,7 @@ class ParameterNotFoundError(ParameterError): self.message = 'Parameter not found: "%s"' % paramname -class Host: +class Host(Sequence): r"""Host(name, ip, mac) -> Host instance :: Class representing a host. name - Host name, should be a fully qualified name, but no checks are done. @@ -111,13 +124,11 @@ class Host: self.ip = ip self.mac = mac - def __iter__(self): - r"Iterate over a host." - yield self.name - yield self.ip - yield self.mac + def as_tuple(self): + r"Return a tuple representing the host." + return (self.name, self.ip, self.mac) -class HostHandler: +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 @@ -130,14 +141,14 @@ class HostHandler: r"Initialize HostHandler object, see class documentation for details." self.hosts = hosts - @handler + @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 + @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: @@ -147,14 +158,14 @@ class HostHandler: if mac is not None: self.hosts[name].mac = mac - @handler + @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] - @handler + @handler(u'Get information about a host.') def get(self, name): r"""get(name) -> CSV string :: List all the information of a host. @@ -162,28 +173,26 @@ class HostHandler: """ if not name in self.hosts: raise HostNotFoundError(name) - h = self.hosts[name] - return ','.join(list(h)) + return self.hosts[name] - @handler + @handler(u'List hosts.') def list(self): r"""list() -> CSV string :: List all the hostnames. The list is returned as a single CSV line with all the hostnames. """ - return ','.join(self.hosts) + return self.hosts.keys() - @handler + @handler(u'Get information about all hosts.') def show(self): r"""show() -> CSV string :: List all the complete hosts information. The hosts are returned as a CSV list with each host in a line, like: hostname,ip,mac """ - hosts = self.hosts.values() - return '\n'.join(','.join(h) for h in hosts) + return self.hosts.values() -class DhcpHandler: +class DhcpHandler(Handler): r"""DhcpHandler([pickle_dir[, config_dir]]) -> DhcpHandler instance. Handles DHCP service commands for the dhcpd program. @@ -221,29 +230,29 @@ class DhcpHandler: self._write_config() self.host = HostHandler(self.hosts) - @handler + @handler(u'Set a DHCP parameter.') def set(self, param, value): r"set(param, value) -> None :: Set a DHCP parameter." if not param in self.vars: raise ParameterNotFoundError(param) self.vars[param] = value - @handler + @handler(u'Get a DHCP parameter.') def get(self, param): r"get(param) -> None :: Get a DHCP parameter." if not param in self.vars: raise ParameterNotFoundError(param) return self.vars[param] - @handler + @handler(u'List all available DHCP parameters.') def list(self): r"""list() -> CSV string :: List all the parameter names. The list is returned as a single CSV line with all the names. """ - return ','.join(self.vars) + return self.vars.keys() - @handler + @handler(u'Get all DHCP parameters, with their values.') def show(self): r"""show() -> CSV string :: List all the parameters (with their values). @@ -251,37 +260,37 @@ class DhcpHandler: line, like: name,value """ - return '\n'.join(('%s,%s' % (k, v) for (k, v) in self.vars.items())) + return self.vars.items() - @handler + @handler(u'Start the service.') def start(self): r"start() -> None :: Start the DHCP service." #esto seria para poner en una interfaz #y seria el hook para arrancar el servicio pass - @handler + @handler(u'Stop the service.') def stop(self): r"stop() -> None :: Stop the DHCP service." #esto seria para poner en una interfaz #y seria el hook para arrancar el servicio pass - @handler + @handler(u'Restart the service.') def restart(self): r"restart() -> None :: Restart the DHCP service." #esto seria para poner en una interfaz #y seria el hook para arrancar el servicio pass - @handler + @handler(u'Reload the service config (without restarting, if possible).') def reload(self): r"reload() -> None :: Reload the configuration of the DHCP service." #esto seria para poner en una interfaz #y seria el hook para arrancar el servicio pass - @handler + @handler(u'Commit the changes (reloading the service, if necessary).') def commit(self): r"commit() -> None :: Commit the changes and reload the DHCP service." #esto seria para poner en una interfaz @@ -291,7 +300,7 @@ class DhcpHandler: self._write_config() self.reload() - @handler + @handler(u'Discard all the uncommited changes.') def rollback(self): r"rollback() -> None :: Discard the changes not yet commited." self._load()