def on_tableWidget_cellDoubleClicked(self, row=0, col=0): try: self.__explore_directory() except Exception as e: MSG = "Could explore directory" a99.get_python_logger().exception(MSG) a99.show_error("%s: %s" % (MSG, str(e)))
def __generic_save_as(self): """Returns False if user has cancelled operation, otherwise True.""" index = self.__get_index() editor, text, wild = self.editors[index], self.save_as_texts[ index], self.wilds[index] if not editor.f: return True if editor.f.filename: d = editor.f.filename else: d = os.path.join( self.save_dir if self.save_dir is not None else self.load_dir if self.load_dir is not None else ".", editor.f.default_filename) new_filename = QFileDialog.getSaveFileName(self, text, d, wild)[0] if new_filename: self.save_dir, _ = os.path.split(str(new_filename)) try: editor.f.save_as(str(new_filename)) self.flags_changed[index] = False self._update_labels_fn() self.__update_tab_texts() return True except Exception as e: a99.show_error(str(e)) raise return False
def __generic_open(self): index = self.__get_index() editor = self.editors[index] text = self.open_texts[index] cls = self.clss[index] label = self.labels_fn[index] # makes wildcard options a list and adds "*" if not present SEP = ";;" __wild = self.wilds[index] _wild = [x.strip() for x in __wild.split(SEP)] if "*" not in _wild or "*.*" not in _wild: _wild.append("*") wild = SEP.join(_wild) try: d = self.load_dir if self.load_dir is not None \ else self.save_dir if self.save_dir is not None \ else "." new_filename = QFileDialog.getOpenFileName(self, text, d, wild)[0] if new_filename: self.load_dir, _ = os.path.split(str(new_filename)) f = cls() f.load(str(new_filename)) editor.load(f) self._update_labels_fn() self.__update_tab_texts() except Exception as e: a99.show_error(str(e)) raise
def on_retry_failed(self): try: self.rm.retry_failed() except Exception as e: MSG = "Could not retry failed" a99.get_python_logger().exception(MSG) a99.show_error("%s: %s" % (MSG, str(e)))
def on_help(self, _): try: base_dir = os.path.dirname(sys.argv[0]) webbrowser.open_new(os.path.join(base_dir, "mled.html")) a99.show_message("Help file mled.html was opened in web browser.") except Exception as e: a99.show_error(str(e))
def on_save(self, _): self.disable_save_actions() try: self.save() except Exception as e: a99.show_error(str(e)) finally: self.enable_save_actions()
def plot_lines(self): try: self.clear_markers() o = self.sol if o is not None: self.figure.clear() n = sum([info.flag for info in self.plot_info]) # number of subplots (0, 1 or 2) # map to reuse plotting routine, contains what differs between each plot map_ = [(SOL_HEADERS_PLOT[i], o.__getattribute__(SOL_ATTR_NAMES[i])) for i in range(1, len(SOL_HEADERS_PLOT))] i_subplot = 1 for i in range(len(map_)): y_label = map_[i][0] pi = self.plot_info[i] pi.y_vector = _y = map_[i][1] if pi.flag: if not self.flag_sort: x = o.lmbdam y = _y else: _x = np.array(o.lmbdam) _y = np.array(_y) ii = np.argsort(_x) x = _x[ii] y = _y[ii] a99.format_BLB() self.figure.add_subplot(n, 1, i_subplot) pi.axis = ax = self.figure.gca() ax.clear() ax.plot(x, y, 'k'+('' if len(x) > 1 else 'x')) ax.set_xlabel('Wavelength ($\AA$)') ax.set_ylabel(y_label) # x-limits xmin, xmax = min(x), max(x) k = .02*(xmax-xmin) ax.set_xlim([xmin-k, xmax+k]) # y-limits ymin, ymax = min(y), max(y) k = .02*(ymax-ymin) ax.set_ylim([ymin-k, ymax+k]) i_subplot += 1 if i_subplot > 1: plt.tight_layout() self.canvas.draw() self.draw_markers() except Exception as e: a99.show_error("Error drawing plots: {}".format(str(e)))
def _can_calculate(self): """Shows error if the case. Returns True/False""" reason = None if self.moldb is None: reason = "I need a molecular constants database" elif self.molconsts is None: reason = "I am blank" if reason: a99.show_error(reason) return reason is None
def on_collect_errors(self): # TODO **duplicate code** export a_XExplorer's one and use because the latter is more complete try: k = a99.ErrorCollector() k.collect_errors(".") w = f311.XHTML(self, k.get_html(), "Errors in '.' and subdirectories") w.show() except Exception as e: MSG = "Could not collect errors" a99.get_python_logger().exception(MSG) a99.show_error("%s: %s" % (MSG, str(e)))
def on_save(self, _): self.disable_save_actions() try: if not self.editor.flag_valid: a99.show_error(PARAMS_INVALID) else: self.save() except Exception as e: a99.show_error(str(e)) raise finally: self.enable_save_actions()
def on_save_as(self, _): self.disable_save_actions() try: if self.f: new_filename = QFileDialog.getSaveFileName(self, "Save file", self.save_dir, "*.dat")[0] if new_filename: self.save_dir, _ = os.path.split(str(new_filename)) self.save_as(new_filename) except Exception as e: a99.show_error(str(e)) finally: self.enable_save_actions()
def view_hlf(self): """Opens text window with HLFs on them""" if self.moldb is None: a99.show_error( "Cannot view HLFs because I need a molecular constants database" ) elif self.molconsts is None: a99.show_error("Cannot view HLFs because I am blank") else: form = _XHLF(self, self.moldb, self.molconsts, self._get_fcf_dict()) form.show()
def on_tableWidget_cellChanged(self, row, column): if not self.flag_populating: item = self.tableWidget.item(row, column) try: value = float(item.text()) except ValueError: # restores original value a99.show_error("Invalid floating point value: %s" % item.text()) item.setText( str( self.parent.sol.__getattribute__( SOL_ATTR_NAMES[column])[row])) else: self.parent.MolLinesEditor_cell_changed(row, column, value)
def on_run_multi(self): import pyfant as pf errors = self._check_single_setup() if not self.multi_editor.f: errors.append("abundances X FWHM's configuration not set") else: # forces validation because validity is linked to FileAbonds managed in other tab self.multi_editor.f.validate() if not self.multi_editor.flag_valid: errors.append("error(s) in abundances X FWHM's configuration") if self.checkbox_multi_custom_id.isChecked(): s = self.__get_multi_custom_session_id() if len(s) == 0: errors.append("Please inform custom session id.") elif len( errors ) == 0: # will only offer to remove directory if everything is ok so far dirname = pyfant.get_custom_multisession_dirname(s) if os.path.isdir(dirname): r = QMessageBox.question( self, "Directory exists", "Directory '%s' already exists.\n\nWould you like to remove it?" % dirname, QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes) if r == QMessageBox.Yes: try: shutil.rmtree(dirname) except Exception as e: errors.append(str(e)) else: return if len(errors) == 0: try: self.setEnabled(False) self.__submit_multi() self._manager_form.show() except Exception as e: errors.append(str(e)) a99.get_python_logger().exception("Cannot submit multi-job") finally: self.setEnabled(True) if len(errors) > 0: a99.show_error("Cannot submit multi-job:\n - " + ("\n - ".join(errors)))
def on_save_as(self, _): self.disable_save_actions() try: if self.editor.f: if not self.editor.flag_valid: a99.show_error(PARAMS_INVALID) else: new_filename = QFileDialog.getSaveFileName(self, "Save file", self.save_dir, "*.dat") if new_filename: self.save_dir, _ = os.path.split(str(new_filename)) self.save_as(new_filename) except Exception as e: a99.show_error(str(e)) raise finally: self.enable_save_actions()
def _do_on_edit(self): continuum = self._get_continuum() name = continuum["name"] while True: r, form = a99.show_edit_form({"name": name}, None, "Edit continuum name") if not (r == QDialog.Accepted): return newname = form.get_kwargs()["name"] if any([cuum_["name"] == newname for cuum_ in self._continua if cuum_ != continuum]): a99.show_error("Name already exists") continue continuum["name"] = newname self.tableWidget.item(self._get_continuum_index(), 0).setText(newname) return True
def on_export_dissoc(self, _): self.disable_save_actions() try: if self.editor.f: if not self.editor.flag_valid: a99.show_error(PARAMS_INVALID) else: new_filename = QFileDialog.getSaveFileName(self, "Save file", os.path.join(".", pyfant.FileDissoc.default_filename), "*.dat")[0] if new_filename: f = self.editor.f.get_file_dissoc() f.title = "Created using abed.py" f.save_as(new_filename) except Exception as e: a99.show_error(str(e)) raise finally: self.enable_save_actions()
def __generic_save(self): """Returns False if user has cancelled a "save as" operation, otherwise True.""" index = self.__get_index() editor = self.editors[index] f = editor.f if not f: return True if not editor.flag_valid: a99.show_error("Cannot save, %s has error(s)!" % f.description) if f.filename: try: f.save_as() self.flags_changed[index] = False self.__update_tab_texts() return True except Exception as e: a99.show_error(str(e)) raise else: return self.__generic_save_as()
def _do_on_insert(self): name = a99.random_name() while True: params = a99.Parameters({"name": name}) form = a99.XParametersEditor(specs=params, title="Insert continuum") r = form.exec_() if not (r == QDialog.Accepted): return name = form.get_kwargs()["name"] if self._find_continuum_name(name) is not None: a99.show_error("Name already exists") continue self._continua.append(self._get_new_continuum(name)) continuum = self._get_continuum() self._populate() if continuum is not None: self._position_at_name(continuum["name"]) return True
def on_submit(self): flag_ok = True errors = self._check_single_setup() if len(errors) == 0: # more error checking if self.checkbox_custom_id.isChecked(): s = self.__get_custom_session_id() if len(s) == 0: errors.append("Please inform custom session id.") elif len( errors ) == 0: # will only offer to remove directory if everything is ok so far dirname = _get_custom_dirname(s) if os.path.isdir(dirname): r = QMessageBox.question( self, "Directory exists", "Directory '%s' already exists.\n\n" "Would you like to remove it?" % dirname, QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes) if r == QMessageBox.Yes: try: shutil.rmtree(dirname) except Exception as e: errors.append(str(e)) else: return if len(errors) == 0: try: self._manager_form.show() self._manager_form.raise_() self._manager_form.activateWindow() self.__submit_job() except Exception as e: errors.append(str(e)) a99.get_python_logger().exception("Cannot submit job") if len(errors) > 0: a99.show_error("Cannot submit job:\n - " + ("\n - ".join(errors)))
def on_tableWidget_cellChanged(self, row, column): if self.flag_process_changes: self.flag_process_changes = False try: t = self.tableWidget item = t.item(row, column) flag_done = False if column == 0: # Element try: item.setText(self._validate_element(row, item.text())) flag_done = True except Exception as E: a99.show_error(str(E)) if flag_done: self._update_file_abonds() finally: self.flag_process_changes = True self._update_file_abonds() self.changed.emit()