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 create_resource_path(self, *path_fragments):
|
---|
22 | return resource_filename(self.resource_module,
|
---|
23 | os.path.join(*path_fragments))
|
---|
24 |
|
---|
25 | def get_resource_path(self, *path_fragments):
|
---|
26 | for mod, full_path_fragments in self.lang_locations(path_fragments):
|
---|
27 | path = resource_filename(mod, os.path.join(*full_path_fragments))
|
---|
28 | if os.path.exists(path):
|
---|
29 | return path
|
---|
30 | raise ResourceNotFound(os.path.join(*path_fragments))
|
---|
31 |
|
---|
32 | def lang_locations(self, path_fragments):
|
---|
33 | '''
|
---|
34 | For each resource module, yield:
|
---|
35 | * (<module>, (<lang>_<dialect>, <path>))
|
---|
36 | * (<module>, (<lang>, <path>))
|
---|
37 | * (<module>, (<path>, ))
|
---|
38 | '''
|
---|
39 | for module in (self.resource_module,):
|
---|
40 | if self.lang_dialect:
|
---|
41 | yield (module, (self.lang_dialect,) + path_fragments)
|
---|
42 | if self.language != self.lang_dialect:
|
---|
43 | yield (module, (self.language,) + path_fragments)
|
---|
44 | yield (module, path_fragments)
|
---|
45 |
|
---|
46 | def get_file(self, *path_fragments, **kw):
|
---|
47 | mode = kw.get('mode', "rU")
|
---|
48 | try:
|
---|
49 | path = self.get_resource_path(*path_fragments)
|
---|
50 | except ResourceNotFound:
|
---|
51 | if 'w' in mode:
|
---|
52 | path = self.create_resource_path(*path_fragments)
|
---|
53 | else:
|
---|
54 | raise
|
---|
55 | return file(path, mode)
|
---|
56 |
|
---|
57 | def get_image(self, *name_fragments, **kw):
|
---|
58 | transforms = kw.get('transforms', ())
|
---|
59 | basedir = kw.get('basedir', 'images')
|
---|
60 |
|
---|
61 | path = (basedir,) + name_fragments
|
---|
62 |
|
---|
63 | if path not in self._cache:
|
---|
64 | fn = self.get_resource_path(*path)
|
---|
65 | image = pygame.image.load(fn)
|
---|
66 | if self.CONVERT_ALPHA:
|
---|
67 | if not pygame.display.get_init():
|
---|
68 | print >> sys.stderr, ("Display not initialized, "
|
---|
69 | "image '%s' not loaded."
|
---|
70 | % os.path.join(*path))
|
---|
71 | return
|
---|
72 | image = image.convert_alpha(pygame.display.get_surface())
|
---|
73 | self._cache[path] = image
|
---|
74 |
|
---|
75 | key = (path, transforms)
|
---|
76 | if key not in self._cache:
|
---|
77 | image = self._cache[path]
|
---|
78 | for mutator in transforms:
|
---|
79 | image = mutator(image)
|
---|
80 | self._cache[key] = image
|
---|
81 |
|
---|
82 | return self._cache[key]
|
---|
83 |
|
---|
84 | def get_font(self, file_name, font_size):
|
---|
85 | basedir = 'fonts'
|
---|
86 | key = (basedir, file_name, font_size)
|
---|
87 |
|
---|
88 | if key not in self._cache:
|
---|
89 | fn = self.get_resource_path(basedir, file_name)
|
---|
90 | self._cache[key] = pygame.font.Font(fn, font_size)
|
---|
91 |
|
---|
92 | return self._cache[key]
|
---|
93 |
|
---|
94 |
|
---|
95 | resources = Resources('data')
|
---|