Mercurial > boomslang
annotate pyntnclick/resources.py @ 591:4117d7b201a4 pyntnclick
mutators -> transforms.
author | Jeremy Thurgood <firxen@gmail.com> |
---|---|
date | Sat, 11 Feb 2012 17:34:23 +0200 |
parents | f20d211d2c91 |
children | 1eb1537173ef |
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 = {} |
591 | 28 self._transformed_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): | |
587
f20d211d2c91
load_image() -> get_image() and more docs.
Jeremy Thurgood <firxen@gmail.com>
parents:
584
diff
changeset
|
51 """Get list of resource paths to search. |
f20d211d2c91
load_image() -> get_image() and more docs.
Jeremy Thurgood <firxen@gmail.com>
parents:
584
diff
changeset
|
52 """ |
561 | 53 paths = [] |
54 for module in [self.resource_module, self.DEFAULT_RESOURCE_MODULE]: | |
55 if self.language: | |
56 fn = os.path.join(self.language, resource_path) | |
57 paths.append(resource_filename(module, fn)) | |
58 paths.append(resource_filename(module, resource_path)) | |
59 return paths | |
571
20e296d4a3a5
Add load_image to Resources.
Jeremy Thurgood <firxen@gmail.com>
parents:
561
diff
changeset
|
60 |
591 | 61 def get_image(self, image_name_fragments, transforms=(), basedir='images'): |
587
f20d211d2c91
load_image() -> get_image() and more docs.
Jeremy Thurgood <firxen@gmail.com>
parents:
584
diff
changeset
|
62 """Load an image and optionally apply mutators. |
f20d211d2c91
load_image() -> get_image() and more docs.
Jeremy Thurgood <firxen@gmail.com>
parents:
584
diff
changeset
|
63 |
f20d211d2c91
load_image() -> get_image() and more docs.
Jeremy Thurgood <firxen@gmail.com>
parents:
584
diff
changeset
|
64 The `image_name_fragments` parameter may be either a string or a list |
f20d211d2c91
load_image() -> get_image() and more docs.
Jeremy Thurgood <firxen@gmail.com>
parents:
584
diff
changeset
|
65 of strings. The latter is a convenience for things that compose an |
f20d211d2c91
load_image() -> get_image() and more docs.
Jeremy Thurgood <firxen@gmail.com>
parents:
584
diff
changeset
|
66 image name out of several fragments. |
f20d211d2c91
load_image() -> get_image() and more docs.
Jeremy Thurgood <firxen@gmail.com>
parents:
584
diff
changeset
|
67 |
591 | 68 The `transforms` param may contain transforms, which modify an image |
69 in-place to apply various effects. TODO: Implement transforms | |
70 somewhere, so this becomes useful. | |
587
f20d211d2c91
load_image() -> get_image() and more docs.
Jeremy Thurgood <firxen@gmail.com>
parents:
584
diff
changeset
|
71 |
f20d211d2c91
load_image() -> get_image() and more docs.
Jeremy Thurgood <firxen@gmail.com>
parents:
584
diff
changeset
|
72 The `basedir` param defaults to 'images', but may be overriden to load |
f20d211d2c91
load_image() -> get_image() and more docs.
Jeremy Thurgood <firxen@gmail.com>
parents:
584
diff
changeset
|
73 images from other places. ('icons', for example.) |
f20d211d2c91
load_image() -> get_image() and more docs.
Jeremy Thurgood <firxen@gmail.com>
parents:
584
diff
changeset
|
74 """ |
f20d211d2c91
load_image() -> get_image() and more docs.
Jeremy Thurgood <firxen@gmail.com>
parents:
584
diff
changeset
|
75 |
f20d211d2c91
load_image() -> get_image() and more docs.
Jeremy Thurgood <firxen@gmail.com>
parents:
584
diff
changeset
|
76 # Juggle our various params and find an appropriate image path. |
584
96ff2d8a8a9a
Allow list of image name fragments in load_image. Also, some docs.
Jeremy Thurgood <firxen@gmail.com>
parents:
580
diff
changeset
|
77 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
|
78 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
|
79 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
|
80 |
591 | 81 key = (image_path, transforms) |
82 if key in self._transformed_image_cache: | |
587
f20d211d2c91
load_image() -> get_image() and more docs.
Jeremy Thurgood <firxen@gmail.com>
parents:
584
diff
changeset
|
83 # We already have this cached, so shortcut the whole process. |
591 | 84 return self._transformed_image_cache[key] |
587
f20d211d2c91
load_image() -> get_image() and more docs.
Jeremy Thurgood <firxen@gmail.com>
parents:
584
diff
changeset
|
85 |
571
20e296d4a3a5
Add load_image to Resources.
Jeremy Thurgood <firxen@gmail.com>
parents:
561
diff
changeset
|
86 if image_path not in self._image_cache: |
20e296d4a3a5
Add load_image to Resources.
Jeremy Thurgood <firxen@gmail.com>
parents:
561
diff
changeset
|
87 image = pygame.image.load(image_path) |
20e296d4a3a5
Add load_image to Resources.
Jeremy Thurgood <firxen@gmail.com>
parents:
561
diff
changeset
|
88 if self.CONVERT_ALPHA: |
20e296d4a3a5
Add load_image to Resources.
Jeremy Thurgood <firxen@gmail.com>
parents:
561
diff
changeset
|
89 image = image.convert_alpha(pygame.display.get_surface()) |
20e296d4a3a5
Add load_image to Resources.
Jeremy Thurgood <firxen@gmail.com>
parents:
561
diff
changeset
|
90 self._image_cache[image_path] = image |
575
970cdc219e15
Add image mutation to resource loader.
Jeremy Thurgood <firxen@gmail.com>
parents:
571
diff
changeset
|
91 image = self._image_cache[image_path] |
970cdc219e15
Add image mutation to resource loader.
Jeremy Thurgood <firxen@gmail.com>
parents:
571
diff
changeset
|
92 |
587
f20d211d2c91
load_image() -> get_image() and more docs.
Jeremy Thurgood <firxen@gmail.com>
parents:
584
diff
changeset
|
93 # Apply any mutators we're given. |
591 | 94 for transform in transforms: |
95 image = transform(image) | |
96 self._transformed_image_cache[key] = image | |
575
970cdc219e15
Add image mutation to resource loader.
Jeremy Thurgood <firxen@gmail.com>
parents:
571
diff
changeset
|
97 |
587
f20d211d2c91
load_image() -> get_image() and more docs.
Jeremy Thurgood <firxen@gmail.com>
parents:
584
diff
changeset
|
98 return image |