X-Git-Url: https://git.llucax.com/software/pymin.git/blobdiff_plain/2269d334bc9d3ae1c0de8229875b068e4cb29ac9..83738d4fffe1910d9d75849657086d2e4e921a15:/services/dhcp/__init__.py?ds=sidebyside diff --git a/services/dhcp/__init__.py b/services/dhcp/__init__.py index 618f702..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,11 +119,9 @@ 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: r"""HostHandler(hosts) -> HostHandler instance :: Handle a list of hosts. @@ -162,8 +168,7 @@ class HostHandler: """ if not name in self.hosts: raise HostNotFoundError(name) - h = self.hosts[name] - return ','.join(list(h)) + return self.hosts[name] @handler def list(self): @@ -171,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): @@ -180,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(','.join(h) for h in hosts) + return self.hosts.values() class DhcpHandler: r"""DhcpHandler([pickle_dir[, config_dir]]) -> DhcpHandler instance. @@ -241,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): @@ -251,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):