rpc.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. import gc
  2. import logging
  3. from copy import deepcopy
  4. from flask import (Blueprint, Flask, abort, current_app, jsonify, redirect,
  5. render_template, request)
  6. from flask.ext.babel import Babel
  7. from flask_bootstrap import Bootstrap
  8. from cachelib import SimpleCache
  9. from greenlet import greenlet
  10. from .config import get_conf
  11. from .dbadmin import db
  12. rpc = Blueprint("rpc", __name__, url_prefix=get_conf()["ROUTE_PREFIX"] + "/api")
  13. viewui = Blueprint(
  14. "view",
  15. __name__,
  16. url_prefix=get_conf()["ROUTE_PREFIX"] + "/view",
  17. template_folder="templates",
  18. )
  19. def send_to_parent(kind, *args):
  20. """similar to the behaviour of a ParentedLet"""
  21. if not hasattr(current_app, "queue"):
  22. logging.debug("no parent queue; aborting send")
  23. return
  24. msg = {
  25. "emitter": current_app._get_current_object(),
  26. "class": current_app._get_current_object().__class__.__name__,
  27. "kind": kind,
  28. "args": args,
  29. }
  30. current_app.queue.put(msg)
  31. @rpc.route("/")
  32. def rpc_index():
  33. rules = list(current_app.url_map.iter_rules())
  34. return jsonify({"rules": [r.rule for r in rules if r.rule.startswith("/api")]})
  35. @rpc.route("/refresh")
  36. def rpc_refresh():
  37. send_to_parent("refresh")
  38. return jsonify(dict(status="ok"))
  39. @rpc.route("/audiospec", methods=["GET"])
  40. def get_audiospec():
  41. return jsonify(current_app.larigira.controller.player.continous_audiospec)
  42. @rpc.route("/audiospec", methods=["PUT"])
  43. def change_audiospec():
  44. player = current_app.larigira.controller.player
  45. if request.json is None:
  46. abort(400, "Must send application/json data")
  47. if "spec" not in request.json or type(request.json["spec"]) is not dict:
  48. abort(400, "Object must have a key 'spec' whose value is an object")
  49. player.continous_audiospec = request.json["spec"]
  50. if "kind" not in request.json["spec"]:
  51. abort(400, "invalid audiospec")
  52. return jsonify(player.continous_audiospec)
  53. @rpc.route("/audiospec", methods=["DELETE"])
  54. def reset_audiospec():
  55. player = current_app.larigira.controller.player
  56. player.continous_audiospec = None
  57. return jsonify(player.continous_audiospec)
  58. @rpc.route("/eventsenabled/toggle", methods=["POST"])
  59. def toggle_events_enabled():
  60. status = current_app.larigira.controller.player.events_enabled
  61. current_app.larigira.controller.player.events_enabled = not status
  62. return jsonify(dict(events_enabled=not status))
  63. @rpc.route("/eventsenabled", methods=["GET"])
  64. def get_events_enabled():
  65. status = current_app.larigira.controller.player.events_enabled
  66. return jsonify(dict(events_enabled=status))
  67. @rpc.route("/eventsenabled", methods=["PUT"])
  68. def set_events_enabled():
  69. player = current_app.larigira.controller.player
  70. if request.json is None:
  71. abort(400, "Must send application/json data")
  72. if type(request.json) is not bool:
  73. abort(400, "Content must be a JSON boolean")
  74. player.events_enabled = request.json
  75. return jsonify(dict(events_enabled=request.json))
  76. def get_scheduled_audiogen():
  77. larigira = current_app.larigira
  78. running = larigira.controller.monitor.running
  79. events = {t: {} for t in running.keys()}
  80. for timespec_eid in events:
  81. orig_info = running[timespec_eid]
  82. info = events[timespec_eid]
  83. info["running_time"] = orig_info["running_time"].isoformat()
  84. info["audiospecs"] = orig_info["audiospecs"]
  85. info["timespec"] = orig_info["timespec"]
  86. info["timespec"]["actions"] = {
  87. aid: spec
  88. for aid, spec in zip(info["timespec"]["actions"], info["audiospecs"])
  89. }
  90. info["greenlet"] = hex(id(orig_info["greenlet"]))
  91. return events
  92. @viewui.route("/status/running")
  93. def ui_wip():
  94. audiogens = get_scheduled_audiogen()
  95. return render_template(
  96. "running.html",
  97. audiogens=sorted(audiogens.items(), key=lambda x: x[1]["running_time"]),
  98. )
  99. @rpc.route("/debug/running")
  100. def rpc_wip():
  101. def treeify(flat):
  102. roots = [obid for obid in flat if flat[obid]["parent"] not in flat]
  103. tree = deepcopy(flat)
  104. for obid in tree:
  105. tree[obid]["children"] = {}
  106. to_remove = []
  107. for obid in tree:
  108. if obid in roots:
  109. continue
  110. if obid not in tree:
  111. current_app.logger.warning("How strange, {} not in tree".format(obid))
  112. continue
  113. tree[tree[obid]["parent"]]["children"][obid] = tree[obid]
  114. to_remove.append(obid)
  115. for obid in to_remove:
  116. del tree[obid]
  117. return tree
  118. greenlets = {}
  119. for ob in filter(lambda obj: isinstance(obj, greenlet), gc.get_objects()):
  120. objrepr = {"repr": repr(ob), "class": ob.__class__.__name__}
  121. if hasattr(ob, "parent_greenlet") and ob.parent_greenlet is not None:
  122. objrepr["parent"] = hex(id(ob.parent_greenlet))
  123. else:
  124. objrepr["parent"] = hex(id(ob.parent)) if ob.parent is not None else None
  125. if hasattr(ob, "doc"):
  126. objrepr["doc"] = ob.doc.split("\n")[0]
  127. elif ob.__doc__:
  128. objrepr["doc"] = ob.__doc__.split("\n")[0]
  129. greenlets[hex(id(ob))] = objrepr
  130. # TODO: make it a tree
  131. return jsonify(
  132. dict(
  133. greenlets=greenlets,
  134. greenlets_tree=treeify(greenlets),
  135. audiogens=get_scheduled_audiogen(),
  136. )
  137. )
  138. def babel_get_locale():
  139. try:
  140. if request.accept_languages:
  141. return request.accept_languages[0][0]
  142. finally:
  143. return None
  144. def create_app(queue, larigira):
  145. app = Flask("larigira", static_url_path=get_conf()["ROUTE_PREFIX"] + "/static")
  146. app.config.update(get_conf())
  147. Bootstrap(app)
  148. babel = Babel(app)
  149. babel.localeselector(babel_get_locale)
  150. app.register_blueprint(rpc)
  151. app.register_blueprint(viewui)
  152. app.register_blueprint(db)
  153. app.route(get_conf()["ROUTE_PREFIX"] + "/")(
  154. lambda: redirect(get_conf()["ROUTE_PREFIX"] + get_conf()["HOME_URL"])
  155. )
  156. app.queue = queue
  157. app.larigira = larigira
  158. app.cache = SimpleCache()
  159. return app