def selectXlsFiles(self): projFolder = toUnicode(self.projFolderEdit.text()) xlsFiles = QFileDialog.getOpenFileNames( directory=projFolder, filter='*.xls; *.xlsx', caption=toUnicode(MSG['msg06'])) textString = '' for xls in xlsFiles: textString = xls + ';' textString = textString[0: -1] self.xlsFilesEdit.setText(textString) items = list() itemNames = list() for xlsFile in xlsFiles: Species = readXls(xlsFile) for specie in Species: if toUnicode(specie[0][13]) not in itemNames: itemNames.append(toUnicode(specie[0][13])) specieListItem = specieItem(toUnicode(specie[0][13]), specie, self.specieListWidget) items.append(specieListItem) for item in items: self.specieListWidget.addItem(item) if items: self.specieListWidget.setCurrentRow(0) item = self.specieListWidget.currentItem() self.showAttribute(item)
def run_calc(self, calc_id=None, file_names=None, directory=None): """ Run a calculation. If `calc_id` is given, it means we want to run a calculation re-using the output of the given calculation """ text = self.tr('Select the files needed to run the calculation,' ' or the zip archive containing those files.') if directory is None: default_dir = QSettings().value('irmt/run_oqengine_calc_dir', QDir.homePath()) else: default_dir = directory if not file_names: file_names = QFileDialog.getOpenFileNames(self, text, default_dir) if not file_names: return if directory is None: selected_dir = QFileInfo(file_names[0]).dir().path() QSettings().setValue('irmt/run_oqengine_calc_dir', selected_dir) else: file_names = [os.path.join(directory, os.path.basename(file_name)) for file_name in file_names] if len(file_names) == 1: file_full_path = file_names[0] _, file_ext = os.path.splitext(file_full_path) if file_ext == '.zip': zipped_file_name = file_full_path else: # NOTE: an alternative solution could be to check if the single # file is .ini, to look for all the files specified in the .ini # and to build a zip archive with all them msg = "Please select all the files needed, or a zip archive" log_msg(msg, level='C', message_bar=self.message_bar) return else: _, zipped_file_name = tempfile.mkstemp() with zipfile.ZipFile(zipped_file_name, 'w') as zipped_file: for file_name in file_names: zipped_file.write(file_name) run_calc_url = "%s/v1/calc/run" % self.hostname with WaitCursorManager('Starting calculation...', self.message_bar): if calc_id is not None: # FIXME: currently the web api is expecting a hazard_job_id # although it could be any kind of job_id. This will have to be # changed as soon as the web api is updated. data = {'hazard_job_id': calc_id} else: data = {} files = {'archive': open(zipped_file_name, 'rb')} try: resp = self.session.post( run_calc_url, files=files, data=data, timeout=20) except HANDLED_EXCEPTIONS as exc: self._handle_exception(exc) return if resp.ok: self.refresh_calc_list() return resp.json() else: log_msg(resp.text, level='C', message_bar=self.message_bar)
def selectTECFile(self): Caption = 'Please select a _TEC_.dat file or multiple files' projFolder = toUnicode(self.dlg.projFolderEdit.text()) filePathes = QFileDialog.getOpenFileNames( self.dlg, Caption, os.path.join(projFolder, 'sim'), "*.dat") for path in filePathes: path = toUnicode(path) fileWidget = TECfile(self.dlg.fileListWidget, 0, toUnicode(path), self.iface) self.dlg.fileListWidget.addItem(fileWidget)
def select_files(self): """ Open a file browser to select multiple files in the ipt_dir, and return the list of names of selected files """ try: ipt_dir = self.parent().ipt_dir file_names = QFileDialog.getOpenFileNames(self.parent().parent(), 'Select files', ipt_dir) ls = [os.path.basename(file_name) for file_name in file_names] except Exception as exc: return {'ret': 1, 'content': None, 'reason': str(exc)} else: return {'ret': 0, 'content': ls, 'reason': 'ok'}
def select_and_copy_files_to_ipt_dir(self): """ Open a file browser pointing to the most recently browsed directory, where multiple files can be selected. The selected files will be copied inside the ipt_dir """ try: default_dir = QSettings().value('irmt/ipt_browsed_dir', QDir.homePath()) text = 'The selected files will be copied to the ipt directory' file_paths = QFileDialog.getOpenFileNames(self.parent().parent(), text, default_dir) if not file_paths: return {'ret': 1, 'reason': 'No file was selected'} selected_dir = QFileInfo(file_paths[0]).dir().path() QSettings().setValue('irmt/ipt_browsed_dir', selected_dir) ipt_dir = self.parent().ipt_dir for file_path in file_paths: basename = os.path.basename(file_path) copyfile(file_path, os.path.join(ipt_dir, basename)) except Exception as exc: return {'ret': 2, 'reason': str(exc)} else: return {'ret': 0, 'reason': 'ok'}