Cache.py 1.2 KB

123456789101112131415161718192021222324252627282930313233
  1. import hashlib
  2. import os
  3. import logging
  4. from core.FileManager import FileManager
  5. class Cache:
  6. DEFAULT_MODULE_NAME = "default"
  7. DEFAULT_CACHE_PATH = "/tmp/jarvis/tts"
  8. DEFAULT_CACHE_EXTENSION = "tts"
  9. DEFAULT_LANGUAGE = "default"
  10. DEFAULT_VOICE = "default"
  11. def __init__(self, module_name=DEFAULT_MODULE_NAME, cache_path=DEFAULT_CACHE_PATH, cache_extension=DEFAULT_CACHE_EXTENSION):
  12. self._module_name = module_name
  13. self._cache_path = cache_path
  14. self._cache_extension = cache_extension
  15. def get_audio_file_cache_path(self, words, voice=DEFAULT_VOICE, language=DEFAULT_LANGUAGE):
  16. md5 = hashlib.sha1(words).hexdigest()
  17. filename = voice + "." + md5 + "." + self._cache_extension
  18. cache_directory = os.path.join(self._cache_path, self._module_name, language)
  19. file_path = os.path.join(cache_directory, filename)
  20. FileManager.create_directory(cache_directory)
  21. logging.debug("Cache directory %s exists and File path for audio is: %s", cache_directory, file_path)
  22. return file_path
  23. @staticmethod
  24. def remove_audio_file(file_path, cache):
  25. if not cache:
  26. FileManager.remove_file(file_path)