6 from pkg_resources import resource_filename
8 # OK, we're most likely running under py2exe, so this is safe
9 # (for values of safe that are unconcerned about py2exe being involved)
10 def resource_filename(mod, path):
11 # There are better ways of doing this, but I've spent too much
12 # time going down this rabbithole already
13 return os.path.join(os.path.dirname(__file__), '..', '..', 'data',
24 class ResourceNotFound(Exception):
31 def __init__(self, resource_module, language=None):
32 self.resource_module = resource_module
33 self.lang_dialect = language
34 self.language = language.split('_', 1)[0] if language else None
37 def create_resource_path(self, *path_fragments):
38 return resource_filename(self.resource_module,
39 os.path.join(*path_fragments))
41 def get_resource_path(self, *path_fragments):
42 for mod, full_path_fragments in self.lang_locations(path_fragments):
43 path = resource_filename(mod, os.path.join(*full_path_fragments))
44 if os.path.exists(path):
46 raise ResourceNotFound(os.path.join(*path_fragments))
48 def lang_locations(self, path_fragments):
50 For each resource module, yield:
51 * (<module>, (<lang>_<dialect>, <path>))
52 * (<module>, (<lang>, <path>))
53 * (<module>, (<path>, ))
55 for module in (self.resource_module,):
57 yield (module, (self.lang_dialect,) + path_fragments)
58 if self.language != self.lang_dialect:
59 yield (module, (self.language,) + path_fragments)
60 yield (module, path_fragments)
62 def get_file(self, *path_fragments, **kw):
63 mode = kw.get('mode', "rU")
65 path = self.get_resource_path(*path_fragments)
66 except ResourceNotFound:
68 path = self.create_resource_path(*path_fragments)
71 return file(path, mode)
73 def get_image(self, *name_fragments, **kw):
74 transforms = kw.get('transforms', ())
75 basedir = kw.get('basedir', 'images')
77 path = (basedir,) + name_fragments
79 if path not in self._cache:
80 fn = self.get_resource_path(*path)
81 image = pygame.image.load(fn)
82 if self.CONVERT_ALPHA:
83 if not pygame.display.get_init():
84 print >> sys.stderr, ("Display not initialized, "
85 "image '%s' not loaded."
86 % os.path.join(*path))
88 image = image.convert_alpha(pygame.display.get_surface())
89 self._cache[path] = image
91 key = (path, transforms)
92 if key not in self._cache:
93 image = self._cache[path]
94 for mutator in transforms:
95 image = mutator(image)
96 self._cache[key] = image
98 return self._cache[key]
100 def get_font(self, file_name, font_size):
102 key = (basedir, file_name, font_size)
104 if key not in self._cache:
105 fn = self.get_resource_path(basedir, file_name)
106 self._cache[key] = pygame.font.Font(fn, font_size)
108 return self._cache[key]
110 def get_yaml(self, *path_fragments):
112 raise ResourceNotFound("%s - %s" % (
113 os.path.join(*path_fragments),
114 "YAML module not available"))
115 with self.get_file(*path_fragments) as yaml_file:
116 return yaml.safe_load(yaml_file)
118 def get_json(self, *path_fragments):
119 with self.get_file(*path_fragments) as json_file:
120 return json.load(json_file)