bin/ir-irdb2wig

filter: Statementstmt 65.7% 75 / 114 Branchbran 42.5% 23 / 54 Conditioncond 60.0% 15 / 25 MC/DCmcdc 42.8% 6 / 14 Subroutinesub 76.4% 13 / 17 Podpod n/a totaltotal 58.9% 132 / 224 CC 30 SCAR 22.4
CC
30
Cov
59%
derive_meta
16.5
repo_path
12.0
kind_for_devicetype
12.0
CRAP 9.4
« blib/lib/Protocol/IR/Proto/NEC.pm ↑ Summary blib/lib/Protocol/IR/Format/JSON.pm »
1 #!/usr/bin/env perl
2 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 6 ▶use FindBin;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use lib "$FindBin::Bin/../lib";
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 6 ▶use Getopt::Long qw(GetOptions :config no_ignore_case);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 ▶use HTTP::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 6 ▶use Protocol::IR::Converter;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 6 my $USAGE = <<'USAGE';
14 ir-irdb2wig - convert an IRDB CSV file to a HAIR wig JSON file
15  
16 IRDB (https://github.com/probonopd/irdb) stores remote control codes as
17 CSV files named <manufacturer>/<devicetype>/<device>,<subdevice>.csv.
18 This tool converts such a file to HAIR's portable hair-wig/3 JSON format
19 (https://github.com/DAB-LABS/HAIR): one wig file, one remote, raw Pronto
20 hex as the signal payload.
21  
22 Usage:
23 ir-irdb2wig [options] <csv-file | url | device-path>
24  
25 The input may be:
26 a local CSV file ir-irdb2wig remote.csv
27 an http(s) URL to a CSV file ir-irdb2wig https://example.org/remote.csv
28 a path in the IRDB repository ir-irdb2wig Samsung/TV/7,7.csv
29 ir-irdb2wig codes/Samsung/TV/7,7.csv
30  
31 For a repository path the manufacturer and model/device are taken from the
32 path (as per the IRDB naming convention) and the file is downloaded.
33  
34 Options:
35 --base URL Base URL of the IRDB codes tree (default:
36 https://cdn.jsdelivr.net/gh/probonopd/irdb@master/codes)
37 --name NAME Remote name for the wig (default: "<brand> <model>")
38 --brand BRAND Manufacturer/brand (default: from a repository path)
39 --model MODEL Model (default: the "device,subdevice" file stem)
40 --kind KIND HAIR device kind slug, e.g. tv, soundbar, ac
41 --notes TEXT Free-form notes
42 --out FILE Write the wig to FILE instead of stdout
43 --overwrite Allow --out to overwrite an existing file
44 --quiet Suppress progress and skip warnings on stderr
45 -h, --help Show this help
46  
47 Examples:
48 ir-irdb2wig Samsung/TV/7,7.csv --out samsung_tv.wig.json
49 ir-irdb2wig remote.csv --brand Acme --model RM-100 --kind vcr
50 USAGE
51  
52 6 my %opt = (base => 'https://cdn.jsdelivr.net/gh/probonopd/irdb@master/codes');
53 GetOptions(
54 'base=s' => \$opt{base},
55 'name=s' => \$opt{name},
56 'brand=s' => \$opt{brand},
57 'model=s' => \$opt{model},
58 'kind=s' => \$opt{kind},
59 'notes=s' => \$opt{notes},
60 'out=s' => \$opt{out},
61 'overwrite!' => \$opt{overwrite},
62 'quiet!' => \$opt{quiet},
63 'help|h' => \$opt{help},
64 6 ▶) or usage_error();
Branch: 1 branch
BranchTrueFalseTotal
&GetOptions("base=s", \$opt{"base"}, "name=s", \$opt{"name"}, "brand=s", \$opt{"brand"}, "model=s", \$opt{"model"}, "kind=s", \$opt{"kind"}, "notes=s", \$opt{"notes"}, "out=s", \$opt{"out"}, "overwrite!", \$opt{"overwrite"}, "quiet!", \$opt{"quiet"}, "help|h", \$opt{"help"}) or usage_error() 6 0 6
65  
66 6 ▶if ($opt{help}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"help"}) 1 5 6
67 1 print $USAGE;
68 1 exit 0;
69 }
70 5 ▶usage_error() unless @ARGV == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless @ARGV == 1 1 4 5
71 4 ▶usage_error("--base must be an http(s) URL") if $opt{base} !~ m{^https?://};
Branch: 1 branch
BranchTrueFalseTotal
unless $opt{"base"} =~ m[^https?://] 0 4 4
72  
73 4 my ($csv_text, $src_meta) = fetch_input($ARGV[0], \%opt);
74  
75 4 my $converter = Protocol::IR::Converter->new();
76 4 my $codes = $converter->import_format('CSV', $csv_text);
77 4 ▶die "No decodable IR signals in the input (are the protocols supported?)\n"
Branch: 1 branch
BranchTrueFalseTotal
unless @$codes 0 4 4
78 unless @$codes;
79  
80 4 my $rows = count_rows($csv_text);
81 4 ▶if ($rows > @$codes) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rows > @$codes) 0 4 4
82 warn "Skipped " . ($rows - @$codes) . " of $rows rows "
83 . "(unsupported protocol or unparseable)\n"
84 0 ▶ unless $opt{quiet};
Branch: 1 branch
BranchTrueFalseTotal
unless $opt{"quiet"} 0 0 0
85 }
86  
87 4 my $meta = derive_meta(\%opt, $src_meta);
88 4 my $wig = $converter->export_codes('wig', $codes, %$meta);
89 4 write_output($wig, $opt{out}, $opt{overwrite});
90 3 exit 0;
91  
92 # ----------------------------------------------------------------------
93  
94 # Return (text, metadata) for the positional input: a local file, an
95 # http(s) URL, or a path inside the IRDB repository (downloaded).
96 4 ▶sub fetch_input {
Subroutine fetch_input: called CC=5 SCAR=24.1
97 4 my ($spec, $opt) = @_;
98  
99 4 ▶ if (-e $spec) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $spec) 4 0 4
100 4 ▶ open my $fh, '<:raw', $spec or die "Cannot open '$spec': $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:raw", $spec or die "Cannot open '${spec}': $!\n" 4 0 4
101 4 local $/;
102 4 my $text = <$fh>;
103 4 close $fh;
104 4 return ($text, {});
105 }
106  
107 0 my $url;
108 my %src_meta;
109 0 ▶ if ($spec =~ m{^https?://}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec =~ m[^https?://]) { } 0 0 0
110 0 $url = $spec;
111 } else {
112 0 my $path = repo_path($spec);
113 0 $url = $opt->{base} . '/' . $path;
114 0 %src_meta = %{ meta_from_path($path) };
115 0 ▶ warn "Fetching $url\n" unless $opt->{quiet};
Branch: 1 branch
BranchTrueFalseTotal
unless $opt->{"quiet"} 0 0 0
116 }
117 0 return (fetch_url($url), \%src_meta);
118 }
119  
120 # Normalize a repository path: tolerate a leading "codes/" (as in the
121 # index file) and a missing ".csv" extension.
122 0 ▶sub repo_path {
Subroutine repo_path: not called CC=3 SCAR=24.8
123 0 my ($spec) = @_;
124 0 my $path = $spec;
125 0 $path =~ s{^/}{};
126 0 $path =~ s{^codes/}{};
127 0 $path =~ s{/$}{};
128 0 ▶ $path .= '.csv' unless $path =~ /\.csv$/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $path =~ /\.csv$/i 0 0 0
129 0 ▶ die "Expected MANUFACTURER/DEVICETYPE/DEVICE,SUBDEVICE.csv, got: $spec\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $path =~ m[^[^/]+/[^/]+/[^/]+\.csv$]i 0 0 0
130 unless $path =~ m{^[^/]+/[^/]+/[^/]+\.csv$}i;
131 0 return $path;
132 }
133  
134 # Manufacturer and model/device from an IRDB path, per the repo's
135 # naming convention <manufacturer>/<devicetype>/<device>,<subdevice>.csv.
136 0 ▶sub meta_from_path {
Subroutine meta_from_path: not called CC=2 SCAR=17.9
137 0 my ($path) = @_;
138 0 my ($man, $devtype, $stem) = $path =~ m{^([^/]+)/([^/]+)/([^/]+)\.csv$}i;
139 0 my %meta = (brand => $man, model => $stem);
140 0 my $kind = kind_for_devicetype($devtype);
141 0 ▶ $meta{kind} = $kind if defined $kind;
Branch: 1 branch
BranchTrueFalseTotal
if defined $kind 0 0 0
142 0 return \%meta;
143 }
144  
145 # Map IRDB device-type directories to HAIR kind slugs; fall back to a
146 # lowercased slug when there is no known mapping.
147 0 my %KIND = (
148 TV => 'tv',
149 AC => 'ac',
150 AIRCONDITIONER => 'ac',
151 'AIR CONDITIONER' => 'ac',
152 AMPLIFIER => 'amplifier',
153 AMP => 'amplifier',
154 AUDIO => 'audio',
155 BLURAY => 'bluray',
156 'BLU-RAY' => 'bluray',
157 DVD => 'dvd',
158 DVR => 'dvr',
159 GAME => 'console',
160 'GAME CONSOLE' => 'console',
161 PROJECTOR => 'projector',
162 RECEIVER => 'receiver',
163 SAT => 'satellite',
164 SATELITE => 'satellite',
165 SATELLITE => 'satellite',
166 SOUNDBAR => 'soundbar',
167 STB => 'stb',
168 VCR => 'vcr',
169 );
170 0 ▶sub kind_for_devicetype {
Subroutine kind_for_devicetype: not called CC=3 SCAR=24.8
171 0 my ($devtype) = @_;
172 0 my $up = uc $devtype;
173 0 ▶ return $KIND{$up} if exists $KIND{$up};
Branch: 1 branch
BranchTrueFalseTotal
if exists $KIND{$up} 0 0 0
174 0 (my $slug = lc $devtype) =~ s/[^a-z0-9]+/_/g;
175 0 $slug =~ s/^_+|_+$//g;
176 0 ▶ return length($slug) ? $slug : undef;
Branch: 1 branch
BranchTrueFalseTotal
length $slug ? : 0 0 0
177 }
178  
179 0 ▶sub fetch_url {
Subroutine fetch_url: not called CC=3 SCAR=24.8
180 0 my ($url) = @_;
181 0 my $http = HTTP::Tiny->new(timeout => 60, verify_SSL => 1);
182 0 my $res = $http->get($url);
183 0 ▶ unless ($res->{success}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res->{"success"}) 0 0 0
184 0 ▶ if ($res->{status} == 599) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->{"status"} == 599) 0 0 0
185 0 die "Cannot fetch $url: $res->{reason}\n"
186 . " https URLs need IO::Socket::SSL (cpanm IO::Socket::SSL);\n"
187 . " alternatively pass --base with an http:// URL.\n";
188 }
189 0 die "Cannot fetch $url: $res->{status} $res->{reason}\n";
190 }
191 0 return $res->{content};
192 }
193  
194 # Count non-empty data rows (ignoring the header) so we can warn when
195 # rows were skipped during conversion.
196 4 ▶sub count_rows {
Subroutine count_rows: called CC=3 SCAR=11.8
197 4 my ($text) = @_;
198 4 my @lines = grep { /\S/ } split /\r?\n/, $text;
199 4 ▶ shift @lines if @lines && $lines[0] =~ /^functionname\b/i;
Branch: 1 branch
BranchTrueFalseTotal
if @lines and $lines[0] =~ /^functionname\b/i 4 0 4
Condition33%1/3
@lines and $lines[0] =~ /^functionname\b/i
!ll&&!rl&&r
004
MC/DC0%0/2
@lines and $lines[0] =~ /^functionname\b/i
@lines $lines[0] =~ /^functionname\b/i
Truth table33%1/3
A and B
A= @linesB= $lines[0] =~ /^functionname\b/i
ABresult
0X0
100
111
200 4 return scalar @lines;
201 }
202  
203 # Merge command-line metadata over what was derived from a repository
204 # path, and default the remote name from brand/model.
205 4 ▶sub derive_meta {
Subroutine derive_meta: called CC=13 SCAR=28.0
206 4 my ($opt, $src) = @_;
207 4 ▶ my $brand = $opt->{brand} // $src->{brand};
Condition33%1/3
$opt->{"brand"} // $src->{"brand"}
l!l&&r!l&&!r
400
MC/DC0%0/2
$opt->{"brand"} // $src->{"brand"}
$opt->{"brand"} $src->{"brand"}
Truth table33%1/3
A // B
A= $opt->{"brand"}B= $src->{"brand"}
ABresult
1X1
011
000
208 4 ▶ my $model = $opt->{model} // $src->{model};
Condition66%2/3
$opt->{"model"} // $src->{"model"}
l!l&&r!l&&!r
103
MC/DC50%1/2
$opt->{"model"} // $src->{"model"}
$opt->{"model"} $src->{"model"}
Truth table66%2/3
A // B
A= $opt->{"model"}B= $src->{"model"}
ABresult
1X1
011
000
209 my $name = $opt->{name}
210 4 ▶ // (($brand && $model) ? "$brand $model" : ($brand // 'Untitled'));
Branch: 1 branch
BranchTrueFalseTotal
$brand && $model ? : 0 3 3
Condition57%4/7
$opt->{"name"} // ($brand && $model ? "$brand $model" : $brand // "Untitled")
l!l
13
$brand and $model
!ll&&!rl&&r
030
$brand // "Untitled"
l!l
30
MC/DC100%1/1
$opt->{"name"} // ($brand && $model ? "$brand $model" : $brand // "Untitled")
$opt->{"name"}
MC/DC0%0/1
$brand // "Untitled"
$brand
Truth table100%2/2
A
A= $opt->{"name"}
Aresult
11
00
Truth table50%1/2
A
A= $brand
Aresult
11
00
211 4 my %meta = (name => $name, origin => 'converted:irdb');
212 4 ▶ $meta{brand} = $brand if defined $brand;
Branch: 1 branch
BranchTrueFalseTotal
if defined $brand 4 0 4
213 4 ▶ $meta{model} = $model if defined $model;
Branch: 1 branch
BranchTrueFalseTotal
if defined $model 1 3 4
214 4 ▶ my $kind = $opt->{kind} // $src->{kind};
Condition66%2/3
$opt->{"kind"} // $src->{"kind"}
l!l&&r!l&&!r
103
MC/DC50%1/2
$opt->{"kind"} // $src->{"kind"}
$opt->{"kind"} $src->{"kind"}
Truth table66%2/3
A // B
A= $opt->{"kind"}B= $src->{"kind"}
ABresult
1X1
011
000
215 4 ▶ $meta{kind} = $kind if defined $kind && length $kind;
Branch: 1 branch
BranchTrueFalseTotal
if defined $kind and length $kind 1 3 4
Condition66%2/3
defined $kind and length $kind
!ll&&!rl&&r
301
MC/DC50%1/2
defined $kind and length $kind
defined $kind length $kind
Truth table66%2/3
A and B
A= defined $kindB= length $kind
ABresult
0X0
100
111
216 4 ▶ $meta{notes} = $opt->{notes} if defined $opt->{notes};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opt->{"notes"} 0 4 4
217 4 return \%meta;
218 }
219  
220 4 ▶sub write_output {
Subroutine write_output: called CC=5 SCAR=16.1
221 4 my ($text, $out, $overwrite) = @_;
222 4 ▶ if (defined $out) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $out) { } 3 1 4
223 3 ▶ die "Refusing to overwrite '$out' (use --overwrite)\n"
Branch: 1 branch
BranchTrueFalseTotal
if -e $out and not $overwrite 1 2 3
Condition100%3/3
-e $out and not $overwrite
!ll&&!rl&&r
111
MC/DC100%2/2
-e $out and not $overwrite
-e $out not $overwrite
Truth table100%3/3
A and not(B)
A= -e $outB= not $overwrite
ABresult
0X0
100
111
224 if -e $out && !$overwrite;
225 2 ▶ open my $fh, '>:utf8', $out or die "Cannot write '$out': $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">:utf8", $out or die "Cannot write '${out}': $!\n" 2 0 2
226 2 print {$fh} $text;
227 2 close $fh;
228 } else {
229 1 binmode STDOUT, ':utf8';
230 1 print $text;
231 }
232 }
233  
234 1 ▶sub usage_error {
Subroutine usage_error: called CC=1 SCAR=0.0
235 1 print STDERR $USAGE;
236 1 exit 2;
237 }
« blib/lib/Protocol/IR/Proto/NEC.pm ↑ Summary blib/lib/Protocol/IR/Format/JSON.pm »