cache.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. ########################################################################
  2. # Wiizard - A Wii games manager
  3. # Copyright (C) 2023 CYBERDEViL
  4. #
  5. # This file is part of Wiizard.
  6. #
  7. # Wiizard is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # Wiizard is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  19. #
  20. ########################################################################
  21. import os
  22. import sys
  23. from wiizard.const import (
  24. IMG_FRONT_COVER,
  25. IMG_FRONT_3D_COVER,
  26. IMG_DISC,
  27. IMG_FULL
  28. )
  29. class ImageCacheStructure:
  30. Front2d = "covers/2D/{id6}.png"
  31. Front3d = "covers/3D/{id6}.png"
  32. Full = "covers/Full/{id6}.png"
  33. Disc = "covers/Disc/{id6}.png"
  34. @staticmethod
  35. def subdirs():
  36. return (
  37. os.path.dirname(__class__.Front2d),
  38. os.path.dirname(__class__.Front3d),
  39. os.path.dirname(__class__.Full),
  40. os.path.dirname(__class__.Disc)
  41. )
  42. @staticmethod
  43. def getType(imgType):
  44. if (imgType & IMG_FRONT_COVER):
  45. return __class__.Front2d
  46. if (imgType & IMG_FRONT_3D_COVER):
  47. return __class__.Front3d
  48. if (imgType & IMG_FULL):
  49. return __class__.Full
  50. if (imgType & IMG_DISC):
  51. return __class__.Disc
  52. @staticmethod
  53. def getPath(imgType):
  54. return os.path.dirname(__class__.getType(imgType))
  55. @staticmethod
  56. def getFullPath(imgType, id6Str, language):
  57. return __class__.getType(imgType).format_map({'id6':id6Str, 'lang':language})
  58. @staticmethod
  59. def getFilename(imgType, id6Str, language):
  60. return os.path.basename(__class__.getType(imgType).format(id6=id6Str, lang=language))
  61. class ImageCache:
  62. def __init__(self, path="./cache", structure=ImageCacheStructure):
  63. self.__path = path
  64. self.__struct = structure
  65. """ Main image cache dir """
  66. if os.path.exists(path):
  67. if not os.path.isdir(path):
  68. print("ERROR ImageCache path exists but is not a dir:", path)
  69. sys.exit(1) # TODO do not exit
  70. # Create image cache path
  71. else:
  72. os.makedirs(path)
  73. """ structure subs dirs """
  74. for subdir in structure.subdirs():
  75. realpath = os.path.join(path, subdir)
  76. if os.path.exists(realpath):
  77. if not os.path.isdir(realpath):
  78. print("ERROR ImageCache path exists but is not a dir:",realpath)
  79. sys.exit(1) # TODO do not exit
  80. else:
  81. os.makedirs(realpath)
  82. @property
  83. def path(self):
  84. return self.__path
  85. def setPath(self, path):
  86. self.__path = path
  87. def getImagePath(self, id6Str, imgType, lang=""):
  88. return os.path.join(self.__path, self.__struct.getFullPath(imgType, id6Str, lang))
  89. def getImage(self, id6Str, imgType, lang=""):
  90. path = self.getImagePath(id6Str, imgType, lang)
  91. if os.path.exists(path):
  92. return path
  93. return None