2
0

Repository.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. VERTEX_SHADER_EXTENSION = ('.vs')
  30. PIXEL_SHADER_EXTENSION = ('.ps')
  31. SOUND_EXTENSION = ('.wav')
  32. resource_extensions = ('.txt', '.tga', '.dae', '.lua', '.vs', '.ps', '.wav')
  33. # Represents the folder containing the resources
  34. # Can filter resources by type and other useful stuff
  35. class Repository:
  36. def __init__(self, root_path):
  37. self.m_resources = []
  38. self.set_root_path(root_path)
  39. # Sets the root path triggering a complete project scan()
  40. def set_root_path(self, root_path):
  41. self.m_root_path = root_path
  42. self.scan()
  43. # Returns the root path
  44. def root_path(self):
  45. return self.m_root_path
  46. # Returns a list of all the resources found
  47. def all_resources(self):
  48. return self.m_resources
  49. # Returns a list of all the texture resources found
  50. def texture_resources(self):
  51. textures = []
  52. for res in self.m_resources:
  53. if (res.endswith(TEXTURE_EXTENSION)):
  54. textures.append(res)
  55. return textures
  56. # Returns a list of all the text resources found
  57. def text_resources(self):
  58. texts = []
  59. for res in self.m_resources:
  60. if (res.endswith(TEXT_EXTENSION)):
  61. texts.append(res)
  62. return texts
  63. # Returns a list of all the mesh resources found
  64. def mesh_resources(self):
  65. meshes = []
  66. for res in self.m_resources:
  67. if (res.endswith(MESH_EXTENSION)):
  68. meshes.append(res)
  69. return meshes
  70. # Returns a list of all the lua resources found
  71. def script_resources(self):
  72. scripts = []
  73. for res in self.m_resources:
  74. if (res.endswith(LUA_EXTENSION)):
  75. scripts.append(res)
  76. return scripts
  77. # Returns a list of all the vertex shader resources found
  78. def vertex_shader_resources(self):
  79. vss = []
  80. for res in self.m_resources:
  81. if (res.endswith(VERTEX_SHADER_EXTENSION)):
  82. vss.append(res)
  83. return vss
  84. # Returns a list of all the pixel shader resources found
  85. def pixel_shader_resources(self):
  86. pss = []
  87. for res in self.m_resources:
  88. if (res.endswith(PIXEL_SHADER_EXTENSION)):
  89. pss.append(res)
  90. return pss
  91. # Returns a list of all the sound resources found
  92. def sound_resources(self):
  93. sounds = []
  94. for res in self.m_resources:
  95. if (res.endswith(SOUND_EXTENSION)):
  96. sounds.append(res)
  97. return sounds
  98. # Scans the root path to find resources
  99. def scan(self):
  100. # Clear the resources
  101. self.m_resources = []
  102. for dirname, dirnames, filenames in os.walk(self.m_root_path):
  103. for filename in filenames:
  104. # Get the resource name
  105. abs_path = os.path.join(dirname, filename)
  106. resource_name = os.path.relpath(abs_path, self.m_root_path)
  107. # Normalize resource name, OSs != Windows/Linux may need
  108. # additional processing
  109. resource_name = resource_name.replace("\\", "/")
  110. # Filter resource names by type
  111. if resource_name.endswith(resource_extensions):
  112. self.m_resources.append(resource_name)