Mercurial > boomslang
annotate pyntnclick/resources.py @ 833:ea43d3d0e8a5 pyntnclick
Remove no longer needed detail image
author | Neil Muller <neil@dip.sun.ac.za> |
---|---|
date | Tue, 29 Jan 2013 15:43:26 +0200 |
parents | 032c8c5563b8 |
children |
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 | |
785 | 26 self.lang_dialect = language |
561 | 27 self.language = language |
785 | 28 if language: |
29 self.language = language.split('_', 1)[0] | |
571
20e296d4a3a5
Add load_image to Resources.
Jeremy Thurgood <firxen@gmail.com>
parents:
561
diff
changeset
|
30 self._image_cache = {} |
638
d1ec9e739e23
Make TextWidget work (probably)
Stefano Rivera <stefano@rivera.za.net>
parents:
625
diff
changeset
|
31 self._font_cache = {} |
591 | 32 self._transformed_image_cache = {} |
561 | 33 |
571
20e296d4a3a5
Add load_image to Resources.
Jeremy Thurgood <firxen@gmail.com>
parents:
561
diff
changeset
|
34 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
|
35 """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
|
36 |
96ff2d8a8a9a
Allow list of image name fragments in load_image. Also, some docs.
Jeremy Thurgood <firxen@gmail.com>
parents:
580
diff
changeset
|
37 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
|
38 |
785 | 39 * /<lang>_<dialect>/<resource_module>/ |
769
43b49f1de828
Merge i18n for before the pyntnclick split
Stefano Rivera <stefano@rivera.za.net>
parents:
638
diff
changeset
|
40 * /<lang>/<resource_module>/ |
584
96ff2d8a8a9a
Allow list of image name fragments in load_image. Also, some docs.
Jeremy Thurgood <firxen@gmail.com>
parents:
580
diff
changeset
|
41 * <resource_module>/ |
785 | 42 * /<lang>_<dialect>/<default_resource_module>/ |
769
43b49f1de828
Merge i18n for before the pyntnclick split
Stefano Rivera <stefano@rivera.za.net>
parents:
638
diff
changeset
|
43 * /<lang>/<default_resource_module>/ |
584
96ff2d8a8a9a
Allow list of image name fragments in load_image. Also, some docs.
Jeremy Thurgood <firxen@gmail.com>
parents:
580
diff
changeset
|
44 * <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
|
45 |
96ff2d8a8a9a
Allow list of image name fragments in load_image. Also, some docs.
Jeremy Thurgood <firxen@gmail.com>
parents:
580
diff
changeset
|
46 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
|
47 are skipped. |
96ff2d8a8a9a
Allow list of image name fragments in load_image. Also, some docs.
Jeremy Thurgood <firxen@gmail.com>
parents:
580
diff
changeset
|
48 """ |
96ff2d8a8a9a
Allow list of image name fragments in load_image. Also, some docs.
Jeremy Thurgood <firxen@gmail.com>
parents:
580
diff
changeset
|
49 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
|
50 resource_name = os.path.join(*resource_name.split('/')) |
561 | 51 for path in self.get_paths(resource_name): |
769
43b49f1de828
Merge i18n for before the pyntnclick split
Stefano Rivera <stefano@rivera.za.net>
parents:
638
diff
changeset
|
52 if os.path.exists(path): |
561 | 53 return path |
54 raise ResourceNotFound(resource_name) | |
55 | |
56 def get_paths(self, resource_path): | |
587
f20d211d2c91
load_image() -> get_image() and more docs.
Jeremy Thurgood <firxen@gmail.com>
parents:
584
diff
changeset
|
57 """Get list of resource paths to search. |
f20d211d2c91
load_image() -> get_image() and more docs.
Jeremy Thurgood <firxen@gmail.com>
parents:
584
diff
changeset
|
58 """ |
561 | 59 paths = [] |
60 for module in [self.resource_module, self.DEFAULT_RESOURCE_MODULE]: | |
790
032c8c5563b8
Don't search the same path twice, if language == lang_dialect
Stefano Rivera <stefano@rivera.za.net>
parents:
785
diff
changeset
|
61 if self.lang_dialect: |
785 | 62 fn = os.path.join(self.lang_dialect, resource_path) |
63 paths.append(resource_filename(module, fn)) | |
790
032c8c5563b8
Don't search the same path twice, if language == lang_dialect
Stefano Rivera <stefano@rivera.za.net>
parents:
785
diff
changeset
|
64 if self.language != self.lang_dialect: |
561 | 65 fn = os.path.join(self.language, resource_path) |
66 paths.append(resource_filename(module, fn)) | |
67 paths.append(resource_filename(module, resource_path)) | |
68 return paths | |
571
20e296d4a3a5
Add load_image to Resources.
Jeremy Thurgood <firxen@gmail.com>
parents:
561
diff
changeset
|
69 |
625
85153f422522
Differently yuck get_image().
Jeremy Thurgood <firxen@gmail.com>
parents:
620
diff
changeset
|
70 def get_image(self, *image_name_fragments, **kw): |
587
f20d211d2c91
load_image() -> get_image() and more docs.
Jeremy Thurgood <firxen@gmail.com>
parents:
584
diff
changeset
|
71 """Load an image and optionally apply mutators. |
f20d211d2c91
load_image() -> get_image() and more docs.
Jeremy Thurgood <firxen@gmail.com>
parents:
584
diff
changeset
|
72 |
625
85153f422522
Differently yuck get_image().
Jeremy Thurgood <firxen@gmail.com>
parents:
620
diff
changeset
|
73 All positional params end up in `image_name_fragments` and are joined |
85153f422522
Differently yuck get_image().
Jeremy Thurgood <firxen@gmail.com>
parents:
620
diff
changeset
|
74 with the path separator. |
85153f422522
Differently yuck get_image().
Jeremy Thurgood <firxen@gmail.com>
parents:
620
diff
changeset
|
75 |
85153f422522
Differently yuck get_image().
Jeremy Thurgood <firxen@gmail.com>
parents:
620
diff
changeset
|
76 Two keyword parameters are also accepted: |
587
f20d211d2c91
load_image() -> get_image() and more docs.
Jeremy Thurgood <firxen@gmail.com>
parents:
584
diff
changeset
|
77 |
625
85153f422522
Differently yuck get_image().
Jeremy Thurgood <firxen@gmail.com>
parents:
620
diff
changeset
|
78 * `transforms` may contain transforms, which modify an image in-place |
85153f422522
Differently yuck get_image().
Jeremy Thurgood <firxen@gmail.com>
parents:
620
diff
changeset
|
79 to apply various effects. |
587
f20d211d2c91
load_image() -> get_image() and more docs.
Jeremy Thurgood <firxen@gmail.com>
parents:
584
diff
changeset
|
80 |
625
85153f422522
Differently yuck get_image().
Jeremy Thurgood <firxen@gmail.com>
parents:
620
diff
changeset
|
81 * `basedir` defaults to 'images', but may be overridden to load images |
85153f422522
Differently yuck get_image().
Jeremy Thurgood <firxen@gmail.com>
parents:
620
diff
changeset
|
82 from other places. ('icons', for example.) |
587
f20d211d2c91
load_image() -> get_image() and more docs.
Jeremy Thurgood <firxen@gmail.com>
parents:
584
diff
changeset
|
83 """ |
f20d211d2c91
load_image() -> get_image() and more docs.
Jeremy Thurgood <firxen@gmail.com>
parents:
584
diff
changeset
|
84 |
625
85153f422522
Differently yuck get_image().
Jeremy Thurgood <firxen@gmail.com>
parents:
620
diff
changeset
|
85 transforms = kw.get('transforms', ()) |
85153f422522
Differently yuck get_image().
Jeremy Thurgood <firxen@gmail.com>
parents:
620
diff
changeset
|
86 basedir = kw.get('basedir', 'images') |
85153f422522
Differently yuck get_image().
Jeremy Thurgood <firxen@gmail.com>
parents:
620
diff
changeset
|
87 |
584
96ff2d8a8a9a
Allow list of image name fragments in load_image. Also, some docs.
Jeremy Thurgood <firxen@gmail.com>
parents:
580
diff
changeset
|
88 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
|
89 |
591 | 90 key = (image_path, transforms) |
91 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
|
92 # We already have this cached, so shortcut the whole process. |
591 | 93 return self._transformed_image_cache[key] |
587
f20d211d2c91
load_image() -> get_image() and more docs.
Jeremy Thurgood <firxen@gmail.com>
parents:
584
diff
changeset
|
94 |
571
20e296d4a3a5
Add load_image to Resources.
Jeremy Thurgood <firxen@gmail.com>
parents:
561
diff
changeset
|
95 if image_path not in self._image_cache: |
20e296d4a3a5
Add load_image to Resources.
Jeremy Thurgood <firxen@gmail.com>
parents:
561
diff
changeset
|
96 image = pygame.image.load(image_path) |
20e296d4a3a5
Add load_image to Resources.
Jeremy Thurgood <firxen@gmail.com>
parents:
561
diff
changeset
|
97 if self.CONVERT_ALPHA: |
20e296d4a3a5
Add load_image to Resources.
Jeremy Thurgood <firxen@gmail.com>
parents:
561
diff
changeset
|
98 image = image.convert_alpha(pygame.display.get_surface()) |
20e296d4a3a5
Add load_image to Resources.
Jeremy Thurgood <firxen@gmail.com>
parents:
561
diff
changeset
|
99 self._image_cache[image_path] = image |
575
970cdc219e15
Add image mutation to resource loader.
Jeremy Thurgood <firxen@gmail.com>
parents:
571
diff
changeset
|
100 image = self._image_cache[image_path] |
970cdc219e15
Add image mutation to resource loader.
Jeremy Thurgood <firxen@gmail.com>
parents:
571
diff
changeset
|
101 |
593
1eb1537173ef
Add some image transforms.
Jeremy Thurgood <firxen@gmail.com>
parents:
591
diff
changeset
|
102 # Apply any transforms we're given. |
591 | 103 for transform in transforms: |
104 image = transform(image) | |
105 self._transformed_image_cache[key] = image | |
575
970cdc219e15
Add image mutation to resource loader.
Jeremy Thurgood <firxen@gmail.com>
parents:
571
diff
changeset
|
106 |
587
f20d211d2c91
load_image() -> get_image() and more docs.
Jeremy Thurgood <firxen@gmail.com>
parents:
584
diff
changeset
|
107 return image |
638
d1ec9e739e23
Make TextWidget work (probably)
Stefano Rivera <stefano@rivera.za.net>
parents:
625
diff
changeset
|
108 |
d1ec9e739e23
Make TextWidget work (probably)
Stefano Rivera <stefano@rivera.za.net>
parents:
625
diff
changeset
|
109 def get_font(self, file_name, font_size, basedir=None): |
d1ec9e739e23
Make TextWidget work (probably)
Stefano Rivera <stefano@rivera.za.net>
parents:
625
diff
changeset
|
110 """Load a a font, cached if possible.""" |
d1ec9e739e23
Make TextWidget work (probably)
Stefano Rivera <stefano@rivera.za.net>
parents:
625
diff
changeset
|
111 if basedir is None: |
d1ec9e739e23
Make TextWidget work (probably)
Stefano Rivera <stefano@rivera.za.net>
parents:
625
diff
changeset
|
112 basedir = 'fonts' |
d1ec9e739e23
Make TextWidget work (probably)
Stefano Rivera <stefano@rivera.za.net>
parents:
625
diff
changeset
|
113 key = (basedir, file_name, font_size) |
d1ec9e739e23
Make TextWidget work (probably)
Stefano Rivera <stefano@rivera.za.net>
parents:
625
diff
changeset
|
114 if key not in self._font_cache: |
d1ec9e739e23
Make TextWidget work (probably)
Stefano Rivera <stefano@rivera.za.net>
parents:
625
diff
changeset
|
115 fontfn = self.get_resource_path(basedir, file_name) |
d1ec9e739e23
Make TextWidget work (probably)
Stefano Rivera <stefano@rivera.za.net>
parents:
625
diff
changeset
|
116 self._font_cache[key] = pygame.font.Font(fontfn, font_size) |
d1ec9e739e23
Make TextWidget work (probably)
Stefano Rivera <stefano@rivera.za.net>
parents:
625
diff
changeset
|
117 return self._font_cache[key] |