rpc.py 871 B

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