-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjson2toml
executable file
·53 lines (40 loc) · 1.17 KB
/
json2toml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/env python3
# SPDX-License-Identifier: WTFPL
# /// script
# dependencies = ["tomlkit"]
# ///
import argparse
import json
import locale
import signal
import sys
import tomlkit
def _parse_dict(d):
d = d.copy()
for k, v in list(d.items()):
if isinstance(v, str) and "\n" in v:
d[k] = tomlkit.string(v, multiline=True)
return d
def main():
locale.setlocale(locale.LC_ALL, "")
signal.signal(signal.SIGINT, signal.SIG_DFL)
signal.signal(signal.SIGPIPE, signal.SIG_DFL)
argparser = argparse.ArgumentParser(epilog="Outputs TOML to stdout")
argparser.add_argument(
"--multiline-strings", action="store_const", const=_parse_dict,
help="Use multiline syntax for strings with multiple lines",
)
argparser.add_argument(
"file", nargs="?", default="-",
help="Input file to read, or '-' for stdin (default)",
)
args = argparser.parse_args()
if args.file == "-":
fp = sys.stdin.buffer
else:
fp = open(args.file, "rb")
with fp:
d = json.load(fp, object_hook=args.multiline_strings)
tomlkit.dump(d, sys.stdout)
if __name__ == "__main__":
main()