Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

138 lines
3.8KB

  1. import sys
  2. from flask import request, send_from_directory, json
  3. from importlib import import_module
  4. from modules.core.core import cbpi
  5. from git import Repo
  6. import os
  7. import requests
  8. import yaml
  9. import shutil
  10. from flask_classy import FlaskView, route
  11. modules = {}
  12. class PluginView(FlaskView):
  13. def merge(self, source, destination):
  14. """
  15. Helper method to merge two dicts
  16. :param source:
  17. :param destination:
  18. :return:
  19. """
  20. for key, value in source.items():
  21. if isinstance(value, dict):
  22. # get node or create one
  23. node = destination.setdefault(key, {})
  24. self.merge(value, node)
  25. else:
  26. destination[key] = value
  27. return destination
  28. @route('/', methods=['GET'])
  29. def get(self):
  30. """
  31. Get Plugin List
  32. ---
  33. tags:
  34. - plugin
  35. responses:
  36. 200:
  37. description: List of all plugins
  38. """
  39. response = requests.get("https://raw.githubusercontent.com/Manuel83/craftbeerpi-plugins/master/plugins.yaml")
  40. self.api.cache["plugins"] = self.merge(yaml.load(response.text), self.api.cache["plugins"])
  41. for key, value in cbpi.cache["plugins"].iteritems():
  42. value["installed"] = os.path.isdir("./plugins/%s/" % (key))
  43. return json.dumps(cbpi.cache["plugins"])
  44. @route('/<name>', methods=['DELETE'])
  45. def delete(self,name):
  46. """
  47. Delete Plugin
  48. ---
  49. tags:
  50. - plugin
  51. parameters:
  52. - in: path
  53. name: name
  54. schema:
  55. type: string
  56. required: true
  57. description: Plugin name
  58. responses:
  59. 200:
  60. description: Plugin deleted
  61. """
  62. if os.path.isdir("./plugins/"+name) is False:
  63. return ('Dir not found', 500)
  64. shutil.rmtree("./plugins/"+name)
  65. cbpi.notify("Plugin deleted", "Plugin %s deleted successfully" % name)
  66. return ('', 204)
  67. @route('/<name>/download', methods=['POST'])
  68. def download(self, name):
  69. """
  70. Download Plugin
  71. ---
  72. tags:
  73. - plugin
  74. parameters:
  75. - in: path
  76. name: name
  77. schema:
  78. type: string
  79. required: true
  80. description: Plugin name
  81. responses:
  82. 200:
  83. description: Plugin downloaded
  84. """
  85. plugin = self.api.cache["plugins"].get(name)
  86. plugin["loading"] = True
  87. if plugin is None:
  88. return ('', 404)
  89. try:
  90. Repo.clone_from(plugin.get("repo_url"), "./modules/plugins/%s/" % (name))
  91. self.api.notify("Download successful", "Plugin %s downloaded successfully" % name)
  92. finally:
  93. plugin["loading"] = False
  94. return ('', 204)
  95. @route('/<name>/update', methods=['POST'])
  96. def update(self, name):
  97. """
  98. Pull Plugin Update
  99. ---
  100. tags:
  101. - plugin
  102. parameters:
  103. - in: path
  104. name: name
  105. schema:
  106. type: string
  107. required: true
  108. description: Plugin name
  109. responses:
  110. 200:
  111. description: Plugin updated
  112. """
  113. repo = Repo("./modules/plugins/%s/" % (name))
  114. o = repo.remotes.origin
  115. info = o.pull()
  116. self.api.notify("Plugin Updated", "Plugin %s updated successfully. Please restart the system" % name)
  117. return ('', 204)
  118. @cbpi.addon.core.initializer()
  119. def init(cbpi):
  120. cbpi.cache["plugins"] = {}
  121. PluginView.api = cbpi
  122. PluginView.register(cbpi._app, route_base='/api/plugin')