Chris Mutel avatar Chris Mutel committed 7d32e85

Fix bw2iapackage import and export, and add setup functions

Comments (0)

Files changed (7)

bw2data/__init__.py

 from database import Database
 from method import Method
 from query import Query, Filter, Result
+from utils import set_data_dir, setup
 import proxies
 import utils
 import validate

bw2data/io/bw2package.py

 # -*- coding: utf-8 -*
-from .. import Database, databases, config, JsonWrapper
+from .. import Database, databases, config, JsonWrapper, methods, Method
 from ..utils import database_hash, download_file
 import bz2
 import os
+import warnings
 
 
 class BW2PackageExporter(object):
     @classmethod
+    def _prepare_method(cls, name):
+        data = {
+            "metadata": methods[name],
+            "cfs": [{
+                "database": o[0][0],
+                "code": o[0][1],
+                "amount": o[1],
+                "location": o[2],
+                "uncertainty type": 0,
+                "sigma": None,
+                "maximum": None,
+                "minimum": None
+            } for o in Method(name).load()]
+        }
+        data["metadata"]["name"] = name
+        return data
+
+    @classmethod
     def export_ia_method(cls, name):
-        assert name in methods, "Can't find this IA method"
+        filepath = os.path.join(config.request_dir("export"),
+            ".".join(name) + ".bw2iapackage")
+        with bz2.BZ2File(filepath, "w") as f:
+            f.write(JsonWrapper.dumps([cls._prepare_method(name)]))
+        return filepath
 
+    @classmethod
+    def export_all_methods(cls):
+        filepath = os.path.join(config.request_dir("export"),
+            "methods.bw2iapackage")
+        with bz2.BZ2File(filepath, "w") as f:
+            f.write(JsonWrapper.dumps(
+                [cls._prepare_method(name) for name in methods.list]
+                ))
+        return filepath
+
+    @classmethod
     def export_database(cls, name, include_dependencies):
         assert name in databases, "Can't find this database"
         if include_dependencies:
             raise NotImplementedError
         with bz2.BZ2File(filepath) as f:
             package_data = JsonWrapper.loads(f.read())
-        for name, data in package_data.iteritems():
-            db_data = dict([((name, key), value) for key, value in \
-                data["data"].iteritems()])
-            if name in databases:
-                raise ValueError("Dataase %s already exists" % name)
-            metadata = data["metadata"]
-            database = Database(name)
-            database.register(
-                format=metadata["from format"],
-                depends=metadata["depends"],
-                num_processes=metadata["number"],
-                version=metadata["version"]
-                )
-            database.write(db_data)
+        with warnings.catch_warnings():
+            warnings.simplefilter("ignore")
+            for name, data in package_data.iteritems():
+                db_data = dict([((name, key), value) for key, value in \
+                    data["data"].iteritems()])
+                if name in databases:
+                    raise ValueError("Dataase %s already exists" % name)
+                metadata = data["metadata"]
+                database = Database(name)
+                database.register(
+                    format=metadata["from format"],
+                    depends=metadata["depends"],
+                    num_processes=metadata["number"],
+                    version=metadata["version"]
+                    )
+                database.write(db_data)
+                database.process()
 
     @classmethod
     def import_method(cls, filepath, overwrite):
         if overwrite:
             raise NotImplementedError
-        pass
+        with bz2.BZ2File(filepath) as f:
+            package_data = JsonWrapper.loads(f.read())
+        with warnings.catch_warnings():
+            warnings.simplefilter("ignore")
+            for data in package_data:
+                name = tuple(data["metadata"]["name"])
+                if name in methods:
+                    raise ValueError("Duplicate method")
+                method = Method(name)
+                method.register(
+                    unit=data["metadata"]["unit"],
+                    description=data["metadata"]["description"],
+                    num_cfs=data["metadata"]["num_cfs"]
+                    )
+                method.write([
+                    [(o["database"], o["code"]), o["amount"], o["location"]
+                    ] for o in data["cfs"]])
+                method.process()
 
 
 def download_biosphere():

bw2data/io/import_ecospold.py

             manager = Database(name)
             manager.register(("Ecospold", 1), depends, len(data))
             manager.write(data)
-            # manager.process()
+            manager.process()

bw2data/io/import_method.py

             :align: center
 
         Args:
-            *path* (str): A filepath or directory.
+            * *path* (str): A filepath or directory.
 
         """
         if os.path.isdir(path):
             warnings.simplefilter("ignore")
             method = Method(name)
             method.register(unit, description, len(data))
-            method.write(dict([(("biosphere", o[0]), o[1]) for o in data]))
+            method.write([
+                [("biosphere", o[0]), o[1], "GLO"] for o in data])
             method.process()
 
     def add_cf(self, cf):

bw2data/method.py

         """
         if self.method not in methods:
             raise UnknownObject("This database is not yet registered")
-        mapping.add(data.keys())
-        geo_codes = [x[1] for x in data.values() if isinstance(x, (tuple, list))]
-        if geo_codes:
-            geomapping.add(geo_codes)
+        mapping.add(set([x[0] for x in data]))
+        geomapping.add(set([x[2] for x in data]))
         filepath = os.path.join(config.dir, "intermediate",
             "%s.pickle" % self.get_abbreviation())
         with open(filepath, "wb") as f:
             ('negative', np.bool)]
         arr = np.zeros((len(data), ), dtype=dtype)
         arr['minimum'] = arr['maximum'] = arr['sigma'] = np.NaN
-        for i, (key, value) in enumerate(data.iteritems()):
-            if isinstance(value, (tuple, list)):
-                value, geo = value
-            else:
-                geo = "GLO"
-
+        for i, (key, value, geo) in enumerate(data):
             arr[i] = (
                 0,
                 mapping[key],
 # -*- coding: utf-8 -*-
-from . import config
+from . import config, reset_meta
 import hashlib
 import os
+import platform
 import random
 import re
 import requests
     "biosphere": 2,
     }
 
-DOWNLOAD_URL = "http://secret.brightwaylca.org/data/"
+DOWNLOAD_URL = "http://brightwaylca.org/data/"
 
 
 def natural_sort(l):
                 break
             f.write(segment)
     return filepath
+
+
+def set_data_dir(dirpath):
+    if not os.path.exists(dirpath):
+        os.mkdir(dirpath)
+
+    user_dir = os.path.expanduser("~")
+    if platform.system == "Windows":
+        filename = "brightway2path.txt"
+    else:
+        filename = ".brightway2path"
+    with open(os.path.join(user_dir, filename), "w") as f:
+        f.write(dirpath)
+
+    config.reset()
+    config.is_temp_dir = False
+    config.create_basic_directories()
+    reset_meta()
+
+
+def setup():
+    from io import download_biosphere, download_methods
+    config.create_basic_directories()
+    config.is_temp_dir = False
+    download_biosphere()
+    download_methods()

bw2data/validate.py

     }},
     extra=True)
 
-ia_validator = Schema({valid_tuple: float or (object, float)})
+ia_validator = Schema([[valid_tuple, float, object]])
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.