Info.pm

filter: Statementstmt 12.1% 17 / 140 Branchbran 0.0% 0 / 54 Conditioncond 0.0% 0 / 32 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 40.0% 4 / 10 Podpod 100% 3 / 3 totaltotal 9.4% 24 / 255 SCAR 75.3
CC
48
Cov
8%
_load_plugins
420.0
new
342.0
_activate_plugin
72.0
CRAP 1870.2
↑ Summary Info/PluginBase.pm »
1 package URI::Info;
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 1 ▶use Log::ger;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
9 1 our $DATE = '2021-10-17'; # DATE
10 1 our $DIST = 'URI-Info'; # DIST
11 1 our $VERSION = '0.002'; # VERSION
12  
13 1 my $p = "URI::Info::Plugin::";
14 sub new {
15 0 ▶ my ($class, %args) = @_;
Subroutine new: not called CC=18 SCAR=58.3
Pod documented
16  
17 0 for (keys %args) {
18 0 ▶ die "Unknown argument '$_', known arguments are include_plugins, exclude_plugins"
Branch: 1 branch
BranchTrueFalseTotal
unless /\A(include_plugins|exclude_plugins)\z/ 0 0 0
19 unless /\A(include_plugins|exclude_plugins)\z/;
20 }
21 0 ▶ if (!defined($args{include_plugins}) && !defined($args{exclude_plugins})
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $args{"include_plugins"} || defined $args{"exclude_plugins"} and defined $ENV{"URI_INFO_PLUGINS"} and length $ENV{"URI_INFO_PLUGINS"}) 0 0 0
Condition0%0/9
not defined $args{"include_plugins"} || defined $args{"exclude_plugins"} and defined $ENV{"URI_INFO_PLUGINS"} and length $ENV{"URI_INFO_PLUGINS"}
!ll&&!rl&&r
000
not defined $args{"include_plugins"} || defined $args{"exclude_plugins"} and defined $ENV{"URI_INFO_PLUGINS"}
!ll&&!rl&&r
000
defined $args{'include_plugins'} || defined $args{'exclude_plugins'}
l!l&&r!l&&!r
000
MC/DC0%0/4
not defined $args{"include_plugins"} || defined $args{"exclude_plugins"} and defined $ENV{"URI_INFO_PLUGINS"} and length $ENV{"URI_INFO_PLUGINS"}
defined $args{'include_plugins'} defined $args{'exclude_plugins'} defined $ENV{"URI_INFO_PLUGINS"} length $ENV{"URI_INFO_PLUGINS"}
Truth table0%0/5
not(A || B) and C and D
A= defined $args{'include_plugins'}B= defined $args{'exclude_plugins'}C= defined $ENV{"URI_INFO_PLUGINS"}D= length $ENV{"URI_INFO_PLUGINS"}
ABCDresult
1XXX0
01XX0
000X0
00100
00111
22 && defined $ENV{URI_INFO_PLUGINS} && length $ENV{URI_INFO_PLUGINS}) {
23 0 $args{include_plugins} = undef;
24 0 $args{exclude_plugins} = [];
25 0 my @elems = split /\s*,\s*/, $ENV{URI_INFO_PLUGINS};
26 0 my ($exclude_sign, $plugin, $args);
27 0 $args = [];
28 0 ▶ while (@elems) {
Branch: 1 branch
BranchTrueFalseTotal
if (@elems) 0 0 0
29 0 my $elem = shift @elems;
30 0 ▶ if ($elem =~ /\A([+-])(.+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elem =~ /\A([+-])(.+)/) { } 0 0 0
31 0 ▶ if (defined $plugin) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $plugin) 0 0 0
32 # add previous plugin
33 0 ▶ my $key = $exclude_sign eq '-' ? 'exclude_plugins' : 'include_plugins';
Branch: 1 branch
BranchTrueFalseTotal
$exclude_sign eq '-' ? : 0 0 0
34 0 ▶ $args{$key} //= [];
Condition0%0/2
$args{$key} //= []
l!l
00
MC/DC0%0/1
$args{$key} //= []
$args{$key}
Truth table0%0/2
A
A= $args{$key}
Aresult
11
00
35 0 ▶ push @{$args{$key}}, $plugin . (@$args ? "=".join(",", @$args) : "");
Branch: 1 branch
BranchTrueFalseTotal
@$args ? : 0 0 0
36 }
37 0 ($exclude_sign, $plugin) = ($1, $2);
38 } else {
39 0 push @$args, $elem;
40 }
41 }
42 # add last plugin
43 0 ▶ die "[URI::Info] Invalid syntax in URI_INFO_PLUGINS variable: no plugins specified, only args"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $plugin 0 0 0
44 unless defined $plugin;
45 0 ▶ my $key = $exclude_sign eq '-' ? 'exclude_plugins' : 'include_plugins';
Branch: 1 branch
BranchTrueFalseTotal
$exclude_sign eq '-' ? : 0 0 0
46 0 ▶ $args{$key} //= [];
Condition0%0/2
$args{$key} //= []
l!l
00
MC/DC0%0/1
$args{$key} //= []
$args{$key}
Truth table0%0/2
A
A= $args{$key}
Aresult
11
00
47 0 ▶ push @{$args{$key}}, $plugin . (@$args ? "=".join(",", @$args) : "");
Branch: 1 branch
BranchTrueFalseTotal
@$args ? : 0 0 0
48 }
49  
50 0 ▶ unless (defined $args{include_plugins}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args{"include_plugins"}) 0 0 0
51 0 require Module::List::Wildcard;
52 0 my $mods = Module::List::Wildcard::list_modules($p, {list_modules=>1, recurse=>1});
53 0 $args{include_plugins} = [map {my $mod=$_; $mod=~s/\A\Q$p\E//; $mod} sort keys %$mods];
54 }
55  
56 0 my $self = bless \%args, $class;
57  
58 0 $self->{plugin_objs_by_host} = {}; # key=host, value=[plugin_obj1, ...]
59  
60 0 $self->_load_plugins;
61 0 $self;
62 }
63  
64 sub _load_plugins {
65 0 ▶ require Module::List::Wildcard;
Subroutine _load_plugins: not called CC=20 SCAR=60.4
66 0 require Module::Path::More;
67 0 require String::Wildcard::Bash;
68  
69 0 my $self = shift;
70  
71 0 my %exclude_plugins;
72 0 ▶ if ($self->{exclude_plugins} && @{ $self->{exclude_plugins} }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"exclude_plugins"} and @{$self->{"exclude_plugins"};}) 0 0 0
Condition0%0/3
$self->{"exclude_plugins"} and @{$self->{"exclude_plugins"};}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"exclude_plugins"} and @{$self->{"exclude_plugins"};}
$self->{"exclude_plugins"} @{$self->{"exclude_plugins"};}
Truth table0%0/3
A and B
A= $self->{"exclude_plugins"}B= @{$self->{"exclude_plugins"};}
ABresult
0X0
100
111
73 0 for my $prefix (@{ $self->{exclude_plugins} }) {
74 0 ▶ if (ref $prefix) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $prefix) 0 0 0
75 0 die "[URI::Info] exclude_plugins entry cannot be array/reference: $prefix";
76 }
77 0 ▶ if ($prefix eq '' || $prefix =~ /::\z/ ||
Branch: 1 branch
BranchTrueFalseTotal
if ($prefix eq '' or $prefix =~ /::\z/ or String::Wildcard::Bash::contains_wildcard($prefix)) { } 0 0 0
Condition0%0/6
$prefix eq "" or $prefix =~ /::\z/ or String::Wildcard::Bash::contains_wildcard($prefix)
l!l&&r!l&&!r
000
$prefix eq "" or $prefix =~ /::\z/
l!l&&r!l&&!r
000
MC/DC0%0/3
$prefix eq "" or $prefix =~ /::\z/ or String::Wildcard::Bash::contains_wildcard($prefix)
$prefix eq "" $prefix =~ /::\z/ String::Wildcard::Bash::contains_wildcard($prefix)
Truth table0%0/4
A or B or C
A= $prefix eq ""B= $prefix =~ /::\z/C= String::Wildcard::Bash::contains_wildcard($prefix)
ABCresult
1XX1
01X1
0011
0000
78 String::Wildcard::Bash::contains_wildcard($prefix)) {
79 0 my $mods = Module::List::Wildcard::list_modules(
80 "${p}$prefix", {list_modules=>1, wildcard=>1});
81 0 for (keys %$mods) {
82 0 s/\A\Q$p\E//;
83 0 $exclude_plugins{$_}++;
84 }
85 } else {
86 0 my $path = Module::Path::More::module_path(
87 module=>"${p}$prefix");
88 0 ▶ if ($path) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path) 0 0 0
89 0 $exclude_plugins{$prefix}++;
90 }
91 }
92 }
93 }
94  
95 0 for my $entry (@{ $self->{include_plugins} }) {
96 0 my ($prefix, $args);
97 0 ▶ if (ref $entry eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $entry eq 'ARRAY') { } 0 0 0
elsif ($entry =~ /\A(\w+(?:::\w+)*)=(.*)\z/) { } 0 0 0
98 0 $prefix = $entry->[0];
99 0 $args = $entry->[1];
100 } elsif ($entry =~ /\A(\w+(?:::\w+)*)=(.*)\z/) {
101 0 $prefix = $1;
102 0 $args = [split /\s*,\s*/, $2];
103 } else {
104 0 $prefix = $entry;
105 0 $args = {};
106 }
107 0 my @plugins;
108 0 ▶ if ($prefix eq '' || $prefix =~ /::\z/ ||
Branch: 1 branch
BranchTrueFalseTotal
if ($prefix eq '' or $prefix =~ /::\z/ or String::Wildcard::Bash::contains_wildcard($prefix)) { } 0 0 0
Condition0%0/6
$prefix eq "" or $prefix =~ /::\z/ or String::Wildcard::Bash::contains_wildcard($prefix)
l!l&&r!l&&!r
000
$prefix eq "" or $prefix =~ /::\z/
l!l&&r!l&&!r
000
MC/DC0%0/3
$prefix eq "" or $prefix =~ /::\z/ or String::Wildcard::Bash::contains_wildcard($prefix)
$prefix eq "" $prefix =~ /::\z/ String::Wildcard::Bash::contains_wildcard($prefix)
Truth table0%0/4
A or B or C
A= $prefix eq ""B= $prefix =~ /::\z/C= String::Wildcard::Bash::contains_wildcard($prefix)
ABCresult
1XX1
01X1
0011
0000
109 String::Wildcard::Bash::contains_wildcard($prefix)) {
110 0 my $mods = Module::List::Wildcard::list_modules(
111 "${p}$prefix", {list_modules=>1, wildcard=>1});
112 0 for (keys %$mods) {
113 0 my $mod = $_;
114 0 s/\A\Q$p\E//;
115 0 ▶ if ($exclude_plugins{$_}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exclude_plugins{$_}) 0 0 0
116 0 log_debug "[URI::Info] plugin '$_' is excluded (matches $prefix)";
117 0 next;
118 }
119 0 log_debug "[URI::Info] Loading plugin module $mod ...";
120 0 (my $mod_pm = "$mod.pm") =~ s!::!/!g;
121 0 require $mod_pm;
122 0 $self->_activate_plugin($mod, $args);
123 }
124 } else {
125 0 my $mod = "${p}$prefix";
126 0 my $path = Module::Path::More::module_path(module=>$mod);
127 0 ▶ if ($path) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path) { } 0 0 0
128 0 ▶ if ($exclude_plugins{$prefix}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exclude_plugins{$prefix}) 0 0 0
129 0 log_debug "[URI::Info] plugin '$mod' is excluded";
130 0 next;
131 }
132 0 log_debug "[URI::Info] Loading plugin module $mod ...";
133 0 (my $mod_pm = "$mod.pm") =~ s!::!/!g;
134 0 require $mod_pm;
135 0 $self->_activate_plugin($mod, $args);
136 } else {
137 0 die "[URI::Info] plugin '$mod' cannot be found";
138 }
139 }
140 }
141 }
142  
143 # this will install plugins by host
144 sub _activate_plugin {
145 0 ▶ my ($self, $mod, $args) = @_;
Subroutine _activate_plugin: not called CC=8 SCAR=42.8
146  
147 # module must already be loaded at this point
148  
149 # instantiate plugin object
150 my $plugin_obj = $mod->new(
151 0 ▶ ref $args eq 'HASH' ? %$args : @{$args // []});
Branch: 1 branch
BranchTrueFalseTotal
ref $args eq 'HASH' ? : 0 0 0
Condition0%0/2
$args // []
l!l
00
MC/DC0%0/1
$args // []
$args
Truth table0%0/2
A
A= $args
Aresult
11
00
152 0 my $plugin_meta = $plugin_obj->meta;
153 0 my $plugin_host0 = $plugin_meta->{host};
154  
155 0 my @plugin_hosts;
156 0 ▶ if (ref $plugin_host0 eq 'ARRAY') {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $plugin_host0 eq 'ARRAY') { } 0 0 0
elsif (ref $plugin_host0 eq 'CODE') { } 0 0 0
elsif (not ref $plugin_host0) { } 0 0 0
157 0 @plugin_hosts = @$plugin_host0;
158 } elsif (ref $plugin_host0 eq 'CODE') {
159 0 @plugin_hosts = $plugin_host0->();
160 } elsif (!ref($plugin_host0)) {
161 0 @plugin_hosts = ($plugin_host0);
162 } else {
163 0 die "[URI::Info] invalid plugin host '$plugin_host0', must be array/code/scalar";
164 }
165  
166 0 for my $plugin_host (@plugin_hosts) {
167 0 ▶ $self->{plugin_objs_by_host}{$plugin_host} //= [];
Condition0%0/2
$self->{"plugin_objs_by_host"}{$plugin_host} //= []
l!l
00
MC/DC0%0/1
$self->{"plugin_objs_by_host"}{$plugin_host} //= []
$self->{"plugin_objs_by_host"}{$plugin_host}
Truth table0%0/2
A
A= $self->{"plugin_objs_by_host"}{$plugin_host}
Aresult
11
00
168 0 push @{ $self->{plugin_objs_by_host}{$plugin_host} }, $plugin_obj;
169 }
170 }
171  
172 sub _call_plugins {
173 0 ▶ my ($self, $meth, $stash) = @_;
Subroutine _call_plugins: not called CC=5 SCAR=34.0
174  
175 0 my $host0 = $stash->{url}->host;
176 0 my @hosts;
177 0 push @hosts, $host0;
178 0 ▶ while ($host0 =~ s/\A[\w-]+(?:\.|\z)//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($host0 =~ s/\A[\w-]+(?:\.|\z)//) 0 0 0
179 0 push @hosts, $host0;
180 }
181 0 for my $host (@hosts) {
182 0 my $plugin_objs = $self->{plugin_objs_by_host}{$host};
183 0 ▶ next unless $plugin_objs;
Branch: 1 branch
BranchTrueFalseTotal
unless $plugin_objs 0 0 0
184 0 for my $plugin_obj (@$plugin_objs) {
185 0 log_trace "[URI::Info] Calling plugin %s %s", ref($plugin_obj), $meth;
186 0 $plugin_obj->$meth($stash);
187 # XXX currently ignore return value
188 }
189 }
190 }
191  
192 sub info {
193 0 ▶ require URI::URL;
Subroutine info: not called CC=1 SCAR=6.9
Pod documented
194 0 require URI::QueryParam;
195  
196 0 my ($self, $url) = @_;
197 0 my $stash = {
198 url => URI::URL->new($url),
199 res => {
200 url => $url,
201 },
202 };
203 0 $self->_call_plugins('get_info', $stash);
204 0 $stash->{res};
205 }
206  
207 sub uri_info {
208 0 ▶ state $obj = __PACKAGE__->new;
Subroutine uri_info: not called CC=1 SCAR=6.9
Pod documented
209 0 $obj->info($_);
210 }
211  
212 1 1;
213 # ABSTRACT: Extract various information from a URI (URL)
214  
215 __END__
↑ Summary Info/PluginBase.pm »