|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +import sys, os, importlib |
| 4 | +sys.path.append(os.path.abspath("..")) |
| 5 | +import msgflo, gevent |
| 6 | +import logging |
| 7 | + |
| 8 | +def find_participant_classes(mod): |
| 9 | + cls = [] |
| 10 | + for name in dir(mod): |
| 11 | + symbol = getattr(mod, name) |
| 12 | + if hasattr(symbol, 'send') and hasattr(symbol, 'process'): # XXX: HACK |
| 13 | + cls.append(symbol) |
| 14 | + return cls |
| 15 | + |
| 16 | +def load_module_file(filepath): |
| 17 | + modulename = os.path.basename(filepath) |
| 18 | + modulename = modulename.replace('.py', '') |
| 19 | + dirpath = os.path.dirname(filepath) |
| 20 | + print dirpath, modulename |
| 21 | + sys.path.append(dirpath) # XXX: HACK |
| 22 | + module = importlib.import_module(modulename) |
| 23 | + return module |
| 24 | + |
| 25 | +def main(): |
| 26 | + prog, args = sys.argv[0], sys.argv[1:] |
| 27 | + try: |
| 28 | + modulepath, role = args |
| 29 | + except ValueError, e: |
| 30 | + sys.stderr.write("Usage: msgflo-python MODULE.py ROLE\n") |
| 31 | + return 1 |
| 32 | + |
| 33 | + module = load_module_file(modulepath) |
| 34 | + classes = find_participant_classes(module) |
| 35 | + Participant = classes[0] |
| 36 | + |
| 37 | + participant = Participant(role) |
| 38 | + d = participant.definition |
| 39 | + waiter = gevent.event.AsyncResult() |
| 40 | + engine = msgflo.run(participant, done_cb=waiter.set) |
| 41 | + print "%s(%s) running on %s" % (d['role'], d['component'], engine.broker_url) |
| 42 | + sys.stdout.flush() |
| 43 | + waiter.wait() |
| 44 | + |
| 45 | +if __name__ == '__main__': |
| 46 | + logging.basicConfig() |
| 47 | + main() |
0 commit comments