04-fix-relative-paths-web-backend.patch 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. --- contrib/python/matplotlib/py3/matplotlib/backends/backend_webagg.py (f7af9e31ba1993f2d29753345505773d8e71ca62)
  2. +++ contrib/python/matplotlib/py3/matplotlib/backends/backend_webagg.py (working tree)
  3. @@ -19,6 +19,10 @@ import random
  4. import sys
  5. import signal
  6. import threading
  7. +import tempfile
  8. +import os
  9. +
  10. +from library.python.resource import iteritems
  11. try:
  12. import tornado
  13. @@ -177,12 +181,14 @@ class WebAggApplication(tornado.web.Application):
  14. assert url_prefix[0] == '/' and url_prefix[-1] != '/', \
  15. 'url_prefix must start with a "/" and not end with one.'
  16. + self._store_resources()
  17. + package_resources_abspath = os.path.join(self._stored_package_path, core.FigureManagerWebAgg.get_static_file_path())
  18. super().__init__(
  19. [
  20. # Static files for the CSS and JS
  21. (url_prefix + r'/_static/(.*)',
  22. tornado.web.StaticFileHandler,
  23. - {'path': core.FigureManagerWebAgg.get_static_file_path()}),
  24. + {'path': package_resources_abspath}),
  25. # Static images for the toolbar
  26. (url_prefix + r'/_images/(.*)',
  27. @@ -210,7 +216,19 @@ class WebAggApplication(tornado.web.Application):
  28. (url_prefix + r'/([0-9]+)/download.([a-z0-9.]+)',
  29. self.Download),
  30. ],
  31. - template_path=core.FigureManagerWebAgg.get_static_file_path())
  32. + template_path=package_resources_abspath)
  33. +
  34. + def _store_resources(self):
  35. + self._stored_package_dir = tempfile.TemporaryDirectory()
  36. + self._stored_package_path = self._stored_package_dir.name
  37. + package_path = os.path.join(*"contrib/python/matplotlib/py3/".split("/"))
  38. + for key, data in iteritems(prefix="resfs/file/" + package_path, strip_prefix=True):
  39. + path = os.path.join(self._stored_package_path, *os.path.split(package_path), *os.path.split(key))
  40. + dir = os.path.dirname(path)
  41. + if not os.path.exists(dir):
  42. + os.makedirs(dir)
  43. + with open(path, "wb") as file:
  44. + file.write(data)
  45. @classmethod
  46. def initialize(cls, url_prefix='', port=None, address=None):