1 """ Utilities for loading resource files. """
11 from .constants import DEBUG
15 """ Load data files from beneath a prefix. """
17 def __init__(self, prefix):
21 def full_path(self, *parts):
22 path = "/".join(parts)
23 rel_path = os.path.join(*path.split("/"))
24 abs_path = os.path.join(self._prefix, rel_path)
29 def open_file(self, *parts):
30 return file(self.full_path(*parts), "rb")
32 def load_station(self, *parts):
33 with self.open_file("stations", *parts) as f:
36 def load_image(self, *parts):
37 """Return a pygame surface of the requested image."""
38 fn = self.full_path("images", *parts)
39 img = self._cache.get(fn, None)
41 img = pygame.image.load(fn)
42 # We assume pygame.display has been initialised
43 # Fix this if that changes
44 img.convert_alpha(pygame.display.get_surface())
48 def load_font(self, *parts, **kwargs):
49 """Return a pygame font of the given size"""
50 size = kwargs.get('size', 12)
51 fn = self.full_path("fonts", *parts)
52 font = pygame.font.Font(fn, size)
53 # Do we need to cache this?
56 def load_sound(self, *parts):
57 """Return a pygame sound"""
58 fn = self.full_path("sounds", *parts)
59 sound = self._cache.get(fn, None)
61 sound = pygame.mixer.Sound(fn)
62 self._cache[fn] = sound
66 _DATA_PREFIX = os.path.abspath(
67 os.path.join(os.path.dirname(__file__), "..", "data"))
69 loader = Loader(_DATA_PREFIX)