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.
import signal
import socket
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
r"""PyminDaemon(bind_addr, routes) -> PyminDaemon instance
This class is well suited to run as a single process. It handles
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind(bind_addr)
# Create 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)
- self.dispatcher = Dispatcher(routes)
+ self.dispatcher = dispatcher.Dispatcher(routes)
# Signal handling
def quit(signum, frame):
print "Shuting down ..."
# Signal handling
def quit(signum, frame):
print "Shuting down ..."
(msg, addr) = self.file.recvfrom(65535)
try:
result = self.dispatcher.dispatch(msg)
(msg, addr) = self.file.recvfrom(65535)
try:
result = self.dispatcher.dispatch(msg)
+ if result is not None:
+ result = serializer.serialize(result)
+ response = u'OK '
- result = str(e)
- response = 'ERROR '
+ result = unicode(e)
+ response = u'ERROR '
- 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()
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__':
pass
if __name__ == '__main__':
+ from dispatcher import handler
+
@handler
def test_handler(*args):
print 'test:', args
@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()
import cPickle as pickle
except ImportError:
import pickle
import cPickle as pickle
except ImportError:
import pickle
try:
from dispatcher import handler
except ImportError:
try:
from dispatcher import handler
except ImportError:
self.message = 'Parameter not found: "%s"' % paramname
self.message = 'Parameter not found: "%s"' % paramname
+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.
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
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.
class HostHandler:
r"""HostHandler(hosts) -> HostHandler instance :: Handle a list of hosts.
"""
if not name in self.hosts:
raise HostNotFoundError(name)
"""
if not name in self.hosts:
raise HostNotFoundError(name)
- h = self.hosts[name]
- return ','.join(list(h))
+ return self.hosts[name]
The list is returned as a single CSV line with all the hostnames.
"""
The list is returned as a single CSV line with all the hostnames.
"""
- return ','.join(self.hosts)
+ return self.hosts.keys()
The hosts are returned as a CSV list with each host in a line, like:
hostname,ip,mac
"""
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.
class DhcpHandler:
r"""DhcpHandler([pickle_dir[, config_dir]]) -> DhcpHandler instance.
The list is returned as a single CSV line with all the names.
"""
The list is returned as a single CSV line with all the names.
"""
- return ','.join(self.vars)
+ return self.vars.keys()
line, like:
name,value
"""
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):
@handler
def start(self):