1 """ Utilities for loading resource files. """
10 from .constants import DEBUG
14 """ Load data files from beneath a prefix. """
16 def __init__(self, prefix):
20 def full_path(self, *parts):
21 path = "/".join(parts)
22 rel_path = os.path.join(*path.split("/"))
23 abs_path = os.path.join(self._prefix, rel_path)
28 def open_file(self, *parts):
29 return file(self.full_path(*parts), "rb")
31 def load_station(self, *parts):
32 with self.open_file("stations", *parts) as f:
35 def load_image(self, *parts):
36 """Return a pygame surface of the requested image."""
37 fn = self.full_path("images", *parts)
38 img = self._cache.get(fn, None)
40 img = pygame.image.load(fn)
41 # We assume pygame.display has been initialised
42 # Fix this if that changes
43 img.convert_alpha(pygame.display.get_surface())
47 def load_font(self, *parts, **kwargs):
48 """Return a pygame font of the given size"""
49 size = kwargs.get('size', 12)
50 fn = self.full_path("fonts", *parts)
51 font = pygame.font.Font(fn, size)
52 # Do we need to cache this?
56 _DATA_PREFIX = os.path.abspath(
57 os.path.join(os.path.dirname(__file__), "..", "data"))
59 loader = Loader(_DATA_PREFIX)