| 10 | |
| 11 | @with_default_category('Config Category') |
| 12 | class ConfigCommandSet(CommandSet, ArgumentsManager): |
| 13 | def __init__(self, args, section=None) -> None: |
| 14 | super().__init__() |
| 15 | if hasattr(cmd2.Cmd,'do_set'): |
| 16 | delattr(cmd2.Cmd, 'do_set') |
| 17 | self._section = section |
| 18 | if section is None: |
| 19 | self._config = Settings.setting |
| 20 | else: |
| 21 | self._section = section |
| 22 | self._config = Settings.setting[section] |
| 23 | |
| 24 | def _ask_overwrite(self, filename, choose='N'): |
| 25 | if choose.lower() != 'y': |
| 26 | AnsiPrint.print(locale.get("config.overwrite_file"), end='') |
| 27 | choose = input('') |
| 28 | if choose == '': |
| 29 | choose = 'y' |
| 30 | |
| 31 | if choose.lower() == 'y': |
| 32 | Settings.save_settings(filename, override=True) |
| 33 | AnsiPrint.print_locale("config.saved", filename=filename) |
| 34 | |
| 35 | parser = Cmd2ArgumentParser(add_help=locale.get("help.save_config")) |
| 36 | parser.add_argument("filename", help=locale.get("help.filename")) |
| 37 | |
| 38 | @with_argparser(parser) |
| 39 | def do_save(self, args): |
| 40 | try: |
| 41 | Settings.save_settings(args.filename, override=False) |
| 42 | except MapXploreException as e: |
| 43 | if not e.isError: |
| 44 | self._ask_overwrite(args.filename, 'n') |
| 45 | else: |
| 46 | AnsiPrint.print_error(e) |
| 47 | except Exception as ex: |
| 48 | AnsiPrint.print_error(ex) |
| 49 | |
| 50 | |
| 51 | @with_argparser(parser) |
| 52 | def do_load(self, args:cmd2.Statement): |
| 53 | try: |
| 54 | Settings.load_settings(args.filename) |
| 55 | AnsiPrint.print_locale("config.loaded", filename=args.filename) |
| 56 | except MapXploreException as e: |
| 57 | AnsiPrint.print_error(e) |
| 58 | |
| 59 | |
| 60 | |
| 61 | def do_unset(self, args:cmd2.Statement): |
| 62 | self.do_set(args) |
| 63 | |
| 64 | def complete_unset(self, text, line, idx, endx): |
| 65 | return self.complete_set(text, line, idx, endx) |
| 66 | |
| 67 | def complete_set(self, text, line, idx, endx): |
| 68 | completions = [] |
| 69 | for item in self._config.keys(): |