2022-01-16 08:24:05 +08:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2014-08-20 07:03:48 +08:00
|
|
|
import argparse
|
2017-03-21 01:42:48 +08:00
|
|
|
import json
|
2024-10-12 07:30:07 +08:00
|
|
|
from collections.abc import Sequence
|
2021-02-17 04:38:57 +08:00
|
|
|
from typing import Any
|
|
|
|
|
|
|
|
|
|
|
|
def raise_duplicate_keys(
|
2022-01-16 08:24:05 +08:00
|
|
|
ordered_pairs: list[tuple[str, Any]],
|
|
|
|
) -> dict[str, Any]:
|
2021-02-17 04:38:57 +08:00
|
|
|
d = {}
|
|
|
|
for key, val in ordered_pairs:
|
|
|
|
if key in d:
|
|
|
|
raise ValueError(f'Duplicate key: {key}')
|
|
|
|
else:
|
|
|
|
d[key] = val
|
|
|
|
return d
|
2015-01-05 08:05:20 +08:00
|
|
|
|
2014-08-20 07:03:48 +08:00
|
|
|
|
2022-01-16 08:24:05 +08:00
|
|
|
def main(argv: Sequence[str] | None = None) -> int:
|
2014-08-20 07:03:48 +08:00
|
|
|
parser = argparse.ArgumentParser()
|
2019-02-12 11:56:15 +08:00
|
|
|
parser.add_argument('filenames', nargs='*', help='Filenames to check.')
|
2014-08-20 07:03:48 +08:00
|
|
|
args = parser.parse_args(argv)
|
|
|
|
|
|
|
|
retval = 0
|
|
|
|
for filename in args.filenames:
|
2020-02-06 03:10:42 +08:00
|
|
|
with open(filename, 'rb') as f:
|
|
|
|
try:
|
2021-02-17 04:38:57 +08:00
|
|
|
json.load(f, object_pairs_hook=raise_duplicate_keys)
|
2020-05-15 07:29:55 +08:00
|
|
|
except ValueError as exc:
|
2020-02-06 03:10:42 +08:00
|
|
|
print(f'{filename}: Failed to json decode ({exc})')
|
|
|
|
retval = 1
|
2014-08-20 07:03:48 +08:00
|
|
|
return retval
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2021-10-24 01:23:50 +08:00
|
|
|
raise SystemExit(main())
|