def save_as(self): """save file menu action""" if cfg.confront_file_timestamp(): return cfg.update_yaml_file(self.mainwindow.editor.text()) if cfg.curr_file is None: if cfg.imported_file_name is not None: new_file = cfg.imported_file_name else: new_file = cfg.config.data_dir + os.path.sep + "NewFile.yaml" else: new_file = cfg.curr_file dialog = QtWidgets.QFileDialog(self.mainwindow, 'Save as YAML File', new_file, "YAML Files (*.yaml)") dialog.setDefaultSuffix('.yaml') dialog.setFileMode(QtWidgets.QFileDialog.AnyFile) dialog.setAcceptMode(QtWidgets.QFileDialog.AcceptSave) dialog.setOption(QtWidgets.QFileDialog.DontConfirmOverwrite, False) dialog.setViewMode(QtWidgets.QFileDialog.Detail) if dialog.exec_(): self.autosave.delete_backup() file_name = dialog.selectedFiles()[0] cfg.save_as(file_name) self.mainwindow.update_recent_files() self._update_document_name() self.mainwindow.show_status_message("File is saved") return True return False
def save_old_file(self): """ If file not saved, display confirmation dialeg and if is needed, do it return: False if action have to be aborted """ cfg.update_yaml_file(self.mainwindow.editor.text()) if cfg.changed: msg_box = QMessageBox() msg_box.setWindowTitle("Confirmation") msg_box.setIcon(QMessageBox.Question) msg_box.setStandardButtons(QMessageBox.Yes | QMessageBox.No | QMessageBox.Abort) msg_box.setDefaultButton(QMessageBox.Abort) msg_box.setText("The document has unsaved changes, do you want to save it?") reply = msg_box.exec_() if reply == QtWidgets.QMessageBox.Abort: return False if reply == QtWidgets.QMessageBox.Yes: if cfg.curr_file is None: return self.save_as() else: self.save_file() if reply == QtWidgets.QMessageBox.No: self.autosave.delete_backup() return True
def main(): """Launches the export cli.""" parser = argparse.ArgumentParser( description='Export the YAML configuration file to CON format') parser.add_argument( '--destination-file', nargs='?', default=None, help='The destination file if is different from source file') parser.add_argument('--yaml_file', help='YAML input file', required=True) args = parser.parse_args() if args.destination_file: file = args.destination_file else: file = os.path.splitext( args.yaml_file)[0] + '.con' # replace extension if os.path.isfile(file): raise Exception("File already exists") cfg.init(None) if cfg.open_file(args.yaml_file): con_text = cfg.export_file() file_d = open(file, 'w') file_d.write(con_text) file_d.close()
def __init__(self): # load config cfg.init(None) # main window self._app = QtWidgets.QApplication(sys.argv) #print("Model app: ", str(self._app)) self._app.setWindowIcon(icon.get_app_icon("me-geomap")) self.mainwindow = MainWindow(self) cfg.main_window = self.mainwindow # set default values self._update_document_name() # show self.mainwindow.show() self.autosave = Autosave(cfg.config.CONFIG_DIR, lambda: cfg.curr_file, self.mainwindow.editor.text) """Object handling automatic saving""" self._restore_backup() if len(cfg.document) > 0: self.mainwindow.reload() self.mainwindow.editor.textChanged.connect(self.autosave.start_autosave) self.reloader_timer = QtCore.QTimer() """timer for file time checking in ms""" self.reloader_timer.timeout.connect(self.check_file) self.reloader_timer.start(RELOAD_INTERVAL)
def transform(self, to_version): """Run transformation to version to_version.""" cfg.update_yaml_file(self.mainwindow.editor.text()) cfg.transform(to_version) # synchronize cfg document with editor text self.mainwindow.editor.setText(cfg.document, keep_history=True) self.mainwindow.reload()
def open_set_file(self, file): """open set file""" cfg.open_file(file) self._restore_backup() self.mainwindow.reload() self.mainwindow.update_recent_files() self._update_document_name() self.mainwindow.show_status_message("File '" + file + "' is opened")
def select_format(self, filename): """Selects format file by filename.""" if cfg.curr_format_file == filename: return cfg.curr_format_file = filename cfg.update_format() self.mainwindow.reload() self.mainwindow.show_status_message("Format of file is changed")
def save_file(self): """save file menu action""" if cfg.curr_file is None: return self.save_as() if cfg.confront_file_timestamp(): return cfg.update_yaml_file(self.mainwindow.editor.text()) cfg.save_file() self.mainwindow.show_status_message("File is saved") self.autosave.delete_backup()
def open_recent(self, action): """open recent file menu action""" if action.data() == cfg.curr_file: return if not self.save_old_file(): return cfg.open_recent_file(action.data()) self._restore_backup() self.mainwindow.reload() self.mainwindow.update_recent_files() self._update_document_name() self.mainwindow.show_status_message("File '" + action.data() + "' is opened")
def reload(self): """reload panels after structure changes""" self._reload_icon.setVisible(True) self._reload_icon.update() self.editor.setUpdatesEnabled(False) cfg.update() self.editor.setUpdatesEnabled(True) self.editor.reload() self.tree.reload() self.err.reload() line, index = self.editor.getCursorPosition() self._reload_node(line+1, index+1) self._reload_icon_timer.start(700)
def import_file(self): """import con file menu action""" if not self.save_old_file(): return con_file = QtWidgets.QFileDialog.getOpenFileName( self.mainwindow, "Choose Con Model File", cfg.config.data_dir, "Con Files (*.con)") if con_file[0]: cfg.import_file(con_file[0]) self.mainwindow.reload() self.mainwindow.update_recent_files() self._update_document_name() self.mainwindow.show_status_message("File '" + con_file[0] + "' is imported")
def main(): """Launches the import cli.""" parser = argparse.ArgumentParser( description='Import the YAML configuration file from CON format') parser.add_argument('--transformation-name', nargs='*', help='Transformation rules contained in the Model Editor that are ' 'processed after import') parser.add_argument('--destination-file', nargs='?', default=None, help='The destination file if is different from source file') parser.add_argument('--con_file', help='CON input file', required=True) args = parser.parse_args() if args.destination_file: file = args.destination_file else: file = os.path.splitext(args.con_file)[0] + '.yaml' # replace extension if os.path.isfile(file): raise Exception("File already exists") cfg.init(None) cfg.set_current_format_file("1.8.3") cfg.import_file(args.con_file) if args.transformation_name is not None: for transf in args.transformation_name: cfg.transform_con(transf) file_d = open(file, 'w') file_d.write(cfg.document) file_d.close()
def open_file(self): """open file menu action""" if not self.save_old_file(): return yaml_file = QtWidgets.QFileDialog.getOpenFileName( self.mainwindow, "Choose Yaml Model File", cfg.config.data_dir, "Yaml Files (*.yaml)") if yaml_file[0]: cfg.open_file(yaml_file[0]) self._restore_backup() self.mainwindow.reload() self.mainwindow.update_recent_files() self._update_document_name() self.mainwindow.show_status_message("File '" + yaml_file[0] + "' is opened")
def edit_format(self): """Open selected format file in Json Editor""" text = cfg.get_curr_format_text() if text is not None: dlg = JsonEditorDlg(cfg.format_dir, cfg.curr_format_file, "Format", text, self.mainwindow) dlg.exec_()
def edit_transformation_file(self, file): """edit transformation rules in file""" text = cfg.get_transformation_text(file) if text is not None: dlg = JsonEditorDlg(cfg.transformation_dir, file, "Transformation rules:", text, self.mainwindow) dlg.exec_()
def _restore_backup(self): """recover file from backup file if it exists and if user wishes so""" if self.autosave.restore_backup(): cfg.document = cfg.read_file(self.autosave.backup_filename()) cfg.changed = True self.mainwindow.editor.reload() self.autosave.autosave_timer.stop()
def _reload_node(self, line, index): """reload info after changing node selection""" node = cfg.get_data_node(Position(line, index)) self.editor.set_new_node(node) cursor_type = self.editor.cursor_type_position self._update_info(cursor_type) if cfg.config.DEBUG_MODE: self.debug_tab.show_data_node(node)
def add_action(self, label, name, tip, slot): ac = QAction(label, self) shortcut = cfg.get_shortcut(name) if shortcut: ac.setShortcut(shortcut.key_sequence) ac.setStatusTip(tip) ac.triggered.connect(slot) self._actions[name] = ac self.addAction(ac)
def new_file(self): """new file menu action""" if not self.save_old_file(): return dialog = NewFileDialog(self.mainwindow, cfg.config.data_dir) if dialog.exec_() == dialog.Rejected: return cfg.new_file() for template in dialog.templates(): with open(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'resources', 'yaml_templates',template),'r') as file: cfg.document += file.read() cfg.save_as(dialog.get_file_name()) self.mainwindow.reload() self.mainwindow.update_recent_files(0) self._update_document_name() self.mainwindow.info_page.update_from_data({'record_id': cfg.root_input_type['id']}, False) self.mainwindow.show_status_message("New file is opened")
def test_meconfig_static(request): Config.SERIAL_FILE = "ModelEditorData_test" cfg.init(None) # cfg.config is assigned assert cfg.config.__class__ == Config def fin_test_config(): import gm_base.config gm_base.config.delete_config_file("ModelEditorData_test") request.addfinalizer(fin_test_config) cfg.format_files = [] cfg._read_format_files() # read format files assert '1.8.3' in cfg.format_files cfg.curr_format_file = None cfg.set_current_format_file('1.8.3') # good name assert cfg.curr_format_file == '1.8.3' cfg.set_current_format_file('bad_name') # bad name assert cfg.curr_format_file == '1.8.3' cfg.document = "#test" cfg.changed = True cfg.curr_file = "test" cfg.new_file() # new file test assert cfg.document == "" assert cfg.changed is False assert cfg.curr_file is None cfg.document = "#test" cfg.changed = True cfg.curr_file = "test.yaml" cfg.config.add_recent_file("test.yaml", "1.8.3") cfg.save_file() def fin_test_static(): import os if os.path.isfile("test.yaml"): os.remove("test.yaml") if os.path.isfile("test2.yaml"): os.remove("test2.yaml") request.addfinalizer(fin_test_static) # save file test assert cfg.changed is False assert cfg.curr_file == "test.yaml" assert cfg.config.recent_files[0] == "test.yaml" assert cfg.config.format_files[0] == "1.8.3" cfg.document = "#test2" cfg.changed = True cfg.save_as("test2.yaml") # save us test assert cfg.changed is False assert cfg.curr_file == "test2.yaml" assert cfg.config.recent_files[0] == "test2.yaml" assert cfg.config.format_files[0] == "1.8.3" assert cfg.config.recent_files[1] == "test.yaml" assert cfg.config.format_files[1] == "1.8.3" cfg.document = "#test2" cfg.changed = True cfg.set_current_format_file('1.8.3') cfg.open_file("test.yaml") # open file test assert cfg.changed is False assert cfg.curr_file == "test.yaml" assert cfg.document == "#test" assert cfg.config.recent_files[1] == "test2.yaml" assert cfg.config.format_files[1] == "1.8.3" assert cfg.config.recent_files[0] == "test.yaml" assert cfg.config.format_files[0] == "1.8.3" assert cfg.curr_format_file == '1.8.3' cfg.document = "" cfg.changed = True cfg.set_current_format_file('1.8.3') cfg.open_recent_file("test2.yaml") # open recent file test assert cfg.changed is False assert cfg.curr_file == "test2.yaml" assert cfg.document == "#test2" assert cfg.config.recent_files[0] == "test2.yaml" assert cfg.config.format_files[0] == "1.8.3" assert cfg.config.recent_files[1] == "test.yaml" assert cfg.config.format_files[1] == "1.8.3" assert cfg.curr_format_file == '1.8.3' cfg.update_yaml_file("#new test") # test update_yaml_file 1 assert cfg.changed == True assert cfg.document == "#new test" cfg.changed = False cfg.update_yaml_file("#new test") # test update_yaml_file 2 assert cfg.changed is False assert cfg.document == "#new test" # test document parsing cfg.document = "n: 1" cfg.update() assert cfg.root.children[0].value == 1
def test_config(request): Config.SERIAL_FILE = "ModelEditorData_test" cfg.init(None) cfg.config = Config() # cfg.config is assigned assert cfg.config.__class__ == Config def fin_test_config(): import gm_base.config gm_base.config.delete_config_file("ModelEditorData_test") request.addfinalizer(fin_test_config) import os cwd = os.getcwd() # current_working_dir for first opened config is cwd assert cwd == cfg.config.current_working_dir config = Config() # new config have current_working_dir == cwd assert cwd == config.current_working_dir cfg.config.add_recent_file("test_file1", "test_format_file1") # add first file assert cfg.config.recent_files[0] == "test_file1" assert cfg.config.format_files[0] == "test_format_file1" cfg.config.add_recent_file("test_file1", "test_format_file_new_1") # change only format assert cfg.config.format_files[0] == "test_format_file_new_1" assert len(cfg.config.format_files) == 1 cfg.config.add_recent_file("test_file2", "1.8.3") cfg.config.add_recent_file("test_file3", "test_format_file3") # add 3 files assert len(cfg.config.format_files) == 3 assert cfg.config.recent_files[0] == "test_file3" assert cfg.config.format_files[0] == "test_format_file3" assert cfg.config.recent_files[2] == "test_file1" assert cfg.config.format_files[2] == "test_format_file_new_1" cfg.config.add_recent_file("test_file2", "1.8.3") # move 2 to first line assert len(cfg.config.format_files) == 3 assert cfg.config.recent_files[0] == "test_file2" assert cfg.config.format_files[0] == "1.8.3" assert cfg.config.recent_files[1] == "test_file3" assert cfg.config.format_files[1] == "test_format_file3" assert cfg.config.recent_files[2] == "test_file1" assert cfg.config.format_files[2] == "test_format_file_new_1" # test get_format_file function assert cfg.config.get_format_file("test_file1") == "test_format_file_new_1" assert cfg.config.get_format_file("test_file2") == "1.8.3" config.update_current_working_dir("/home/test.yaml") # test update_current_working_dir assert config.current_working_dir == "/home" cfg.config.save() cfg.config.recent_files = [] cfg.config.format_files = [] cfg.config = Config().open() # save config assert len(cfg.config.format_files) == 3 assert cfg.config.recent_files[0] == "test_file2" assert cfg.config.format_files[0] == "1.8.3" assert cfg.config.recent_files[1] == "test_file3" assert cfg.config.format_files[1] == "test_format_file3" assert cfg.config.recent_files[2] == "test_file1" assert cfg.config.format_files[2] == "test_format_file_new_1"
def _structure_changed(self, line, column): """Editor structure change signal""" if cfg.update_yaml_file(self.editor.text()): self.reload() else: self._reload_node(line, column)
def check_file(self): """timer for file time checking in ms""" if self.mainwindow.isActiveWindow(): if cfg.confront_file_timestamp(): self.mainwindow.reload()
def _on_node_selected(self, line, column): """Handles nodeSelected event from editor.""" node = cfg.get_data_node(Position(line, column)) self.tree.select_data_node(node)
def on_change_file_format(self): """Handle change format action - display dialog.""" dialog = ChangeISTDialog(self.parent) if dialog.exec_(): cfg.update_format() cfg.main_window.reload()
def transform_get_version_list(self): """Returns list of versions available to transformation.""" return cfg.transform_get_version_list()
def set_empty_config(): Config.SERIAL_FILE = "ModelEditorData_test" cfg.config = Config() cfg.init(None)