Mercurial > boomslang
changeset 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 | 66c2e084b8b3 |
children | ec13a5a0b213 |
files | pyntnclick/resources.py pyntnclick/tests/test_resources.py |
diffstat | 2 files changed, 29 insertions(+), 3 deletions(-) [+] |
line wrap: on
line diff
--- a/pyntnclick/resources.py Sat Feb 11 16:43:05 2012 +0200 +++ b/pyntnclick/resources.py Sat Feb 11 16:45:07 2012 +0200 @@ -11,6 +11,13 @@ class Resources(object): + """Resource loader and manager. + + The `CONVERT_ALPHA` flag allows alpha conversions to be disabled so that + images may be loaded without having a display initialised. This is useful + in unit tests, for example. + """ + DEFAULT_RESOURCE_MODULE = "pyntnclick.data" CONVERT_ALPHA = True @@ -21,7 +28,20 @@ self._mutated_image_cache = {} def get_resource_path(self, *resource_path_fragments): - resource_name = os.path.join(*resource_path_fragments) + """Find the resource in one of a number of different places. + + The following directories are searched, in order: + + * <resource_module>/<lang>/ + * <resource_module>/ + * <default_resource_module>/<lang>/ + * <default_resource_module>/ + + If the `language` attribute is `None`, the paths with <lang> in them + are skipped. + """ + resource_name = '/'.join(resource_path_fragments) + resource_name = os.path.join(*resource_name.split('/')) for path in self.get_paths(resource_name): if os.path.exists(path): return path @@ -36,8 +56,10 @@ paths.append(resource_filename(module, resource_path)) return paths - def load_image(self, image_name, mutators=(), basedir='images'): - image_path = self.get_resource_path(basedir, image_name) + def load_image(self, image_name_fragments, mutators=(), basedir='images'): + if isinstance(image_name_fragments, basestring): + image_name_fragments = [image_name_fragments] + image_path = self.get_resource_path(basedir, *image_name_fragments) if image_path not in self._image_cache: image = pygame.image.load(image_path)
--- a/pyntnclick/tests/test_resources.py Sat Feb 11 16:43:05 2012 +0200 +++ b/pyntnclick/tests/test_resources.py Sat Feb 11 16:45:07 2012 +0200 @@ -52,6 +52,10 @@ image = self.res.load_image('pyntnclick/hand.png') self.assertTrue(isinstance(image, Surface)) + def test_load_image_fragments(self): + image = self.res.load_image(['pyntnclick', 'hand.png']) + self.assertTrue(isinstance(image, Surface)) + def test_load_image_different_basedir(self): image = self.res.load_image('hand.png', basedir='images/pyntnclick') self.assertTrue(isinstance(image, Surface))