Commits

Andrew Davison committed 87c2ccd

Was having a problem with Sphinx when including more than one image in a document, that each time it would attempt to recreate a record store (in the same process), which Django doesn't like. So now we cache the project (and its associated store) so it is only loaded once.

  • Participants
  • Parent commits d2e593d

Comments (0)

Files changed (2)

File sumatra/publishing/utils.py

 from sumatra.datastore import DataKey
 
 
+_cache = {}
+
 def mkdir(path):
     try:
         os.makedirs(path)
         else: raise
 
 
+class cache(object):
+    """Cache decorator"""
+    global _cache
+    def __init__(self, func):
+        self.func = func
+    def __call__(self, options):
+        assert isinstance(options, dict)
+        if 'project' in options and 'record_store' in options:
+            cache_key = (options['project'], options['record_store'])
+            if cache_key in _cache:
+                return _cache[cache_key]
+            else:
+                obj = self.func(options)
+                _cache[cache_key] = obj
+                return obj
+        else:
+            return self.func(options)
+
+
+@cache
 def determine_project(sumatra_options):
     if 'project_dir' in sumatra_options and sumatra_options['project_dir']:
         prj = load_project(sumatra_options['project_dir'])
-    elif os.path.exists(os.path.join('.smt', 'project')):
-        prj = load_project()
     else:
-        prj = None
+        try:
+            prj = load_project()
+        except IOError:
+            prj = None
     return prj
 
 

File test/unittests/test_publishing.py

 
 class TestUtils(unittest.TestCase):
     
+    def setUp(self):
+        utils._cache = {}
+    
     @patch(utils, "load_project", MockProject)
     def test_determine_project_with_project_dir(self):
         prj = utils.determine_project({'project_dir': '/path/to/project'})
 
 class TestLaTeX(unittest.TestCase):
     
+    def setUp(self):
+        utils._cache = {}
+    
     def test_read_config(self):
         config = dedent("""
             [sumatra]
     sumatra_link_icon: icon_info.png
     """
     
-    #def test_build_options(self):
+    def setUp(self):
+        utils._cache = {}
 
     def tearDown(self):
         os.remove("docutils.conf")