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
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 ..."
(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()
import cPickle as pickle
except ImportError:
import pickle
+
+import seqtools
try:
from dispatcher import handler
except ImportError:
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.
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.
"""
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):
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(','.join(h) for h in hosts)
+ return self.hosts.values()
class DhcpHandler:
r"""DhcpHandler([pickle_dir[, config_dir]]) -> DhcpHandler instance.
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):