Chris Mutel avatar Chris Mutel committed 8de885c

0.8.2: Use warnings instead of print statements, automatically update number of processes when saving a database, compatibility with UI v. 0.4.5

Comments (0)

Files changed (6)

bw2data/_config.py

         self.cache = {}
 
     def check_dir(self, dir=None):
-        """Check is directory is writeable."""
-        return os.access(dir or self.dir, os.W_OK)
+        """Check is directory is a directory and writeable."""
+        return os.path.isdir(self.dir) and os.access(dir or self.dir, os.W_OK)
 
     def reset(self, path=None):
         """Reset to original configuration. Useful for testing."""
         try:
-            self.dir = os.path.dirname(self.get_home_directory(path))
+            self.dir = self.get_home_directory(path)
         except OSError:
             self.dir = tempfile.mkdtemp()
             self.is_temp_dir = True
             except:
                 pass
         else:
-            return os.path.join(user_dir, "brightway2/")
+            return os.path.join(user_dir, "brightway2")
 
     def request_dir(self, dirname):
         """Return ``True`` if directory already exists or can be created."""

bw2data/database.py

         """
         if self.database not in databases:
             raise UnknownObject("This database is not yet registered")
-        databases.increment_version(self.database)
+        databases.increment_version(self.database, len(data))
         mapping.add(data.keys())
         if config.p.get("use_cache", False) and self.database in config.cache:
             config.cache[self.database] = data

bw2data/io/import_ecospold.py

 from ..logs import get_logger
 from ..errors import UnknownExchange
 from lxml import objectify
+from stats_toolkit.distributions import *
 import math
 import os
 import progressbar
-from stats_toolkit.distributions import *
+import warnings
 
 BIOSPHERE = ("air", "water", "soil", "resource")
 
 
         data = dict([((name, int(o["code"])), o) for o in data])
 
-        manager = Database(name)
-        manager.register(("Ecospold", 1), depends, len(data))
-        manager.write(data)
+        with warnings.catch_warnings():
+            warnings.simplefilter("ignore")
+            manager = Database(name)
+            manager.register(("Ecospold", 1), depends, len(data))
+            manager.write(data)
 
         pbar.finish()
 

bw2data/io/import_method.py

 # -*- coding: utf-8 -*
-from .. import Database, mapping, Method, methods
+from .. import Database, mapping, Method, methods, databases
 from lxml import objectify
+import hashlib
 import os
 import progressbar
+import warnings
 try:
     import cPickle as pickle
 except:
 
         """
         if os.path.isdir(path):
-            files = [os.path.join(path, filter(lambda x: x[-4:].lower(
-                ) == ".xml", os.listdir(path)))]
+            files = [os.path.join(path, name) for name in \
+                filter(lambda x: x[-4:].lower() == ".xml", os.listdir(path))]
         else:
             files = [path]
 
-        self.biosphere_data = Database("biosphere").load()
+        self.new_flows = {}
+
+        try:
+            self.biosphere_data = Database("biosphere").load()
+        except:
+            # Biosphere not loaded
+            raise ValueError("Can't find biosphere database; check configuration.")
         if progressbar:
             widgets = ['Files: ', progressbar.Percentage(), ' ',
                 progressbar.Bar(marker=progressbar.RotatingMarker()), ' ',
         unit = ref_func.get("unit") or ""
         data = {}
         for cf in ds.flowData.iterchildren():
-            if ("biosphere", int(cf.get("number"))) not in mapping:
-                # Add new biosphere flow
-                code = int(cf.get("number"))
-                new_flow = {
-                    "name": cf.get("name"),
-                    "categories": (cf.get("category"),
-                        cf.get("subCategory") or "unspecified"),
-                    "code": code,
-                    "unit": cf.get("unit"),
-                    "exchanges": []
-                }
+            code = self.get_code(cf)
+            if ("biosphere", code) not in mapping:
+                self.add_flow(cf, code)
+            data[("biosphere", code)] = float(cf.get("meanValue"))
+        assert name not in methods
 
-                # Convert ("foo", "unspecified") to ("foo",)
-                while new_flow["categories"][-1] == "unspecified":
-                    new_flow["categories"] = new_flow["categories"][:-1]
+        if self.new_flows:
+            biosphere = Database("biosphere")
+            bio_data = biosphere.load()
+            bio_data.update(self.new_flows)
+            biosphere.write(bio_data)
+            biosphere.process()
 
-                # Emission or resource
-                resource = new_flow["categories"][0] == "resource"
-                new_flow["type"] = "resource" if resource else "emission"
+        with warnings.catch_warnings():
+            warnings.simplefilter("ignore")
+            method = Method(name)
+            method.register(unit, description, len(data))
+            method.write(data)
+            method.process()
 
-                # Write modified biosphere database
-                biosphere = Database("biosphere")
-                bio_data = biosphere.load()
-                bio_data[("biosphere", code)] = new_flow
-                biosphere.write(bio_data)
-                return ("biosphere", code)
-            data[("biosphere", int(cf.get("number")))] = float(
-                cf.get("meanValue"))
-        assert name not in methods
-        method = Method(name)
-        method.register(unit, description, len(data))
-        method.write(data)
-        method.process()
+    def get_code(self, cf):
+        try:
+            int_code = int(cf.get("number"))
+            assert int_code
+            return int_code
+        except:
+            hasher = hashlib.md5()
+            cat_string = "-".join([cf.get("category"), cf.get("subCategory")])
+            hasher.update(cat_string)
+            return hasher.hexdigest()[-8:]
+
+    def add_flow(self, cf, code):
+        """Add new biosphere flow"""
+        new_flow = {
+            "name": cf.get("name"),
+            "categories": (cf.get("category"),
+                cf.get("subCategory") or "unspecified"),
+            "code": code,
+            "unit": cf.get("unit"),
+            "exchanges": []
+        }
+
+        # Convert ("foo", "unspecified") to ("foo",)
+        while new_flow["categories"][-1] == "unspecified":
+            new_flow["categories"] = new_flow["categories"][:-1]
+
+        # Emission or resource
+        resource = new_flow["categories"][0] == "resource"
+        new_flow["type"] = "resource" if resource else "emission"
+        self.new_flows[("biosphere", code)] = new_flow
     """A dictionary for database metadata. This class includes methods to manage database versions. File data is saved in ``databases.json``."""
     _filename = "databases.json"
 
-    def increment_version(self, database):
+    def increment_version(self, database, number=None):
         """Increment the ``database`` version. Returns the new version."""
         self.data[database]["version"] += 1
+        if number is not None:
+            self.data[database]["number"] = number
         self.flush()
         return self.data[database]["version"]
 
 
 setup(
   name='bw2data',
-  version="0.8.1",
+  version="0.8.2",
   packages=packages,
   author="Chris Mutel",
   author_email="cmutel@gmail.com",
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.