Commits

Chris Mutel committed 1703913

Make name of default biosphere database settable in preferences

Comments (0)

Files changed (6)

         """Return `dir` in Unicode"""
         return self.dir.decode('utf-8')
 
+    @property
+    def biosphere(self):
+        if not hasattr(self, "p"):
+            self.load_preferences()
+        return self.p.get("biosphere_database", "biosphere")
+
 
 config = Config()
             The intermediate data, a dictionary.
 
         """
+        print "Loading database:", self.database
         if self.database not in databases:
             raise UnknownObject("This database is not yet registered")
         if version is None and config.p.get("use_cache", False) and \

bw2data/io/import_ecospold.py

 # -*- coding: utf-8 -*
 from __future__ import division
-from .. import Database, mapping
+from .. import Database, mapping, config
 from ..logs import get_io_logger
 from ..utils import activity_hash
 from ..units import normalize_units
     Does not have any arguments; instead, instantiate the class, and then import using the ``importer`` method, i.e. ``Ecospold1Importer().importer(filepath)``.
 
     """
-    def importer(self, path, name, depends=["biosphere"]):
+    def importer(self, path, name, depends=[config.biosphere]):
         """Import an inventory dataset, or a directory of inventory datasets.
 
         .. image:: images/import-method.png
         data = linked_data + self.new_activities
 
         if self.new_biosphere:
-            self.new_biosphere = dict([((u"biosphere", o.pop("hash")), o) \
+            self.new_biosphere = dict([((config.biosphere, o.pop("hash")), o) \
                 for o in self.new_biosphere])
-            biosphere = Database("biosphere")
+            biosphere = Database(config.biosphere)
             biosphere_data = biosphere.load()
             biosphere_data.update(self.new_biosphere)
             biosphere.write(biosphere_data)
         """Set the ``type`` attribute for each exchange, one of either (``production``, ``technosphere``, ``biosphere``). ``production`` defines the amount produced by the activity dataset (default is 1)."""
         for ds in data:
             for exc in ds["exchanges"]:
-                if exc["input"][0] == "biosphere":
+                if exc["input"][0] == config.biosphere:
                     exc["type"] = "biosphere"
                 elif exc["input"][1] == ds["hash"]:
                     exc["type"] = "production"

bw2data/io/import_method.py

 # -*- coding: utf-8 -*
-from .. import Database, Method, methods, mapping
+from .. import Database, Method, methods, mapping, config
 from ..logs import get_io_logger
 from ..units import normalize_units
 from ..utils import activity_hash
         self.log, self.logfile = get_io_logger("lcia-import")
 
         try:
-            self.biosphere_data = Database("biosphere").load()
+            self.biosphere_data = Database(config.biosphere).load()
         except:
             # Biosphere not loaded
             raise ValueError("Can't find biosphere database; check configuration.")
         data = [self.add_cf(o) for o in ds.flowData.iterchildren()]
 
         if self.new_flows:
-            biosphere = Database("biosphere")
+            biosphere = Database(config.biosphere)
             biosphere_data = biosphere.load()
             # Could be considered dirty to .pop() inside list comprehension
             # but it works. The dictionary constructor also eliminates
             # duplicates.
-            biosphere_data.update(dict([(("biosphere", o.pop("hash")), o
+            biosphere_data.update(dict([((config.biosphere, o.pop("hash")), o
                 ) for o in self.new_flows]))
             biosphere.write(biosphere_data)
             biosphere.process()
             method = Method(name)
             method.register(unit=unit, description=description, num_cfs=len(data))
             method.write([
-                [("biosphere", o[0]), o[1], "GLO"] for o in data])
+                [(config.biosphere, o[0]), o[1], "GLO"] for o in data])
             method.process()
 
     def add_cf(self, cf):

bw2data/io/import_simapro.py

 # -*- coding: utf-8 -*
-from .. import Database, databases
+from .. import Database, databases, config
 from ..logs import get_io_logger
 from ..utils import activity_hash
 from ..units import normalize_units
                             value['location'])] = key
             self.background[(value['name'].lower(), value['unit'])] = key
 
-        self.biosphere = Database("biosphere").load()
+        self.biosphere = Database(config.biosphere).load()
 
     def link_exchanges(self, dataset):
         """Link all exchanges in a given dataset"""

bw2data/tests/config.py

         self.assertEqual(config.p['saved'], "yep")
         data = json.load(open(os.path.join(config.dir, "preferences.json")))
         self.assertEqual(data['saved'], "yep")
+
+    def test_default_biosphere(self):
+        self.assertEqual(config.biosphere, "biosphere")
+
+    def test_set_retrieve_biosphere(self):
+        config.p['biosphere_database'] = "foo"
+        config.save_preferences()
+        config.load_preferences()
+        self.assertEqual(config.biosphere, "foo")
+        del config.p['biosphere_database']
+        config.save_preferences()