brightway2-ui / bw2ui / controller.py

# -*- coding: utf-8 -*-
from brightway2 import databases, methods, Database, Method, config, reset_meta
from brightway2.io import EcospoldImporter, EcospoldImpactAssessmentImporter
from errors import UnknownAction


class Controller(object):
    def database_or_method(self, name):
        if name in databases:
            return (name, "database")
        elif name in methods:
            return (name, "method")
        elif tuple(name.split(":")) in methods:
            return (tuple(name.split(":")), "method")
        else:
            raise ValueError

    def dispatch(self, **kwargs):
        if kwargs.get('list', None):
            return self.list(kwargs)
        elif kwargs.get('details', None):
            return self.details(kwargs)
        elif kwargs.get('remove', None):
            return self.remove(kwargs)
        elif kwargs.get('import', None) and kwargs.get('database', None):
            return self.import_database(kwargs)
        elif kwargs.get('import', None) and kwargs.get('method', None):
            return self.import_method(kwargs)
        raise UnknownAction("No suitable action found")

    def list(self, kwargs):
        if kwargs.get('databases', None):
            return databases.list
        else:
            return methods.list

    def details(self, kwargs):
        name, kind = self.database_or_method(kwargs.get('<name>', None))
        if kind == "database":
            return databases[name]
        else:
            return methods[name]

    def remove(self, kwargs):
        name, kind = self.database_or_method(kwargs.get('<name>', None))
        if kind == "database":
            Database(name).deregister()
        else:
            Method(name).deregister()

    def import_database(self, path):
        EcospoldImporter.import_directory(path)

    def import_method(self, path):
        EcospoldImpactAssessmentImporter(path)

    def setup_directory(self, path):
        config.dir = path
        config.create_basic_directories()
        reset_meta()
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.