()
| 68 | |
| 69 | |
| 70 | def xml2json(): |
| 71 | parser = argparse.ArgumentParser(prog=PROGRAM_NAME, add_help=True, |
| 72 | description="decode a set of XML files to JSON.") |
| 73 | parser.usage = "%(prog)s [OPTION]... [FILE]...\n" \ |
| 74 | "Try '%(prog)s --help' for more information." |
| 75 | |
| 76 | parser.add_argument('-v', dest='verbosity', action='count', default=0, |
| 77 | help="increase output verbosity.") |
| 78 | parser.add_argument('--schema', type=str, metavar='PATH', |
| 79 | help="path or URL to an XSD schema.") |
| 80 | parser.add_argument('--version', type=xsd_version_number, default='1.0', |
| 81 | help="XSD schema validator to use (default is 1.0).") |
| 82 | parser.add_argument('-L', dest='locations', nargs=2, type=str, action='append', |
| 83 | metavar="URI/URL", help="schema location hint overrides.") |
| 84 | parser.add_argument('--converter', type=str, metavar='NAME', |
| 85 | help="use a different XML to JSON convention instead of " |
| 86 | "the default converter. Option value can be one of " |
| 87 | "{!r}.".format(tuple(CONVERTERS_MAP))) |
| 88 | parser.add_argument('--indent', type=int, default=None, |
| 89 | help="indentation for a pretty-printed JSON output " |
| 90 | "(default is the most compact representation)") |
| 91 | parser.add_argument('--lazy', action='store_true', default=False, |
| 92 | help="use lazy decoding mode (slower but use less memory).") |
| 93 | parser.add_argument('--defuse', metavar='(always, remote, never)', |
| 94 | type=defuse_data, default='remote', |
| 95 | help="when to defuse XML data, on remote resources for default.") |
| 96 | parser.add_argument('-o', '--output', type=str, default='.', |
| 97 | help="where to write the encoded XML files, current dir by default.") |
| 98 | parser.add_argument('-f', '--force', action="store_true", default=False, |
| 99 | help="do not prompt before overwriting.") |
| 100 | parser.add_argument('files', metavar='[XML_FILE ...]', nargs='+', |
| 101 | help="XML files to be decoded to JSON.") |
| 102 | |
| 103 | args = parser.parse_args() |
| 104 | |
| 105 | loglevel = get_loglevel(args.verbosity) |
| 106 | schema_class = XMLSchema if args.version == '1.0' else XMLSchema11 |
| 107 | converter = get_converter(args.converter) |
| 108 | if args.schema is not None: |
| 109 | schema = schema_class(args.schema, locations=args.locations, loglevel=loglevel) |
| 110 | else: |
| 111 | schema = None |
| 112 | |
| 113 | json_options = {} |
| 114 | if args.indent is not None and args.indent >= 0: |
| 115 | json_options['indent'] = args.indent |
| 116 | |
| 117 | base_path = pathlib.Path(args.output) |
| 118 | if not base_path.exists(): |
| 119 | base_path.mkdir() |
| 120 | elif not base_path.is_dir(): |
| 121 | raise XMLSchemaValueError(f"{str(base_path)!r} is not a directory") |
| 122 | |
| 123 | tot_errors = 0 |
| 124 | for xml_path in map(pathlib.Path, args.files): |
| 125 | json_path = base_path.joinpath(xml_path.name).with_suffix('.json') |
| 126 | if json_path.exists() and not args.force: |
| 127 | print(f"skip {str(json_path)}: the destination file exists!") |
searching dependent graphs…