示例#1
0
    def import_file(cls, file_name):
        """
        read con file and transform it to yaml structure

        return: if file have good format (boolean)
        """
        try:
            with open(file_name, 'r') as file_d:
                con = file_d.read()
            cls.document = parse_con(con)
            # find available file name
            base_name = os.path.splitext(os.path.basename(file_name))[0]
            cls.imported_file_name = base_name
            i = 1
            dir_path = cls.config.last_data_dir + os.path.sep
            while os.path.isfile(dir_path + cls.imported_file_name + '.yaml'):
                if i > 999:
                    break
                cls.imported_file_name = "{0}{1:03d}".format(base_name, i)
                i += 1
            cls.imported_file_name = dir_path + cls.imported_file_name + '.yaml'
            cls.curr_file = None
            cls.update()
            cls.document = fix_intendation(cls.document, cls.root)
            cls.update()
            cls.document, need_move_forward = fix_tags(cls.document, cls.root)
            cls.update()
            cls.document = rewrite_comments(con, cls.document, cls.root)
            cls.update()
            data = {'actions': [{'action': 'move-key-forward', 'parameters': {'path': '/system'}},
                                {'action': 'delete-key', 'parameters': {'path': '/system', 'deep': True}}]}
            for path in need_move_forward:
                data['actions'].append({'action': 'move-key-forward', 'parameters': {'path': path}})
            transformator = Transformator(None, data)
            cls.document = transformator.transform(cls.document, cls)
            cls.update_format()
            cls.changed = True
            return True
        except (RuntimeError, IOError) as err:
            if cls.main_window is not None:
                cls._report_error("Can't open import file", err)
            else:
                raise err
        except Exception as err:
            if cls.main_window is not None:
                cls._report_error("Can't import file from con format", err)
            else:
                raise err
        return False
示例#2
0
    def transform(cls, file):
        """Run transformation according rules in set file"""
        cls.update()
        text = cls.get_transformation_text(file)
        try:
            transformator = Transformator(text)
        except (ValueError, TransformationFileFormatError) as err:
            if cls.main_window is not None:
                cls._report_error("Can't decode transformation file", err)
            else:
                raise err
            return
        dialog = None
        res = True
        if cls.main_window is not None:
            import PyQt5.QtWidgets as QtWidgets
            from ui.dialogs import TranformationDetailDlg

            dialog = TranformationDetailDlg(transformator.name,
                                            transformator.description,
                                            transformator.old_version,
                                            cls.curr_format_file,
                                            transformator.new_version,
                                            transformator.new_version in cls.transformation_files,
                                            cls.main_window)
            res = QtWidgets.QDialog.Accepted == dialog.exec_()
        if res:
            try:
                cls.document = transformator.transform(cls.document, cls)
                if len(transformator.err)>0:
                    if cls.main_window is not None:
                        cls._report_notify(transformator.err)
                    else:
                        for err in transformator.err:
                            print(err)
            except TransformationFileFormatError as err :
                if cls.main_window is not None:
                    cls._report_error("Transformation format error", err)
                else:
                    raise err
                return
            if transformator.new_version in cls.transformation_files:
                cls.set_current_format_file(transformator.new_version)
            else:
                cls.update()