Pilot/Commands.pm

filter: Statementstmt 98.6% 74 / 75 Branchbran 72.2% 13 / 18 Conditioncond 60.0% 3 / 5 MC/DCmcdc 33.3% 1 / 3 Subroutinesub 100% 15 / 15 Podpod 75.0% 3 / 4 totaltotal 90.8% 109 / 120 CC 14 SCAR 11.4
CC
14
Cov
92%
run_cmd
5.6
load_lib
5.0
__ANON__
4.0
CRAP 3.1
« Pilot.pm ↑ Summary Pilot/Logger.pm »
1 # Copyright (c) 2014 Timm Murray
2 # All rights reserved.
3 #
4 # Redistribution and use in source and binary forms, with or without
5 # modification, are permitted provided that the following conditions are met:
6 #
7 # * Redistributions of source code must retain the above copyright notice,
8 # this list of conditions and the following disclaimer.
9 # * Redistributions in binary form must reproduce the above copyright
10 # notice, this list of conditions and the following disclaimer in the
11 # documentation and/or other materials provided with the distribution.
12 #
13 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
14 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
17 # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
23 # POSSIBILITY OF SUCH DAMAGE.
24 package UAV::Pilot::Commands;
25 2 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 2 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 2 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 2 ▶use constant MOD_PREFIX => 'UAV::Pilot';
Subroutine BEGIN: called CC=1 SCAR=0.0
31 2 ▶use constant MOD_SUFFIX => 'Commands';
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33  
34 has 'lib_dirs' => (
35 is => 'ro',
36 isa => 'ArrayRef[Str]',
37 traits => [ 'Array' ],
38 1 ▶ default => sub {[]},
Subroutine __ANON__: called CC=1 SCAR=0.0
39 2 handles => {
40 add_lib_dir => 'push',
41 },
42 );
43 2 has 'condvar' => (
44 is => 'ro',
45 isa => 'AnyEvent::CondVar',
46 );
47 2 has 'controller_callback_ardrone' => (
48 is => 'ro',
49 isa => 'CodeRef',
50 );
51 2 has 'controller_callback_wumpusrover' => (
52 is => 'ro',
53 isa => 'CodeRef',
54 );
55 has 'quit_subs' => (
56 traits => ['Array'],
57 is => 'ro',
58 isa => 'ArrayRef[CodeRef]',
59 1 ▶ default => sub {[]},
Subroutine __ANON__: called CC=1 SCAR=0.0
60 2 handles => {
61 '_push_quit_sub' => 'push',
62 },
63 );
64  
65 2 our $s;
66  
67 #
68 # Sole command that can run without loading other libraries
69 #
70 sub load ($;$)
71 {
72 4 ▶ my ($mod_name, $args) = @_;
Subroutine load: called CC=2 SCAR=7.1
Pod documented
73 4 ▶ $$args{condvar} = $s->condvar unless exists $$args{condvar};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args->{"condvar"} 4 0 4
74 4 $s->load_lib( $mod_name, $args );
75 }
76  
77  
78 sub run_cmd
79 {
80 8 ▶ my ($self, $cmd) = @_;
Subroutine run_cmd: called CC=5 SCAR=17.3
Pod documented
81 8 ▶ if( (! defined $self) && (! ref($self)) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self or ref $self) 0 8 8
Condition33%1/3
defined $self or ref $self
l!l&&r!l&&!r
800
MC/DC0%0/2
defined $self or ref $self
defined $self ref $self
Truth table33%1/3
A or B
A= defined $selfB= ref $self
ABresult
1X1
011
000
82 # Must be called with a $self, not directly via package
83 0 return 0;
84 }
85 8 ▶ return 1 unless defined $cmd;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cmd 0 8 8
86  
87 8 $s = $self;
88 8 eval $cmd;
89 8 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 3 5 8
90  
91 5 return 1;
92 }
93  
94 sub quit
95 {
96 1 ▶ my ($self) = @_;
Subroutine quit: called CC=2 SCAR=6.9
Pod undocumented
97 1 $_->() for @{ $self->{quit_subs} };
98 1 return 1;
99 }
100  
101  
102 sub load_lib
103 {
104 4 ▶ my ($self, $mod_name, $args) = @_;
Subroutine load_lib: called CC=5 SCAR=16.1
Pod documented
105 4 ▶ my $dest_namespace = delete $args->{namespace} // 'UAV::Pilot::Commands';
Condition100%2/2
delete $args->{"namespace"} // "UAV::Pilot::Commands"
l!l
13
MC/DC100%1/1
delete $args->{"namespace"} // "UAV::Pilot::Commands"
delete $args->{"namespace"}
Truth table100%2/2
A
A= delete $args->{"namespace"}
Aresult
11
00
106
107 # This works via the hooks placed into @INC array, which is documented
108 # in perlfunc under the require() entry. In short, we can stick a
109 # subref in @INC and mess around with how Perl loads up the module.
110 # By choosing the starting text, we can control the exact namespace
111 # where the module will end up.
112  
113 4 my @orig_inc = @INC;
114 4 local @INC = (
115 $self->_get_load_module_sub( $dest_namespace, \@orig_inc ),
116 @INC,
117 );
118  
119 4 my $full_mod_name = $self->MOD_PREFIX
120 . '::' . $mod_name
121 . '::' . $self->MOD_SUFFIX;
122  
123 4 eval "require $full_mod_name";
124 4 ▶ die "Could not load $mod_name: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 1 3 4
125  
126 3 ▶ if( my $call = $dest_namespace->can( 'uav_module_init' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $call = $dest_namespace->can("uav_module_init")) 1 2 3
127 1 $call->( $dest_namespace, $self, $args );
128  
129 # Clear uav_module_init. Would prefer a solution without
130 # eval( STRING ), though a symbol table manipulation method may be
131 # considered just as evil.
132 1 my $del_str = 'delete $' . $dest_namespace . '::{uav_module_init}';
133 1 eval $del_str;
134 }
135  
136 3 ▶ if( my $quit_call = $dest_namespace->can( 'uav_module_quit' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $quit_call = $dest_namespace->can("uav_module_quit")) 3 0 3
137 3 $self->_push_quit_sub( $quit_call );
138 }
139  
140 # If we want to reload the module, we need to delete its entry from the
141 # %INC cache
142 3 my @mod_name_components = split /::/, $full_mod_name;
143 3 my $mod_name_path = File::Spec->catfile( @mod_name_components ) . '.pm';
144 3 delete $INC{$mod_name_path};
145  
146 3 return 1;
147 }
148  
149 sub _get_load_module_sub
150 {
151 4 ▶ my ($self, $dest_namespace, $inc) = @_;
Subroutine _get_load_module_sub: called CC=1 SCAR=0.0
152 4 my $init_source = "package $dest_namespace;";
153  
154 my $sub = sub {
155 4 ▶ my ($this_sub, $file) = @_;
Subroutine __ANON__: called CC=4 SCAR=13.9
156  
157 4 my @return;
158 4 foreach (@$inc) {
159 14 my $full_path = File::Spec->catfile( $_, $file );
160 14 ▶ if( -e $full_path ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $full_path) 3 11 14
161 3 ▶ open( my $in, '<', $full_path )
Branch: 1 branch
BranchTrueFalseTotal
open my $in, "<", $full_path or die "Can't open '${full_path}': $!\n" 0 3 3
162 or die "Can't open '$full_path': $!\n";
163  
164 3 @return = (
165 \$init_source,
166 $in,
167 );
168 3 last;
169 }
170 }
171  
172 4 return @return;
173 4 };
174  
175 4 return $sub;
176 }
177  
178  
179 2 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
180 2 __PACKAGE__->meta->make_immutable;
181 2 1;
182 __END__
183  
184  
185 =head1 NAME
186  
187 UAV::Pilot::Commands
188  
189 =head1 SYNOPSIS
190  
191 my $device; # Some UAV::Pilot::Control instance, defined elsewhere
192 my $cmds = UAV::Pilot::Commands->new({
193 device => $device,
194 controller_callback_ardrone => \&make_ardrone_controller,
195 controller_callback_wumpusrover => \&make_wumpusrover_controller,
196 });
197
198 $cmds->load_lib( 'ARDrone' );
199 $cmds->run_cmd( 'takeoff;' );
200 $cmds->run_cmd( 'land;' );
201  
202 =head1 DESCRIPTION
203  
204 Provides an interface for loading UAV extensions and running them, particularly for
205 REPL shells.
206  
207 =head1 METHODS
208  
209 =head2 new
210  
211 new({
212 condvar => $cv,
213 controller_callback_ardrone => sub { ... },
214 controller_callback_wumpusrover => sub { .. },
215 })
216  
217 Constructor. The C<condvar> parameter is an C<AnyEvent::Condvar>.
218  
219 The C<controller_callback_*> parameters take a sub ref. The subroutines take
220 a the parameters C<($cmd, $cv, $easy_event)>, where C<$cmd> is this
221 C<UAV::Pilot::Commands> instance, C<$cv> is the condvar passed above, and
222 C<$easy_event> is an C<UAV::Pilot::EasyEvent> instance. It should return a
223 C<UAV::Pilot::Control> object of the associated type (generally one of the
224 C<*::Event> types with C<init_event_loop()> called).
225  
226 Note that this API is likely to change to a factory pattern in the near future.
227  
228 =head2 load_lib
229  
230 load_lib( 'ARDrone', {
231 pack => 'AR',
232 })
233  
234 Loads an extension by name. The C<pack> paramter will load the library into a specific
235 namespace. If you don't specify it, you won't need to qualify commands with a namespace
236 prefix. Example:
237  
238 load_lib( 'ARDrone', { pack => 'AR' } );
239 run_cmd( 'takeoff;' ); # Error: no subroutine named 'takeoff'
240 run_cmd( 'AR::takeoff;' ); # This works
241
242 load_lib( 'ARDrone' );
243 run_cmd( 'takeoff;' ); # Now this works, too
244  
245 Any other parmaeters you pass will be passed to the module's C<uav_module_init()>
246 subroutine.
247  
248 =head2 run_cmd
249  
250 run_cmd( 'takeoff;' )
251  
252 Executes a command. Note that this will execute arbitrary Perl statements.
253  
254 =head1 COMMANDS
255  
256 Commands provide an easy interface for writing simple UAV programms in a REPL shell.
257 They are usually thin interfaces over a L<UAV::Pilot::Control>. If you're writing a
258 complicated script, it's suggested that you skip this interface and write to the
259 L<UAV::Pilot::Control> directly.
260  
261 =head2 load
262  
263 load 'ARDrone', {
264 namespace => 'AR',
265 };
266  
267 Direct call to C<load_lib>. The C<namespace> paramter will load the library
268 into a specific namespace. If you don't specify it, you won't need to qualify
269 commands with a namespace prefix. Example:
270  
271 load 'ARDrone', { namespace => 'AR' };
272 takeoff; # Error: no subroutine named 'takeoff'
273 AR::takeoff; # This works
274
275 load ARDrone;
276 takeoff; # Now this works, too
277  
278 Any other parmaeters you pass will be passed to the module's
279 C<uav_module_init()> subroutine.
280  
281 =head1 WRITING YOUR OWN EXTENSIONS
282  
283 When calling C<load_lib( 'Foo' )>, we look for C<UAV::Pilot::Foo::Commands>
284 in the current C<@INC>.
285  
286 You write them much like any Perl module, but don't use a C<package>
287 statement--the package will be controlled by C<UAV::Pilot::Command> when
288 loaded. Like a Perl module, it should return true as its final statement
289 (put a C<1;> at the end).
290  
291 Likewise, be careful not to make any assumptions about what package you're in.
292 Modules may or may not get loaded into different, arbitrary packages.
293  
294 For ease of use, it's recommended to use function prototypes to reduce the need
295 for parens.
296  
297 The method C<uav_module_init()> is called with the package name as the first
298 argument. Subsquent arguments will be the hashref passed to
299 C<load()/load_lib()>. After being called, this sub will be deleted from the
300 package.
301  
302 The method C<uav_module_quit()> is called when the REPL is closing.
« Pilot.pm ↑ Summary Pilot/Logger.pm »