From: Leandro Lucarella Date: Thu, 27 Sep 2007 20:48:09 +0000 (-0300) Subject: Use the serializer mode to automatically serialize handlers results. X-Git-Url: https://git.llucax.com/software/pymin.git/commitdiff_plain/9c40f2f2d207eac67ca6add337ab5808deedea84 Use the serializer mode to automatically serialize handlers results. Handlers now don't have to worry about serialization. All they have to do is return list of values (which can be lists or iterable object). Then the PyminDaemon serialize them to UTF-8 encoded CSV strings and send them through socket. --- diff --git a/pymindaemon.py b/pymindaemon.py index b8369b9..98ced48 100644 --- a/pymindaemon.py +++ b/pymindaemon.py @@ -10,10 +10,12 @@ command-line. import signal import socket -from dispatcher import Dispatcher -from eventloop import EventLoop, LoopInterruptedError -class PyminDaemon(EventLoop): +import dispatcher +import eventloop +import serializer + +class PyminDaemon(eventloop.EventLoop): r"""PyminDaemon(bind_addr, routes) -> PyminDaemon instance This class is well suited to run as a single process. It handles @@ -41,9 +43,9 @@ class PyminDaemon(EventLoop): sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) sock.bind(bind_addr) # Create EventLoop - EventLoop.__init__(self, sock) + eventloop.EventLoop.__init__(self, sock) # Create Dispatcher - self.dispatcher = Dispatcher(routes) + self.dispatcher = dispatcher.Dispatcher(routes) # Signal handling def quit(signum, frame): print "Shuting down ..." @@ -60,28 +62,37 @@ class PyminDaemon(EventLoop): (msg, addr) = self.file.recvfrom(65535) try: result = self.dispatcher.dispatch(msg) - response = 'OK ' + if result is not None: + result = serializer.serialize(result) + response = u'OK ' except Exception, e: - result = str(e) - response = 'ERROR ' + result = unicode(e) + response = u'ERROR ' if result is None: - response += '0' + response += u'0' else: - response += '%d\n%s' % (len(str(result)), result) + response += u'%d\n%s' % (len(result), result) self.file.sendto(response, addr) def run(self): r"run() -> None :: Run the event loop (shortcut to loop())" try: return self.loop() - except LoopInterruptedError, e: + except eventloop.LoopInterruptedError, e: pass if __name__ == '__main__': + from dispatcher import handler + @handler def test_handler(*args): print 'test:', args - PyminDaemon(('', 9999), dict(test=test_handler)).run() + @handler + def echo_handler(message): + print 'echo:', message + return message + + PyminDaemon(('', 9999), dict(test=test_handler, echo=echo_handler)).run() diff --git a/services/dhcp/__init__.py b/services/dhcp/__init__.py index 618f702..5734c2f 100644 --- a/services/dhcp/__init__.py +++ b/services/dhcp/__init__.py @@ -7,6 +7,8 @@ try: import cPickle as pickle except ImportError: import pickle + +import seqtools try: from dispatcher import handler except ImportError: @@ -97,7 +99,7 @@ class ParameterNotFoundError(ParameterError): self.message = 'Parameter not found: "%s"' % paramname -class Host: +class Host(seqtools.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 +113,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 +162,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 +170,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 +179,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 +239,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 +249,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):