UiConfigPlugin.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. from Plugin import PluginManager
  2. from Config import config
  3. from Translate import Translate
  4. from cStringIO import StringIO
  5. if "_" not in locals():
  6. _ = Translate("plugins/UiConfig/languages/")
  7. @PluginManager.afterLoad
  8. def importPluginnedClasses():
  9. from Ui import UiWebsocket
  10. UiWebsocket.admin_commands.add("configList")
  11. @PluginManager.registerTo("UiRequest")
  12. class UiRequestPlugin(object):
  13. def actionWrapper(self, path, extra_headers=None):
  14. if path.strip("/") != "Config":
  15. return super(UiRequestPlugin, self).actionWrapper(path, extra_headers)
  16. if not extra_headers:
  17. extra_headers = {}
  18. script_nonce = self.getScriptNonce()
  19. self.sendHeader(extra_headers=extra_headers, script_nonce=script_nonce)
  20. site = self.server.site_manager.get(config.homepage)
  21. return iter([super(UiRequestPlugin, self).renderWrapper(
  22. site, path, "uimedia/plugins/uiconfig/config.html",
  23. "Config", extra_headers, show_loadingscreen=False, script_nonce=script_nonce
  24. )])
  25. def actionUiMedia(self, path, *args, **kwargs):
  26. if path.startswith("/uimedia/plugins/uiconfig/"):
  27. file_path = path.replace("/uimedia/plugins/uiconfig/", "plugins/UiConfig/media/")
  28. if config.debug and (file_path.endswith("all.js") or file_path.endswith("all.css")):
  29. # If debugging merge *.css to all.css and *.js to all.js
  30. from Debug import DebugMedia
  31. DebugMedia.merge(file_path)
  32. if file_path.endswith("js"):
  33. data = _.translateData(open(file_path).read(), mode="js")
  34. elif file_path.endswith("html"):
  35. data = _.translateData(open(file_path).read(), mode="html")
  36. else:
  37. data = open(file_path).read()
  38. return self.actionFile(file_path, file_obj=StringIO(data), file_size=len(data))
  39. else:
  40. return super(UiRequestPlugin, self).actionUiMedia(path)
  41. @PluginManager.registerTo("UiWebsocket")
  42. class UiWebsocketPlugin(object):
  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.iteritems():
  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