comparison pyntnclick/resources.py @ 854:79b5c1be9a5e default tip

Remove pyntnclick, it's its own library, now
author Stefano Rivera <stefano@rivera.za.net>
date Sat, 21 Jun 2014 22:06:09 +0200
parents f95830b58336
children
comparison
equal deleted inserted replaced
852:f95830b58336 854:79b5c1be9a5e
1 # -*- test-case-name: pyntnclick.tests.test_resources -*-
2
3 import os
4 from pkg_resources import resource_filename
5
6 import pygame
7
8
9 class ResourceNotFound(Exception):
10 pass
11
12
13 class Resources(object):
14 """Resource loader and manager.
15
16 The `CONVERT_ALPHA` flag allows alpha conversions to be disabled so that
17 images may be loaded without having a display initialised. This is useful
18 in unit tests, for example.
19 """
20
21 DEFAULT_RESOURCE_MODULE = "pyntnclick.data"
22 CONVERT_ALPHA = True
23
24 def __init__(self, resource_module, language=None):
25 self.resource_module = resource_module
26 self.lang_dialect = language
27 self.language = language
28 if language:
29 self.language = language.split('_', 1)[0]
30 self._image_cache = {}
31 self._font_cache = {}
32 self._transformed_image_cache = {}
33
34 def get_resource_path(self, *resource_path_fragments):
35 """Find the resource in one of a number of different places.
36
37 The following directories are searched, in order:
38
39 * /<lang>_<dialect>/<resource_module>/
40 * /<lang>/<resource_module>/
41 * <resource_module>/
42 * /<lang>_<dialect>/<default_resource_module>/
43 * /<lang>/<default_resource_module>/
44 * <default_resource_module>/
45
46 If the `language` attribute is `None`, the paths with <lang> in them
47 are skipped.
48 """
49 resource_name = '/'.join(resource_path_fragments)
50 resource_name = os.path.join(*resource_name.split('/'))
51 for path in self.get_paths(resource_name):
52 if os.path.exists(path):
53 return path
54 raise ResourceNotFound(resource_name)
55
56 def get_paths(self, resource_path):
57 """Get list of resource paths to search.
58 """
59 paths = []
60 for module in [self.resource_module, self.DEFAULT_RESOURCE_MODULE]:
61 if self.lang_dialect:
62 fn = os.path.join(self.lang_dialect, resource_path)
63 paths.append(resource_filename(module, fn))
64 if self.language != self.lang_dialect:
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
69
70 def get_image(self, *image_name_fragments, **kw):
71 """Load an image and optionally apply mutators.
72
73 All positional params end up in `image_name_fragments` and are joined
74 with the path separator.
75
76 Two keyword parameters are also accepted:
77
78 * `transforms` may contain transforms, which modify an image in-place
79 to apply various effects.
80
81 * `basedir` defaults to 'images', but may be overridden to load images
82 from other places. ('icons', for example.)
83 """
84
85 transforms = kw.get('transforms', ())
86 basedir = kw.get('basedir', 'images')
87
88 image_path = self.get_resource_path(basedir, *image_name_fragments)
89
90 key = (image_path, transforms)
91 if key in self._transformed_image_cache:
92 # We already have this cached, so shortcut the whole process.
93 return self._transformed_image_cache[key]
94
95 if image_path not in self._image_cache:
96 image = pygame.image.load(image_path)
97 if self.CONVERT_ALPHA:
98 image = image.convert_alpha(pygame.display.get_surface())
99 self._image_cache[image_path] = image
100 image = self._image_cache[image_path]
101
102 # Apply any transforms we're given.
103 for transform in transforms:
104 image = transform(image)
105 self._transformed_image_cache[key] = image
106
107 return image
108
109 def get_font(self, file_name, font_size, basedir=None):
110 """Load a a font, cached if possible."""
111 if basedir is None:
112 basedir = 'fonts'
113 key = (basedir, file_name, font_size)
114 if key not in self._font_cache:
115 fontfn = self.get_resource_path(basedir, file_name)
116 self._font_cache[key] = pygame.font.Font(fontfn, font_size)
117 return self._font_cache[key]