blib/lib/App/CSelUtils.pm

filter: Statementstmt 15.1% 29 / 192 Branchbran 0.0% 0 / 68 Conditioncond 0.0% 0 / 28 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 31.8% 7 / 22 Podpod 66.6% 2 / 3 totaltotal 11.6% 38 / 327 CC 57 SCAR 63.3
CC
57
Cov
10%
foosel
1200.3
__ANON__
30.0
__ANON__
20.0
CRAP 560.5
↑ Summary
1 package App::CSelUtils;
2  
3 1 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use Data::Dmp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Scalar::Util qw(refaddr);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
11 1 our $DATE = '2022-10-17'; # DATE
12 1 our $DIST = 'App-CSelUtils'; # DIST
13 1 our $VERSION = '0.089'; # VERSION
14  
15 1 our %SPEC;
16  
17 # arguments for utilities like orgsel, htmlsel
18  
19 our %foosel_args_common = (
20 select_action => {
21 summary => 'Specify how we should select nodes',
22 schema => ['str*', in=>['csel', 'root']],
23 default => 'csel',
24 description => <<'_',
25  
26 The default is `csel`, which will select nodes from the tree using the CSel
27 expression. Note that the root node itself is not included. For more details on
28 CSel expression, refer to <pm:Data::CSel>.
29  
30 `root` will return a single node which is the root node.
31  
32 _
33 cmdline_aliases => {
34 0 ▶ root => {is_flag=>1, summary=>'Shortcut for --select-action=root', code=>sub {$_[0]{select_action} = 'root'}},
Subroutine __ANON__: not called CC=1 SCAR=6.9
35 },
36 },
37 expr => {
38 schema => 'str*',
39 pos => 1,
40 },
41 file => {
42 schema => 'filename*',
43 pos => 0,
44 default => '-',
45 },
46 node_actions => {
47 summary => 'Specify action(s) to perform on matching nodes',
48 'x.name.is_plural' => 1,
49 schema => ['array*', {
50 of => ['str*', {
51 match => qr/\A(dump(:\w+(\.\w+)*)?|eval:.+|print_as_string|print_method:\w+(\.\w+)*|count)\z/,
52 }],
53 }],
54 default => ['print_as_string'],
55 cmdline_aliases => {
56 print => {
57 summary => 'Shortcut for --node-action print_as_string',
58 is_flag => 1,
59 0 ▶ code => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
60 0 my $args = shift;
61 0 ▶ $args->{node_actions} //= [];
Condition0%0/2
$args->{"node_actions"} //= []
l!l
00
MC/DC0%0/1
$args->{"node_actions"} //= []
$args->{"node_actions"}
Truth table0%0/2
A
A= $args->{"node_actions"}
Aresult
11
00
62 0 my $actions = $args->{node_actions};
63 0 ▶ unless (grep {$_ eq 'print_as_string'} @$actions) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$_ eq "print_as_string";} @$actions) 0 0 0
64 0 push @$actions, 'print_as_string';
65 }
66 },
67 },
68 count => {
69 summary => 'Shortcut for --node-action count',
70 is_flag => 1,
71 0 ▶ code => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
72 0 my $args = shift;
73 0 ▶ $args->{node_actions} //= [];
Condition0%0/2
$args->{"node_actions"} //= []
l!l
00
MC/DC0%0/1
$args->{"node_actions"} //= []
$args->{"node_actions"}
Truth table0%0/2
A
A= $args->{"node_actions"}
Aresult
11
00
74 0 my $actions = $args->{node_actions};
75 0 ▶ unless (grep {$_ eq 'count'} @$actions) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$_ eq "count";} @$actions) 0 0 0
76 0 push @$actions, 'count';
77 }
78 },
79 },
80 dump => {
81 summary => 'Shortcut for --node-action dump',
82 is_flag => 1,
83 0 ▶ code => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
84 0 my $args = shift;
85 0 ▶ $args->{node_actions} //= [];
Condition0%0/2
$args->{"node_actions"} //= []
l!l
00
MC/DC0%0/1
$args->{"node_actions"} //= []
$args->{"node_actions"}
Truth table0%0/2
A
A= $args->{"node_actions"}
Aresult
11
00
86 0 my $actions = $args->{node_actions};
87 0 ▶ unless (grep {$_ eq 'dump'} @$actions) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$_ eq "dump";} @$actions) 0 0 0
88 0 push @$actions, 'dump';
89 }
90 },
91 },
92 eval => {
93 summary => '--eval E is shortcut for --action eval:E',
94 0 ▶ code => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
95 0 my ($args, $val) = @_;
96 0 ▶ $args->{node_actions} //= [];
Condition0%0/2
$args->{"node_actions"} //= []
l!l
00
MC/DC0%0/1
$args->{"node_actions"} //= []
$args->{"node_actions"}
Truth table0%0/2
A
A= $args->{"node_actions"}
Aresult
11
00
97 0 push @{ $args->{node_actions} }, "eval:$val";
98 },
99 },
100 e => {
101 summary => '-e E is shortcut for --action eval:E',
102 0 ▶ code => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
103 0 my ($args, $val) = @_;
104 0 ▶ $args->{node_actions} //= [];
Condition0%0/2
$args->{"node_actions"} //= []
l!l
00
MC/DC0%0/1
$args->{"node_actions"} //= []
$args->{"node_actions"}
Truth table0%0/2
A
A= $args->{"node_actions"}
Aresult
11
00
105 0 push @{ $args->{node_actions} }, "eval:$val";
106 },
107 },
108 print_method => {
109 summary => '--print-method M is shortcut for --node-action print_method:M',
110 0 ▶ code => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
111 0 my ($args, $val) = @_;
112 0 ▶ $args->{node_actions} //= [];
Condition0%0/2
$args->{"node_actions"} //= []
l!l
00
MC/DC0%0/1
$args->{"node_actions"} //= []
$args->{"node_actions"}
Truth table0%0/2
A
A= $args->{"node_actions"}
Aresult
11
00
113 0 push @{ $args->{node_actions} }, "print_method:$val";
114 },
115 },
116 },
117 description => <<'_',
118  
119 Each action can be one of the following:
120  
121 * `count` will print the number of matching nodes.
122  
123 * `print_method` will call on or more of the node object's methods and print the
124 result. Example:
125  
126 print_method:as_string
127  
128 * `dump` will show a indented text representation of the node and its
129 descendants. Each line will print information about a single node: its class,
130 followed by the value of one or more attributes. You can specify which
131 attributes to use in a dot-separated syntax, e.g.:
132  
133 dump:tag.id.class
134  
135 which will result in a node printed like this:
136  
137 HTML::Element tag=p id=undef class=undef
138  
139 By default, if no attributes are specified, `id` is used. If the node class does
140 not support the attribute, or if the value of the attribute is undef, then
141 `undef` is shown.
142  
143 * `eval` will execute Perl code for each matching node. The Perl code will be
144 called with arguments: `($node)`. For convenience, `$_` is also locally set to
145 the matching node. Example in <prog:htmlsel> you can add this action:
146  
147 eval:'print $_->tag'
148  
149 which will print the tag name for each matching <pm:HTML::Element> node.
150  
151 _
152 },
153 node_actions_on_descendants => {
154 summary => 'Specify how descendants should be actioned upon',
155 schema => ['str*', in=>['', 'descendants_depth_first']],
156 default => '',
157 description => <<'_',
158  
159 This option sets how node action is performed (See `node_actions` option).
160  
161 When set to '' (the default), then only matching nodes are actioned upon.
162  
163 When set to 'descendants_depth_first', then after each matching node is actioned
164 upon by an action, the descendants of the matching node are also actioned, in
165 depth-first order. This option is sometimes necessary e.g. when your node's
166 `as_string()` method shows a node's string representation that does not include
167 its descendants.
168  
169 _
170 0 ▶ cmdline_aliases => {R=>{is_flag=>1, summary=>'Shortcut for --node-action-on-descendants=descendants_depth_first', code=>sub { $_[0]{node_actions_on_descendants} = 'descendants_depth_first'}}},
Subroutine __ANON__: not called CC=1 SCAR=6.9
171 },
172 1 );
173  
174 $SPEC{parse_csel} = {
175 1 v => 1.1,
176 summary => 'Parse CSel expression',
177 args => {
178 expr => {
179 schema => 'str*',
180 req => 1,
181 pos => 0,
182 },
183 },
184 'cmdline.default_format' => 'json-pretty',
185 };
186 0 ▶sub parse_csel {
Subroutine parse_csel: not called CC=1 SCAR=6.9
Pod documented
187 0 require Data::CSel;
188 0 my %args = @_;
189 0 [200, "OK", Data::CSel::parse_csel($args{expr})];
190 }
191  
192 0 ▶sub _elide {
Subroutine _elide: not called CC=3 SCAR=24.8
193 0 my ($str, $len) = @_;
194 0 ▶ return $str if length($str) <= $len;
Branch: 1 branch
BranchTrueFalseTotal
if length $str <= $len 0 0 0
195 0 my $show_len = $len - 3;
196 0 ▶ $show_len = 0 if $show_len < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $show_len < 0 0 0 0
197 0 substr($str, 0, $show_len) . ' ..';
198 }
199  
200 0 ▶sub foosel {
Subroutine foosel: not called CC=36 SCAR=70.9
Pod undocumented
201 0 my %args = @_;
202  
203 0 ▶ my $select_action = $args{select_action} // 'csel';
Condition0%0/2
$args{"select_action"} // "csel"
l!l
00
MC/DC0%0/1
$args{"select_action"} // "csel"
$args{"select_action"}
Truth table0%0/2
A
A= $args{"select_action"}
Aresult
11
00
204 0 my $expr = $args{expr};
205 0 my $node_actions = $args{node_actions};
206 0 ▶ my $node_actions_on_descendants = $args{node_actions_on_descendants} // '';
Condition0%0/2
$args{"node_actions_on_descendants"} // ""
l!l
00
MC/DC0%0/1
$args{"node_actions_on_descendants"} // ""
$args{"node_actions_on_descendants"}
Truth table0%0/2
A
A= $args{"node_actions_on_descendants"}
Aresult
11
00
207  
208 PARSE_CSEL: {
209 0 ▶ unless ($select_action eq 'root') {
Branch: 1 branch
BranchTrueFalseTotal
unless ($select_action eq "root") 0 0 0
210 0 ▶ defined $expr or return [400, "Please specify a CSel expression"];
Branch: 1 branch
BranchTrueFalseTotal
defined $expr or return [400, "Please specify a CSel expression"] 0 0 0
211 # parse first so we can bail early on error without having to read
212 # the input
213 0 require Data::CSel;
214 0 ▶ Data::CSel::parse_csel($expr)
Branch: 1 branch
BranchTrueFalseTotal
Data::CSel::parse_csel($expr) or return [400, "Invalid CSel expression '${expr}'"] 0 0 0
215 or return [400, "Invalid CSel expression '$expr'"];
216 }
217 }
218  
219 0 my $tree;
220 READ_TREE: {
221 0 $tree = $args{code_read_tree}->(\%args);
222 }
223  
224 0 my @matches;
225 SELECT_NODES: {
226 0 ▶ if ($select_action eq 'root') {
Branch: 1 branch
BranchTrueFalseTotal
if ($select_action eq 'root') { } 0 0 0
227 0 @matches = ($tree);
228 } else {
229 0 require Data::CSel;
230 0 ▶ @matches = Data::CSel::csel($args{csel_opts} // {}, $expr, $tree);
Condition0%0/2
$args{"csel_opts"} // {}
l!l
00
MC/DC0%0/1
$args{"csel_opts"} // {}
$args{"csel_opts"}
Truth table0%0/2
A
A= $args{"csel_opts"}
Aresult
11
00
231  
232 # skip root node itself to avoid duplication
233 0 ▶ @matches = grep { refaddr($_) ne refaddr($tree) } @matches
Branch: 1 branch
BranchTrueFalseTotal
unless @matches <= 1 0 0 0
234 unless @matches <= 1;
235 }
236 }
237  
238 TRANSFORM_NODE_ACTIONS: {
239 0 $args{code_transform_node_actions}->(\%args)
240 0 ▶ if $args{code_transform_node_actions};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"code_transform_node_actions"} 0 0 0
241 }
242  
243 0 my $res = [200, "OK"];
244 PERFORM_NODE_ACTIONS: {
245 0 my $actions = $args{node_actions};
246  
247 0 my @action_targets;
248 0 ▶ if ($node_actions_on_descendants) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node_actions_on_descendants) { } 0 0 0
249 0 require Code::Includable::Tree::NodeMethods;
250 @action_targets = map {
251 0 ($_, Code::Includable::Tree::NodeMethods::descendants_depth_first($_))
252 } @matches;
253 } else {
254 0 @action_targets = @matches;
255 }
256  
257 0 for my $action (@$actions) {
258 0 ▶ if ($action =~ /\Adump(?::(.+))?/) {
Branch: 7 branches
BranchTrueFalseTotal
if ($action =~ /\Adump(?::(.+))?/) { } 0 0 0
elsif ($action =~ /\Aeval:(.+)/) { } 0 0 0
elsif ($action eq 'count') { } 0 0 0
elsif ($action eq 'print_as_string') { } 0 0 0
elsif ($action =~ /\Aprint_method:(.+)\z/) { } 0 0 0
elsif ($action =~ /\Aprint_func:(.+)\z/) { } 0 0 0
elsif ($action =~ /\Aprint_func_or_meth:(.+)\z/) { } 0 0 0
259 0 ▶ my $cols = $ENV{COLUMNS} // do {
Condition0%0/5
($ENV{"COLUMNS"} // do { my $cols; eval { do { require Term::Size; ($cols) = Term::Size::chars(scalar *STDOUT{"IO"}) } }; $cols }) // 80
l!l
00
$ENV{"COLUMNS"} // do { my $cols; eval { do { require Term::Size; ($cols) = Term::Size::chars(scalar *STDOUT{"IO"}) } }; $cols }
l!l&&r!l&&!r
000
MC/DC0%0/2
($ENV{"COLUMNS"} // do { my $cols; eval { do { require Term::Size; ($cols) = Term::Size::chars(scalar *STDOUT{"IO"}) } }; $cols }) // 80
$ENV{"COLUMNS"} do { my $cols; eval { do { require Term::Size; ($cols) = Term::Size::chars(scalar *STDOUT{"IO"}) } }; $cols }
Truth table0%0/3
A // B
A= $ENV{"COLUMNS"}B= do { my $cols; eval { do { require Term::Size; ($cols) = Term::Size::chars(scalar *STDOUT{"IO"}) } }; $cols }
ABresult
1X1
011
000
260 0 my $cols;
261 0 eval {
262 0 require Term::Size;
263 0 ($cols) = Term::Size::chars(*STDOUT{IO});
264 };
265 0 $cols;
266 } // 80;
267  
268 0 require Tree::To::TextLines;
269 0 my @attrs = split /\./, $1;
270 0 ▶ @attrs = ('id') unless @attrs;
Branch: 1 branch
BranchTrueFalseTotal
unless @attrs 0 0 0
271 0 push @{ $res->[2] }, map {
272 0 Tree::To::TextLines::render_tree_as_text({
273 show_guideline => 1,
274 0 ▶ on_show_node => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
275 0 my ($node, $level, $seniority, $is_last_child, $opts) = @_;
276 my $str = ref($node)." ".
277 join(", ", map {
278 0 ▶ (@attrs > 1 ? "$_=":"") .
Branch: 2 branches
BranchTrueFalseTotal
@attrs > 1 ? : 0 0 0
$node->can($_) ? : 0 0 0
Condition0%0/2
($node->can($_) ? $node->$_ : undef) // "undef"
l!l
00
MC/DC0%0/1
($node->can($_) ? $node->$_ : undef) // "undef"
($node->can($_) ? $node->$_ : undef)
Truth table0%0/2
A
A= ($node->can($_) ? $node->$_ : undef)
Aresult
11
00
279 dmp(($node->can($_) ? $node->$_ : undef) // 'undef')
280 } @attrs);
281 0 _elide($str, $cols - $level*4);
282 },
283 0 }, $_)
284 } @action_targets;
285 } elsif ($action =~ /\Aeval:(.+)/) {
286 0 my $string_code = $1;
287 0 my $compiled_code =
288 eval "package main; no strict; no warnings; sub { $string_code }"; ## no critic: BuiltinFunctions::ProhibitStringyEval
289 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
290 0 die "Can't compile code in eval: $@\n";
291 }
292 0 for my $node (@action_targets) {
293 0 local $_ = $node;
294 0 $compiled_code->($node);
295 }
296 } elsif ($action eq 'count') {
297 0 ▶ if (@$actions == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$actions == 1) { } 0 0 0
298 0 $res->[2] = ~~@matches;
299 } else {
300 0 push @{ $res->[2] }, ~~@matches;
301 }
302 } elsif ($action eq 'print_as_string') {
303 0 push @{ $res->[2] }, map {$_->as_string} @action_targets;
304 } elsif ($action =~ /\Aprint_method:(.+)\z/) {
305 0 my @meths = split /\./, $1;
306 0 for my $node (@action_targets) {
307 0 my $node_res = $node;
308 0 for my $meth (@meths) {
309 0 eval { $node_res = $node_res->$meth };
310 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
311 0 $node_res = undef;
312 0 last;
313 }
314 }
315 0 push @{ $res->[2] }, $node_res;
316 }
317 } elsif ($action =~ /\Aprint_func:(.+)\z/) {
318 1 ▶ no strict 'refs'; ## no critic: TestingAndDebugging::ProhibitNoStrict
Subroutine BEGIN: called CC=1 SCAR=0.0
319 0 my @funcs = split /\./, $1;
320 0 for my $node (@action_targets) {
321 0 my $node_res = $node;
322 0 for my $func (@funcs) {
323 0 eval { $node_res = &{$func}($node_res) };
324 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
325 0 $node_res = undef;
326 0 last;
327 }
328 }
329 0 push @{ $res->[2] }, $node_res;
330 }
331 } elsif ($action =~ /\Aprint_func_or_meth:(.+)\z/) {
332 1 ▶ no strict 'refs'; ## no critic: TestingAndDebugging::ProhibitNoStrict
Subroutine BEGIN: called CC=1 SCAR=0.0
333 0 my @entries = split /\./, $1;
334 0 for my $node (@action_targets) {
335 0 my $node_res = $node;
336 0 for my $entry (@entries) {
337 0 ▶ my ($type, $name) = $entry =~ /\A(func|meth)::?(.+)\z/ or
Branch: 1 branch
BranchTrueFalseTotal
() = $entry =~ /\A(func|meth)::?(.+)\z/ or return [400, "For action print_func_or_meth, specify func:FUNCNAME or meth:METHNAME"] 0 0 0
338 return [400, "For action print_func_or_meth, ".
339 "specify func:FUNCNAME or meth:METHNAME"];
340 0 eval {
341 0 ▶ if ($type eq 'func') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'func') { } 0 0 0
342 #use DD; say "func: $name(", DD::dump($node_res), ")";
343 0 $node_res = &{$name}($node_res);
344 } else {
345 #use DD; say "meth: $name on ", DD::dump($node_res);
346 0 $node_res = $node_res->$name;
347 }
348 };
349 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
350 #warn $@;
351 0 $node_res = undef;
352 0 last;
353 }
354 }
355 0 push @{ $res->[2] }, $node_res;
356 }
357 } else {
358 0 return [400, "Unknown action '$action'"];
359 }
360 } # for $action
361 }
362 0 $res;
363 }
364  
365 $SPEC{ddsel} = {
366 1 v => 1.1,
367 summary => 'Select Perl data structure elements using CSel (CSS-selector-like) syntax',
368 description => <<'_',
369  
370 Note that this operates against Perl data structure, not Perl source code
371 elements (see <prog:ppisel> for that). File is Perl source code that defines
372 data structure, e.g.:
373  
374 {
375 summary => 'This is a hash',
376 # this is an array inside a hash
377 array => [
378 1, 2, 3,
379 ],
380 };
381  
382 _
383 args => {
384 %foosel_args_common,
385 },
386 };
387 0 ▶sub ddsel {
Subroutine ddsel: not called CC=1 SCAR=6.9
Pod documented
388 foosel(
389 @_,
390  
391 0 ▶ code_read_tree => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
392 0 my $args = shift;
393 0 my $data;
394 0 ▶ if ($args->{file} eq '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{'file'} eq '-') { } 0 0 0
395 0 binmode STDIN, ":encoding(utf8)";
396 0 $data = eval join("", <>); ## no critic: BuiltinFunctions::ProhibitStringyEval
397 0 ▶ die if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
398 } else {
399 0 require File::Slurper;
400 0 $data = eval File::Slurper::read_text($args->{file}); ## no critic: BuiltinFunctions::ProhibitStringyEval
401 0 ▶ die if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
402 }
403  
404 0 require Data::CSel::WrapStruct;
405 0 my $tree = Data::CSel::WrapStruct::wrap_struct($data);
406 0 $tree;
407 },
408  
409 csel_opts => {class_prefixes=>['Data::CSel::WrapStruct']},
410  
411 0 ▶ code_transform_node_actions => sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
412 0 my $args = shift;
413  
414 0 for my $action (@{ $args->{node_actions} }) {
415 0 ▶ if ($action eq 'print' || $action eq 'print_as_string') {
Branch: 2 branches
BranchTrueFalseTotal
if ($action eq 'print' or $action eq 'print_as_string') { } 0 0 0
elsif ($action eq 'dump') { } 0 0 0
Condition0%0/3
$action eq "print" or $action eq "print_as_string"
l!l&&r!l&&!r
000
MC/DC0%0/2
$action eq "print" or $action eq "print_as_string"
$action eq "print" $action eq "print_as_string"
Truth table0%0/3
A or B
A= $action eq "print"B= $action eq "print_as_string"
ABresult
1X1
011
000
416 0 $action = 'print_func_or_meth:meth:value.func:Data::Dmp::dmp';
417 } elsif ($action eq 'dump') {
418 0 $action = 'dump:value';
419 }
420 }
421 },
422 0 );
423 }
424  
425 1 1;
426  
427 # ABSTRACT: Utilities related to Data::CSel
428  
429 __END__
↑ Summary