DirWatch.pm

filter: Statementstmt 84.3% 70 / 83 Branchbran 55.0% 22 / 40 Conditioncond 16.6% 1 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 68.4% 13 / 19 Podpod 100% 5 / 5 totaltotal 70.7% 111 / 157 CC 23 SCAR 19.3
CC
23
Cov
72%
_poll
17.6
shutdown
6.0
_resume
4.2
CRAP 6.9
« DirWatch/Unmodified.pm ↑ Summary DirWatch/Modified.pm »
1 package POE::Component::DirWatch;
2  
3 5 our $VERSION = "0.300001";
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::MOP;
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::MOP::load_class("POE::Component::AIO") };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { Class::MOP::load_class("POE::Component::AIO") } } 0 7 7
14 7 ▶ if (eval { Class::MOP::load_class("POE::Component::DirWatch::Role::AIO") }){
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { Class::MOP::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 1;
202  
203 __END__;
204  
205 =head1 NAME
206  
207 POE::Component::DirWatch - POE directory watcher
208  
209 =head1 SYNOPSIS
210  
211 use POE::Component::DirWatch;
212  
213 my $watcher = POE::Component::DirWatch->new
214 (
215 alias => 'dirwatch',
216 directory => '/some_dir',
217 filter => sub { $_[0]->is_file ? $_[0] =~ /\.gz$/ : 1 },
218 dir_callback => sub{ ... },
219 file_callback => sub{ ... },
220 interval => 1,
221 );
222  
223 $poe_kernel->run;
224  
225 =head1 DESCRIPTION
226  
227 POE::Component::DirWatch watches a directory for files or directories.
228 Upon finding either it will invoke a user-supplied callback function
229 depending on whether the item is a file or directory.
230  
231 =head1 ASYNCHRONOUS IO SUPPORT
232  
233 This object supports asynchronous IO access using L<IO::AIO>. At load time,
234 the class will detect whether IO::AIO is present in the host system and, if it
235 is present, apply the L<POE::Component::DirWatch::Role::AIO> role to the
236 current class, adding the C<aio> attribute, the <aio_callback> event, and
237 replacing C<_poll> with an asynchronous version. If you do not wish to use AIO
238 you can specify so with he C<no_aio> flag like this:
239  
240 use POE::Component::DirWatch (no_aio => 1);
241  
242 =head1 ATTRIBUTES
243  
244 =head2 alias
245  
246 Read only alias for the DirWatch session. Defaults to C<dirwatch> if not
247 specified. You can NOT rename a session at runtime.
248  
249 =head2 directory
250  
251 Read-write, required. A L<Path::Class::Dir> object for the directory watched.
252 Automatically coerces strings into L<Path::Class::Dir> objects.
253  
254 =head2 interval
255  
256 Required read-write integer representing interval between the end of a poll
257 event and the scheduled start of the next. Defaults to 1.
258  
259 =head2 file_callback
260  
261 =over 4
262  
263 =item B<has_file_callback> - predicate
264  
265 =item B<clear_file_callback> - clearer
266  
267 =back
268  
269 Optional read-write code reference to call when a file is found. The code
270 reference will passed a single argument, a L<Path::Class::File> object
271 representing the file found. It usually makes most sense to process the file
272 and remove it from the directory to avoid duplicate processing
273  
274 =head2 dir_callback
275  
276 =over 4
277  
278 =item B<has_dir_callback> - predicate
279  
280 =item B<clear_dir_callback> - clearer
281  
282 =back
283  
284 Optional read-write code reference to call when a directory is found. The code
285 reference will passed a single argument, a L<Path::Class::Dir> object
286 representing the directory found.
287  
288 =head2 filter
289  
290 =over 4
291  
292 =item B<has_filter> - predicate
293  
294 =item B<clear_filter> - clearer
295  
296 =back
297  
298 An optional read-write code reference that, if present, will be called for each
299 item in the watched directory. The code reference will passed a single
300 argument, a L<Path::Class::File> or L<Path::Class::Dir> object representing
301 the file/dir found. The code should return true if the callback should be
302 called and false if the file should be ignored.
303  
304 =head2 next_poll
305  
306 =over 4
307  
308 =item B<has_next_poll> - predicate
309  
310 =item B<clear_next_poll> - clearer
311  
312 =back
313  
314 The ID of the alarm for the next scheduled poll, if any. Has clearer
315 and predicate methods named C<clear_next_poll> and C<has_next_poll>.
316 Please note that clearing the C<next_poll> just clears the next poll id,
317 it does not remove the alarm, please use C<pause> for that.
318  
319 =head1 OBJECT METHODS
320  
321 =head2 new( \%attrs)
322  
323 See SYNOPSIS and ATTRIBUTES.
324  
325 =head2 session
326  
327 Returns a reference to the actual POE session.
328 Please avoid this unless you are subclassing. Even then it is recommended that
329 it is always used as C<$watcher-E<gt>session-E<gt>method> because copying the
330 object reference around could create a problem with lingering references.
331  
332 =head2 pause [$until]
333  
334 Synchronous call to _pause. This just posts an immediate _pause event to the
335 kernel.
336  
337 =head2 resume [$when]
338  
339 Synchronous call to _resume. This just posts an immediate _resume event to the
340 kernel.
341  
342 =head2 shutdown
343  
344 Convenience method that posts a FIFO shutdown event.
345  
346 =head2 meta
347  
348 See L<Moose>;
349  
350 =head1 EVENT HANDLING METHODS
351  
352 These methods are not part of the public interface of this class, and expect
353 to be called from whithin POE with the standard positional arguments.
354 Use them at your own risk.
355  
356 =head2 _start
357  
358 Runs when C<$poe_kernel-E<gt>run> is called to set the session's alias and
359 schedule the first C<poll> event.
360  
361 =head2 _poll
362  
363 Triggered by the C<poll> event this is the re-occurring action. _poll will use
364 get a list of all items in the directory and call the appropriate callback.
365  
366 =head2 _file_callback
367  
368 Will execute the C<file_callback> code reference, if any.
369  
370 =head2 _pause [$until]
371  
372 Triggered by the C<_pause> event this method will remove the alarm scheduling
373 the next directory poll. It takes an optional argument of $until, which
374 dictates when the polling should begin again. If $until is an integer smaller
375 than the result of time() it will treat $until as the number of seconds to wait
376 before polling. If $until is an integer larger than the result of time() it
377 will treat $until as an epoch timestamp.
378  
379 #these two are the same thing
380 $watcher->pause( time() + 60);
381 $watcher->pause( 60 );
382  
383 #this is one also the same
384 $watcher->pause;
385 $watcher->resume( 60 );
386  
387  
388 =head2 _resume [$when]
389  
390 Triggered by the C<_resume> event this method will remove the alarm scheduling
391 the next directory poll (if any) and schedule a new poll alarm. It takes an
392 optional argument of $when, which dictates when the polling should begin again.
393 If $when is an integer smaller than the result of time() it will treat $until
394 as the number of seconds to wait before polling. If $until is an integer larger
395 than the result of time() it will treat $when as an epoch timestamp and
396 schedule the poll alarm accordingly. If not specified, the alarm will be
397 scheduled with a delay of zero.
398  
399 =head2 _shutdown
400  
401 Delete the C<heap>, remove the alias we are using and remove all set alarms.
402  
403 =head2 BUILD
404  
405 Constructor. C<create()>s a L<POE::Session>.
406  
407 =head1 TODO
408  
409 =over 4
410  
411 =item More examples
412  
413 =item More tests
414  
415 =item ChangeNotify support (patches welcome!)
416  
417 =back
418  
419 =head1 SEE ALSO
420  
421 L<POE::Session>, L<POE::Component>, L<Moose>, L<POE>,
422  
423 The git repository for this project can be found in on github,
424 L<http://github.com/arcanez/poe-component-dirwatch/>
425  
426 =head1 AUTHOR
427  
428 Guillermo Roditi, <groditi@cpan.org>
429  
430 =head1 BUGS
431  
432 Please report any bugs or feature requests to
433 C<bug-poe-component-dirwatch at rt.cpan.org>, or through the web interface at
434 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=POE-Component-DirWatch>.
435 I will be notified, and then you'll automatically be notified of progress on
436 your bug as I make changes.
437  
438 =head1 ACKNOWLEDGEMENTS
439  
440 =over 4
441  
442 =item #poe & #moose on irc.perl.org
443  
444 =item Matt S Trout
445  
446 =item Rocco Caputo
447  
448 =item Charles Reiss
449  
450 =item Stevan Little
451  
452 =item Eric Cholet
453  
454 =back
455  
456 =head1 COPYRIGHT
457  
458 Copyright 2006-2008 Guillermo Roditi. This is free software; you may
459 redistribute it and/or modify it under the same terms as Perl itself.
460  
461 =cut
« DirWatch/Unmodified.pm ↑ Summary DirWatch/Modified.pm »