lib/App/Relate/Complex.pm

filter: Statementstmt 20.9% 42 / 201 Branchbran 4.5% 1 / 22 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 42.4% 14 / 33 Podpod 100% 19 / 19 totaltotal 26.8% 76 / 283 CC 15 SCAR 22.9
CC
15
Cov
19%
init
30.0
setup_locate_options
30.0
relate_complex
20.0
CRAP 9.9
↑ Summary
1 package App::Relate::Complex;
2 2 ▶use base qw( Class::Base );
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 =head1 NAME
5  
6 App::Relate::Complex - backend for "relate" script (filtered locate)
7  
8 =head1 SYNOPSIS
9  
10 use App::Relate::Complex;
11  
12 # search the standard locate database
13 my $rh = App::Relate::Complex->new();
14 my $matches = $rh->relate_complex( \@search_terms );
15  
16  
17 # screen the results with a named filter
18 my $matches = $rh->relate_complex( \@terms, { add_filters => [':omit'] });
19  
20  
21 # using options (1) a non-standard locate db, (2) alternate filter storage,
22 # (3) case-insensitive matches
23  
24 # generating a newly created locate database
25 use File::Locate::Harder;
26 my $db_file = "/tmp/special_locate.db";
27 my $flh = File::Locate::Harder->new( db => undef );
28 $flh->create_database( $dir_to_be_indexed, $db_file );
29  
30 # filter storage search path consisting of:
31 # (1) yaml file (2) a DBI database connection
32 my $alt_storage_aref = [
33 $yaml_file,
34 { format => 'DBI',
35 connect_to => $connect_to,
36 owner => $owner,
37 password => $password,
38 },
39 ];
40  
41 my $rh = App::Relate::Complex->new( {
42 storage => $alt_storage_aref,
43 locatedb => $db_file,
44 modifiers => 'i', # force case-insensitive matches
45 } );
46 my $matches = $rh->relate_complex( \@terms );
47  
48  
49 # save_filters_when_used can be used to make user-modifiable copies
50 # of standard filters
51 my $lfar = App::Relate::Complex->new( {
52 storage => $yaml_file,
53 save_filters_when_used => 1,
54 } );
55 my $result = $lfar->relate_complex( \@search_terms ); # by default uses ':skipdull'
56 # A copy of the ":skipdull" filter should now be found in
57 # $yaml_file, where it can be edited: it will take precedence
58 # over the default definition on later runs.
59  
60  
61 =head1 DESCRIPTION
62  
63 Implements the functionality for the L<relate_complex> script, which uses
64 the system's "locate" database to find file names that match
65 multiple search terms.
66  
67 This version uses the List::Filter modules to get some
68 flexibility and persistance advantages.
69  
70 It tries to access the locate database through File::Locate::Harder
71 in order to be relatively portable.
72  
73 =head2 METHODS
74  
75 =over
76  
77 =cut
78  
79 2 ▶use 5.8.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
80 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
81 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
82 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
83 2 ▶use Hash::Util qw( lock_keys unlock_keys );
Subroutine BEGIN: called CC=1 SCAR=0.0
84 2 ▶use Env qw(HOME);
Subroutine BEGIN: called CC=1 SCAR=0.0
85  
86 2 my $DEBUG = 0;
87 2 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=9.2
88 2 ▶ if ($DEBUG) {
Branch: 1 branch
BranchTrueFalseTotal
if ($DEBUG) 0 2 2
89 0 require Data::Dumper;
90 }
91 };
92  
93 2 ▶use File::Path qw(mkpath);
Subroutine BEGIN: called CC=1 SCAR=0.0
94 2 ▶use File::Locate::Harder;
Subroutine BEGIN: called CC=1 SCAR=0.0
95 2 ▶use List::Filter;
Subroutine BEGIN: called CC=1 SCAR=0.0
96 2 ▶use List::Filter::Storage;
Subroutine BEGIN: called CC=1 SCAR=0.0
97 2 ▶use List::Filter::Transform;
Subroutine BEGIN: called CC=1 SCAR=0.0
98 2 ▶use List::Filter::Internal;
Subroutine BEGIN: called CC=1 SCAR=0.0
99  
100 2 our $VERSION = '0.04';
101  
102 =item new
103  
104 Instantiates a new App::Relate::Complex object.
105  
106 Takes an optional hashref as an argument, with named fields,
107 which are very similar but not quite identical to those of
108 L<List::Filter::Storage>.
109  
110 =over
111  
112 =item storage
113  
114 Search path for L<List::Filter> filters, but with an
115 automatically appended handle for the standard filters
116 (defined in the code libraries List::Filter::Library::*)
117  
118 =item write_storage
119  
120 The location (typically a yaml file) that filters are saved to.
121 When we run with "save_filters_when_used" on, accessible copies
122 will be exist here for any filter that's been invoked, even the
123 standard filters.
124  
125 =item transform_storage
126  
127 Like "storage", except for L<List::Filter::Transform> "transforms".
128 Used internally to access one standard filter (the "dwim" transform
129 of "^" into a boundary match). Unlike the case with filter storage,
130 copies of standard transforms are not saved to the write_storage.
131  
132 =item locatedb
133  
134 The "locate" database file used for primary searches. Optional,
135 defaults to the system's main locate database. Setting this is
136 typically done only for testing purposes.
137  
138 =item modifiers
139  
140 Yet another place where perl regexp modifiers can be specified.
141 E.g. a "i" will force case-insensitive matches, overriding any
142 internal modifier settings.
143  
144 =item save_filters_when_used
145  
146 Option to create accessible copies of standard filters that are used
147 (gives the user an easy way to make modifications that override
148 the standard definitions).
149  
150 =item search_filter_name
151  
152 An internally used, temporary filter name, defaults to a value
153 which should be unique to the user (to avoid collisons if shared
154 filter storage is in use). Note: List::Filter at present lacks
155 "anonymous filter" features, this is a work around.
156 Default: _prev_relate_<user>.
157  
158 =back
159  
160 =cut
161  
162 # Note:
163 # "new" is inherited from Class::Base.
164 # It calls the following "init" routine automatically.
165  
166 =item init
167  
168 Initialize object attributes and then lock them down to prevent
169 accidental creation of new ones.
170  
171 =cut
172  
173 0 ▶sub init {
Subroutine init: not called CC=5 SCAR=34.0
Pod documented
174 0 my $self = shift;
175 0 my $args = shift;
176 0 unlock_keys( %{ $self } );
177  
178 0 ▶ my $transform_storage = $args->{ transform_storage } || [];
Condition0%0/2
$args->{'transform_storage'} || []
l!l
00
MC/DC0%0/1
$args->{'transform_storage'} || []
$args->{'transform_storage'}
Truth table0%0/2
A
A= $args->{'transform_storage'}
Aresult
11
00
179  
180 0 my $default_stash_dir = "$HOME/.list-filter";
181 0 mkpath( $default_stash_dir );
182 0 my $default_stash_name = "filters.yaml";
183 0 my $default_stash = "$default_stash_dir/$default_stash_name";
184  
185 0 my $lfi = List::Filter::Internal->new( { default_stash => $default_stash } );
186 0 my $storage = $lfi->qualify_storage( $args->{ storage } );
187  
188 0 ▶ my $write_storage = $args->{write_storage} || $storage->[0];
Condition0%0/3
$args->{'write_storage'} || $storage->[0]
l!l&&r!l&&!r
000
MC/DC0%0/2
$args->{'write_storage'} || $storage->[0]
$args->{'write_storage'} $storage->[0]
Truth table0%0/3
A || B
A= $args->{'write_storage'}B= $storage->[0]
ABresult
1X1
011
000
189  
190 0 my $code_connect_params_filter = {
191 format => 'CODE',
192 };
193 0 push @{ $storage }, $code_connect_params_filter;
194 my $filter_storage = List::Filter::Storage->new(
195 { storage => $storage,
196 save_filters_when_used => $args->{ save_filters_when_used },
197 0 } );
198  
199 0 push @{ $transform_storage }, { format => 'CODE' };
200 0 my $lfth = List::Filter::Storage->new(
201 { type => 'transform',
202 storage => $transform_storage,
203 } );
204  
205 my $flh = File::Locate::Harder->new(
206 { db => $args->{ locatedb },
207 0 } );
208  
209 # creating a filter name for internal use
210 0 my $search_filter_name = '_prev_relate';
211 0 my $user = $ENV{ USER };
212 0 ▶ $search_filter_name .= "_$user" if ($user);
Branch: 1 branch
BranchTrueFalseTotal
if $user 0 0 0
213  
214 # define new attributes
215 my $attributes = {
216 filter_storage => $filter_storage,
217 lfth => $lfth, # list filter transform handle (?)
218 flh => $flh,
219 locatedb => $args->{ locatedb },
220 modifiers => $args->{ modifiers },
221 0 search_filter_name => $search_filter_name,
222 };
223  
224 0 ▶ if( $DEBUG ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($DEBUG) 0 0 0
225 0 $self->debugging(1);
226 }
227  
228 # add attributes to object
229 0 my @fields = (keys %{ $attributes });
230 0 @{ $self }{ @fields } = @{ $attributes }{ @fields }; # hash slice
231  
232 0 lock_keys( %{ $self } );
233 0 return $self;
234 }
235  
236 =item setup_filter_names
237  
238 Routine used internally to setup fitler names from options settings and so on.
239  
240 =cut
241  
242 0 ▶sub setup_filter_names {
Subroutine setup_filter_names: not called CC=3 SCAR=24.8
Pod documented
243 0 my $self = shift;
244 0 my $opt = shift;
245 0 my $default_filters = shift;
246  
247 0 my $filter_names = [];
248 0 ▶ if ( not ($opt->{ no_default_filters })) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opt->{"no_default_filters"}) 0 0 0
249 0 push @{ $filter_names }, @{ $default_filters };
250 };
251 0 ▶ if( my $add_filters_string = $opt->{ add_filters } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $add_filters_string = $opt->{"add_filters"}) 0 0 0
252 # strip any leading and trailing quotes that might have leaked through
253 0 $add_filters_string =~ s{ ^ ['"] }{}x;
254 0 $add_filters_string =~ s{ ['"] $ }{}x;
255 0 my @add_filters = split(' ', $add_filters_string);
256 0 push @{ $filter_names }, @add_filters;
257 }
258  
259 0 return $filter_names;
260 }
261  
262 =item setup_locate_options
263  
264 Routine used internally (by the relate_complex routine) to set up the
265 string of option switches that need to be used with the locate
266 command (now handled by File::Locate::Harder).
267  
268 =cut
269  
270 0 ▶sub setup_locate_options {
Subroutine setup_locate_options: not called CC=5 SCAR=34.0
Pod documented
271 0 my $self = shift;
272 0 my $opt = shift;
273  
274 0 my $modifiers = $self->modifiers;
275 0 my $case_insensitive = 0;
276 0 ▶ if ($modifiers) {
Branch: 1 branch
BranchTrueFalseTotal
if ($modifiers) 0 0 0
277 0 ▶ $case_insensitive = 1 if $modifiers =~ /i/;
Branch: 1 branch
BranchTrueFalseTotal
if $modifiers =~ /i/ 0 0 0
278 }
279  
280 # -r => primary term is a POSIX regexp
281 0 my $regexp_flag;
282 0 ▶ if ( $opt->{regexp} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{"regexp"}) 0 0 0
283 0 $regexp_flag = 1;
284 }
285  
286 # -e => primary term is a POSIX "extended" regexp
287 0 my $posix_extended;
288 0 ▶ if ( $opt->{posix_extended} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{"posix_extended"}) 0 0 0
289 0 $posix_extended = 1;
290 }
291  
292 0 my $flh = $self->flh;
293  
294 0 my $db_file = $self->locatedb;
295 0 $flh->set_db( $db_file );
296 0 $flh->set_case_insensitive( $case_insensitive );
297 0 $flh->set_regexp( $regexp_flag );
298 0 $flh->set_posix_extended( $posix_extended );
299  
300 0 return $flh;
301 }
302  
303  
304  
305 =item relate_complex
306  
307 Searches for matching items in the output from the system's
308 "locate" command, using some named filters (L<List::Filter>)
309 to winnow the results.
310  
311 The list of search patterns may all be perl regexp's (where \x
312 should be assumed), except for the first term, which must be a
313 simple string that can be fed into the locate command (L<locate>).
314 Efficiency is greatly improved if this string
315 is relatively unique.
316  
317 This defaults to using the standard ":skipdull" filter on the
318 search results. If the "save_filters_when_used" option has been
319 enabled, a copy of this will be found in the "write_location": it
320 can be edited, and the modified version will take precedence over
321 the default definition on later runs.
322  
323 Input:
324 (1) (aref) list of search patterns (with the first item simple, but unique)
325 (2) (href) options:
326  
327 =over
328  
329 =item no_default_filters
330  
331 supresses the default filter(s) (cf. ':skipdull')
332  
333 =item add_filters
334  
335 list of filter names to use (in addition to the defaults unless,
336 those have been supressed): this is a space seperated string (not
337 an aref)
338  
339 =item regexp
340  
341 The first item is a POSIX regexp.
342  
343 =back
344  
345 Returns: (aref) matching items that pass the filters
346  
347 Example:
348  
349 @terms = qw( china www var images );
350 $china_pics = $self->relate_complex( \@terms, { filters => [":jpeg"] } );
351  
352 =cut
353  
354 # In outline: here we use two filters with one transform *on* one of the filters
355 0 ▶sub relate_complex {
Subroutine relate_complex: not called CC=4 SCAR=30.0
Pod documented
356 0 my $self = shift;
357 0 my $search_patterns = shift;
358 0 my $opt = shift;
359  
360 0 my $default_filters = [':skipdull'];
361 0 my $filter_names = $self->setup_filter_names( $opt, $default_filters );
362  
363 #(0) shift off the special first term (the seed)
364 #(1) transform input array of regexps $search_patterns
365 # ( or vice-versa, if -r is in use )
366  
367 0 my $dwim_upcaret_for_paths
368 = $self->lfth->lookup( ':dwim_upcaret' );
369  
370 0 my ($primary, $primary_save, $dwim_search_patterns);
371 0 $primary_save = shift( @{ $search_patterns } );
372 0 ▶ if( $opt->{regexp} ) { # if the first item is a regexp, than do the dwim magic to it too
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{'regexp'}) { } 0 0 0
373  
374 0 $primary = $dwim_upcaret_for_paths->apply( [ $primary_save ] )->[0];
375  
376 0 $dwim_search_patterns =
377 $dwim_upcaret_for_paths->apply( $search_patterns );
378  
379 } else {
380  
381 0 $primary = $primary_save;
382  
383 0 $dwim_search_patterns =
384 $dwim_upcaret_for_paths->apply( $search_patterns );
385 }
386  
387  
388 #(2) get raw listing of hits from locate using the seed term
389  
390 0 $self->setup_locate_options( $opt );
391 0 my $flh = $self->flh;
392 0 my $raw = $flh->locate( $primary );
393  
394 #(3) filter the raw results with a newly created filter from input array
395  
396 0 my $search_filter_name = $self->search_filter_name;
397  
398 0 my $modifiers = $self->modifiers;
399 0 my $search_filter = List::Filter->new( {
400 name => $search_filter_name,
401 description => "The last relate search",
402 method => "match_all_any_order",
403 terms => $dwim_search_patterns,
404 modifiers => $modifiers,
405 } );
406  
407 0 my $intermed = $search_filter->apply( $raw );
408  
409 # (4) filter using standard named omit filter(s) (":skipdull")
410  
411 # loop over filters applying each one in turn.
412 0 my $filter_storage = $self->filter_storage;
413 0 foreach my $filter_name (@{ $filter_names }) {
414 0 ▶ if ( my $filter = $filter_storage->lookup( $filter_name ) ){
Branch: 1 branch
BranchTrueFalseTotal
if (my $filter = $filter_storage->lookup($filter_name)) { } 0 0 0
415  
416 0 $intermed = $filter->apply( $intermed );
417 } else {
418 0 warn "Could not lookup filter named: $filter_name";
419 }
420 }
421 0 my $result = $intermed;
422  
423 # (5) cleaning up modification to search terms aref
424 # (avoiding a surprising side-effect, without copying array)
425 0 unshift @{ $search_patterns }, $primary_save;
426  
427 0 return $result;
428 }
429  
430  
431  
432 =item list_filters
433  
434 Returns a list of all avaliable named filters.
435  
436 =cut
437  
438 0 ▶sub list_filters {
Subroutine list_filters: not called CC=1 SCAR=6.9
Pod documented
439 0 my $self = shift;
440 0 my $search_patterns = shift;
441 0 my $opt = shift;
442  
443 0 my $modifiers = $self->modifiers;
444  
445 0 my $filter_storage = $self->filter_storage;
446 0 my $filters = $filter_storage->list_filters;
447  
448 0 my $search_filter_name = $self->search_filter_name;
449  
450 0 my $search_filter = List::Filter->new( {
451 name => $search_filter_name,
452 description => "The last relate filter listing",
453 method => "match_all_any_order",
454 terms => $search_patterns,
455 modifiers => $modifiers,
456 } );
457  
458 0 my $filtered_filters = $search_filter->apply( $filters );
459  
460 0 return $filtered_filters;
461 }
462  
463  
464  
465 =back
466  
467 =head2 basic setters and getters
468  
469 =over
470  
471 =item storage
472  
473 Getter for object attribute storage
474  
475 =cut
476  
477 0 ▶sub storage {
Subroutine storage: not called CC=1 SCAR=6.9
Pod documented
478 0 my $self = shift;
479 0 my $storage = $self->{ storage };
480 0 return $storage;
481 }
482  
483 =item set_storage
484  
485 Setter for object attribute set_storage
486  
487 =cut
488  
489 0 ▶sub set_storage {
Subroutine set_storage: not called CC=1 SCAR=6.9
Pod documented
490 0 my $self = shift;
491 0 my $storage = shift;
492 0 $self->{ storage } = $storage;
493 0 return $storage;
494 }
495  
496  
497  
498 =item locatedb
499  
500 Getter for object attribute locatedb
501  
502 =cut
503  
504 0 ▶sub locatedb {
Subroutine locatedb: not called CC=1 SCAR=6.9
Pod documented
505 0 my $self = shift;
506 0 my $locatedb = $self->{ locatedb };
507 0 return $locatedb;
508 }
509  
510 =item set_locatedb
511  
512 Setter for object attribute locatedb
513  
514 =cut
515  
516 0 ▶sub set_locatedb {
Subroutine set_locatedb: not called CC=1 SCAR=6.9
Pod documented
517 0 my $self = shift;
518 0 my $locatedb = shift;
519 0 $self->{ locatedb } = $locatedb;
520 0 return $locatedb;
521 }
522  
523  
524  
525 =item modifiers
526  
527 Getter for object attribute modifiers
528  
529 =cut
530  
531 0 ▶sub modifiers {
Subroutine modifiers: not called CC=1 SCAR=6.9
Pod documented
532 0 my $self = shift;
533 0 my $modifiers = $self->{ modifiers };
534 0 return $modifiers;
535 }
536  
537 =item set_modifiers
538  
539 Setter for object attribute set_modifiers
540  
541 =cut
542  
543 0 ▶sub set_modifiers {
Subroutine set_modifiers: not called CC=1 SCAR=6.9
Pod documented
544 0 my $self = shift;
545 0 my $modifiers = shift;
546 0 $self->{ modifiers } = $modifiers;
547 0 return $modifiers;
548 }
549  
550  
551 =item filter_storage
552  
553 Getter for object attribute filter_storage
554  
555 =cut
556  
557 0 ▶sub filter_storage {
Subroutine filter_storage: not called CC=1 SCAR=6.9
Pod documented
558 0 my $self = shift;
559 0 my $filter_storage = $self->{ filter_storage };
560 0 return $filter_storage;
561 }
562  
563 =item set_filter_storage
564  
565 Setter for object attribute set_filter_storage
566  
567 =cut
568  
569 0 ▶sub set_filter_storage {
Subroutine set_filter_storage: not called CC=1 SCAR=6.9
Pod documented
570 0 my $self = shift;
571 0 my $filter_storage = shift;
572 0 $self->{ filter_storage } = $filter_storage;
573 0 return $filter_storage;
574 }
575  
576 =item lfth
577  
578 Getter for object attribute lfth
579  
580 =cut
581  
582 0 ▶sub lfth {
Subroutine lfth: not called CC=1 SCAR=6.9
Pod documented
583 0 my $self = shift;
584 0 my $lfth = $self->{ lfth };
585 0 return $lfth;
586 }
587  
588 =item set_lfth
589  
590 Setter for object attribute set_lfth
591  
592 =cut
593  
594 0 ▶sub set_lfth {
Subroutine set_lfth: not called CC=1 SCAR=6.9
Pod documented
595 0 my $self = shift;
596 0 my $lfth = shift;
597 0 $self->{ lfth } = $lfth;
598 0 return $lfth;
599 }
600  
601  
602 =item flh
603  
604 Getter for object attribute flh
605  
606 =cut
607  
608 0 ▶sub flh {
Subroutine flh: not called CC=1 SCAR=6.9
Pod documented
609 0 my $self = shift;
610 0 my $flh = $self->{ flh };
611 0 return $flh;
612 }
613  
614 =item set_flh
615  
616 Setter for object attribute set_flh
617  
618 =cut
619  
620 0 ▶sub set_flh {
Subroutine set_flh: not called CC=1 SCAR=6.9
Pod documented
621 0 my $self = shift;
622 0 my $flh = shift;
623 0 $self->{ flh } = $flh;
624 0 return $flh;
625 }
626  
627  
628 =item search_filter_name
629  
630 Getter for object attribute search_filter_name
631  
632 =cut
633  
634 0 ▶sub search_filter_name {
Subroutine search_filter_name: not called CC=1 SCAR=6.9
Pod documented
635 0 my $self = shift;
636 0 my $search_filter_name = $self->{ search_filter_name };
637 0 return $search_filter_name;
638 }
639  
640 =item set_search_filter_name
641  
642 Setter for object attribute set_search_filter_name
643  
644 =cut
645  
646 0 ▶sub set_search_filter_name {
Subroutine set_search_filter_name: not called CC=1 SCAR=6.9
Pod documented
647 0 my $self = shift;
648 0 my $search_filter_name = shift;
649 0 $self->{ search_filter_name } = $search_filter_name;
650 0 return $search_filter_name;
651 }
652  
653 2 1;
654  
655 =back
656  
657 =head1 SEE ALSO
658  
659 L<List::Filter>
660 L<List::Filter::Project>
661  
662 =head1 AUTHOR
663  
664 Joseph Brenner, E<lt>doom@kzsu.stanford.eduE<gt>
665  
666 =head1 COPYRIGHT AND LICENSE
667  
668 Copyright (C) 2007 by Joseph Brenner
669  
670 This library is free software; you can redistribute it and/or modify
671 it under the same terms as Perl itself, either Perl version 5.8.2 or,
672 at your option, any later version of Perl 5 you may have available.
673  
674 =head1 BUGS
675  
676 None reported... yet.
677  
678 =cut
↑ Summary