rpc.py 861 B

12345678910111213141516171819202122232425262728293031323334353637
  1. import logging
  2. from flask import current_app, Blueprint, Flask
  3. rpc = Blueprint('rpc', __name__, url_prefix='/api')
  4. def send_to_parent(kind, *args):
  5. '''similar to the behaviour of a ParentedLet'''
  6. if not hasattr(current_app, 'queue'):
  7. logging.debug('no parent queue; aborting send')
  8. return
  9. msg = {
  10. 'emitter': current_app._get_current_object(),
  11. 'class': current_app._get_current_object().__class__.__name__,
  12. 'kind': kind,
  13. 'args': args
  14. }
  15. current_app.queue.put(msg)
  16. @rpc.route('/')
  17. def rpc_index():
  18. return 'So, what command would you like to give?'
  19. @rpc.route('/refresh')
  20. def rpc_refresh():
  21. print current_app.queue
  22. send_to_parent('rpc')
  23. return 'ok, put'
  24. def create_app(queue):
  25. app = Flask(__name__)
  26. app.register_blueprint(rpc)
  27. app.queue = queue
  28. return app