brightway2-data / bw2data / io /

# -*- coding: utf-8 -*
from .. import Database, databases, config, JsonWrapper
from ..utils import database_hash, download_file
import bz2
import os

class BW2PackageExporter(object):
    def export_ia_method(cls, name):
        assert name in methods, "Can't find this IA method"

    def export_database(cls, name, include_dependencies):
        assert name in databases, "Can't find this database"
        if include_dependencies:
            for dependency in databases[name]["depends"]:
                assert dependency in databases, \
                    "Can't find dependent database %s" % dependency
            to_export = [name] + databases[name]["depends"]
            filename = name + ".fat.bw2package"
            to_export = [name]
            filename = name + ".bw2package"
        filepath = os.path.join(config.request_dir("export"), filename)
        with bz2.BZ2File(filepath, "w") as f:
            f.write(JsonWrapper.dumps({db_name: {
                "metadata": databases[db_name],
                "data": {k[1]: v for k, v in Database(db_name).load().iteritems()}
                } for db_name in to_export}))
        return filepath

    def export(cls, name, include_dependencies=False):
        if isinstance(name, (tuple, list)):
            return cls.export_ia_method(name)
        elif isinstance(name, basestring):
            return cls.export_database(name, include_dependencies)
            raise ValueError("Unknown input data")

class BW2PackageImporter(object):
    def importer(cls, filepath, overwrite=False):
        if overwrite:
            raise NotImplementedError
        if filepath.split(".")[-1] == "bw2package":
            return cls.import_database(filepath, overwrite)
        elif filepath.split(".")[-1] == "bw2iapackage":
            return cls.import_method(filepath, overwrite)
            raise ValueError("Unknown input data")

    def import_database(cls, filepath, overwrite):
        if overwrite:
            raise NotImplementedError
        with bz2.BZ2File(filepath) as f:
            package_data = JsonWrapper.loads(
        for name, data in package_data.iteritems():
            db_data = dict([((name, key), value) for key, value in \
            if name in databases:
                raise ValueError("Dataase %s already exists" % name)
            metadata = data["metadata"]
            database = Database(name)
                format=metadata["from format"],

    def import_method(cls, filepath, overwrite):
        if overwrite:
            raise NotImplementedError

def download_biosphere():
    filepath = download_file("biosphere.bw2package")

def download_methods():
    filepath = download_file("methods.bw2iapackage")
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
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.