Mercurial > boomslang
comparison pyntnclick/resources.py @ 561:e207dfad0d9e pyntnclick
Start of resource manager.
author | Jeremy Thurgood <firxen@gmail.com> |
---|---|
date | Sat, 11 Feb 2012 15:05:34 +0200 |
parents | |
children | 20e296d4a3a5 |
comparison
equal
deleted
inserted
replaced
560:28f03563f4db | 561:e207dfad0d9e |
---|---|
1 # -*- test-case-name: pyntnclick.tests.test_resources -*- | |
2 | |
3 import os | |
4 from pkg_resources import resource_filename | |
5 | |
6 | |
7 class ResourceNotFound(Exception): | |
8 pass | |
9 | |
10 | |
11 class Resources(object): | |
12 DEFAULT_RESOURCE_MODULE = "pyntnclick.data" | |
13 | |
14 def __init__(self, resource_module, language=None): | |
15 self.resource_module = resource_module | |
16 self.language = language | |
17 | |
18 def get_resource_path(self, resource_name): | |
19 for path in self.get_paths(resource_name): | |
20 if os.path.exists(path): | |
21 return path | |
22 raise ResourceNotFound(resource_name) | |
23 | |
24 def get_paths(self, resource_path): | |
25 paths = [] | |
26 for module in [self.resource_module, self.DEFAULT_RESOURCE_MODULE]: | |
27 if self.language: | |
28 fn = os.path.join(self.language, resource_path) | |
29 paths.append(resource_filename(module, fn)) | |
30 paths.append(resource_filename(module, resource_path)) | |
31 return paths |