# HG changeset patch # User Jeremy Thurgood # Date 1328965534 -7200 # Node ID e207dfad0d9e62c6ee120a328d131b2389763a57 # Parent 28f03563f4db78da4f7f34fcaabbfcf5ed8b21eb Start of resource manager. diff -r 28f03563f4db -r e207dfad0d9e pyntnclick/data.py --- a/pyntnclick/data.py Sat Feb 11 14:47:44 2012 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,25 +0,0 @@ -'''Simple data loader module. - -Loads data files from the "data" directory shipped with a game. - -Enhancing this to handle caching etc. is left as an exercise for the reader. -''' - -import os - -data_py = os.path.abspath(os.path.dirname(__file__)) -data_dir = os.path.normpath(os.path.join(data_py, '..', 'Resources')) - - -def filepath(filename): - '''Determine the path to a file in the data directory. - ''' - return os.path.join(data_dir, filename) - - -def load(filename, mode='rb'): - '''Open a file in the data directory. - - "mode" is passed as the second arg to open(). - ''' - return open(os.path.join(data_dir, filename), mode) diff -r 28f03563f4db -r e207dfad0d9e pyntnclick/data/__init__.py diff -r 28f03563f4db -r e207dfad0d9e pyntnclick/resources.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/pyntnclick/resources.py Sat Feb 11 15:05:34 2012 +0200 @@ -0,0 +1,31 @@ +# -*- test-case-name: pyntnclick.tests.test_resources -*- + +import os +from pkg_resources import resource_filename + + +class ResourceNotFound(Exception): + pass + + +class Resources(object): + DEFAULT_RESOURCE_MODULE = "pyntnclick.data" + + def __init__(self, resource_module, language=None): + self.resource_module = resource_module + self.language = language + + def get_resource_path(self, resource_name): + for path in self.get_paths(resource_name): + if os.path.exists(path): + return path + raise ResourceNotFound(resource_name) + + def get_paths(self, resource_path): + paths = [] + for module in [self.resource_module, self.DEFAULT_RESOURCE_MODULE]: + if self.language: + fn = os.path.join(self.language, resource_path) + paths.append(resource_filename(module, fn)) + paths.append(resource_filename(module, resource_path)) + return paths diff -r 28f03563f4db -r e207dfad0d9e pyntnclick/tests/__init__.py diff -r 28f03563f4db -r e207dfad0d9e pyntnclick/tests/test_resources.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/pyntnclick/tests/test_resources.py Sat Feb 11 15:05:34 2012 +0200 @@ -0,0 +1,24 @@ +import os.path +from unittest import TestCase + +from pyntnclick.resources import Resources + + +TEST_PATH = os.path.dirname(__file__) +DATA_PATH = os.path.join(os.path.dirname(TEST_PATH), 'data') + +test_path = lambda p: os.path.join(TEST_PATH, p) +data_path = lambda p: os.path.join(DATA_PATH, p) + + +class ResourcesTestCase(TestCase): + def test_get_paths_no_lang(self): + res = Resources('pyntnclick.tests') + self.assertEqual([test_path('thing'), data_path('thing')], + res.get_paths('thing')) + + def test_get_paths_lang(self): + res = Resources('pyntnclick.tests', 'en') + self.assertEqual([test_path('en/thing'), test_path('thing'), + data_path('en/thing'), data_path('thing')], + res.get_paths('thing'))