Mercurial > boomslang
annotate pyntnclick/resources.py @ 584:96ff2d8a8a9a pyntnclick
Allow list of image name fragments in load_image. Also, some docs.
author | Jeremy Thurgood <firxen@gmail.com> |
---|---|
date | Sat, 11 Feb 2012 16:45:07 +0200 |
parents | 27809609eeca |
children | f20d211d2c91 |
rev | line source |
---|---|
561 | 1 # -*- test-case-name: pyntnclick.tests.test_resources -*- |
2 | |
3 import os | |
4 from pkg_resources import resource_filename | |
5 | |
571
20e296d4a3a5
Add load_image to Resources.
Jeremy Thurgood <firxen@gmail.com>
parents:
561
diff
changeset
|
6 import pygame |
20e296d4a3a5
Add load_image to Resources.
Jeremy Thurgood <firxen@gmail.com>
parents:
561
diff
changeset
|
7 |
561 | 8 |
9 class ResourceNotFound(Exception): | |
10 pass | |
11 | |
12 | |
13 class Resources(object): | |
584
96ff2d8a8a9a
Allow list of image name fragments in load_image. Also, some docs.
Jeremy Thurgood <firxen@gmail.com>
parents:
580
diff
changeset
|
14 """Resource loader and manager. |
96ff2d8a8a9a
Allow list of image name fragments in load_image. Also, some docs.
Jeremy Thurgood <firxen@gmail.com>
parents:
580
diff
changeset
|
15 |
96ff2d8a8a9a
Allow list of image name fragments in load_image. Also, some docs.
Jeremy Thurgood <firxen@gmail.com>
parents:
580
diff
changeset
|
16 The `CONVERT_ALPHA` flag allows alpha conversions to be disabled so that |
96ff2d8a8a9a
Allow list of image name fragments in load_image. Also, some docs.
Jeremy Thurgood <firxen@gmail.com>
parents:
580
diff
changeset
|
17 images may be loaded without having a display initialised. This is useful |
96ff2d8a8a9a
Allow list of image name fragments in load_image. Also, some docs.
Jeremy Thurgood <firxen@gmail.com>
parents:
580
diff
changeset
|
18 in unit tests, for example. |
96ff2d8a8a9a
Allow list of image name fragments in load_image. Also, some docs.
Jeremy Thurgood <firxen@gmail.com>
parents:
580
diff
changeset
|
19 """ |
96ff2d8a8a9a
Allow list of image name fragments in load_image. Also, some docs.
Jeremy Thurgood <firxen@gmail.com>
parents:
580
diff
changeset
|
20 |
561 | 21 DEFAULT_RESOURCE_MODULE = "pyntnclick.data" |
571
20e296d4a3a5
Add load_image to Resources.
Jeremy Thurgood <firxen@gmail.com>
parents:
561
diff
changeset
|
22 CONVERT_ALPHA = True |
561 | 23 |
24 def __init__(self, resource_module, language=None): | |
25 self.resource_module = resource_module | |
26 self.language = language | |
571
20e296d4a3a5
Add load_image to Resources.
Jeremy Thurgood <firxen@gmail.com>
parents:
561
diff
changeset
|
27 self._image_cache = {} |
575
970cdc219e15
Add image mutation to resource loader.
Jeremy Thurgood <firxen@gmail.com>
parents:
571
diff
changeset
|
28 self._mutated_image_cache = {} |
561 | 29 |
571
20e296d4a3a5
Add load_image to Resources.
Jeremy Thurgood <firxen@gmail.com>
parents:
561
diff
changeset
|
30 def get_resource_path(self, *resource_path_fragments): |
584
96ff2d8a8a9a
Allow list of image name fragments in load_image. Also, some docs.
Jeremy Thurgood <firxen@gmail.com>
parents:
580
diff
changeset
|
31 """Find the resource in one of a number of different places. |
96ff2d8a8a9a
Allow list of image name fragments in load_image. Also, some docs.
Jeremy Thurgood <firxen@gmail.com>
parents:
580
diff
changeset
|
32 |
96ff2d8a8a9a
Allow list of image name fragments in load_image. Also, some docs.
Jeremy Thurgood <firxen@gmail.com>
parents:
580
diff
changeset
|
33 The following directories are searched, in order: |
96ff2d8a8a9a
Allow list of image name fragments in load_image. Also, some docs.
Jeremy Thurgood <firxen@gmail.com>
parents:
580
diff
changeset
|
34 |
96ff2d8a8a9a
Allow list of image name fragments in load_image. Also, some docs.
Jeremy Thurgood <firxen@gmail.com>
parents:
580
diff
changeset
|
35 * <resource_module>/<lang>/ |
96ff2d8a8a9a
Allow list of image name fragments in load_image. Also, some docs.
Jeremy Thurgood <firxen@gmail.com>
parents:
580
diff
changeset
|
36 * <resource_module>/ |
96ff2d8a8a9a
Allow list of image name fragments in load_image. Also, some docs.
Jeremy Thurgood <firxen@gmail.com>
parents:
580
diff
changeset
|
37 * <default_resource_module>/<lang>/ |
96ff2d8a8a9a
Allow list of image name fragments in load_image. Also, some docs.
Jeremy Thurgood <firxen@gmail.com>
parents:
580
diff
changeset
|
38 * <default_resource_module>/ |
96ff2d8a8a9a
Allow list of image name fragments in load_image. Also, some docs.
Jeremy Thurgood <firxen@gmail.com>
parents:
580
diff
changeset
|
39 |
96ff2d8a8a9a
Allow list of image name fragments in load_image. Also, some docs.
Jeremy Thurgood <firxen@gmail.com>
parents:
580
diff
changeset
|
40 If the `language` attribute is `None`, the paths with <lang> in them |
96ff2d8a8a9a
Allow list of image name fragments in load_image. Also, some docs.
Jeremy Thurgood <firxen@gmail.com>
parents:
580
diff
changeset
|
41 are skipped. |
96ff2d8a8a9a
Allow list of image name fragments in load_image. Also, some docs.
Jeremy Thurgood <firxen@gmail.com>
parents:
580
diff
changeset
|
42 """ |
96ff2d8a8a9a
Allow list of image name fragments in load_image. Also, some docs.
Jeremy Thurgood <firxen@gmail.com>
parents:
580
diff
changeset
|
43 resource_name = '/'.join(resource_path_fragments) |
96ff2d8a8a9a
Allow list of image name fragments in load_image. Also, some docs.
Jeremy Thurgood <firxen@gmail.com>
parents:
580
diff
changeset
|
44 resource_name = os.path.join(*resource_name.split('/')) |
561 | 45 for path in self.get_paths(resource_name): |
46 if os.path.exists(path): | |
47 return path | |
48 raise ResourceNotFound(resource_name) | |
49 | |
50 def get_paths(self, resource_path): | |
51 paths = [] | |
52 for module in [self.resource_module, self.DEFAULT_RESOURCE_MODULE]: | |
53 if self.language: | |
54 fn = os.path.join(self.language, resource_path) | |
55 paths.append(resource_filename(module, fn)) | |
56 paths.append(resource_filename(module, resource_path)) | |
57 return paths | |
571
20e296d4a3a5
Add load_image to Resources.
Jeremy Thurgood <firxen@gmail.com>
parents:
561
diff
changeset
|
58 |
584
96ff2d8a8a9a
Allow list of image name fragments in load_image. Also, some docs.
Jeremy Thurgood <firxen@gmail.com>
parents:
580
diff
changeset
|
59 def load_image(self, image_name_fragments, mutators=(), basedir='images'): |
96ff2d8a8a9a
Allow list of image name fragments in load_image. Also, some docs.
Jeremy Thurgood <firxen@gmail.com>
parents:
580
diff
changeset
|
60 if isinstance(image_name_fragments, basestring): |
96ff2d8a8a9a
Allow list of image name fragments in load_image. Also, some docs.
Jeremy Thurgood <firxen@gmail.com>
parents:
580
diff
changeset
|
61 image_name_fragments = [image_name_fragments] |
96ff2d8a8a9a
Allow list of image name fragments in load_image. Also, some docs.
Jeremy Thurgood <firxen@gmail.com>
parents:
580
diff
changeset
|
62 image_path = self.get_resource_path(basedir, *image_name_fragments) |
575
970cdc219e15
Add image mutation to resource loader.
Jeremy Thurgood <firxen@gmail.com>
parents:
571
diff
changeset
|
63 |
571
20e296d4a3a5
Add load_image to Resources.
Jeremy Thurgood <firxen@gmail.com>
parents:
561
diff
changeset
|
64 if image_path not in self._image_cache: |
20e296d4a3a5
Add load_image to Resources.
Jeremy Thurgood <firxen@gmail.com>
parents:
561
diff
changeset
|
65 image = pygame.image.load(image_path) |
20e296d4a3a5
Add load_image to Resources.
Jeremy Thurgood <firxen@gmail.com>
parents:
561
diff
changeset
|
66 if self.CONVERT_ALPHA: |
20e296d4a3a5
Add load_image to Resources.
Jeremy Thurgood <firxen@gmail.com>
parents:
561
diff
changeset
|
67 image = image.convert_alpha(pygame.display.get_surface()) |
20e296d4a3a5
Add load_image to Resources.
Jeremy Thurgood <firxen@gmail.com>
parents:
561
diff
changeset
|
68 self._image_cache[image_path] = image |
575
970cdc219e15
Add image mutation to resource loader.
Jeremy Thurgood <firxen@gmail.com>
parents:
571
diff
changeset
|
69 |
970cdc219e15
Add image mutation to resource loader.
Jeremy Thurgood <firxen@gmail.com>
parents:
571
diff
changeset
|
70 image = self._image_cache[image_path] |
970cdc219e15
Add image mutation to resource loader.
Jeremy Thurgood <firxen@gmail.com>
parents:
571
diff
changeset
|
71 key = (image_path, mutators) |
970cdc219e15
Add image mutation to resource loader.
Jeremy Thurgood <firxen@gmail.com>
parents:
571
diff
changeset
|
72 |
970cdc219e15
Add image mutation to resource loader.
Jeremy Thurgood <firxen@gmail.com>
parents:
571
diff
changeset
|
73 if key not in self._mutated_image_cache: |
970cdc219e15
Add image mutation to resource loader.
Jeremy Thurgood <firxen@gmail.com>
parents:
571
diff
changeset
|
74 for mutator in mutators: |
970cdc219e15
Add image mutation to resource loader.
Jeremy Thurgood <firxen@gmail.com>
parents:
571
diff
changeset
|
75 image = mutator(image) |
970cdc219e15
Add image mutation to resource loader.
Jeremy Thurgood <firxen@gmail.com>
parents:
571
diff
changeset
|
76 self._mutated_image_cache[key] = image |
970cdc219e15
Add image mutation to resource loader.
Jeremy Thurgood <firxen@gmail.com>
parents:
571
diff
changeset
|
77 |
970cdc219e15
Add image mutation to resource loader.
Jeremy Thurgood <firxen@gmail.com>
parents:
571
diff
changeset
|
78 return self._mutated_image_cache[key] |