Added a template directory search list (vs. the previous single option)
[pyrisk.git] / pyrisk / graphics.py
index a2fc2504a287babd67733357046f358924589d2a..e83de809afa1fe8f083b89fe3f716022b88149d9 100644 (file)
@@ -28,6 +28,12 @@ import os.path
 from .base import NameMixin, ID_CmpMixin
 
 
+TEMPLATE_DIRS = [
+    './templates',
+    '~/share/pyrisk/templates',
+    '/usr/share/pyrisk/templates/',
+    ]
+
 class Template (NameMixin):
     """Setup regions for a particular world.
     """
@@ -44,8 +50,11 @@ class TemplateLibrary (object):
 
     TODO: explain template data format.
     """
-    def __init__(self, template_dir='share/templates/'):
-        self.template_dir = os.path.abspath(os.path.expanduser(template_dir))
+    def __init__(self, template_dirs=None):
+        if template_dirs == None:
+            template_dirs = TEMPLATE_DIRS
+        self.template_dirs = [os.path.abspath(os.path.expanduser(d))
+                              for d in template_dirs]
     def get(self, name):
         region_pointlists,route_pointlists,continent_colors,line_colors, \
             player_colors = \
@@ -54,10 +63,15 @@ class TemplateLibrary (object):
         return Template(name, regions, continent_colors, line_colors, 
                         player_colors)
     def _get_data(self, name):
-        dirname = os.path.join(self.template_dir, name.lower())
-        try:
-            files = os.listdir(dirname)
-        except IOError:
+        files = None
+        for d in self.template_dirs:
+            dirname = os.path.join(d, name.lower())
+            try:
+                files = os.listdir(dirname)
+                break
+            except IOError:
+                pass
+        if files == None:
             return None
         region_pointlists = {}
         route_pointlists = {}