Skip to content
Closed
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
58 changes: 58 additions & 0 deletions bin/psql-vert
Original file line number Diff line number Diff line change
@@ -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'))
4 changes: 3 additions & 1 deletion lib/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ git+https://github.com/wieden-kennedy/haikus#egg=haikus
beautifulsoup4
websocket-client
tabulate
pyobjc
pyobjc
shapely

6 changes: 3 additions & 3 deletions lib/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down