Interact/Role/Prompt.pm

filter: Statementstmt 23.8% 16 / 67 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 36.3% 4 / 11 Podpod 100% 5 / 5 totaltotal 22.5% 25 / 111 CC 12 SCAR 42.1
CC
12
Cov
18%
find_prompt
132.0
_fabricate_actionset
6.0
__ANON__
2.0
CRAP 67.2
« Interact/Phrasebook.pm ↑ Summary Interact/Transport/Wrapper/Base.pm »
1 package Net::CLI::Interact::Role::Prompt;
2 {
3 1 $Net::CLI::Interact::Role::Prompt::VERSION = '2.143070';
4 }
5  
6 1 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use MooX::Types::MooseLike::Base qw(Str RegexpRef);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Net::CLI::Interact::ActionSet;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 with 'Net::CLI::Interact::Role::FindMatch';
11  
12 has 'wake_up_msg' => (
13 is => 'rw',
14 isa => Str,
15 2 ▶ default => sub { (shift)->transport->ors },
Subroutine __ANON__: called CC=1 SCAR=0.0
16 1 predicate => 1,
17 );
18  
19 has '_prompt' => (
20 is => 'rw',
21 isa => RegexpRef,
22 reader => 'prompt_re',
23 predicate => 'has_set_prompt',
24 clearer => 'unset_prompt',
25 trigger => sub {
26 0 ▶ (shift)->logger->log('prompt', 'info', 'prompt has been set to', (shift));
Subroutine __ANON__: not called CC=1 SCAR=6.9
27 },
28 1 );
29  
30 sub set_prompt {
31 0 ▶ my ($self, $name) = @_;
Subroutine set_prompt: not called CC=1 SCAR=6.9
Pod documented
32 0 $self->_prompt( $self->phrasebook->prompt($name)->first->value->[0] );
33 }
34  
35 sub last_prompt {
36 0 ▶ my $self = shift;
Subroutine last_prompt: not called CC=1 SCAR=6.9
Pod documented
37 0 return $self->last_actionset->item_at(-1)->response;
38 }
39  
40 sub last_prompt_re {
41 0 ▶ my $self = shift;
Subroutine last_prompt_re: not called CC=1 SCAR=6.9
Pod documented
42 0 my $prompt = $self->last_prompt;
43 0 return qr/^\Q$prompt\E$/;
44 }
45  
46 sub prompt_looks_like {
47 0 ▶ my ($self, $name) = @_;
Subroutine prompt_looks_like: not called CC=1 SCAR=6.9
Pod documented
48 0 return $self->find_match(
49 $self->last_prompt, $self->phrasebook->prompt($name)->first->value
50 );
51 }
52  
53 # create an ActionSet of one send and one match Action, for the wake_up
54 sub _fabricate_actionset {
55 0 ▶ my $self = shift;
Subroutine _fabricate_actionset: not called CC=2 SCAR=17.9
56  
57 0 my $output = $self->transport->flush;
58 0 my $irs_re = $self->transport->irs_re;
59  
60 0 $output =~ s/^(?:$irs_re)+//;
61 0 my @output_lines = split $irs_re, $output;
62 0 my $last_output_line = pop @output_lines;
63 0 my $current_match = [$self->prompt_re];
64  
65 0 ▶ my $set = Net::CLI::Interact::ActionSet->new({
Branch: 1 branch
BranchTrueFalseTotal
$self->has_wake_up_msg ? : 0 0 0
66 current_match => $current_match,
67 actions => [
68 {
69 type => 'send',
70 value => ($self->has_wake_up_msg ? $self->wake_up_msg : ''),
71 response => (join "\n", @output_lines, ''),
72 },
73 {
74 type => 'match',
75 response => $last_output_line,
76 value => $current_match,
77 prompt_hit => $current_match->[0],
78 },
79 ],
80 });
81  
82 0 return $set;
83 }
84  
85 # pump until any of the prompts matches the output buffer
86 sub find_prompt {
87 0 ▶ my ($self, $wake_up) = @_;
Subroutine find_prompt: not called CC=11 SCAR=48.8
Pod documented
88 0 $self->logger->log('prompt', 'notice', 'finding prompt');
89  
90 # make connection on transport if not yet done
91 0 ▶ $self->transport->init if not $self->transport->connect_ready;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->transport->connect_ready 0 0 0
92  
93 0 eval {
94 0 my $started_pumping = time;
95 0 PUMPING: while (1) {
96 0 $self->transport->pump;
97 0 $self->logger->log('dump', 'debug', "SEEN:\n". $self->transport->buffer);
98 0 foreach my $prompt ($self->phrasebook->prompt_names) {
99 # prompts consist of only one match action
100 0 ▶ if ($self->find_match(
Branch: 1 branch
BranchTrueFalseTotal
if ($self->find_match($self->transport->buffer, $self->phrasebook->prompt($prompt)->first->value)) 0 0 0
101 $self->transport->buffer,
102 $self->phrasebook->prompt($prompt)->first->value)) {
103 0 $self->logger->log('prompt', 'info', "hit, matches prompt $prompt");
104 0 $self->set_prompt($prompt);
105 0 $self->last_actionset( $self->_fabricate_actionset() );
106 0 $self->logger->log('dialogue', 'info',
107 "trimmed command response:\n". $self->last_response);
108 0 last PUMPING;
109 }
110 0 $self->logger->log('prompt', 'debug', "nope, doesn't (yet) match $prompt");
111 }
112 0 $self->logger->log('prompt', 'debug', 'no match so far, more data?');
113 0 ▶ last if $self->transport->timeout
Branch: 1 branch
BranchTrueFalseTotal
if $self->transport->timeout and time > $started_pumping + $self->transport->timeout 0 0 0
Condition0%0/3
$self->transport->timeout and time > $started_pumping + $self->transport->timeout
!ll&&!rl&&r
000
MC/DC0%0/2
$self->transport->timeout and time > $started_pumping + $self->transport->timeout
$self->transport->timeout time > $started_pumping + $self->transport->timeout
Truth table0%0/3
A and B
A= $self->transport->timeoutB= time > $started_pumping + $self->transport->timeout
ABresult
0X0
100
111
114 and time > ($started_pumping + $self->transport->timeout);
115 }
116 };
117  
118 0 ▶ if ($@ and $self->has_wake_up_msg and $wake_up) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ and $self->has_wake_up_msg and $wake_up) { } 0 0 0
Condition0%0/6
$@ and $self->has_wake_up_msg and $wake_up
!ll&&!rl&&r
000
$@ and $self->has_wake_up_msg
!ll&&!rl&&r
000
MC/DC0%0/3
$@ and $self->has_wake_up_msg and $wake_up
$@ $self->has_wake_up_msg $wake_up
Truth table0%0/4
A and B and C
A= $@B= $self->has_wake_up_msgC= $wake_up
ABCresult
0XX0
10X0
1100
1111
119 0 $self->logger->log('prompt', 'notice',
120 "failed: [$@], sending WAKE_UP and trying again");
121  
122 0 eval {
123 0 $self->transport->put( $self->wake_up_msg );
124 0 $self->find_prompt(--$wake_up);
125 };
126 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
127 # really died, so this time bail out - with possible transport err
128 0 my $output = $self->transport->flush;
129 0 $self->transport->disconnect;
130 0 die $output;
131 }
132 }
133 else {
134 0 ▶ if (not $self->has_set_prompt) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->has_set_prompt) 0 0 0
135 # trouble... we were asked to find a prompt but failed :-(
136 0 $self->logger->log('prompt', 'critical', 'failed to find prompt! wrong phrasebook?');
137  
138 # bail out with what we have...
139 0 my $output = $self->transport->flush;
140 0 $self->transport->disconnect;
141 0 die $output;
142 }
143 }
144 }
145  
146 1 1;
147  
148 # ABSTRACT: Command-line prompt management
149  
150  
151 __END__
152 =pod
153  
154 =head1 NAME
155  
156 Net::CLI::Interact::Role::Prompt - Command-line prompt management
157  
158 =head1 VERSION
159  
160 version 2.143070
161  
162 =head1 DESCRIPTION
163  
164 This is another core component of L<Net::CLI::Interact>, and its role is to
165 keep track of the current prompt on the connected command line interface. The
166 idea is that most CLI have a prompt where you issue commands, and are returned
167 some output which this module gathers. The prompt is a demarcation between
168 each command and its response data.
169  
170 Note that although we "keep track" of the prompt, Net::CLI::Interact is not a
171 state machine, and the choice of command issued to the connected device bears
172 no relation to the current (or last matched) prompt.
173  
174 =head1 INTERFACE
175  
176 =head2 set_prompt( $prompt_name )
177  
178 This method will be used most commonly by applications to select and set a
179 prompt from the Phrasebook which matches the current context of the connected
180 CLI session. This allows a sequence of commands to be sent which share the
181 same Prompt.
182  
183 The name you pass in is looked up in the loaded Phrasebook and the entry's
184 regular expression stored internally. An exception is thrown if the named
185 Prompt is not known.
186  
187 Typically you would either refer to a Prompt in a Macro, or set the prompt you
188 are expecting once for a sequence of commands in a particular CLI context.
189  
190 When a Macro completes and it has been defined in the Phrasebook with an
191 explicit named Prompt at the end, we can assume the user is indicating some
192 change of context. Therefore the C<prompt> is I<automatically updated> on such
193 occasions to have the regular expression from that named Prompt.
194  
195 =head2 prompt_re
196  
197 Returns the current Prompt in the form of a regular expression reference. The
198 Prompt is used as a default to catch the end of command response output, when
199 a Macro has not been set up with explicit Prompt matching.
200  
201 Typically you would either refer to a Prompt in a Macro, or set the prompt you
202 are expecting once for a sequence of commands in a particular CLI context.
203  
204 =head2 unset_prompt
205  
206 Use this method to empty the current C<prompt> setting (see above). The effect
207 is that the module will automatically set the Prompt for itself based on the
208 last line of output received from the connected CLI. Do not use this option
209 unless you know what you are doing.
210  
211 =head2 has_set_prompt
212  
213 Returns True if there is currently a Prompt set, otherwise returns False.
214  
215 =head2 prompt_looks_like( $name )
216  
217 Returns True if the current prompt matches the given named prompt. This is
218 useful when you wish to make a more specific check on the current prompt.
219  
220 =head2 find_prompt( $wake_up? )
221  
222 A helper method that consumes output from the connected CLI session until a
223 line matches any one of the named Prompts in the loaded Phrasebook, at which
224 point no more output is consumed. As a consequence the C<prompt> will be set
225 (see above).
226  
227 This might be used when you're connecting to a device which maintains CLI
228 state between session disconnects (for example a serial console), and you need
229 to discover the current state. However, C<find_prompt> is executed
230 automatically for you if you call a C<cmd> or C<macro> before any interaction
231 with the CLI.
232  
233 The current device output will be scanned against all known named Prompts. If
234 nothing is found, the default behaviour is to die. Passing a positive number
235 to the method (as C<$wake_up>) will instead send the content of our
236 C<wake_up_msg> slot (see below), typically a carriage return, and try to match
237 again. The idea is that by sending one carriage return, the connected device
238 will print its CLI prompt. This "send and try to match" process will be
239 repeated up to "C<$wake_up>" times.
240  
241 =head2 wake_up_msg
242  
243 Text sent to a device within the C<find_prompt> method if no output has so far
244 matched any known named Prompt. Default is the value of the I<output record
245 separator> from the L<Transport|Net::CLI::Interact::Transport> (one newline).
246  
247 =head2 last_prompt
248  
249 Returns the Prompt which most recently was matched and terminated gathering of
250 output from the connected CLI. This is a simple text string.
251  
252 =head2 last_prompt_re
253  
254 Returns the text which was most recently matched and terminated gathering of
255 output from the connected CLI, as a quote-escaped regular expression with line
256 start and end anchors.
257  
258 =head1 AUTHOR
259  
260 Oliver Gorwits <oliver@cpan.org>
261  
262 =head1 COPYRIGHT AND LICENSE
263  
264 This software is copyright (c) 2014 by Oliver Gorwits.
265  
266 This is free software; you can redistribute it and/or modify it under
267 the same terms as the Perl 5 programming language system itself.
268  
269 =cut
270  
« Interact/Phrasebook.pm ↑ Summary Interact/Transport/Wrapper/Base.pm »