Pilot/Commands.pm

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