Repository.py 3.8 KB

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