changeset 561:e207dfad0d9e pyntnclick

Start of resource manager.
author Jeremy Thurgood <firxen@gmail.com>
date Sat, 11 Feb 2012 15:05:34 +0200
parents 28f03563f4db
children f22953c43c6d
files pyntnclick/data.py pyntnclick/data/__init__.py pyntnclick/resources.py pyntnclick/tests/__init__.py pyntnclick/tests/test_resources.py
diffstat 3 files changed, 55 insertions(+), 25 deletions(-) [+]
line wrap: on
line diff
--- 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)
--- /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
--- /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'))