Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,6 @@ env/

# Misc
.DS_Store

# pytest
.pytest_cache/
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2025 Michael Tisza

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ pip install .
| `-t TITLE` | Plot title |
| `-d DELIM` | Output column delimiter for uplot |
| `--dry-run` | Print the `awk \| uplot` command without running it |
| `--uplot-args ARGS` | Extra arguments passed through to uplot (quote the string) |
| `--version` | Show version and exit |
| `--help` | Show usage |

## Examples
Expand Down
68 changes: 55 additions & 13 deletions awkplot_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,24 @@
"""

import argparse
import os
import shlex
import shutil
import signal
import subprocess
import sys

__version__ = "0.1.0"

PLOT_TYPES = ["hist", "bar", "line", "lineplot", "scatter", "density", "box", "count"]

# Flags that belong to awkplot and should not appear after the awk program
_AWKPLOT_FLAGS = {
"-p", "--plot", "-H", "--header", "-c", "--colors",
"-s", "--size", "-t", "--title", "-d", "--delimiter",
"--dry-run", "--uplot-args", "--version",
}


def build_parser():
p = argparse.ArgumentParser(
Expand All @@ -34,6 +44,8 @@ def build_parser():
-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
--uplot-args extra arguments passed through to uplot (quote the string)
--version show version and exit

examples:
awkplot -p hist '{print $3}' data.tsv
Expand Down Expand Up @@ -69,6 +81,11 @@ def build_parser():
p.add_argument("--dry-run", dest="dry_run", action="store_true",
help="print the command pipeline without executing")

p.add_argument("--uplot-args", dest="uplot_args", metavar="ARGS",
help="extra arguments passed through to uplot (quote the string)")

p.add_argument("--version", action="version", version=f"%(prog)s {__version__}")

# ── positionals ───────────────────────────────────────────────────────────
p.add_argument("args", nargs=argparse.REMAINDER,
help="awk program (first arg) then input files, or just files when -f is used")
Expand Down Expand Up @@ -97,6 +114,20 @@ def check_deps():
sys.exit("awkplot: required tools not found on PATH:\n " + "\n ".join(missing))


def _check_swallowed_flags(positionals):
"""Error out if any positional looks like an awkplot flag that was
placed after the awk program (and therefore swallowed by REMAINDER)."""
for token in positionals:
if token in _AWKPLOT_FLAGS or (token.startswith("-") and not os.path.exists(token)):
# Heuristic: if a token starts with '-' and is not an existing
# file, it is almost certainly a misplaced flag.
sys.exit(
f"awkplot: unrecognised or misplaced option {token!r}\n"
" hint: all awkplot/uplot flags must come *before* the awk program\n"
" usage: awkplot [opts] 'awk program' [file ...]"
)


def build_awk_cmd(ns):
cmd = ["awk"]
if ns.field_sep is not None:
Expand All @@ -109,14 +140,19 @@ def build_awk_cmd(ns):
positionals = ns.args
if ns.prog_files:
# all positionals are input files
_check_swallowed_flags(positionals)
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:]
# Default to '{print}' so stdin-only invocations work
# (e.g. some_cmd | awkplot -p hist)
cmd.append("{print}")
else:
cmd.append(positionals[0])
files = positionals[1:]
_check_swallowed_flags(files)
cmd += files

return cmd

Expand All @@ -137,6 +173,8 @@ def build_uplot_cmd(ns):
cmd += ["--title", ns.title]
if ns.delimiter:
cmd += ["--delimiter", ns.delimiter]
if ns.uplot_args:
cmd += shlex.split(ns.uplot_args)
return cmd


Expand All @@ -155,25 +193,29 @@ def main():

# ── execute pipeline ───────────────────────────────────────────────────────
# Ignore SIGPIPE in the parent so closing the write end doesn't crash us.
signal.signal(signal.SIGPIPE, signal.SIG_IGN)
if hasattr(signal, "SIGPIPE"):
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()
awk_output, _ = awk_proc.communicate()
awk_rc = awk_proc.returncode

if awk_rc != 0:
sys.exit(awk_rc)

if not awk_output or not awk_output.strip():
sys.exit("awkplot: awk produced no output")

uplot_rc = uplot_proc.wait()
awk_rc = awk_proc.wait()
uplot_proc = subprocess.Popen(uplot_cmd, stdin=subprocess.PIPE)
uplot_proc.communicate(input=awk_output)
uplot_rc = uplot_proc.returncode

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)

Expand Down
Empty file added tests/__init__.py
Empty file.
Loading