util.py 1.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. # coding: utf-8
  2. import os
  3. import typing
  4. from os import path
  5. from synergine2_cocos2d.exception import FileNotFound
  6. def get_map_file_path_from_dir(map_dir_path: str) -> str:
  7. # TODO: path is temp here
  8. return '{}.tmx'.format(os.path.join(map_dir_path, os.path.basename(map_dir_path)))
  9. class PathManager(object):
  10. def __init__(
  11. self,
  12. include_paths: typing.List[str],
  13. ) -> None:
  14. self._include_paths = [] # type: typing.List[str]
  15. self.include_paths = include_paths
  16. @property
  17. def include_paths(self) -> typing.Tuple[str, ...]:
  18. return tuple(self._include_paths)
  19. @include_paths.setter
  20. def include_paths(self, value: typing.List[str]) -> None:
  21. self._include_paths = value
  22. self._include_paths.sort(reverse=True)
  23. def add_included_path(self, included_path: str) -> None:
  24. self._include_paths.append(included_path)
  25. self._include_paths.sort(reverse=True)
  26. def path(self, file_path: str) -> str:
  27. # Search in configured paths
  28. for include_path in self._include_paths:
  29. complete_file_path = path.join(include_path, file_path)
  30. if path.isfile(complete_file_path):
  31. return complete_file_path
  32. # If not in include last chance in current dir
  33. if path.isfile(file_path):
  34. return file_path
  35. raise FileNotFound('File "{}" not found in paths {}'.format(
  36. file_path,
  37. self._include_paths,
  38. ))