Import configuration from a file
(self)
| 420 | del input_win |
| 421 | |
| 422 | def _import_config(self): |
| 423 | """Import configuration from a file""" |
| 424 | height, width = self.stdscr.getmaxyx() |
| 425 | |
| 426 | # Create input window |
| 427 | input_win = curses.newwin(5, width - 20, height // 2 - 2, 10) |
| 428 | input_win.box() |
| 429 | input_win.attron(curses.color_pair(2)) |
| 430 | input_win.addstr(0, 2, " Import Configuration ") |
| 431 | input_win.attroff(curses.color_pair(2)) |
| 432 | input_win.addstr(2, 2, "File:") |
| 433 | input_win.refresh() |
| 434 | |
| 435 | # Get input |
| 436 | curses.echo() |
| 437 | curses.curs_set(1) |
| 438 | |
| 439 | try: |
| 440 | filename = input_win.getstr(2, 8, width - 32).decode('utf-8').strip() |
| 441 | |
| 442 | if filename and os.path.isfile(filename): |
| 443 | try: |
| 444 | # Read config file |
| 445 | config = _configparser.ConfigParser() |
| 446 | config.read(filename) |
| 447 | |
| 448 | imported_count = 0 |
| 449 | |
| 450 | # Load values into fields |
| 451 | for tab in self.tabs: |
| 452 | for option in tab['options']: |
| 453 | dest = option['dest'] |
| 454 | |
| 455 | # Search for option in all sections |
| 456 | for section in config.sections(): |
| 457 | if config.has_option(section, dest): |
| 458 | value = config.get(section, dest) |
| 459 | |
| 460 | # Convert based on type |
| 461 | if option['type'] == 'bool': |
| 462 | option['value'] = value.lower() in ('true', '1', 'yes', 'on') |
| 463 | elif option['type'] == 'int': |
| 464 | try: |
| 465 | option['value'] = int(value) if value else None |
| 466 | except ValueError: |
| 467 | option['value'] = None |
| 468 | elif option['type'] == 'float': |
| 469 | try: |
| 470 | option['value'] = float(value) if value else None |
| 471 | except ValueError: |
| 472 | option['value'] = None |
| 473 | else: |
| 474 | option['value'] = value if value else None |
| 475 | |
| 476 | imported_count += 1 |
| 477 | break |
| 478 | |
| 479 | # Show success message |