Interact/Role/Engine.pm

filter: Statementstmt 39.1% 36 / 92 Branchbran 3.5% 1 / 28 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 53.3% 8 / 15 Podpod 75.0% 3 / 4 totaltotal 28.7% 48 / 167 CC 21 SCAR 35.4
CC
21
Cov
27%
_execute_actions
90.0
cmd
42.0
macro
20.0
CRAP 34.6
« Interact/ActionSet.pm ↑ Summary Interact/Transport/Role/ConnectCore.pm »
1 package Net::CLI::Interact::Role::Engine;
2 {
3 1 $Net::CLI::Interact::Role::Engine::VERSION = '2.143070';
4 }
5  
6 {
7 1 package # hide from pause
8 Net::CLI::Interact::Role::Engine::ExecuteOptions;
9  
10 1 ▶ use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶ use Sub::Quote;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶ use MooX::Types::MooseLike::Base qw(Bool ArrayRef Str Any);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 has 'no_ors' => (
15 is => 'ro',
16 isa => Bool,
17 default => quote_sub('0'),
18 );
19  
20 1 has 'params' => (
21 is => 'ro',
22 isa => ArrayRef[Str],
23 predicate => 1,
24 );
25  
26 1 has 'timeout' => (
27 is => 'ro',
28 isa => quote_sub(q{die "$_[0] is not a posint!" unless $_[0] > 0 }),
29 );
30  
31 1 has 'match' => (
32 is => 'rw',
33 isa => ArrayRef, # FIXME ArrayRef[RegexpRef|Str]
34 predicate => 1,
35 coerce => quote_sub(q{ (ref [] ne ref $_[0]) ? [$_[0]] : $_[0] }),
36 );
37 }
38  
39 # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
40  
41 1 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 1 ▶use MooX::Types::MooseLike::Base qw(InstanceOf);
Subroutine BEGIN: called CC=1 SCAR=0.0
43  
44 1 with 'Net::CLI::Interact::Role::Prompt';
45  
46 1 ▶use Net::CLI::Interact::Action;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 1 ▶use Net::CLI::Interact::ActionSet;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 1 ▶use Class::Load ();
Subroutine BEGIN: called CC=1 SCAR=0.0
49  
50 # try to load Data::Printer for last_actionset debug output
51 1 ▶if (Class::Load::try_load_class('Data::Printer', {-version => '0.27'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (Class::Load::try_load_class("Data::Printer", {"-version", "0.27"})) 0 1 1
52 0 Data::Printer->import({class => { expand => 'all' }});
53 }
54  
55 1 has 'last_actionset' => (
56 is => 'rw',
57 isa => InstanceOf['Net::CLI::Interact::ActionSet'],
58 trigger => 1,
59 );
60  
61 sub _trigger_last_actionset {
62 0 ▶ my ($self, $new) = @_;
Subroutine _trigger_last_actionset: not called CC=2 SCAR=17.9
63 0 $self->logger->log('prompt', 'notice',
64 sprintf ('output matching prompt was "%s"', $new->item_at(-1)->response));
65 0 ▶ if (Class::Load::is_class_loaded('Data::Printer', {-version => '0.27'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (Class::Load::is_class_loaded("Data::Printer", {"-version", "0.27"})) 0 0 0
66 0 Data::Printer::p($new, output => \my $debug);
67 0 $self->logger->log('object', 'debug', $debug);
68 }
69 }
70  
71 sub last_response {
72 0 ▶ my $self = shift;
Subroutine last_response: not called CC=2 SCAR=17.9
Pod documented
73 0 my $irs_re = $self->transport->irs_re;
74 0 (my $resp = $self->last_actionset->item_at(-2)->response) =~ s/$irs_re/\n/g;
75 0 $resp =~ s/\n+$//;
76 return (wantarray
77 0 ▶ ? (map {$_ .= "\n"} split m/\n/, $resp)
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
78 : ($resp ."\n"));
79 }
80  
81 1 has 'default_continuation' => (
82 is => 'rw',
83 isa => InstanceOf['Net::CLI::Interact::ActionSet'],
84 writer => '_default_continuation',
85 predicate => 1,
86 clearer => 1,
87 );
88  
89 sub set_default_continuation {
90 0 ▶ my ($self, $cont) = @_;
Subroutine set_default_continuation: not called CC=3 SCAR=24.8
Pod undocumented
91 0 ▶ die "missing continuation" unless $cont;
Branch: 1 branch
BranchTrueFalseTotal
unless $cont 0 0 0
92 die "unknown continuation [$cont]" unless
93 0 ▶ eval{ $self->phrasebook->macro($cont) };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { $self->phrasebook->macro($cont) } } 0 0 0
94 0 $self->_default_continuation( $self->phrasebook->macro($cont) );
95 0 $self->logger->log('engine', 'info', 'default continuation set to', $cont);
96 }
97  
98 sub cmd {
99 0 ▶ my ($self, $command, $options) = @_;
Subroutine cmd: not called CC=6 SCAR=37.4
Pod documented
100 0 ▶ $options = Net::CLI::Interact::Role::Engine::ExecuteOptions->new($options || {});
Condition0%0/2
$options || {}
l!l
00
MC/DC0%0/1
$options || {}
$options
Truth table0%0/2
A
A= $options
Aresult
11
00
101  
102 0 $self->logger->log('engine', 'notice', 'running command', $command);
103  
104 0 ▶ if ($options->has_match) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->has_match) 0 0 0
105 # convert prompt name(s) from name into regexpref, or die
106 $options->match([
107 0 ▶ map { ref $_ eq ref '' ? @{ $self->phrasebook->prompt($_)->first->value }
Branch: 1 branch
BranchTrueFalseTotal
ref $_ eq ref '' ? : 0 0 0
108 : $_ }
109 0 @{ $options->match }
110 ]);
111  
112 $self->logger->log('engine', 'info', 'to match',
113 0 ▶ (ref $options->match eq ref [] ? (join '|', @{$options->match})
Branch: 1 branch
BranchTrueFalseTotal
ref $options->match eq ref [] ? : 0 0 0
114 : $options->match));
115 }
116  
117 # command will be run through sprintf but without any params
118 # so convert any embedded % to literal %
119 0 ▶ ($command =~ s/%/%%/g) &&
Branch: 1 branch
BranchTrueFalseTotal
$command =~ s/%/%%/g and $self->logger->log("engine", "debug", "command expanded to:", $command) 0 0 0
120 $self->logger->log('engine', 'debug', 'command expanded to:', $command);
121  
122 0 return $self->_execute_actions(
123 $options,
124 Net::CLI::Interact::Action->new({
125 type => 'send',
126 value => $command,
127 no_ors => $options->no_ors,
128 }),
129 );
130 }
131  
132 sub macro {
133 0 ▶ my ($self, $name, $options) = @_;
Subroutine macro: not called CC=4 SCAR=30.0
Pod documented
134 0 ▶ $options = Net::CLI::Interact::Role::Engine::ExecuteOptions->new($options || {});
Condition0%0/2
$options || {}
l!l
00
MC/DC0%0/1
$options || {}
$options
Truth table0%0/2
A
A= $options
Aresult
11
00
135  
136 0 $self->logger->log('engine', 'notice', 'running macro', $name);
137 $self->logger->log('engine', 'info', 'macro params are:',
138 0 ▶ join ', ', @{ $options->params }) if $options->has_params;
Branch: 1 branch
BranchTrueFalseTotal
if $options->has_params 0 0 0
139  
140 0 my $set = $self->phrasebook->macro($name)->clone;
141 0 ▶ $set->apply_params(@{ $options->params }) if $options->has_params;
Branch: 1 branch
BranchTrueFalseTotal
if $options->has_params 0 0 0
142  
143 0 return $self->_execute_actions($options, $set);
144 }
145  
146 sub _execute_actions {
147 0 ▶ my ($self, $options, @actions) = @_;
Subroutine _execute_actions: not called CC=9 SCAR=45.0
148  
149 0 $self->logger->log('engine', 'notice', 'executing actions');
150  
151 # make connection on transport if not yet done
152 0 ▶ $self->transport->init if not $self->transport->connect_ready;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->transport->connect_ready 0 0 0
153  
154 # user can install a prompt, call find_prompt, or let us trigger that
155 0 ▶ $self->find_prompt(1) if not ($self->prompt_re || $self->last_actionset);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->prompt_re or $self->last_actionset 0 0 0
Condition0%0/3
$self->prompt_re or $self->last_actionset
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->prompt_re or $self->last_actionset
$self->prompt_re $self->last_actionset
Truth table0%0/3
A or B
A= $self->prompt_reB= $self->last_actionset
ABresult
1X1
011
000
156  
157 0 ▶ my $set = Net::CLI::Interact::ActionSet->new({
Branch: 1 branch
BranchTrueFalseTotal
$self->has_default_continuation ? : 0 0 0
Condition0%0/6
$options->match || $self->prompt_re || $self->last_prompt_re
l!l&&r!l&&!r
000
$options->match or $self->prompt_re
l!l&&r!l&&!r
000
MC/DC0%0/3
$options->match || $self->prompt_re || $self->last_prompt_re
$options->match $self->prompt_re $self->last_prompt_re
Truth table0%0/4
A or B || C
A= $options->matchB= $self->prompt_reC= $self->last_prompt_re
ABCresult
1XX1
01X1
0011
0000
158 actions => [@actions],
159 current_match => ($options->match || $self->prompt_re || $self->last_prompt_re),
160 ($self->has_default_continuation ? (default_continuation => $self->default_continuation) : ()),
161 });
162 0 ▶ $set->register_callback(sub { $self->transport->do_action(@_) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
163  
164 0 $self->logger->log('engine', 'debug', 'dispatching to execute method');
165 0 my $timeout_bak = $self->transport->timeout;
166  
167 0 ▶ $self->transport->timeout($options->timeout || $timeout_bak);
Condition0%0/3
$options->timeout || $timeout_bak
l!l&&r!l&&!r
000
MC/DC0%0/2
$options->timeout || $timeout_bak
$options->timeout $timeout_bak
Truth table0%0/3
A || B
A= $options->timeoutB= $timeout_bak
ABresult
1X1
011
000
168 0 $set->execute;
169 0 $self->transport->timeout($timeout_bak);
170 0 $self->last_actionset($set);
171  
172 0 ▶ $self->logger->log('prompt', 'debug',
Condition0%0/2
$self->last_actionset->last->prompt_hit || '<none>'
l!l
00
MC/DC0%0/1
$self->last_actionset->last->prompt_hit || '<none>'
$self->last_actionset->last->prompt_hit
Truth table0%0/2
A
A= $self->last_actionset->last->prompt_hit
Aresult
11
00
173 sprintf 'setting new prompt to %s',
174 $self->last_actionset->last->prompt_hit || '<none>');
175 0 $self->_prompt( $self->last_actionset->last->prompt_hit );
176  
177 0 $self->logger->log('dialogue', 'info',
178 "trimmed command response:\n". $self->last_response);
179 0 return $self->last_response; # context sensitive
180 }
181  
182 1 1;
183  
184 # ABSTRACT: Statement execution engine
185  
186  
187 __END__
188 =pod
189  
190 =head1 NAME
191  
192 Net::CLI::Interact::Role::Engine - Statement execution engine
193  
194 =head1 VERSION
195  
196 version 2.143070
197  
198 =head1 DESCRIPTION
199  
200 This module is the core of L<Net::CLI::Interact>, and serves to take entries
201 from your loaded L<Phrasebooks|Net::CLI::Interact::Phrasebook>, issue them to
202 connected devices, and gather the returned output.
203  
204 =head1 INTERFACE
205  
206 =head2 cmd( $command_statement, \%options? )
207  
208 Execute a single command statement on the connected device, and consume output
209 until there is a match with the current I<prompt>. The statement is executed
210 verbatim on the device, with a newline appended.
211  
212 The following options are supported:
213  
214 =over 4
215  
216 =item C<< timeout => $seconds >> (optional)
217  
218 Sets a value of C<timeout> for the
219 L<Transport|Net::CLI::Interact::Transport> local to this call of C<cmd>, that
220 overrides whatever is set in the Transport, or the default of 10 seconds.
221  
222 =item C<< no_ors => 1 >> (optional)
223  
224 When passed a true value, a newline character (in fact the value of C<ors>)
225 will not be appended to the statement sent to the device.
226  
227 =item C<< match => $name | $regexpref | \@names_and_regexprefs >> (optional)
228  
229 Allows this command (only) to complete with a custom match, which must be one
230 or more of either the name of a loaded phrasebook Prompt or your own regular
231 expression reference (C<< qr// >>). The module updates the current prompt to
232 be the same value on a successful match.
233  
234 =back
235  
236 In scalar context the C<last_response> is returned (see below). In list
237 context the gathered response is returned as a list of lines. In both cases
238 your local platform's newline character will end all lines.
239  
240 =head2 macro( $macro_name, \%options? )
241  
242 Execute the commands contained within the named Macro, which must be loaded
243 from a Phrasebook. Options to control the output, including variables for
244 substitution into the Macro, are passed in the C<%options> hash reference.
245  
246 The following options are supported:
247  
248 =over 4
249  
250 =item C<< params => \@values >> (optional)
251  
252 If the Macro contains commands using C<sprintf> Format variables then the
253 corresponding parameters must be passed in this value as an array reference.
254  
255 Values are consumed from the provided array reference and passed to the
256 C<send> commands in the Macro in order, as needed. An exception will be thrown
257 if there are insufficient parameters.
258  
259 =item C<< timeout => $seconds >> (optional)
260  
261 Sets a value of C<timeout> for the
262 L<Transport|Net::CLI::Interact::Transport> local to this call of C<macro>,
263 that overrides whatever is set in the Transport, or the default of 10 seconds.
264  
265 =back
266  
267 An exception will be thrown if the Match statements in the Macro are not
268 successful against the output returned from the device. This is based on the
269 value of C<timeout>, which controls how long the module waits for matching
270 output.
271  
272 In scalar context the C<last_response> is returned (see below). In list
273 context the gathered response is returned as a list of lines. In both cases
274 your local platform's newline character will end all lines.
275  
276 =head2 last_response
277  
278 Returns the gathered output after issuing the last recent C<send> command
279 within the most recent C<cmd> or C<prompt>. That is, you get the output from
280 the last command sent to the connected device.
281  
282 In scalar context all data is returned. In list context the gathered response
283 is returned as a list of lines. In both cases your local platform's newline
284 character will end all lines.
285  
286 =head2 last_actionset
287  
288 Returns the complete L<ActionSet|Net::CLI::Interact::ActionSet> that was
289 constructed from the most recent C<macro> or C<cmd> execution. This will be a
290 sequence of L<Actions|Net::CLI::Interact::Action> that correspond to C<send>
291 and C<match> statements.
292  
293 In the case of a Macro these directly relate to the contents of your
294 Phrasebook, with the possible addition of C<match> statements added
295 automatically. In the case of a C<cmd> execution, an "anonymous" Macro is
296 constructed which consists of a single C<send> and a single C<match>.
297  
298 =head1 COMPOSITION
299  
300 See the following for further interface details:
301  
302 =over 4
303  
304 =item *
305  
306 L<Net::CLI::Interact::Role::Prompt>
307  
308 =back
309  
310 =head1 AUTHOR
311  
312 Oliver Gorwits <oliver@cpan.org>
313  
314 =head1 COPYRIGHT AND LICENSE
315  
316 This software is copyright (c) 2014 by Oliver Gorwits.
317  
318 This is free software; you can redistribute it and/or modify it under
319 the same terms as the Perl 5 programming language system itself.
320  
321 =cut
322  
« Interact/ActionSet.pm ↑ Summary Interact/Transport/Role/ConnectCore.pm »