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',)
template_dir = path.join(path.dirname(__file__), 'templates')
-class Error(RuntimeError):
+class Error(HandlerError):
r"""
Error(command) -> Error instance :: Base DhcpHandler exception class.
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.
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.
raise HostNotFoundError(name)
del self.hosts[name]
+ @handler
+ def get(self, name):
+ r"""get(name) -> CSV string :: List all the information of a host.
+
+ The host is returned as a CSV list of: hostname,ip,mac
+ """
+ if not name in self.hosts:
+ raise HostNotFoundError(name)
+ return self.hosts[name]
+
@handler
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
def show(self):
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.
raise ParameterNotFoundError(param)
self.vars[param] = value
+ @handler
+ 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
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
def show(self):
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):