1 """ Utilities for loading resource files. """
11 """ Load data files from beneath a prefix. """
13 def __init__(self, prefix):
17 def full_path(self, *parts):
18 path = "/".join(parts)
19 rel_path = os.path.join(*path.split("/"))
20 abs_path = os.path.join(self._prefix, rel_path)
24 def open_file(self, *parts):
25 return file(self.full_path(*parts), "rb")
27 def load_station(self, *parts):
28 with self.open_file("stations", *parts) as f:
31 def load_image(self, *parts):
32 """Return a pygame surface of the requested image."""
33 fn = self.full_path("images", *parts)
34 img = self._cache.get(fn, None)
36 img = pygame.image.load(fn)
37 # We assume pygame.display has been initialised
38 # Fix this if that changes
39 img.convert_alpha(pygame.display.get_surface())
44 _DATA_PREFIX = os.path.abspath(
45 os.path.join(os.path.dirname(__file__), "..", "data"))
47 loader = Loader(_DATA_PREFIX)