def run_import(self, import_dir) -> None: """Use the given dataset path to import the ecospold2 files.""" try: importer = SingleOutputEcospold2Importer( import_dir, self.db_name, extractor=ActivityBrowserExtractor, signal=import_signals.strategy_progress) importer.apply_strategies() importer.write_database(backend='activitybrowser') if not import_signals.cancel_sentinel: import_signals.finished.emit() else: self.delete_canceled_db() except ImportCanceledError: self.delete_canceled_db() except InvalidExchange: # Likely caused by new version of ecoinvent not finding required # biosphere flows. self.delete_canceled_db() import_signals.import_failure.emit(( "Missing exchanges", "The import failed as required biosphere" " exchanges are missing from the biosphere3 database. Please" " update the biosphere by using 'File' -> 'Update biosphere...'" ))
def get_ecoinvent(db_name=None, auto_write=False, download_path=None, store_download=True, **kwargs): """ Download and import ecoinvent to current brightway2 project Optional kwargs: db_name: name to give imported database (string) default is downloaded filename auto_write: automatically write database if no unlinked processes (boolean) default is False (i.e. prompt yes or no) download_path: path to download .7z file to (string) default is download to temporary directory (.7z file is deleted after import) store_download: store the .7z file for later reuse, default is True, only takes effect if no download_path is provided username: ecoinvent username (string) password: ecoivnent password (string) version: ecoinvent version (string), eg '3.5' system_model: ecoinvent system model (string), one of {'cutoff', 'apos', 'consequential'} """ with tempfile.TemporaryDirectory() as td: if download_path is None: if store_download: download_path = eidlstorage.eidl_dir else: download_path = td downloader = EcoinventDownloader(outdir=download_path, **kwargs) downloader.run() downloader.extract(target_dir=td) if not db_name: db_name = downloader.file_name.replace('.7z', '') datasets_path = os.path.join(td, 'datasets') importer = SingleOutputEcospold2Importer(datasets_path, db_name) if 'biosphere3' not in databases: if auto_write: bw2setup() else: print('No biosphere database present in your current ' + 'project: {}'.format(projects.current)) print( 'You can run "bw2setup()" if this is a new project. Run it now?' ) if input('[y]/n ') in {'y', ''}: bw2setup() else: return importer.apply_strategies() datasets, exchanges, unlinked = importer.statistics() if auto_write and not unlinked: print('\nWriting database {} in project {}'.format( db_name, projects.current)) importer.write_database() else: print('\nWrite database {} in project {}?'.format( db_name, projects.current)) if input('[y]/n ') in {'y', ''}: importer.write_database()
def test_importer_mp_error(): class Extractor: def __init__(self): pass def extract(self, *args, **kwargs): raise RuntimeError ext = Extractor() with pytest.raises(MultiprocessingError): SingleOutputEcospold2Importer(FIXTURES, "ei", extractor=ext)
def test_importer_custom_extractor(): class Extractor: def __init__(self): self.data = [] def extract(self, *args, **kwargs): self.data.append(args) return [] ext = Extractor() imp = SingleOutputEcospold2Importer(FIXTURES, "ei", extractor=ext) assert imp.data == [] assert ext.data == [(FIXTURES, "ei")]
def test_importer_signals(): class SignalCatcher: def __init__(self): self.messages = [] def emit(self, *args): self.messages.append(args) catcher = SignalCatcher() bio = Database("biosphere3") bio.write({}) imp = SingleOutputEcospold2Importer(FIXTURES, "ei", signal=catcher) imp.apply_strategies() assert catcher.messages == [(i, 20) for i in range(1, 21)]
def run_import(self): if self.datasets_path is None: self.datasets_path = os.path.join(self.tempdir, 'datasets') try: importer = SingleOutputEcospold2Importer( self.datasets_path, self.db_name, extractor=ActivityBrowserExtractor, signal=import_signals.strategy_progress) importer.apply_strategies() importer.write_database(backend='activitybrowser') if not import_signals.cancel_sentinel: import_signals.finished.emit() else: self.delete_canceled_db() except ImportCanceledError: self.delete_canceled_db()
def get_ecoinvent(self, db_name=None, download_path=None, store_download=True, **kwargs): """ Download and import ecoinvent to FuturaDatabase. Sets :attr:`~db` directly Optional kwargs: :param db_name: name to give imported database. Default is downloaded filename :type db_name: str, optional :param download_path: path to download .7z file to default is download to temporary directory (.7z file is deleted after import) :type download_path: str, optional :param store_download: store the .7z file for later reuse, default is True, only takes effect if no download_path is provided :type store_download: bool, optional :param username: ecoinvent username :type username: str, optional :param password: ecoinvent password :type password: str, optional :param version: ecoinvent version, eg '3.5' :type version: str, optional :param system_model: ecoinvent system model, one of {'cutoff', 'apos', 'consequential'} :type system_model: str, optional :return: None :rtype: None """ username = kwargs.get('username') password = kwargs.get('password') version = str(kwargs.get('version')) system_model = kwargs.get('system_model') write_config = False if version and system_model: print( "Attempting to find stored version of ecoinvent {} {})".format( version, system_model)) check_database_name = "ecoinvent_{}{}".format( system_model, str(version).replace('.', '')) check = check_database(DEFAULT_SETUP_PROJECT, check_database_name) if check: print("Found an existing version - extracting that") self.extract_bw2_database(DEFAULT_SETUP_PROJECT, check_database_name) return if not username or not password: config = storage.config ecoinvent = config.get('ecoinvent') if not username: if ecoinvent: username = ecoinvent.get('username') if not password: if ecoinvent: password = ecoinvent.get('password') if not username: username = input('ecoinvent username: '******'username'] = username write_config = True if not password: password = getpass.getpass('ecoinvent password: '******'password'] = password write_config = True if write_config: storage.write_config(config) with tempfile.TemporaryDirectory() as td: if download_path is None: if store_download: download_path = storage.ecoinvent_dir else: download_path = td downloader = EcoinventDownloader(outdir=download_path, username=username, password=password, version=version, system_model=system_model) downloader.run() print('Extracting datasets to temporary directory {}'.format(td)) downloader.extract(target_dir=td) if not db_name: db_name = "ecoinvent_{}".format( downloader.file_name.replace('.7z', '')) datasets_path = os.path.join(td, 'datasets') importer = SingleOutputEcospold2Importer(datasets_path, db_name) current_project = projects.current projects.set_current(DEFAULT_SETUP_PROJECT) if 'biosphere3' not in databases: bw2setup() if not migrations: create_core_migrations() importer.apply_strategies() datasets, exchanges, unlinked = importer.statistics() if not unlinked: importer.write_database() projects.set_current(current_project) for ds in importer.data: if 'parameters' in ds.keys(): parameters, parameters_full = convert_parameters_to_wurst_style( ds['parameters']) ds['parameters'] = parameters ds['parameters full'] = parameters_full add_input_info_for_indigenous_exchanges(importer.data, [db_name]) if not unlinked: self.db.extend(importer.data) self.database_names.append(db_name)