Created
March 13, 2019 18:33
-
-
Save danielecook/5d5f9a2cd32ff06ab05feb04f6418a28 to your computer and use it in GitHub Desktop.
nim error illegal capture
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sugar | |
import argparse | |
import colorize | |
import strformat | |
import strutils | |
import tables | |
proc print_error*(msg: string) = | |
stderr.write_line "Error".bgWhite.fgRed & fmt": {msg}".fgRed | |
import streams | |
proc infer_delim(header_line: string): string = | |
# Attempt to identify the delimiter | |
if header_line.split("\t").len > 1: | |
return "\t" | |
elif header_line.split(",").len > 1: | |
return "," | |
elif header_line.split("|").len > 1: | |
return "|" | |
proc parse_header(path: string, sep = ","): seq[string] = | |
var split_sep: string | |
var f = openFileStream(filename = path, mode = fmRead) | |
if not isNil(f): | |
var line = f.readLine() | |
if sep == "<auto>": | |
split_sep = infer_delim(line) | |
else: | |
split_sep = sep | |
var c: uint32 = 0 | |
result = lc[x.strip(chars = {'\"', '\''}) | (x <- line.split(split_sep)), string] | |
f.close() | |
proc stack(files: seq[string], sep: var string, output_sep: var string) = | |
var | |
stack_header: seq[string] | |
match_col: int | |
# Identify columns | |
for f in files: | |
for col in parse_header(f, sep): | |
if (col in stack_header) == false: | |
stack_header.add col | |
if output_sep in ["tab", "tabs", "\t"]: | |
output_sep = "\t" | |
# Look for file lists | |
#if files.len == 1 and files[0].endsWith(".list"): | |
# files = openFileStream(filename = files[0], mode = fmRead) | |
# Collate collumns | |
var header_out = false | |
for path in files: | |
# Print header | |
var header = parse_header(path) | |
var col_out = newSeq[string](stack_header.len) | |
var f = openFileStream(filename = path, mode = fmRead) | |
var ln = 0 | |
if not isNil(f): | |
var line = "" | |
while f.readLine(line): | |
if ln > 0: | |
var cols = lc[x.strip(chars = {'\"', '\''}) | (x <- line.split(sep)), string] | |
for ncol in 0..<header.len: | |
match_col = stack_header.find(header[ncol]) | |
if match_col > -1: | |
col_out[match_col] = cols[ncol] | |
stdout.write_line col_out.join(output_sep) | |
elif ln == 0: | |
if sep == "<auto>": | |
sep = infer_delim(line) | |
if output_sep == "<auto>": | |
output_sep = sep | |
if header_out == false: | |
# Infer line delimiter of first file and output header | |
stdout.write_line stack_header.join(output_sep) | |
header_out = true | |
ln += 1 | |
f.close() | |
var p = newParser("tbl"): | |
help("Table Utilities") | |
command("stack"): | |
arg("files", nargs= -1, help="List files to stack") | |
option("-t", "--threads", help="Threads") | |
option("-d", "--delimiter", help="What separater is used", default="<auto>") | |
option("-p", "--output-delimiter", help="Separater to output; Defaults to that found in first file", default="<auto>") | |
flag("-s", "--slugify", help="Slugify field names") | |
flag("-g", "--group", help="Add a column for the source field") | |
flag("--debug", help="Debug") | |
run: | |
if commandLineParams().len == 1: | |
stderr.write p.help() | |
elif opts.files.len == 0: | |
print_error("No files specified") | |
quit() | |
stack(opts.files, opts.delimiter, opts.outputDelimiter) | |
quit() | |
if commandLineParams().find("--help") > -1 or commandLineParams().find("-h") > -1 or commandLineParams().len == 0: | |
stderr.write p.help() | |
quit() | |
else: | |
var opts = p.parse(commandLineParams()) | |
p.run(commandLineParams()) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment