lib/Badger/Filesystem/Visitor.pm

filter: Statementstmt 93.4% 100 / 107 Branchbran 69.4% 50 / 72 Conditioncond 58.0% 18 / 31 MC/DCmcdc 27.7% 5 / 18 Subroutinesub 90.0% 18 / 20 Podpod 55.5% 10 / 18 totaltotal 75.5% 201 / 266 CC 52 SCAR 20.7
CC
52
Cov
80%
init_filters
14.6
filter
13.6
identify
12.0
CRAP 7.9
« lib/Badger/Config/Schema.pm ↑ Summary lib/Badger/Data/Facet/Text.pm »
1 #========================================================================
2 #
3 # Badger::Filesystem::Visitor
4 #
5 # DESCRIPTION
6 # Base class visitor object for traversing a filesystem.
7 #
8 # AUTHOR
9 # Andy Wardley <abw@wardley.org>
10 #
11 #========================================================================
12  
13 package Badger::Filesystem::Visitor;
14  
15 use Badger::Class
16 1 version => 0.01,
17 debug => 0,
18 base => 'Badger::Base',
19 import => 'class',
20 utils => 'params',
21 constants => 'ARRAY CODE REGEX ON WILDCARD',
22 config => [
23 'files|accept|class:FILES',
24 'no_files|ignore|class:NO_FILES',
25 'dirs|directories|class:DIRS',
26 'no_dirs|no_directories|class:NO_DIRS',
27 'in_dirs|in_directories|enter|class:IN_DIRS',
28 'not_in_dirs|not_in_directories|leave|class:NOT_IN_DIRS',
29 'accept_file',
30 'reject_file',
31 'accept_dir|accept_directory',
32 'reject_dir|reject_directory',
33 'enter_dir|enter_directory',
34 'leave_dir|leave_directory',
35 ],
36 messages => {
37 no_node => 'No node specified to %s',
38 bad_filter => 'Invalid test in %s specification: %s',
39 },
40 alias => {
41 init => \&init_visitor,
42 collect_dir => \&collect_dir,
43 enter_dir => \&enter_directory,
44 visit_dir => \&visit_directory,
45 visit_dir_kids => \&visit_directory_children,
46 1 ▶ };
Subroutine BEGIN: called CC=1 SCAR=0.0
47  
48 1 ▶use Badger::Debug ':dump';
Subroutine BEGIN: called CC=1 SCAR=0.0
49 1 our @FILTERS = qw( files dirs in_dirs no_files no_dirs not_in_dirs );
50 1 our $ALL = 0;
51 1 our $FILES = 1;
52 1 our $DIRS = 1;
53 1 our $IN_DIRS = 0;
54 1 our $NO_FILES = 0;
55 1 our $NO_DIRS = 0;
56 1 our $NOT_IN_DIRS = 0;
57  
58  
59 sub init_visitor {
60 14 ▶ my ($self, $config) = @_;
Subroutine init_visitor: called CC=2 SCAR=6.9
Pod undocumented
61 14 my $class = $self->class;
62 14 my ($item, $long);
63  
64 14 $self->configure($config);
65
66 $self->{ in_dirs } = 1
67 14 ▶ if $config->{ recurse };
Branch: 1 branch
BranchTrueFalseTotal
if $config->{"recurse"} 2 12 14
68
69 14 $self->{ collect } = [ ];
70 14 $self->{ identify } = { };
71  
72 14 $self->init_filters;
73  
74 14 $self->debug("init_visitor() => ", $self->dump) if DEBUG;
75  
76 14 return $self;
77 }
78  
79  
80 sub init_filters {
81 14 ▶ my $self = shift;
Subroutine init_filters: called CC=14 SCAR=26.8
Pod undocumented
82 14 my ($filter, $tests, $test, $type);
83
84 14 foreach $filter (@FILTERS) {
85 84 ▶ $tests = $self->{ $filter } || next; # skip over false values
Condition100%2/2
$self->{$filter} || (next)
l!l
3846
MC/DC100%1/1
$self->{$filter} || (next)
$self->{$filter}
Truth table100%2/2
A
A= $self->{$filter}
Aresult
11
00
86 38 $self->debug("filter: $filter => $tests\n") if DEBUG;
87 38 ▶ $tests = $self->{ $filter } = [$tests]
Branch: 1 branch
BranchTrueFalseTotal
unless ref $tests eq "ARRAY" 33 5 38
88 unless ref $tests eq ARRAY;
89
90 # NOTE: $test is aliasing list item so we can change it
91 38 foreach $test (@$tests) {
92 43 ▶ $self->debug(" - test: $test\n") if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Badger::Filesystem::Visitor::DEBUG 0 43 43
93 43 ▶ last unless $test; # false test always fails
Branch: 1 branch
BranchTrueFalseTotal
unless $test 0 43 43
94
95 43 ▶ if ($type = ref $test) {
Branch: 3 branches
BranchTrueFalseTotal
if ($type = ref $test) { } 6 37 43
elsif ($test eq 1) { } 16 21 37
elsif ($test =~ /[\*\?]/) { } 6 15 21
96 6 ▶ return $self->error_msg( bad_filter => $filter => $test )
Branch: 1 branch
BranchTrueFalseTotal
unless $type eq "CODE" or $type eq "Regexp" 0 6 6
Condition66%2/3
$type eq "CODE" or $type eq "Regexp"
l!l&&r!l&&!r
420
MC/DC0%0/2
$type eq "CODE" or $type eq "Regexp"
$type eq "CODE" $type eq "Regexp"
Truth table66%2/3
A or B
A= $type eq "CODE"B= $type eq "Regexp"
ABresult
1X1
011
000
97 unless $type eq CODE or $type eq REGEX;
98 # OK
99 }
100 elsif ($test eq ON) {
101 # OK
102 }
103 elsif ($test =~ WILDCARD) {
104 # changing test affects list item via regex
105 6 $test =~ s/\./<<DOT>>/g; # . => <<DOT>> (tmp)
106 6 $test =~ s/\?/./g; # ? => .
107 6 $test =~ s/\*/.*/g; # * => .*
108 6 $test =~ s/<<DOT>>/\\./g; # <<DOT>> => \.
109 6 $test = qr/^$test$/;
110 6 ▶ $self->debug("transmogrified wildcard into regex: $test\n") if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Badger::Filesystem::Visitor::DEBUG 0 6 6
111 }
112 }
113
114 $self->debug(
115 38 ▶ "initialised $filter tests: ",
Branch: 1 branch
BranchTrueFalseTotal
if $Badger::Filesystem::Visitor::DEBUG 0 38 38
116 $self->dump_data_inline($tests),
117 "\n"
118 ) if $DEBUG;
119 }
120 }
121  
122  
123 sub visit {
124 14 ▶ my $self = shift;
Subroutine visit: called CC=2 SCAR=7.1
Pod documented
125 14 ▶ my $node = shift || return $self->error_msg( no_node => 'visit' );
Condition50%1/2
shift() || (return $self->error_msg('no_node', 'visit'))
l!l
140
MC/DC0%0/1
shift() || (return $self->error_msg('no_node', 'visit'))
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
126 14 $node->enter($self);
127 }
128  
129  
130 sub visit_path {
131 0 ▶ my ($self, $path) = @_;
Subroutine visit_path: not called CC=2 SCAR=17.9
Pod documented
132 # TODO: we have nothing going on here
133 0 ▶ $self->debug("visiting path: $path\n") if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Badger::Filesystem::Visitor::DEBUG 0 0 0
134 }
135  
136  
137 sub visit_file {
138 196 ▶ my ($self, $file) = @_;
Subroutine visit_file: called CC=2 SCAR=6.9
Pod documented
139  
140 196 ▶ return $self->filter_file($file)
Branch: 1 branch
BranchTrueFalseTotal
$self->filter_file($file) ? : 43 153 196
141 ? $self->accept_file($file)
142 : $self->reject_file($file);
143 }
144  
145  
146 sub visit_directory {
147 60 ▶ my ($self, $dir) = @_;
Subroutine visit_directory: called CC=6 SCAR=18.5
Pod documented
148 60 ▶ $self->debug("visiting directory: $dir\n") if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Badger::Filesystem::Visitor::DEBUG 0 60 60
149  
150 60 ▶ $self->filter_directory($dir)
Branch: 1 branch
BranchTrueFalseTotal
$self->filter_directory($dir) ? : 7 53 60
Condition50%2/4
$self->accept_directory($dir) or return
l!l
70
$self->reject_directory($dir) or return
l!l
530
MC/DC0%0/1
$self->accept_directory($dir) or return
$self->accept_directory($dir)
MC/DC0%0/1
$self->reject_directory($dir) or return
$self->reject_directory($dir)
Truth table50%1/2
A
A= $self->accept_directory($dir)
Aresult
11
00
Truth table50%1/2
A
A= $self->reject_directory($dir)
Aresult
11
00
151 ? $self->accept_directory($dir) || return
152 : $self->reject_directory($dir) || return;
153  
154 60 ▶ return $self->filter_entry($dir)
Branch: 1 branch
BranchTrueFalseTotal
$self->filter_entry($dir) ? : 50 10 60
155 ? $self->enter_directory($dir)
156 : $self->leave_directory($dir);
157 }
158  
159  
160 sub filter {
161 420 ▶ my ($self, $filter, $method, $item) = @_;
Subroutine filter: called CC=13 SCAR=26.1
Pod documented
162 420 ▶ my $tests = $self->{ $filter } || do {
Condition66%2/3
$self->{$filter} || do { '???'; return 0 }
l!l&&r!l&&!r
2821380
MC/DC0%0/2
$self->{$filter} || do { '???'; return 0 }
$self->{$filter} do { '???'; return 0 }
Truth table33%1/3
A || B
A= $self->{$filter}B= do { '???'; return 0 }
ABresult
1X1
011
000
163 138 $self->debug("No filter defined for $filter") if DEBUG;
164 138 return 0;
165 };
166 282 my ($test, $type);
167  
168 282 ▶ $self->debug("filter($filter, $method, $item) tests: $tests\n") if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Badger::Filesystem::Visitor::DEBUG 0 282 282
169
170 282 foreach $test (@$tests) {
171 318 ▶ $self->debug(" - test: $test\n") if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Badger::Filesystem::Visitor::DEBUG 0 318 318
172 318 ▶ if ($test eq ON) {
Branch: 2 branches
BranchTrueFalseTotal
if ($test eq 1) { } 66 252 318
elsif ($type = ref $test) { } 132 120 252
173 66 return 1;
174 }
175 elsif ($type = ref $test) {
176 132 ▶ if ($type eq CODE) {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'CODE') { } 40 92 132
elsif ($type eq 'Regexp') { } 92 0 92
177 # $self->debug("calling code: ". $test->($item, $self));
178 40 ▶ return 1 if $test->($item, $self);
Branch: 1 branch
BranchTrueFalseTotal
if &$test($item, $self) 13 27 40
179 }
180 elsif ($type eq REGEX) {
181 92 ▶ return 1 if $item->$method =~ $test;
Branch: 1 branch
BranchTrueFalseTotal
if $item->$method =~ /$test/ 19 73 92
182 }
183 else {
184 0 return $self->error_msg( bad_filter => $filter => $test );
185 }
186 }
187 else {
188 120 ▶ return 1 if $item->$method eq $test;
Branch: 1 branch
BranchTrueFalseTotal
if $item->$method eq $test 10 110 120
189 }
190 }
191 174 ▶ $self->debug(" - ALL FAIL - ignore\n") if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Badger::Filesystem::Visitor::DEBUG 0 174 174
192 174 return 0;
193 }
194  
195  
196 sub filter_file {
197 196 ▶ my ($self, $file) = @_;
Subroutine filter_file: called CC=2 SCAR=7.1
Pod undocumented
198 196 ▶ return $self->filter( files => name => $file )
Condition66%2/3
$self->filter('files', 'name', $file) && !$self->filter('no_files', 'name', $file)
!ll&&!rl&&r
153043
MC/DC50%1/2
$self->filter('files', 'name', $file) && !$self->filter('no_files', 'name', $file)
$self->filter('files', 'name', $file) !$self->filter('no_files', 'name', $file)
Truth table66%2/3
A && not(B)
A= $self->filter('files', 'name', $file)B= !$self->filter('no_files', 'name', $file)
ABresult
0X0
100
111
199 && ! $self->filter( no_files => name => $file );
200 }
201  
202  
203 sub filter_directory {
204 60 ▶ my ($self, $dir) = @_;
Subroutine filter_directory: called CC=2 SCAR=7.1
Pod undocumented
205 60 ▶ return $self->filter( dirs => name => $dir )
Condition66%2/3
$self->filter('dirs', 'name', $dir) && !$self->filter('no_dirs', 'name', $dir)
!ll&&!rl&&r
5307
MC/DC50%1/2
$self->filter('dirs', 'name', $dir) && !$self->filter('no_dirs', 'name', $dir)
$self->filter('dirs', 'name', $dir) !$self->filter('no_dirs', 'name', $dir)
Truth table66%2/3
A && not(B)
A= $self->filter('dirs', 'name', $dir)B= !$self->filter('no_dirs', 'name', $dir)
ABresult
0X0
100
111
206 && ! $self->filter( no_dirs => name => $dir );
207 }
208  
209  
210 sub filter_entry {
211 60 ▶ my ($self, $dir) = @_;
Subroutine filter_entry: called CC=2 SCAR=6.9
Pod undocumented
212 60 ▶ return $self->filter( in_dirs => name => $dir )
Condition100%3/3
$self->filter('in_dirs', 'name', $dir) && !$self->filter('not_in_dirs', 'name', $dir)
!ll&&!rl&&r
6450
MC/DC100%2/2
$self->filter('in_dirs', 'name', $dir) && !$self->filter('not_in_dirs', 'name', $dir)
$self->filter('in_dirs', 'name', $dir) !$self->filter('not_in_dirs', 'name', $dir)
Truth table100%3/3
A && not(B)
A= $self->filter('in_dirs', 'name', $dir)B= !$self->filter('not_in_dirs', 'name', $dir)
ABresult
0X0
100
111
213 && ! $self->filter( not_in_dirs => name => $dir );
214 }
215  
216  
217 sub accept_file {
218 43 ▶ my ($self, $file) = @_;
Subroutine accept_file: called CC=2 SCAR=6.9
Pod documented
219 43 $self->debug("accept_file($file)") if DEBUG;
220 $self->{ accept_file }->($self, $file)
221 43 ▶ if $self->{ accept_file };
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"accept_file"} 3 40 43
222 43 return $self->collect($file);
223  
224 # return $self->filter( files => name => @_ )
225 # && ! $self->filter( no_files => name => @_ );
226 }
227  
228  
229 sub reject_file {
230 153 ▶ my ($self, $file) = @_;
Subroutine reject_file: called CC=2 SCAR=7.1
Pod undocumented
231 153 $self->debug("reject_file($file)") if DEBUG;
232 return $self->{ reject_file }
233 153 ▶ ? $self->{ reject_file }->($self, $file)
Branch: 1 branch
BranchTrueFalseTotal
$self->{'reject_file'} ? : 0 153 153
234 : 1;
235 }
236  
237  
238 sub accept_directory {
239 7 ▶ my ($self, $dir) = @_;
Subroutine accept_directory: called CC=3 SCAR=12.7
Pod documented
240 7 $self->debug("accept_dir($dir)") if DEBUG;
241 $self->{ accept_dir }->($self, $dir) || return
242 7 ▶ if $self->{ accept_dir };
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"accept_dir"} 0 7 7
Condition25%1/4
$self->{'accept_dir'} && ($self->{'accept_dir'}->($self, $dir) || (return))
!ll
70
$self->{"accept_dir"}->($self, $dir) or return
l!l
00
MC/DC0%0/2
$self->{'accept_dir'} && ($self->{'accept_dir'}->($self, $dir) || (return))
$self->{'accept_dir'} $self->{"accept_dir"}->($self, $dir)
Truth table33%1/3
A && B
A= $self->{'accept_dir'}B= $self->{"accept_dir"}->($self, $dir)
ABresult
0X0
100
111
243 7 return $self->collect($dir);
244 }
245  
246  
247 sub reject_directory {
248 53 ▶ my ($self, $dir) = @_;
Subroutine reject_directory: called CC=2 SCAR=7.1
Pod undocumented
249 53 $self->debug("reject_directory($dir)") if DEBUG;
250 return $self->{ reject_dir }
251 53 ▶ ? $self->{ reject_dir }->($self, $dir)
Branch: 1 branch
BranchTrueFalseTotal
$self->{'reject_dir'} ? : 0 53 53
252 : 1;
253 }
254  
255 sub enter_directory {
256 64 ▶ my ($self, $dir) = @_;
Subroutine enter_directory: called CC=5 SCAR=18.6
Pod documented
257 64 ▶ $self->debug("visiting directory children: $dir") if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Badger::Filesystem::Visitor::DEBUG 0 64 64
258 $self->{ enter_dir }->($self, $dir) || return
259 64 ▶ if $self->{ enter_dir };
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"enter_dir"} 0 64 64
Condition25%1/4
$self->{'enter_dir'} && ($self->{'enter_dir'}->($self, $dir) || (return))
!ll
640
$self->{"enter_dir"}->($self, $dir) or return
l!l
00
MC/DC0%0/2
$self->{'enter_dir'} && ($self->{'enter_dir'}->($self, $dir) || (return))
$self->{'enter_dir'} $self->{"enter_dir"}->($self, $dir)
Truth table33%1/3
A && B
A= $self->{'enter_dir'}B= $self->{"enter_dir"}->($self, $dir)
ABresult
0X0
100
111
260
261 $_->accept($self)
262 64 for $dir->children;
263 # for $dir->children($self->{ all });
264 64 return 1;
265 }
266  
267  
268 sub leave_directory {
269 10 ▶ my ($self, $dir) = @_;
Subroutine leave_directory: called CC=2 SCAR=7.1
Pod undocumented
270 10 $self->debug("leave_directory($dir)") if DEBUG;
271 return $self->{ leave_dir }
272 10 ▶ ? $self->{ leave_dir }->($self, $dir)
Branch: 1 branch
BranchTrueFalseTotal
$self->{'leave_dir'} ? : 0 10 10
273 : 1;
274 }
275  
276  
277 sub collect {
278 63 ▶ my $self = shift;
Subroutine collect: called CC=3 SCAR=11.0
Pod documented
279 63 my $collect = $self->{ collect };
280 63 ▶ push(@$collect, @_) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 50 13 63
281 return wantarray
282 63 ▶ ? @$collect
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 13 50 63
283 : $collect;
284 }
285  
286  
287 # identify() is not currently used
288  
289 sub identify {
290 0 ▶ my ($self, $params) = self_params(@_);
Subroutine identify: not called CC=3 SCAR=24.8
Pod documented
291 0 my $identify = $self->{ identify };
292 0 ▶ @$identify{ keys %$params } = values %$params
Branch: 1 branch
BranchTrueFalseTotal
if %$params 0 0 0
293 if %$params;
294 return wantarray
295 0 ▶ ? %$identify
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
296 : $identify;
297 }
298  
299  
300 1 1;
301  
302 __END__
« lib/Badger/Config/Schema.pm ↑ Summary lib/Badger/Data/Facet/Text.pm »