DirWatch.pm

filter: Statementstmt 84.8% 73 / 86 Branchbran 55.0% 22 / 40 Conditioncond 16.6% 1 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 70.0% 14 / 20 Podpod 100% 5 / 5 totaltotal 71.4% 115 / 161 CC 23 SCAR 19.1
CC
23
Cov
73%
_poll
17.6
shutdown
6.0
_resume
4.2
CRAP 6.7
« DirWatch/Unmodified.pm ↑ Summary DirWatch/Modified.pm »
1 package POE::Component::DirWatch;
2  
3 5 our $VERSION = "0.300002";
4  
5 5 ▶use POE;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use Class::Load;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use MooseX::Types::Path::Class qw/Dir/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 sub import {
11 8 ▶ my ($class, %args) = @_;
Subroutine import: called CC=4 SCAR=13.9
12 8 ▶ return if delete $args{no_aio};
Branch: 1 branch
BranchTrueFalseTotal
if delete $args{"no_aio"} 1 7 8
13 7 ▶ return unless eval { Class::Load::load_class("POE::Component::AIO") };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { Class::Load::load_class("POE::Component::AIO") } } 0 7 7
14 7 ▶ if (eval { Class::Load::load_class("POE::Component::DirWatch::Role::AIO") }){
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { Class::Load::load_class("POE::Component::DirWatch::Role::AIO") } }) 7 0 7
15 7 $class->meta->make_mutable;
16 7 POE::Component::DirWatch::Role::AIO->meta->apply($class->meta);
17 7 $class->meta->make_immutable;
18 }
19 7 return;
20 }
21  
22 #--------#---------#---------#---------#---------#---------#---------#--------#
23  
24 5 has alias => (
25 is => 'ro',
26 isa => 'Str',
27 required => 1,
28 default => 'dirwatch'
29 );
30  
31 5 has directory => (
32 is => 'rw',
33 isa => Dir,
34 required => 1,
35 coerce => 1
36 );
37  
38 5 has interval => (
39 is => 'rw',
40 isa => 'Int',
41 required => 1
42 );
43  
44 5 has next_poll => (
45 is => 'rw',
46 isa => 'Int',
47 clearer => 'clear_next_poll',
48 predicate => 'has_next_poll'
49 );
50  
51 5 has filter => (
52 is => 'rw',
53 isa => 'CodeRef',
54 clearer => 'clear_filter',
55 predicate => 'has_filter'
56 );
57  
58 5 has dir_callback => (
59 is => 'rw',
60 isa => 'Ref',
61 clearer => 'clear_dir_callback',
62 predicate => 'has_dir_callback'
63 );
64  
65 5 has file_callback => (
66 is => 'rw',
67 isa => 'Ref',
68 clearer => 'clear_file_callback',
69 predicate => 'has_file_callback'
70 );
71  
72 sub BUILD {
73 4 ▶ my ($self, $args) = @_;
Subroutine BUILD: called CC=3 SCAR=12.0
Pod documented
74 4 ▶ POE::Session->create(
Branch: 2 branches
BranchTrueFalseTotal
$self->has_dir_callback ? : 0 4 4
$self->has_file_callback ? : 4 0 4
75 object_states => [
76 $self,
77 {
78 _start => '_start',
79 _pause => '_pause',
80 _resume => '_resume',
81 _child => '_child',
82 _stop => '_stop',
83 shutdown => '_shutdown',
84 poll => '_poll',
85 ($self->has_dir_callback ? (dir_callback => '_dir_callback') : () ),
86 ($self->has_file_callback ? (file_callback => '_file_callback') : () ),
87 },
88 ]
89 );
90 }
91  
92 0 ▶sub session { $poe_kernel->alias_resolve( shift->alias ) }
Subroutine session: not called CC=1 SCAR=6.9
Pod documented
93  
94 #--------#---------#---------#---------#---------#---------#---------#---------
95  
96 sub _start {
97 4 ▶ my ($self, $kernel) = @_[OBJECT, KERNEL];
Subroutine _start: called CC=1 SCAR=0.0
98 4 $kernel->alias_set($self->alias); # set alias for ourselves and remember it
99 4 $self->next_poll( $kernel->delay_set(poll => $self->interval) );
100 }
101  
102 sub _pause {
103 3 ▶ my ($self, $kernel, $until) = @_[OBJECT, KERNEL, ARG0];
Subroutine _pause: called CC=4 SCAR=14.0
104 3 ▶ $kernel->alarm_remove($self->next_poll) if $self->has_next_poll;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_next_poll 3 0 3
105 3 $self->clear_next_poll;
106 3 ▶ return unless defined $until;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $until 2 1 3
107  
108 1 my $t = time;
109 1 ▶ $until += $t if $t > $until;
Branch: 1 branch
BranchTrueFalseTotal
if $t > $until 0 1 1
110 1 $self->next_poll( $kernel->alarm_set(poll => $until) );
111  
112 }
113  
114 sub _resume {
115 2 ▶ my ($self, $kernel, $when) = @_[OBJECT, KERNEL, ARG0];
Subroutine _resume: called CC=4 SCAR=14.3
116 2 ▶ $kernel->alarm_remove($self->next_poll) if $self->has_next_poll;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_next_poll 0 2 2
117 2 $self->clear_next_poll;
118 2 ▶ $when = 0 unless defined $when;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $when 0 2 2
119  
120 2 my $t = time;
121 2 ▶ $when += $t if $t > $when;
Branch: 1 branch
BranchTrueFalseTotal
if $t > $when 0 2 2
122 2 $self->next_poll( $kernel->alarm_set(poll => $when) );
123 }
124  
125 ▶sub _stop {}
Subroutine _stop: called CC=1 SCAR=0.0
126  
127 ▶sub _child {}
Subroutine _child: not called CC=1 SCAR=0.0
128  
129 #--------#---------#---------#---------#---------#---------#---------#---------
130  
131 sub pause {
132 0 ▶ my ($self, $until) = @_;
Subroutine pause: not called CC=1 SCAR=6.9
Pod documented
133 0 $poe_kernel->call($self->alias, _pause => $until);
134 }
135  
136 sub resume {
137 0 ▶ my ($self, $when) = @_;
Subroutine resume: not called CC=1 SCAR=6.9
Pod documented
138 0 $poe_kernel->call($self->alias, _resume => $when);
139 }
140  
141 sub shutdown {
142 0 ▶ my ($self) = @_;
Subroutine shutdown: not called CC=2 SCAR=17.9
Pod documented
143 0 ▶ $poe_kernel->alarm_remove($self->next_poll) if $self->has_next_poll;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_next_poll 0 0 0
144 0 $self->clear_next_poll;
145 0 $poe_kernel->post($self->alias, 'shutdown');
146 }
147  
148 #--------#---------#---------#---------#---------#---------#---------#---------
149  
150 sub _poll {
151 2 ▶ my ($self, $kernel) = @_[OBJECT, KERNEL];
Subroutine _poll: called CC=11 SCAR=28.7
152 2 $self->clear_next_poll;
153  
154 #just do this part once per poll
155 2 ▶ my $filter = $self->has_filter ? $self->filter : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->has_filter ? : 0 2 2
156 2 my $has_dir_cb = $self->has_dir_callback;
157 2 my $has_file_cb = $self->has_file_callback;
158  
159 2 ▶ while (my $child = $self->directory->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $child = $self->directory->next) 8 2 10
160 8 ▶ if($child->is_dir){
Branch: 1 branch
BranchTrueFalseTotal
if ($child->is_dir) { } 4 4 8
161 4 ▶ next unless $has_dir_cb;
Branch: 1 branch
BranchTrueFalseTotal
unless $has_dir_cb 4 0 4
162 0 ▶ next if ref $filter && !$filter->($child);
Branch: 1 branch
BranchTrueFalseTotal
if ref $filter and not &$filter($child) 0 0 0
Condition0%0/3
ref $filter and not &$filter($child)
!ll&&!rl&&r
000
MC/DC0%0/2
ref $filter and not &$filter($child)
ref $filter not &$filter($child)
Truth table0%0/3
A and not(B)
A= ref $filterB= not &$filter($child)
ABresult
0X0
100
111
163 0 $kernel->yield(dir_callback => $child);
164 } else {
165 4 ▶ next unless $has_file_cb;
Branch: 1 branch
BranchTrueFalseTotal
unless $has_file_cb 0 4 4
166 4 ▶ next if $child->basename =~ /^\.+$/;
Branch: 1 branch
BranchTrueFalseTotal
if $child->basename =~ /^\.+$/ 0 4 4
167 4 ▶ next if ref $filter && !$filter->($child);
Branch: 1 branch
BranchTrueFalseTotal
if ref $filter and not &$filter($child) 0 4 4
Condition33%1/3
ref $filter and not &$filter($child)
!ll&&!rl&&r
400
MC/DC0%0/2
ref $filter and not &$filter($child)
ref $filter not &$filter($child)
Truth table33%1/3
A and not(B)
A= ref $filterB= not &$filter($child)
ABresult
0X0
100
111
168 4 $kernel->yield(file_callback => $child);
169 }
170 }
171  
172 2 $self->next_poll( $kernel->delay_set(poll => $self->interval) );
173 }
174  
175 #these are only here so allow method modifiers to hook into them
176 #these are prime candidates for inlining when the class is made immutable
177 sub _file_callback {
178 11 ▶ my ($self, $kernel, $file) = @_[OBJECT, KERNEL, ARG0];
Subroutine _file_callback: called CC=1 SCAR=0.0
179 11 $self->file_callback->($file);
180 }
181  
182 sub _dir_callback {
183 0 ▶ my ($self, $kernel, $dir) = @_[OBJECT, KERNEL, ARG0];
Subroutine _dir_callback: not called CC=1 SCAR=6.9
184 0 $self->dir_callback->($dir);
185 }
186  
187 #--------#---------#---------#---------#---------#---------#---------#---------
188  
189 sub _shutdown {
190 4 ▶ my ($self, $kernel, $heap) = @_[OBJECT, KERNEL, HEAP];
Subroutine _shutdown: called CC=1 SCAR=0.0
191 #cleaup heap, alias, alarms (no lingering refs n ish)
192 4 %$heap = ();
193 4 $kernel->alias_remove($self->alias);
194 4 $kernel->alarm_remove_all();
195 }
196  
197 #--------#---------#---------#---------#---------#---------#---------#---------
198  
199 5 __PACKAGE__->meta->make_immutable;
200  
201 5 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
202  
203 5 1;
204  
205 __END__;
206  
207 =head1 NAME
208  
209 POE::Component::DirWatch - POE directory watcher
210  
211 =head1 SYNOPSIS
212  
213 use POE::Component::DirWatch;
214  
215 my $watcher = POE::Component::DirWatch->new
216 (
217 alias => 'dirwatch',
218 directory => '/some_dir',
219 filter => sub { $_[0]->is_file ? $_[0] =~ /\.gz$/ : 1 },
220 dir_callback => sub{ ... },
221 file_callback => sub{ ... },
222 interval => 1,
223 );
224  
225 $poe_kernel->run;
226  
227 =head1 DESCRIPTION
228  
229 POE::Component::DirWatch watches a directory for files or directories.
230 Upon finding either it will invoke a user-supplied callback function
231 depending on whether the item is a file or directory.
232  
233 =head1 ASYNCHRONOUS IO SUPPORT
234  
235 This object supports asynchronous IO access using L<IO::AIO>. At load time,
236 the class will detect whether IO::AIO is present in the host system and, if it
237 is present, apply the L<POE::Component::DirWatch::Role::AIO> role to the
238 current class, adding the C<aio> attribute, the <aio_callback> event, and
239 replacing C<_poll> with an asynchronous version. If you do not wish to use AIO
240 you can specify so with he C<no_aio> flag like this:
241  
242 use POE::Component::DirWatch (no_aio => 1);
243  
244 =head1 ATTRIBUTES
245  
246 =head2 alias
247  
248 Read only alias for the DirWatch session. Defaults to C<dirwatch> if not
249 specified. You can NOT rename a session at runtime.
250  
251 =head2 directory
252  
253 Read-write, required. A L<Path::Class::Dir> object for the directory watched.
254 Automatically coerces strings into L<Path::Class::Dir> objects.
255  
256 =head2 interval
257  
258 Required read-write integer representing interval between the end of a poll
259 event and the scheduled start of the next. Defaults to 1.
260  
261 =head2 file_callback
262  
263 =over 4
264  
265 =item B<has_file_callback> - predicate
266  
267 =item B<clear_file_callback> - clearer
268  
269 =back
270  
271 Optional read-write code reference to call when a file is found. The code
272 reference will passed a single argument, a L<Path::Class::File> object
273 representing the file found. It usually makes most sense to process the file
274 and remove it from the directory to avoid duplicate processing
275  
276 =head2 dir_callback
277  
278 =over 4
279  
280 =item B<has_dir_callback> - predicate
281  
282 =item B<clear_dir_callback> - clearer
283  
284 =back
285  
286 Optional read-write code reference to call when a directory is found. The code
287 reference will passed a single argument, a L<Path::Class::Dir> object
288 representing the directory found.
289  
290 =head2 filter
291  
292 =over 4
293  
294 =item B<has_filter> - predicate
295  
296 =item B<clear_filter> - clearer
297  
298 =back
299  
300 An optional read-write code reference that, if present, will be called for each
301 item in the watched directory. The code reference will passed a single
302 argument, a L<Path::Class::File> or L<Path::Class::Dir> object representing
303 the file/dir found. The code should return true if the callback should be
304 called and false if the file should be ignored.
305  
306 =head2 next_poll
307  
308 =over 4
309  
310 =item B<has_next_poll> - predicate
311  
312 =item B<clear_next_poll> - clearer
313  
314 =back
315  
316 The ID of the alarm for the next scheduled poll, if any. Has clearer
317 and predicate methods named C<clear_next_poll> and C<has_next_poll>.
318 Please note that clearing the C<next_poll> just clears the next poll id,
319 it does not remove the alarm, please use C<pause> for that.
320  
321 =head1 OBJECT METHODS
322  
323 =head2 new( \%attrs)
324  
325 See SYNOPSIS and ATTRIBUTES.
326  
327 =head2 session
328  
329 Returns a reference to the actual POE session.
330 Please avoid this unless you are subclassing. Even then it is recommended that
331 it is always used as C<$watcher-E<gt>session-E<gt>method> because copying the
332 object reference around could create a problem with lingering references.
333  
334 =head2 pause [$until]
335  
336 Synchronous call to _pause. This just posts an immediate _pause event to the
337 kernel.
338  
339 =head2 resume [$when]
340  
341 Synchronous call to _resume. This just posts an immediate _resume event to the
342 kernel.
343  
344 =head2 shutdown
345  
346 Convenience method that posts a FIFO shutdown event.
347  
348 =head2 meta
349  
350 See L<Moose>;
351  
352 =head1 EVENT HANDLING METHODS
353  
354 These methods are not part of the public interface of this class, and expect
355 to be called from whithin POE with the standard positional arguments.
356 Use them at your own risk.
357  
358 =head2 _start
359  
360 Runs when C<$poe_kernel-E<gt>run> is called to set the session's alias and
361 schedule the first C<poll> event.
362  
363 =head2 _poll
364  
365 Triggered by the C<poll> event this is the re-occurring action. _poll will use
366 get a list of all items in the directory and call the appropriate callback.
367  
368 =head2 _file_callback
369  
370 Will execute the C<file_callback> code reference, if any.
371  
372 =head2 _pause [$until]
373  
374 Triggered by the C<_pause> event this method will remove the alarm scheduling
375 the next directory poll. It takes an optional argument of $until, which
376 dictates when the polling should begin again. If $until is an integer smaller
377 than the result of time() it will treat $until as the number of seconds to wait
378 before polling. If $until is an integer larger than the result of time() it
379 will treat $until as an epoch timestamp.
380  
381 #these two are the same thing
382 $watcher->pause( time() + 60);
383 $watcher->pause( 60 );
384  
385 #this is one also the same
386 $watcher->pause;
387 $watcher->resume( 60 );
388  
389  
390 =head2 _resume [$when]
391  
392 Triggered by the C<_resume> event this method will remove the alarm scheduling
393 the next directory poll (if any) and schedule a new poll alarm. It takes an
394 optional argument of $when, which dictates when the polling should begin again.
395 If $when is an integer smaller than the result of time() it will treat $until
396 as the number of seconds to wait before polling. If $until is an integer larger
397 than the result of time() it will treat $when as an epoch timestamp and
398 schedule the poll alarm accordingly. If not specified, the alarm will be
399 scheduled with a delay of zero.
400  
401 =head2 _shutdown
402  
403 Delete the C<heap>, remove the alias we are using and remove all set alarms.
404  
405 =head2 BUILD
406  
407 Constructor. C<create()>s a L<POE::Session>.
408  
409 =head1 TODO
410  
411 =over 4
412  
413 =item More examples
414  
415 =item More tests
416  
417 =item ChangeNotify support (patches welcome!)
418  
419 =back
420  
421 =head1 SEE ALSO
422  
423 L<POE::Session>, L<POE::Component>, L<Moose>, L<POE>,
424  
425 The git repository for this project can be found in on github,
426 L<http://github.com/arcanez/poe-component-dirwatch/>
427  
428 =head1 AUTHOR
429  
430 Guillermo Roditi, <groditi@cpan.org>
431  
432 =head1 BUGS
433  
434 Please report any bugs or feature requests to
435 C<bug-poe-component-dirwatch at rt.cpan.org>, or through the web interface at
436 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=POE-Component-DirWatch>.
437 I will be notified, and then you'll automatically be notified of progress on
438 your bug as I make changes.
439  
440 =head1 ACKNOWLEDGEMENTS
441  
442 =over 4
443  
444 =item #poe & #moose on irc.perl.org
445  
446 =item Matt S Trout
447  
448 =item Rocco Caputo
449  
450 =item Charles Reiss
451  
452 =item Stevan Little
453  
454 =item Eric Cholet
455  
456 =back
457  
458 =head1 COPYRIGHT
459  
460 Copyright 2006-2008 Guillermo Roditi. This is free software; you may
461 redistribute it and/or modify it under the same terms as Perl itself.
462  
463 =cut
« DirWatch/Unmodified.pm ↑ Summary DirWatch/Modified.pm »