X-Git-Url: https://git.llucax.com/software/pymin.git/blobdiff_plain/2caea9b817885fc1dcb6b1feaf7a06a8a6f54aac..83738d4fffe1910d9d75849657086d2e4e921a15:/services/dhcp/__init__.py?ds=inline diff --git a/services/dhcp/__init__.py b/services/dhcp/__init__.py index 1df0062..b728162 100644 --- a/services/dhcp/__init__.py +++ b/services/dhcp/__init__.py @@ -7,10 +7,18 @@ 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, HandlerError except ImportError: - def handler(f): return f # NOP for testing + # NOP for testing + class HandlerError(RuntimeError): pass + def handler(f): return f __ALL__ = ('DhcpHandler',) @@ -22,7 +30,7 @@ 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. @@ -97,7 +105,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,6 +119,10 @@ class Host: self.ip = ip self.mac = mac + def as_tuple(self): + r"Return a tuple representing the host." + return (self.name, self.ip, self.mac) + class HostHandler: r"""HostHandler(hosts) -> HostHandler instance :: Handle a list of hosts. @@ -156,8 +168,7 @@ class HostHandler: """ if not name in self.hosts: raise HostNotFoundError(name) - h = self.hosts[name] - return '%s,%s,%s' % (h.name, h.ip, h.mac) + return self.hosts[name] @handler def list(self): @@ -165,7 +176,7 @@ class HostHandler: The list is returned as a single CSV line with all the hostnames. """ - return ','.join(self.hosts) + return self.hosts.keys() @handler def show(self): @@ -174,8 +185,7 @@ class HostHandler: 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('%s,%s,%s' % (h.name, h.ip, h.mac) for h in hosts) + return self.hosts.values() class DhcpHandler: r"""DhcpHandler([pickle_dir[, config_dir]]) -> DhcpHandler instance. @@ -235,7 +245,7 @@ class DhcpHandler: The list is returned as a single CSV line with all the names. """ - return ','.join(self.vars) + return self.vars.keys() @handler def show(self): @@ -245,7 +255,7 @@ 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 def start(self):