Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import os
  2. class BaseCache(object):
  3. def __init__(self, path, config):
  4. self._path = path
  5. self._config = config
  6. def check(self, path):
  7. return False
  8. def write(self, data):
  9. pass
  10. def read(self):
  11. pass
  12. class NullCache(BaseCache):
  13. """
  14. A dummy cache that doesn't do anything, handy for developing new Mods
  15. """
  16. def check(self, path):
  17. return False
  18. def write(self, data):
  19. self.data = data
  20. def read(self):
  21. return self.data
  22. class DiskCache(BaseCache):
  23. """
  24. A simple disk-based cache.
  25. """
  26. def _create_folders(self):
  27. """
  28. Create the disk cache path so that the cached image can be stored in
  29. the same hierarchy as the original images.
  30. """
  31. paths = self._path.split(os.path.sep)
  32. paths.pop() # Remove file from path
  33. path = os.path.join(self._config['cache_root'], *paths)
  34. if not os.path.isdir(path):
  35. os.makedirs(path)
  36. def _cache_path(self):
  37. return os.path.join(self._config['cache_root'], self._path)
  38. def check(self, path):
  39. """
  40. Checks the disk cache for an already processed image. If it exists then
  41. we'll check it's timestamp against the original image to make sure it's
  42. newer (and therefore valid). Also creates the folder hierarchy in the
  43. cache for the cached image if it doesn't find it there itself.
  44. """
  45. self._create_folders()
  46. cache = self._cache_path()
  47. original_time = os.path.getmtime(path)
  48. if os.path.exists(cache):
  49. cache_time = os.path.getmtime(cache)
  50. else:
  51. # Cached image does not exist
  52. return False
  53. if original_time > cache_time:
  54. # Cached image is out of date
  55. return False
  56. else:
  57. return True
  58. def write(self, data):
  59. path = self._cache_path()
  60. f = open(path, 'w')
  61. f.write(data)
  62. f.close()
  63. def file_info(self):
  64. """
  65. If we're using X-Sendfile or X-Accel-Redirect we want to return info
  66. about the file, rather than the actual file content
  67. """
  68. size = os.path.getsize(self._cache_path())
  69. return (self._path, size)
  70. def read(self):
  71. f = open(self._cache_path(), "r")
  72. data = f.read()
  73. f.close()
  74. return data