Skip to content

Commit

Permalink
Merge b804be0 into 5fb04d7
Browse files Browse the repository at this point in the history
  • Loading branch information
haasad committed Aug 14, 2018
2 parents 5fb04d7 + b804be0 commit 9c57fee
Showing 1 changed file with 46 additions and 4 deletions.
50 changes: 46 additions & 4 deletions activity_browser/app/ui/wizards/db_import_wizard.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# -*- coding: utf-8 -*-
import os
import io
import tempfile
import subprocess
import zipfile

import bs4
import requests
Expand Down Expand Up @@ -94,9 +96,10 @@ class ImportTypePage(QtWidgets.QWizardPage):
def __init__(self, parent=None):
super().__init__(parent)
self.wizard = self.parent()
options = ['ecoinvent homepage',
'local 7z-archive',
'local directory with ecospold2 files']
options = ['Ecoinvent: download',
'Ecoinvent: local 7z-archive',
'Ecoinvent: local directory with ecospold2 files',
'Forwast: download']
self.radio_buttons = [QtWidgets.QRadioButton(o) for o in options]
self.option_box = QtWidgets.QGroupBox('Choose type of database import')
box_layout = QtWidgets.QVBoxLayout()
Expand All @@ -112,6 +115,9 @@ def __init__(self, parent=None):

def nextId(self):
option_id = [b.isChecked() for b in self.radio_buttons].index(True)
if option_id == 3:
self.wizard.import_type = 'forwast'
return self.wizard.pages.index(self.wizard.db_name_page)
if option_id == 2:
self.wizard.import_type = 'directory'
return self.wizard.pages.index(self.wizard.choose_dir_page)
Expand Down Expand Up @@ -227,6 +233,8 @@ def initializePage(self):
version = self.wizard.version
sys_mod = self.wizard.system_model
self.name_edit.setText(sys_mod + version.replace('.', ''))
elif self.wizard.import_type == 'forwast':
self.name_edit.setText('forwast')

def validatePage(self):
db_name = self.name_edit.text()
Expand Down Expand Up @@ -267,6 +275,10 @@ def initializePage(self):
self.path_label.setText(
'Path to 7z archive:<br><b>{}</b>'.format(
self.field('archivepath')))
elif self.wizard.import_type == 'forwast':
self.path_label.setText(
'Download forwast from https://lca-net.com/wp-content/uploads/forwast.bw2package.zip'
)
else:
self.path_label.setText(
'Ecoinvent version: <b>{}</b><br>Ecoinvent system model: <b>{}</b>'.format(
Expand Down Expand Up @@ -330,6 +342,7 @@ def __init__(self, parent=None):
self.download_thread = DownloadWorkerThread()
self.unarchive_thread_list = []
self.import_thread = ImportWorkerThread()
self.forwast_thread = ForwastWorkerThread()

@QtCore.pyqtSlot(str)
def unarchive_finished_check(self, extract_tempdir):
Expand All @@ -356,6 +369,17 @@ def initializePage(self):
elif self.wizard.import_type == 'archive':
self.archivepath = self.field('archivepath')
self.unarchive()
elif self.wizard.import_type == 'forwast':
self.unarchive_label.hide()
self.unarchive_progressbar.hide()
self.extraction_progressbar.setMaximum(1)
self.extraction_progressbar.setValue(1)
self.strategy_progressbar.setMaximum(1)
self.strategy_progressbar.setValue(1)
self.download_tempdir = tempfile.TemporaryDirectory()
db_name = self.field('db_name')
self.forwast_thread.update(self.download_tempdir.name, db_name)
self.forwast_thread.start()
else:
self.download_label.setVisible(True)
self.download_progressbar.setVisible(True)
Expand Down Expand Up @@ -392,7 +416,7 @@ def update_strategy_progress(self, i, tot):
def update_db_progress(self, i, tot):
self.db_progressbar.setMaximum(tot)
self.db_progressbar.setValue(i)
if i == tot:
if i == tot and tot != 0:
import_signals.finalizing.emit()

def update_finalizing(self):
Expand Down Expand Up @@ -515,6 +539,24 @@ def delete_canceled_db(self):
print(f'Database {self.db_name} deleted!')


class ForwastWorkerThread(ImportWorkerThread):
def __init__(self):
super().__init__()
self.forwast_url = 'https://lca-net.com/wp-content/uploads/forwast.bw2package.zip'

def run(self):
"""
adapted from pjamesjoyce/lcopt
"""
import_signals.db_progress.emit(0, 0)
response = requests.get(self.forwast_url)
forwast_zip = zipfile.ZipFile(io.BytesIO(response.content))
forwast_zip.extractall(self.dirpath)
bw.BW2Package.import_file(os.path.join(self.dirpath, 'forwast.bw2package'))
import_signals.db_progress.emit(1, 1)
import_signals.finished.emit()


class EcoinventLoginPage(QtWidgets.QWizardPage):
def __init__(self, parent=None):
super().__init__(parent)
Expand Down

0 comments on commit 9c57fee

Please sign in to comment.