def main(): import os.path from sys import argv from json import load from zero import Zero, ZeroSetup HERE = os.path.abspath(os.path.join(os.path.dirname(__file__), '..')) conf = HERE + '/log.json' if len(argv) > 1: conf = argv[1] print 'Loading config from', conf with open(conf) as fin: conf = load(fin)['log'] setup = ZeroSetup('pull', conf['port']) path = conf['file'] if path[0] != '/': path = HERE + '/' + path print 'Logger started for', setup print 'Logging to', path with open(path, 'a', 1) as fout: logout = Logout(conf) try: for line in Zero(setup): fout.write(line) fout.write('\n') logout.tty(line) except KeyboardInterrupt: print 'Logger quitting.' print 'Logger stopped on', setup
def main(): 'For CLI use, see usage in __doc__.' import os.path from sys import argv, exit from json import load from os.path import exists from itertools import imap from collections import deque HERE = os.path.abspath(os.path.join(os.path.dirname(__file__), '..')) args = deque(argv[1:]) if len(args) < 3: exit(__doc__) conf = HERE + '/log.json' level = args.popleft() if exists(level): conf = level level = args.popleft() with open(conf) as fin: conf = load(fin)['log'] sender = args.popleft() if args[0] == '-': messages = ZeroSetup.iter_stdin() else: messages = iter(args) messages = imap(lambda x: ZLogger.format(sender, level, x), messages) z = Zero(ZeroSetup('push', conf['port'])) for msg in messages: z(msg)
def zlogger(config, sender): ''' Convenience function for setting up a ZLogger and queue. Returns a ZLogger object with .fyi, .wtf, .omg functions as specified in config['log']['levels']. ''' from Queue import Queue from threading import Thread logq = Queue() slog = Zero( ZeroSetup('push', 'tcp://%(host)s:%(port)s' % config).nonblocking()) def thread(slog=slog): for t in iter(logq.get, ''): slog(t) t = Thread(target=thread) t.daemon = True t.start() return ZLogger(config, logq, sender, gethostname())
def zrpc(sysconfig, workertype): ''' Returns an activated Zero with RPC worker of type workertype as specified in sysconfig. >>> from .test import _get_test_config >>> from zero import zbg >>> from itertools import izip >>> from socket import gethostname >>> from time import time >>> cfg = _get_test_config() >>> z = zrpc(cfg, 'common') >>> o = z.opposite() >>> z # doctest: +ELLIPSIS Zero(ZeroSetup('rep', 8000).binding(True)).activated(<zero.test.CommonRPC object at ...>) >>> o Zero(ZeroSetup('req', 8000).binding(False)) >>> t = zbg(o, [['ping'], ['echo', {'msg': 'Hello'}], ['hostname'], ['time']], lambda x: x) >>> reps = [] >>> for _, msg in izip(range(4), z): # doctest: +ELLIPSIS ... reps.append(msg) ... z(msg) >>> reps[0] 'pong' >>> reps[1] u'Hello' >>> reps[2] == gethostname() True >>> abs(time() - reps[3]) < 1 True >>> t.join() ''' from zero import Zero, ZeroSetup wconf = sysconfig['workers'][workertype] zconf = wconf['zmq'] setup = ZeroSetup(zconf['method'], zconf['port']).debugging(zconf.get('debug', False)) if 'bind' in zconf: setup.binding(zconf['bind']) if 'host' in zconf and not setup.bind: setup._point = 'tcp://%(host)s:%(port)s' % zconf mod = __import__(wconf['module']) for modpart in wconf['module'].split('.')[1:]: mod = getattr(mod, modpart) klass = getattr(mod, wconf['class']) return Zero(setup).activated(klass(sysconfig, workertype))
def _test_rpc(): ''' For doctest >>> from zero.rpc import ZeroRPC >>> ZeroRPC._test_rpc() REP u'hello' REP 100 ''' from zero import Zero, ZeroSetup class Z(ZeroRPC): def hi(self): return "hello" def sqr(self, x): return x * x def listen(): zero = Zero(ZeroSetup('rep', 8000)).activated(Z()) for _, msg in izip(range(2), zero): zero(msg) zero.close() from threading import Thread t = Thread(name='TestRPC', target=listen) t.daemon = True t.start() zero = Zero(ZeroSetup('req', 8000)) msg = ['hi'] rep = zero(msg) print 'REP %r' % rep msg = ['sqr', {'x': 10}] rep = zero(msg) print 'REP %r' % rep zero.close() t.join()
def listen(): zero = Zero(ZeroSetup('rep', 8000)).activated(Z()) for _, msg in izip(range(2), zero): zero(msg) zero.close()