| 1 | #!/usr/bin/env perl | ||||||||||||||||||
| 2 | |||||||||||||||||||
| 3 | 4 | my $copyright = <<'COPYRIGHT'; | |||||||||||||||||
| 4 | # Copyright 2021 by Christian Jaeger <ch@christianjaeger.ch> | ||||||||||||||||||
| 5 | # Published under the same terms as perl itself | ||||||||||||||||||
| 6 | COPYRIGHT | ||||||||||||||||||
| 7 | |||||||||||||||||||
| 8 | 4 | ▶ | use strict; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 9 | 4 | ▶ | use warnings; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 10 | 4 | ▶ | use warnings FATAL => 'uninitialized'; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 11 | 4 | ▶ | use experimental 'signatures'; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 12 | |||||||||||||||||||
| 13 | 4 | my ($email_full) = $copyright =~ / by ([^\n]*)/s; | |||||||||||||||||
| 14 | |||||||||||||||||||
| 15 | 4 | my ($mydir, $myname); | |||||||||||||||||
| 16 | |||||||||||||||||||
| 17 | 4 | ▶ | BEGIN { | ||||||||||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=7.2
| |||||||||||||||||||
| 18 | 4 | ▶ | $0 =~ /(.*?)([^\/]+)\z/s or die "?"; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 19 | 4 | ($mydir, $myname) = ($1, $2); | |||||||||||||||||
| 20 | } | ||||||||||||||||||
| 21 | 4 | ▶ | use lib "$mydir/../lib"; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 22 | |||||||||||||||||||
| 23 | 4 | ▶ | use FP::Text::CSV qw(csv_file_to_rows); | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 24 | 4 | ▶ | use FP::JSON qw(to_json); | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 25 | 4 | ▶ | use Getopt::Long; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 26 | 4 | ▶ | use FP::Hash qw(ziphash); | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 27 | 4 | ▶ | use FP::Div qw(identity); | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 28 | 4 | ▶ | use Chj::xIOUtil qw(stdout_utf8 stdin_utf8); | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 29 | |||||||||||||||||||
| 30 | 0 | ▶ | sub usage { | ||||||||||||||||
|
Subroutine
usage: not called CC=3 SCAR=24.8
| |||||||||||||||||||
| 31 | 0 | ▶ | print STDERR map {"$_\n"} @_ if @_; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 32 | 0 | print "$myname file.csv file.mint | |||||||||||||||||
| 33 | |||||||||||||||||||
| 34 | Convert CSV to JSON or Mint record literal syntax. It expects the | ||||||||||||||||||
| 35 | input file to have a title row, and uses the fields in that as the | ||||||||||||||||||
| 36 | field names (with Mint compatible mangling). | ||||||||||||||||||
| 37 | |||||||||||||||||||
| 38 | file.csv or file.mint can also be '-' for stdin or stdout, | ||||||||||||||||||
| 39 | respectively | ||||||||||||||||||
| 40 | |||||||||||||||||||
| 41 | Options: | ||||||||||||||||||
| 42 | --json output JSON (default) | ||||||||||||||||||
| 43 | --mint output Mint record literal syntax | ||||||||||||||||||
| 44 | --auto-numbers | ||||||||||||||||||
| 45 | treat strings that look like numbers as numbers | ||||||||||||||||||
| 46 | --auto-integers | ||||||||||||||||||
| 47 | strings that look like numbers and only have zeroes after | ||||||||||||||||||
| 48 | the dot are treated as integers; currently implies | ||||||||||||||||||
| 49 | --auto-numbers | ||||||||||||||||||
| 50 | --repl | ||||||||||||||||||
| 51 | open a repl instead of carrying out the identity | ||||||||||||||||||
| 52 | conversion | ||||||||||||||||||
| 53 | --conversion 'code' | ||||||||||||||||||
| 54 | code is Perl that must evaluate to a function that | ||||||||||||||||||
| 55 | receives the inputs and translate to the outputs. | ||||||||||||||||||
| 56 | Example code: | ||||||||||||||||||
| 57 | 'sub(\$records) { groupByNumber(\$records, \"Quadrat\")}' | ||||||||||||||||||
| 58 | or | ||||||||||||||||||
| 59 | 'sub(\$records) { groupByNumber(\$records, \"Quadrat\")->map(sub (\$group){ \$group->sort(on(hashkey(\"Frequency\"), \\&real_cmp))})}' | ||||||||||||||||||
| 60 | |||||||||||||||||||
| 61 | |||||||||||||||||||
| 62 | ($email_full) | ||||||||||||||||||
| 63 | "; | ||||||||||||||||||
| 64 | 0 | ▶ | exit(@_ ? 1 : 0); | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 65 | } | ||||||||||||||||||
| 66 | |||||||||||||||||||
| 67 | 4 | my $verbose = 0; | |||||||||||||||||
| 68 | 4 | our ($opt_json, $opt_mint, $opt_auto_numbers, $opt_auto_integers, $opt_repl, | |||||||||||||||||
| 69 | $opt_conversion); | ||||||||||||||||||
| 70 | GetOptions( | ||||||||||||||||||
| 71 | "verbose" => \$verbose, | ||||||||||||||||||
| 72 | 0 | ▶ | "help" => sub {usage}, | ||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||
| 73 | 4 | ▶ | "json" => \$opt_json, | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 74 | "mint" => \$opt_mint, | ||||||||||||||||||
| 75 | "auto-numbers" => \$opt_auto_numbers, | ||||||||||||||||||
| 76 | "auto-integers" => \$opt_auto_integers, | ||||||||||||||||||
| 77 | "repl" => \$opt_repl, | ||||||||||||||||||
| 78 | "conversion=s" => \$opt_conversion, | ||||||||||||||||||
| 79 | ) or exit 1; | ||||||||||||||||||
| 80 | 4 | ▶ | usage unless @ARGV == 2; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 81 | |||||||||||||||||||
| 82 | 4 | ▶ | our $output_format | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 83 | = $opt_json | ||||||||||||||||||
| 84 | ? ($opt_mint ? usage "both --json and --mint given" : "JSON") | ||||||||||||||||||
| 85 | : ($opt_mint ? "Mint" : "JSON"); | ||||||||||||||||||
| 86 | |||||||||||||||||||
| 87 | # Not sure this is ideal but people will be confused if giving | ||||||||||||||||||
| 88 | # --auto-integers and it doesn't do anything. | ||||||||||||||||||
| 89 | 4 | ▶ | $opt_auto_numbers = 1 if $opt_auto_integers; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 90 | |||||||||||||||||||
| 91 | 4 | my $settings = { | |||||||||||||||||
| 92 | output_format => $output_format, | ||||||||||||||||||
| 93 | auto_numbers => $opt_auto_numbers, | ||||||||||||||||||
| 94 | auto_integers => $opt_auto_integers, | ||||||||||||||||||
| 95 | }; | ||||||||||||||||||
| 96 | |||||||||||||||||||
| 97 | 4 | my ($file_csv, $file_mint) = @ARGV; | |||||||||||||||||
| 98 | |||||||||||||||||||
| 99 | #my $rows= csv_file_to_rows $file_csv; | ||||||||||||||||||
| 100 | |||||||||||||||||||
| 101 | 4 | ▶ | sub convertfile ($fn) { | ||||||||||||||||
|
Subroutine
convertfile: called CC=6 SCAR=18.7
| |||||||||||||||||||
| 102 | 4 | ▶ | my $csvinput = $file_csv eq "-" ? stdin_utf8 : $file_csv; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 103 | 4 | my $rows = csv_file_to_rows($csvinput, { sep_char => ",", eol => "\n" }); | |||||||||||||||||
| 104 | |||||||||||||||||||
| 105 | 4 | my $titles = $rows->first; | |||||||||||||||||
| 106 | |||||||||||||||||||
| 107 | 4 | my $body = $rows->rest; | |||||||||||||||||
| 108 | 4 | ▶ | my $body_hms = $body->map(sub ($row) { ziphash $titles, $row }); | ||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 109 | 4 | my $body_hms2 = $fn->($body_hms); | |||||||||||||||||
| 110 | |||||||||||||||||||
| 111 | my $mint = "[\n" . $body_hms2->map( | ||||||||||||||||||
| 112 | 8 | ▶ | sub ($v) { | ||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 113 | 8 | to_json $v, $settings | |||||||||||||||||
| 114 | } | ||||||||||||||||||
| 115 | 4 | )->strings_join(",\n") | |||||||||||||||||
| 116 | . "\n]\n"; | ||||||||||||||||||
| 117 | |||||||||||||||||||
| 118 | 4 | my $out; | |||||||||||||||||
| 119 | 4 | ▶ | if ($file_mint eq "-") { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 120 | 0 | $out = stdout_utf8; | |||||||||||||||||
| 121 | } else { | ||||||||||||||||||
| 122 | 4 | ▶ | open $out, ">:encoding(UTF-8)", $file_mint | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 123 | or die "can't open file for writing: '$file_mint'"; | ||||||||||||||||||
| 124 | } | ||||||||||||||||||
| 125 | 4 | ▶ | print $out $mint or die "print: $!"; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 126 | 4 | ▶ | close $out or die "close($file_mint): $!"; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 127 | } | ||||||||||||||||||
| 128 | |||||||||||||||||||
| 129 | 0 | ▶ | sub groupByNumber ($records, $key) { | ||||||||||||||||
|
Subroutine
groupByNumber: not called CC=1 SCAR=6.9
| |||||||||||||||||||
| 130 | 0 | $records->sort(on(hashkey($key), \&real_cmp)) | |||||||||||||||||
| 131 | ->group(on(hashkey($key), \&number_eq)) | ||||||||||||||||||
| 132 | } | ||||||||||||||||||
| 133 | |||||||||||||||||||
| 134 | 4 | ▶ | if ($opt_repl) { | ||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||
| 135 | 0 | require FP::Repl; | |||||||||||||||||
| 136 | 0 | FP::Repl::repl(); | |||||||||||||||||
| 137 | } elsif ($opt_conversion) { | ||||||||||||||||||
| 138 | 0 | my $function = eval $opt_conversion; | |||||||||||||||||
| 139 | 0 | ▶ | die $@ if $@; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 140 | 0 | ▶ | ref($function) eq "CODE" | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 141 | or die | ||||||||||||||||||
| 142 | "expecting --conversion's argument to evaluate to a function, but got: $function"; | ||||||||||||||||||
| 143 | 0 | convertfile($function); | |||||||||||||||||
| 144 | } else { | ||||||||||||||||||
| 145 | 4 | convertfile(\&identity); | |||||||||||||||||
| 146 | } | ||||||||||||||||||
| 147 | |||||||||||||||||||
| 148 | #use Chj::ruse; | ||||||||||||||||||
| 149 | #use Chj::Backtrace; | ||||||||||||||||||
| 150 | |||||||||||||||||||