def set_locate(self, locate: str): """Set environment variables.""" if locate == self.env: # If no changed. return self.env = locate logger.debug(f"~Set workplace to: [\"{self.env}\"]")
def save_reply_box(self, title: str, file_name: str): """Show message when successfully saved.""" size = QFileInfo(file_name).size() logger.debug("Size: " + ( f"{size / 1024 / 1024:.02f} MB" if size / 1024 // 1024 else f"{size / 1024:.02f} KB" )) QMessageBox.information( self, f"Initial Saved: {title}", f"Successfully saved:\n{file_name}" ) logger.info(f"Initial saved: [\"{file_name}\"]")
def run(self): """Start the algorithm loop.""" for name, path in self.mech_params['Target'].items(): logger.debug(f"- [P{name}] ({len(path)})") t0 = time() for self.current_loop in range(self.loop): logger.info( f"Algorithm [{self.current_loop + 1}]: {self.type_num}") if self.is_stop: # Cancel the remaining tasks. logger.info("Canceled.") continue self.result.emit(self.__algorithm()) logger.info(f"total cost time: {time() - t0:.02f} [s]") self.finished.emit()
def output_to(self, format_name: str, format_choose: Sequence[str]) -> str: """Simple to support multiple format.""" file_name, suffix = QFileDialog.getSaveFileName( self, f"Save to {format_name}...", self.env + '/' + self.database_widget.file_name.baseName(), ';;'.join(format_choose) ) if file_name: suffix = str_between(suffix, '(', ')').split('*')[-1] logger.debug(f"Format: {suffix}") if QFileInfo(file_name).completeSuffix() != suffix[1:]: file_name += suffix self.set_locate(QFileInfo(file_name).absolutePath()) return file_name
def read(self, file_name: str): """Load database commit.""" self.__connect_database(file_name) history_commit = CommitModel.select().order_by(CommitModel.id) commit_count = len(history_commit) if not commit_count: QMessageBox.warning(self, "Warning", "This file is a non-committed database.") return self.__clear_func() self.reset() self.history_commit = history_commit for commit in self.history_commit: self.__add_commit(commit) logger.debug(f"{commit_count} commit(s) was find in database.") self.__load_commit(self.history_commit.order_by(-CommitModel.id).get()) self.file_name = QFileInfo(file_name) self.__workbook_saved()
def __read_slvs(self, file_name: str): """Read slvs format. + Choose a group. + Read the entities of the group. """ parser = SlvsParser(file_name) if not parser.is_valid(): QMessageBox.warning( self, "Format error", "The format is not support." ) return groups = parser.get_groups() if not groups: QMessageBox.warning( self, "Format error", "The model file is empty." ) return group, ok = QInputDialog.getItem( self, "Solvespace groups", "Choose a group:\n" "(Please know that the group must contain a sketch only.)", ["@".join(g) for g in groups], 0, False ) if not ok: return self.clear() self.database_widget.reset() logger.debug(f"Read from group: {group}") self.parse_expression(parser.parse(group.split('@')[0]))
def input_from( self, format_name: str, format_choose: Sequence[str], multiple: bool = False ) -> str: """Get file name(s).""" args = ( f"Open {format_name} file{'s' if multiple else ''}...", self.env, ';;'.join(format_choose) ) if multiple: file_name_s, suffix = QFileDialog.getOpenFileNames(self, *args) else: file_name_s, suffix = QFileDialog.getOpenFileName(self, *args) if file_name_s: suffix = str_between(suffix, '(', ')').split('*')[-1] logger.debug(f"Format: {suffix}") if type(file_name_s) is str: self.set_locate(QFileInfo(file_name_s).absolutePath()) else: self.set_locate(QFileInfo(file_name_s[0]).absolutePath()) return file_name_s
def save(self, file_name: str, is_branch: bool = False): """Save database, append commit to new branch function.""" author_name = self.FileAuthor.text( ) or self.FileAuthor.placeholderText() branch_name = '' if is_branch else self.branch_current.text() commit_text = self.FileDescription.text() while not author_name: author_name, ok = QInputDialog.getText( self, "Author", "Please enter author's name:", QLineEdit.Normal, "Anonymous") if not ok: return while not branch_name.isidentifier(): branch_name, ok = QInputDialog.getText( self, "Branch", "Please enter a branch name:", QLineEdit.Normal, "master") if not ok: return while not commit_text: commit_text, ok = QInputDialog.getText(self, "Commit", "Please add a comment:", QLineEdit.Normal, "Update mechanism.") if not ok: return if (file_name != self.file_name.absoluteFilePath()) and isfile(file_name): os_remove(file_name) logger.debug("The original file has been overwritten.") self.__connect_database(file_name) is_error = False with _db.atomic(): if author_name in {user.name for user in UserModel.select()}: author_model = (UserModel.select().where( UserModel.name == author_name).get()) else: author_model = UserModel(name=author_name) if branch_name in {branch.name for branch in BranchModel.select()}: branch_model = (BranchModel.select().where( BranchModel.name == branch_name).get()) else: branch_model = BranchModel(name=branch_name) args = { 'author': author_model, 'description': commit_text, 'mechanism': _compress(self.__point_expr_func()), 'linkcolor': _compress(self.__link_expr_func()), 'storage': _compress(list(self.__storage_data_func())), 'pathdata': _compress(self.__path_data_func()), 'collectiondata': _compress(self.__collect_data_func()), 'triangledata': _compress(self.__triangle_data_func()), 'inputsdata': _compress( tuple((b, d) for b, d, a in self.__inputs_data_func())), 'algorithmdata': _compress(self.__algorithm_data_func()), 'branch': branch_model, } try: args['previous'] = (CommitModel.select().where( CommitModel.id == self.commit_current_id.value()).get()) except CommitModel.DoesNotExist: args['previous'] = None new_commit = CommitModel(**args) try: author_model.save() branch_model.save() new_commit.save() except Exception as error: logger.error(error) _db.rollback() is_error = True else: self.history_commit = CommitModel.select().order_by( CommitModel.id) if is_error: os_remove(file_name) logger.critical("The file was removed.") return self.read(file_name) logger.debug(f"Saving \"{file_name}\" successful.") size = QFileInfo(file_name).size() logger.debug("Size: " + (f"{size / 1024 / 1024:.02f} MB" if size / 1024 // 1024 else f"{size / 1024:.02f} KB"))