Mercurial > hg > lxmldump
comparison lxmldump.py @ 70:f887d8a68e5e
Cleanups, move argparser initialization into a function.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Thu, 09 Dec 2021 08:59:41 +0200 |
parents | d53f46341864 |
children | 8481f8f52a84 |
comparison
equal
deleted
inserted
replaced
69:7a77c78ff5b8 | 70:f887d8a68e5e |
---|---|
461 main_sense=msense, | 461 main_sense=msense, |
462 other_senses=pkk_get_list_str(indent + 1, senselist, "sense", False), | 462 other_senses=pkk_get_list_str(indent + 1, senselist, "sense", False), |
463 indent=pkk_geti(indent))) | 463 indent=pkk_geti(indent))) |
464 | 464 |
465 | 465 |
466 def pkk_get_argparser(): | |
467 optparser = argparse.ArgumentParser( | |
468 usage="%(prog)s [options] <input xml file(s)>", | |
469 add_help=False | |
470 ) | |
471 | |
472 optparser.add_argument("filenames", | |
473 type=str, action="extend", nargs="*", | |
474 metavar="filename", | |
475 help="XML filename(s)") | |
476 | |
477 optparser.add_argument("-h", "--help", | |
478 dest="show_help", | |
479 action="store_true", | |
480 help="show this help message") | |
481 | |
482 optparser.add_argument("-m", "--mode", | |
483 dest="mode", | |
484 action=pkk_set_mode, | |
485 default=PKK_MODE_NORMAL, | |
486 help="set output mode (see below)") | |
487 | |
488 optparser.add_argument("-s", "--set", | |
489 action=pkk_set_value, | |
490 metavar="ID=STR", | |
491 help='set format string (see below)') | |
492 | |
493 optparser.add_argument("-n", "--normalize", | |
494 dest="normalize", | |
495 action="store_true", | |
496 help="output NFC normalized Unicode") | |
497 | |
498 optparser.add_argument("-a", "--annotate", | |
499 dest="annotate", | |
500 action="store_true", | |
501 help="annotate strings") | |
502 | |
503 optparser.add_argument("-i", "--indent", | |
504 dest="indent", | |
505 type=functools.partial(pkk_arg_range, vmin=0, vmax=32), default=4, | |
506 metavar="N", | |
507 help='set indentation width (default: %(default)s)') | |
508 | |
509 optparser.add_argument("-p", "--debug", | |
510 dest="debug", | |
511 action="store_true", | |
512 help=argparse.SUPPRESS) | |
513 | |
514 return optparser | |
515 | |
516 | |
466 ### | 517 ### |
467 ### Main program starts | 518 ### Main program starts |
468 ### | 519 ### |
469 signal.signal(signal.SIGINT, pkk_signal_handler) | 520 if __name__ == "__main__": |
470 | 521 signal.signal(signal.SIGINT, pkk_signal_handler) |
471 optparser = argparse.ArgumentParser( | 522 |
472 usage="%(prog)s [options] <input xml file(s)>", | 523 ### Parse arguments |
473 add_help=False | 524 optparser = pkk_get_argparser() |
474 ) | 525 pkk_cfg = optparser.parse_args() |
475 | 526 |
476 optparser.add_argument("filenames", | 527 |
477 type=str, action="extend", nargs="*", | 528 ### Show help if needed |
478 metavar="filename", | 529 if len(pkk_cfg.filenames) == 0 or pkk_cfg.show_help: |
479 help="XML filename(s)") | 530 print("lxmldump - Convert and dump ISO/FDIS 1951 XML file data\n" |
480 | 531 "Programmed and designed by Matti 'ccr' Hämäläinen <ccr@tnsp.org>\n" |
481 optparser.add_argument("-h", "--help", | 532 "(C) Copyright 2021 Tecnic Software productions (TNSP)\n") |
482 dest="show_help", | 533 optparser.print_help() |
483 action="store_true", | 534 print(u"\nAvailable output modes:") |
484 help="show this help message") | 535 print(" " + ", ".join(pkk_modes_list.values())) |
485 | 536 |
486 optparser.add_argument("-m", "--mode", | 537 if pkk_cfg.mode not in [PKK_MODE_NORMAL, PKK_MODE_ANKI]: |
487 dest="mode", | 538 pkk_cfg.mode = PKK_MODE_NORMAL |
488 action=pkk_set_mode, | 539 |
489 default=PKK_MODE_NORMAL, | 540 print(u"\nAvailable format strings and values (mode '{}'):".format( |
490 help="set output mode (see below)") | 541 pkk_modes_list[pkk_cfg.mode])) |
491 | 542 |
492 optparser.add_argument("-s", "--set", | 543 for mid in pkk_mode_defaults: |
493 action=pkk_set_value, | 544 stmp = pkk_get_value(mid).replace("\\", "\\\\").replace("\n", "\\n") |
494 metavar="ID=STR", | 545 print(u" {:22s} : '{}'".format(mid, stmp)) |
495 help='set format string (see below)') | 546 |
496 | 547 sys.exit(0) |
497 optparser.add_argument("-n", "--normalize", | 548 |
498 dest="normalize", | 549 |
499 action="store_true", | 550 ### Handle each input file |
500 help="output NFC normalized Unicode") | 551 for filename in pkk_cfg.filenames: |
501 | 552 # Parse XML file into element tree |
502 optparser.add_argument("-a", "--annotate", | 553 try: |
503 dest="annotate", | 554 uxml = xmlET.parse(filename) |
504 action="store_true", | 555 except Exception as e: |
505 help="annotate strings") | 556 pkk_fatal(u"SVG/XML parsing failed: {0}".format(str(e))) |
506 | 557 |
507 optparser.add_argument("-i", "--indent", | 558 # Dump output |
508 dest="indent", | 559 try: |
509 type=functools.partial(pkk_arg_range, vmin=0, vmax=32), default=4, | 560 xroot = uxml.getroot() |
510 metavar="N", | 561 for dnode in xroot.findall("./DictionaryEntry"): |
511 help='set indentation width (default: %(default)s)') | 562 |
512 | 563 if pkk_cfg.debug and dnode.attrib["identifier"] not in pkk_debug_list: |
513 optparser.add_argument("-p", "--debug", | 564 continue |
514 dest="debug", | 565 |
515 action="store_true", | 566 if pkk_cfg.mode in [PKK_MODE_NORMAL, PKK_MODE_ANKI]: |
516 help=argparse.SUPPRESS) | 567 try: |
517 | 568 pkk_output_node(0, dnode) |
518 | 569 except Exception as e: |
519 ### Parse arguments | 570 print("") |
520 pkk_cfg = optparser.parse_args() | 571 pkk_dump_recursive(0, dnode) |
521 | 572 print(str(e)) |
522 | 573 sys.exit(0) |
523 ### Show help if needed | 574 elif pkk_cfg.mode == PKK_MODE_DUMP: |
524 if len(pkk_cfg.filenames) == 0 or pkk_cfg.show_help: | 575 pkk_dump_recursive(0, dnode) |
525 print("lxmldump - Convert and dump ISO/FDIS 1951 XML file data\n" | |
526 "Programmed and designed by Matti 'ccr' Hämäläinen <ccr@tnsp.org>\n" | |
527 "(C) Copyright 2021 Tecnic Software productions (TNSP)\n") | |
528 optparser.print_help() | |
529 print(u"\nAvailable output modes:") | |
530 print(" " + ", ".join(pkk_modes_list.values())) | |
531 | |
532 if pkk_cfg.mode not in [PKK_MODE_NORMAL, PKK_MODE_ANKI]: | |
533 pkk_cfg.mode = PKK_MODE_NORMAL | |
534 | |
535 print(u"\nAvailable format strings and values (mode '{}'):".format( | |
536 pkk_modes_list[pkk_cfg.mode])) | |
537 | |
538 for mid in pkk_mode_defaults: | |
539 stmp = pkk_get_value(mid).replace("\\", "\\\\").replace("\n", "\\n") | |
540 print(u" {:22s} : '{}'".format(mid, stmp)) | |
541 | |
542 sys.exit(0) | |
543 | |
544 | |
545 ### Handle each input file | |
546 for filename in pkk_cfg.filenames: | |
547 # Parse XML file into element tree | |
548 try: | |
549 uxml = xmlET.parse(filename) | |
550 except Exception as e: | |
551 pkk_fatal(u"SVG/XML parsing failed: {0}".format(str(e))) | |
552 | |
553 # Dump output | |
554 try: | |
555 xroot = uxml.getroot() | |
556 for dnode in xroot.findall("./DictionaryEntry"): | |
557 | |
558 if pkk_cfg.debug and dnode.attrib["identifier"] not in pkk_debug_list: | |
559 continue | |
560 | |
561 if pkk_cfg.mode in [PKK_MODE_NORMAL, PKK_MODE_ANKI]: | |
562 try: | |
563 pkk_output_node(0, dnode) | |
564 except Exception as e: | |
565 print("") | 576 print("") |
566 pkk_dump_recursive(0, dnode) | 577 elif pkk_cfg.mode == PKK_MODE_XML: |
567 print(str(e)) | 578 pkk_print(xmlET.tostring(dnode, encoding="utf8").decode("utf8") + "\n\n") |
568 sys.exit(0) | 579 else: |
569 elif pkk_cfg.mode == PKK_MODE_DUMP: | 580 pkk_fatal("Invalid operation mode?") |
570 pkk_dump_recursive(0, dnode) | 581 |
571 print("") | 582 except (BrokenPipeError, IOError) as e: |
572 elif pkk_cfg.mode == PKK_MODE_XML: | 583 sys.stderr.close() |
573 pkk_print(xmlET.tostring(dnode, encoding="utf8").decode("utf8") + "\n\n") | 584 sys.exit(1) |
574 else: | |
575 pkk_fatal("Invalid operation mode?") | |
576 | |
577 except (BrokenPipeError, IOError) as e: | |
578 sys.stderr.close() | |
579 sys.exit(1) | |
580 | |
581 sys.exit(0) |