Repository.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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_root_path = root_path
  37. self.m_resources = []
  38. # Returns the root path
  39. def root_path(self):
  40. return self.m_root_path
  41. # Returns a list of all the resources found
  42. def all_resources(self):
  43. return self.m_resources
  44. # Returns a list of all the texture resources found
  45. def texture_resources(self):
  46. textures = []
  47. for res in self.m_resources:
  48. if (res.endswith(TEXTURE_EXTENSION)):
  49. textures.append(res)
  50. return textures
  51. # Returns a list of all the text resources found
  52. def text_resources(self):
  53. texts = []
  54. for res in self.m_resources:
  55. if (res.endswith(TEXT_EXTENSION)):
  56. texts.append(res)
  57. return texts
  58. # Returns a list of all the mesh resources found
  59. def mesh_resources(self):
  60. meshes = []
  61. for res in self.m_resources:
  62. if (res.endswith(MESH_EXTENSION)):
  63. meshes.append(res)
  64. return meshes
  65. # Returns a list of all the lua resources found
  66. def script_resources(self):
  67. scripts = []
  68. for res in self.m_resources:
  69. if (res.endswith(LUA_EXTENSION)):
  70. scripts.append(res)
  71. return scripts
  72. # Returns a list of all the vertex shader resources found
  73. def vertex_shader_resources(self):
  74. vss = []
  75. for res in self.m_resources:
  76. if (res.endswith(VERTEX_SHADER_EXTENSION)):
  77. vss.append(res)
  78. return vss
  79. # Returns a list of all the pixel shader resources found
  80. def pixel_shader_resources(self):
  81. pss = []
  82. for res in self.m_resources:
  83. if (res.endswith(PIXEL_SHADER_EXTENSION)):
  84. pss.append(res)
  85. return pss
  86. # Scans the root path to find resources
  87. def scan(self):
  88. # Clear the resources
  89. self.m_resources = []
  90. for dirname, dirnames, filenames in os.walk(self.m_root_path):
  91. for filename in filenames:
  92. # Get the resource name
  93. abs_path = os.path.join(dirname, filename)
  94. resource_name = os.path.relpath(abs_path, self.m_root_path)
  95. # Filter resource names by type
  96. if resource_name.endswith(resource_extensions):
  97. self.m_resources.append(resource_name)