-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathawkplot_cli.py
More file actions
182 lines (149 loc) · 6.88 KB
/
awkplot_cli.py
File metadata and controls
182 lines (149 loc) · 6.88 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
"""
awkplot — awk piped to uplot for terminal plots.
Usage: awkplot [awkplot-opts] [awk-opts] 'awk program' [file ...]
awkplot [awkplot-opts] [awk-opts] -f prog.awk [file ...]
"""
import argparse
import shlex
import shutil
import signal
import subprocess
import sys
PLOT_TYPES = ["hist", "bar", "line", "lineplot", "scatter", "density", "box", "count"]
def build_parser():
p = argparse.ArgumentParser(
prog="awkplot",
usage="%(prog)s [awkplot-opts] [awk-opts] 'program' [file ...]\n"
" %(prog)s [awkplot-opts] [awk-opts] -f prog.awk [file ...]",
description="Run an awk program and pipe its output to uplot for terminal visualization.",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""awk options:
-F SEP field separator (forwarded to awk)
-v VAR=VAL assign variable (repeatable, forwarded to awk)
-f PROGFILE read awk program from file (repeatable; first positional becomes a data file)
plot options:
-p TYPE plot type: hist bar line lineplot scatter density box count [default: hist]
-H treat first output row as column headers (uplot --header)
-c COLORS comma-separated color(s), e.g. red,blue (uplot --color)
-s H:W height:width, e.g. 20:60 (uplot --height / --width)
-t TITLE plot title (uplot --title)
-d DELIM output column delimiter passed to uplot (uplot --delimiter)
--dry-run print the awk | uplot command without running it
examples:
awkplot -p hist '{print $3}' data.tsv
awkplot -F, -H -p scatter -c cyan -s 20:60 '{print $2,$5}' data.csv
cat data | awkplot -v t=10 -p bar '$1>t {print $2,$3}'
awkplot -f prog.awk -p line data.tsv
""",
)
# ── awk passthrough flags ──────────────────────────────────────────────────
p.add_argument("-F", dest="field_sep", metavar="SEP",
help="awk field separator")
p.add_argument("-v", dest="awk_vars", metavar="VAR=VAL", action="append",
help="awk variable assignment (repeatable)")
p.add_argument("-f", dest="prog_files", metavar="PROGFILE", action="append",
help="read awk program from file (repeatable)")
# ── awkplot / uplot flags ─────────────────────────────────────────────────
p.add_argument("-p", "--plot", dest="plot_type", metavar="TYPE",
default="hist", choices=PLOT_TYPES,
help="plot type [default: hist]")
p.add_argument("-H", "--header", dest="header", action="store_true",
help="first output row is column headers")
p.add_argument("-c", "--colors", dest="colors", metavar="COLORS",
help="comma-separated color list, e.g. red,blue")
p.add_argument("-s", "--size", dest="size", metavar="H:W",
help="plot size as height:width, e.g. 20:60")
p.add_argument("-t", "--title", dest="title", metavar="TITLE",
help="plot title")
p.add_argument("-d", "--delimiter", dest="delimiter", metavar="DELIM",
help="output column delimiter for uplot")
p.add_argument("--dry-run", dest="dry_run", action="store_true",
help="print the command pipeline without executing")
# ── positionals ───────────────────────────────────────────────────────────
p.add_argument("args", nargs=argparse.REMAINDER,
help="awk program (first arg) then input files, or just files when -f is used")
return p
def parse_size(size_str):
"""Parse 'H:W' into (height, width) as strings."""
parts = size_str.split(":")
if len(parts) != 2 or not parts[0].strip() or not parts[1].strip():
sys.exit(f"awkplot: -s/--size must be H:W (e.g. 20:60), got: {size_str!r}")
h, w = parts[0].strip(), parts[1].strip()
if not h.isdigit() or not w.isdigit():
sys.exit(f"awkplot: -s/--size values must be integers, got: {size_str!r}")
return h, w
def check_deps():
missing = []
if not shutil.which("awk"):
missing.append("awk (brew install gawk)")
if not shutil.which("uplot"):
missing.append("uplot (brew install youplot)")
if missing:
sys.exit("awkplot: required tools not found on PATH:\n " + "\n ".join(missing))
def build_awk_cmd(ns):
cmd = ["awk"]
if ns.field_sep is not None:
cmd += ["-F", ns.field_sep]
for var in (ns.awk_vars or []):
cmd += ["-v", var]
for pf in (ns.prog_files or []):
cmd += ["-f", pf]
positionals = ns.args
if ns.prog_files:
# all positionals are input files
cmd += positionals
else:
# first positional is the awk program
if not positionals:
sys.exit("awkplot: awk program required as first positional argument\n"
" hint: awkplot [opts] 'awk program' [file ...]")
cmd.append(positionals[0])
cmd += positionals[1:]
return cmd
def build_uplot_cmd(ns):
cmd = ["uplot", ns.plot_type]
if ns.header:
cmd.append("--header")
if ns.colors:
for color in ns.colors.split(","):
color = color.strip()
if color:
cmd += ["--color", color]
if ns.size:
h, w = parse_size(ns.size)
cmd += ["--height", h, "--width", w]
if ns.title:
cmd += ["--title", ns.title]
if ns.delimiter:
cmd += ["--delimiter", ns.delimiter]
return cmd
def main():
parser = build_parser()
ns = parser.parse_args()
awk_cmd = build_awk_cmd(ns)
uplot_cmd = build_uplot_cmd(ns)
if ns.dry_run:
print(shlex.join(awk_cmd) + " | " + shlex.join(uplot_cmd))
sys.exit(0)
check_deps()
# ── execute pipeline ───────────────────────────────────────────────────────
# Ignore SIGPIPE in the parent so closing the write end doesn't crash us.
signal.signal(signal.SIGPIPE, signal.SIG_IGN)
try:
awk_proc = subprocess.Popen(awk_cmd, stdout=subprocess.PIPE)
uplot_proc = subprocess.Popen(uplot_cmd, stdin=awk_proc.stdout)
# Let awk_proc receive SIGPIPE if uplot exits early.
awk_proc.stdout.close()
uplot_rc = uplot_proc.wait()
awk_rc = awk_proc.wait()
except KeyboardInterrupt:
sys.exit(130)
except FileNotFoundError as e:
sys.exit(f"awkplot: {e}")
# Surface the first non-zero exit code, awk takes priority.
if awk_rc != 0:
sys.exit(awk_rc)
if uplot_rc != 0:
sys.exit(uplot_rc)
if __name__ == "__main__":
main()