From 626712efc26a071d3648da4ac87eb48f8f6e4d9d Mon Sep 17 00:00:00 2001 From: Alex Date: Mon, 20 Sep 2021 21:04:31 -0700 Subject: [PATCH 1/2] add `psql-vert` to print psql output vertically Also attempts to pretty-print json and ewkb strings --- bin/psql-vert | 58 ++++++++++++++++++++++++++++++++++++++++++++ lib/requirements.txt | 4 ++- 2 files changed, 61 insertions(+), 1 deletion(-) create mode 100755 bin/psql-vert diff --git a/bin/psql-vert b/bin/psql-vert new file mode 100755 index 0000000..cd9f049 --- /dev/null +++ b/bin/psql-vert @@ -0,0 +1,58 @@ +#!/usr/bin/env python + +# takes csv assumed to be the output of `psql --csv -c "select ..."` and +# prints it vertically while attempting to decode both json and EWKB strings + +# any arguments are assumed to be columns that will NOT be printed, either: +# - column names +# - postive indexes are 1-based ("2" is the 3rd column) +# - negative indexes per python rules ("-1" is the last column) + +import csv, sys, re, json, textwrap +from shapely import wkb +from tabulate import tabulate + +rows = [ x for x in csv.reader(sys.stdin.readlines()) ] +in_headers = rows.pop(0) + +show = [] +for i,h in enumerate(in_headers): + keep = True + for arg in sys.argv[1:]: + if re.match(r'^\-?\d+$', arg): + val = int(arg) + pos_index = (val-1 if val > 0 else val) % len(in_headers) + print(f'{arg} {pos_index}') + if pos_index == i: + keep = False + elif arg == h: + keep = False + + if keep: + show.append(i) + +out_headers = [ in_headers[i] for i in list(dict.fromkeys(show)) ] +out_headers.insert(0, '(count)') +def prettywkt(s): + # neither shapely nor osgeo do something like this, WTF? + s = re.sub(r'(\(+)', r'\1\n', s) + s = re.sub(r'(\)+)', r'\n\1', s) + s = re.sub(r',', r',\n', s) + return s + +for count, row in enumerate(rows): + row = [ row[i] for i in show ] + for i, cell in enumerate(row): + if re.match(r'^[{\[].*[}\]]$', cell): + row[i] = json.dumps(json.loads(cell), indent=4) + elif re.match(r'^01[a-f0-9]+$', cell, flags=re.I): + try: + shape = wkb.loads(bytes.fromhex(cell)) + row[i] = prettywkt(shape.wkt) + except e: + sys.stderr.write(e.message + '\n') + else: + row[i] = textwrap.fill(cell, width=70) + + row.insert(0, count+1) + print(tabulate(list(zip(*[out_headers,row])), tablefmt='psql')) diff --git a/lib/requirements.txt b/lib/requirements.txt index f81fe1d..8ea0a55 100644 --- a/lib/requirements.txt +++ b/lib/requirements.txt @@ -4,4 +4,6 @@ git+https://github.com/wieden-kennedy/haikus#egg=haikus beautifulsoup4 websocket-client tabulate -pyobjc \ No newline at end of file +pyobjc +shapely + From 33d9326b5d34a78b8a7d87462bda6ef8de7b73c0 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Tue, 21 Sep 2021 04:06:40 +0000 Subject: [PATCH 2/2] Bump nth-check from 2.0.0 to 2.0.1 in /lib Bumps [nth-check](https://github.com/fb55/nth-check) from 2.0.0 to 2.0.1. - [Release notes](https://github.com/fb55/nth-check/releases) - [Commits](https://github.com/fb55/nth-check/compare/v2.0.0...v2.0.1) --- updated-dependencies: - dependency-name: nth-check dependency-type: indirect ... Signed-off-by: dependabot[bot] --- lib/yarn.lock | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/yarn.lock b/lib/yarn.lock index e1436aa..43686ce 100644 --- a/lib/yarn.lock +++ b/lib/yarn.lock @@ -4631,9 +4631,9 @@ npm-run-path@^2.0.0: set-blocking "~2.0.0" nth-check@^2.0.0: - version "2.0.0" - resolved "https://registry.yarnpkg.com/nth-check/-/nth-check-2.0.0.tgz#1bb4f6dac70072fc313e8c9cd1417b5074c0a125" - integrity sha512-i4sc/Kj8htBrAiH1viZ0TgU8Y5XqCaV/FziYK6TBczxmeKm3AEFWqqF3195yKudrarqy7Zu80Ra5dobFjn9X/Q== + version "2.0.1" + resolved "https://registry.yarnpkg.com/nth-check/-/nth-check-2.0.1.tgz#2efe162f5c3da06a28959fbd3db75dbeea9f0fc2" + integrity sha512-it1vE95zF6dTT9lBsYbxvqh0Soy4SPowchj0UBGj/V6cTPnXXtQOPUbhZ6CmGzAD/rW22LQK6E96pcdJXk4A4w== dependencies: boolbase "^1.0.0"