UiConfigPlugin.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import io
  2. import os
  3. from Plugin import PluginManager
  4. from Config import config
  5. from Translate import Translate
  6. from util.Flag import flag
  7. plugin_dir = os.path.dirname(__file__)
  8. if "_" not in locals():
  9. _ = Translate(plugin_dir + "/languages/")
  10. @PluginManager.registerTo("UiRequest")
  11. class UiRequestPlugin(object):
  12. def actionWrapper(self, path, extra_headers=None):
  13. if path.strip("/") != "Config":
  14. return super(UiRequestPlugin, self).actionWrapper(path, extra_headers)
  15. if not extra_headers:
  16. extra_headers = {}
  17. script_nonce = self.getScriptNonce()
  18. self.sendHeader(extra_headers=extra_headers, script_nonce=script_nonce)
  19. site = self.server.site_manager.get(config.homepage)
  20. return iter([super(UiRequestPlugin, self).renderWrapper(
  21. site, path, "uimedia/plugins/uiconfig/config.html",
  22. "Config", extra_headers, show_loadingscreen=False, script_nonce=script_nonce
  23. )])
  24. def actionUiMedia(self, path, *args, **kwargs):
  25. if path.startswith("/uimedia/plugins/uiconfig/"):
  26. file_path = path.replace("/uimedia/plugins/uiconfig/", plugin_dir + "/media/")
  27. if config.debug and (file_path.endswith("all.js") or file_path.endswith("all.css")):
  28. # If debugging merge *.css to all.css and *.js to all.js
  29. from Debug import DebugMedia
  30. DebugMedia.merge(file_path)
  31. if file_path.endswith("js"):
  32. data = _.translateData(open(file_path).read(), mode="js").encode("utf8")
  33. elif file_path.endswith("html"):
  34. data = _.translateData(open(file_path).read(), mode="html").encode("utf8")
  35. else:
  36. data = open(file_path, "rb").read()
  37. return self.actionFile(file_path, file_obj=io.BytesIO(data), file_size=len(data))
  38. else:
  39. return super(UiRequestPlugin, self).actionUiMedia(path)
  40. @PluginManager.registerTo("UiWebsocket")
  41. class UiWebsocketPlugin(object):
  42. @flag.admin
  43. def actionConfigList(self, to):
  44. back = {}
  45. config_values = vars(config.arguments)
  46. config_values.update(config.pending_changes)
  47. for key, val in config_values.items():
  48. if key not in config.keys_api_change_allowed:
  49. continue
  50. is_pending = key in config.pending_changes
  51. if val is None and is_pending:
  52. val = config.parser.get_default(key)
  53. back[key] = {
  54. "value": val,
  55. "default": config.parser.get_default(key),
  56. "pending": is_pending
  57. }
  58. return back