1 | import os
|
---|
2 | import sys
|
---|
3 |
|
---|
4 | from pkg_resources import resource_filename
|
---|
5 | import pygame
|
---|
6 |
|
---|
7 |
|
---|
8 | class ResourceNotFound(Exception):
|
---|
9 | pass
|
---|
10 |
|
---|
11 |
|
---|
12 | class Resources(object):
|
---|
13 | CONVERT_ALPHA = True
|
---|
14 |
|
---|
15 | def __init__(self, resource_module, language=None):
|
---|
16 | self.resource_module = resource_module
|
---|
17 | self.lang_dialect = language
|
---|
18 | self.language = language.split('_', 1)[0] if language else None
|
---|
19 | self._cache = {}
|
---|
20 |
|
---|
21 | def get_resource_path(self, *path_fragments):
|
---|
22 | for mod, full_path_fragments in self.lang_locations(path_fragments):
|
---|
23 | path = resource_filename(mod, os.path.join(*full_path_fragments))
|
---|
24 | if os.path.exists(path):
|
---|
25 | return path
|
---|
26 | raise ResourceNotFound(os.path.join(*path_fragments))
|
---|
27 |
|
---|
28 | def lang_locations(self, path_fragments):
|
---|
29 | '''
|
---|
30 | For each resource module, yield:
|
---|
31 | * (<module>, (<lang>_<dialect>, <path>))
|
---|
32 | * (<module>, (<lang>, <path>))
|
---|
33 | * (<module>, (<path>, ))
|
---|
34 | '''
|
---|
35 | for module in (self.resource_module,):
|
---|
36 | if self.lang_dialect:
|
---|
37 | yield (module, (self.lang_dialect,) + path_fragments)
|
---|
38 | if self.language != self.lang_dialect:
|
---|
39 | yield (module, (self.language,) + path_fragments)
|
---|
40 | yield (module, path_fragments)
|
---|
41 |
|
---|
42 | def get_file(self, *path_fragments, **kw):
|
---|
43 | mode = kw.get('mode', "rU")
|
---|
44 | path = self.get_resource_path(*path_fragments)
|
---|
45 | return file(path, mode)
|
---|
46 |
|
---|
47 | def get_image(self, *name_fragments, **kw):
|
---|
48 | transforms = kw.get('transforms', ())
|
---|
49 | basedir = kw.get('basedir', 'images')
|
---|
50 |
|
---|
51 | path = (basedir,) + name_fragments
|
---|
52 |
|
---|
53 | if path not in self._cache:
|
---|
54 | fn = self.get_resource_path(*path)
|
---|
55 | image = pygame.image.load(fn)
|
---|
56 | if self.CONVERT_ALPHA:
|
---|
57 | if not pygame.display.get_init():
|
---|
58 | print >> sys.stderr, ("Display not initialized, "
|
---|
59 | "image '%s' not loaded."
|
---|
60 | % os.path.join(*path))
|
---|
61 | return
|
---|
62 | image = image.convert_alpha(pygame.display.get_surface())
|
---|
63 | self._cache[path] = image
|
---|
64 |
|
---|
65 | key = (path, transforms)
|
---|
66 | if key not in self._cache:
|
---|
67 | image = self._cache[path]
|
---|
68 | for mutator in transforms:
|
---|
69 | image = mutator(image)
|
---|
70 | self._cache[key] = image
|
---|
71 |
|
---|
72 | return self._cache[key]
|
---|
73 |
|
---|
74 | def get_font(self, file_name, font_size):
|
---|
75 | basedir = 'fonts'
|
---|
76 | key = (basedir, file_name, font_size)
|
---|
77 |
|
---|
78 | if key not in self._cache:
|
---|
79 | fn = self.get_resource_path(basedir, file_name)
|
---|
80 | self._cache[key] = pygame.font.Font(fn, font_size)
|
---|
81 |
|
---|
82 | return self._cache[key]
|
---|
83 |
|
---|
84 |
|
---|
85 | resources = Resources('data')
|
---|