Repository.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. # Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  2. # Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  3. #
  4. # Permission is hereby granted, free of charge, to any person
  5. # obtaining a copy of this software and associated documentation
  6. # files (the "Software"), to deal in the Software without
  7. # restriction, including without limitation the rights to use,
  8. # copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. # copies of the Software, and to permit persons to whom the
  10. # Software is furnished to do so, subject to the following
  11. # conditions:
  12. #
  13. # The above copyright notice and this permission notice shall be
  14. # included in all copies or substantial portions of the Software.
  15. #
  16. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17. # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  18. # OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  19. # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  20. # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  21. # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  22. # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  23. # OTHER DEALINGS IN THE SOFTWARE.
  24. import os
  25. TEXTURE_EXTENSION = ('.tga')
  26. TEXT_EXTENSION = ('.txt')
  27. MESH_EXTENSION = ('.dae')
  28. LUA_EXTENSION = ('.lua')
  29. resource_extensions = ('.txt', '.tga', '.dae', '.lua')
  30. # Represents the folder containing the resources
  31. # Can filter resources by type and other useful stuff
  32. class Repository:
  33. def __init__(self, root_path):
  34. self.m_root_path = root_path
  35. self.m_resources = []
  36. # Returns the root path
  37. def root_path(self):
  38. return self.m_root_path
  39. # Returns a list of all the resources found
  40. def all_resources(self):
  41. return self.m_resources
  42. # Returns a list of all the texture resources found
  43. def texture_resources(self):
  44. textures = []
  45. for res in self.m_resources:
  46. if (res.endswith(TEXTURE_EXTENSION)):
  47. textures.append(res)
  48. return textures
  49. # Returns a list of all the text resources found
  50. def text_resources(self):
  51. texts = []
  52. for res in self.m_resources:
  53. if (res.endswith(TEXT_EXTENSION)):
  54. texts.append(res)
  55. return texts
  56. # Returns a list of all the mesh resources found
  57. def mesh_resources(self):
  58. meshes = []
  59. for res in self.m_resources:
  60. if (res.endswith(MESH_EXTENSION)):
  61. meshes.append(res)
  62. return meshes
  63. # Returns a list of all the lua resources found
  64. def script_resources(self):
  65. scripts = []
  66. for res in self.m_resources:
  67. if (res.endswith(LUA_EXTENSION)):
  68. scripts.append(res)
  69. return scripts
  70. # Scans the root path to find resources
  71. def scan(self):
  72. # Clear the resources
  73. self.m_resources = []
  74. for dirname, dirnames, filenames in os.walk(self.m_root_path):
  75. for filename in filenames:
  76. # Get the resource name
  77. abs_path = os.path.join(dirname, filename)
  78. resource_name = os.path.relpath(abs_path, self.m_root_path)
  79. # Filter resource names by type
  80. if resource_name.endswith(resource_extensions):
  81. self.m_resources.append(resource_name)