-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunmini.py
72 lines (66 loc) · 1.83 KB
/
unmini.py
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import argparse
import json
import pyperclip
def main():
cli_args = parse_args()
try:
unminified = None
if cli_args.clip:
try:
unminified = json.loads(pyperclip.paste())
except Exception as e:
print("No valid JSON found in clipboard")
if cli_args.verbose:
print(e)
return
elif cli_args.file:
try:
json_file = open(cli_args.file)
unminified = json.loads(json_file.read())
except Exception as e:
print("File provided is not a valid JSON file")
if cli_args.verbose:
print(e)
return
unminifiedPayload = json.dumps(unminified, indent = 2)
if not cli_args.no_copy:
pyperclip.copy(unminifiedPayload)
if cli_args.verbose:
print(unminifiedPayload)
except:
print("The string provided is not valid JSON.")
# Bootstraps the argparse library for providing readable docs & CLI argument handling
def parse_args():
parser = argparse.ArgumentParser(
prog="unminipy",
description="""
Unminify your JSON data for visibility. Provide valid JSON via your clipboard
or a local file, and its unminified form will be provided in your clipboard.
"""
)
parser.add_argument(
"-v",
"--verbose",
action="store_true",
help="print the unminified JSON after unminifying",
required=False
)
parser.add_argument(
"-n",
"--no-copy",
action="store_true",
help="do not update the clipboard with the unminified JSON",
required=False
)
standalone_args = parser.add_mutually_exclusive_group(required=True)
standalone_args.add_argument(
"--clip",
action="store_true",
help="use the data stored in the clipboard as the input"
)
standalone_args.add_argument(
"--file",
help="provide the JSON via a file"
)
return parser.parse_args()
main()