This repository has been archived by the owner on Dec 29, 2022. It is now read-only.
/
squires.py
executable file
·1467 lines (1232 loc) · 48.2 KB
/
squires.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/python
#
# Copyright 2010 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied. See the License for the specific language governing
# permissions and limitations under the License.
"""Simple QuIck Readline Enhanced Scripts.
This implements a command tree structure, allowing for
easy readline tab completion, command help, and easy
addition of new commands.
The command tree is a dict. Keys are the name of the command
token (eg 'show', 'set', 'interface', 'terse'), and the
value is another Command object, which can have its own
command descendents.
Once a tree is built, one simply has to call the top level
object with the requested command (Eg ['show', 'interface', 'terse'])
and the tree will be descended until it reaches the desired
command, in this case 'show'->'interface'->'terse' then it
will run 'terse's 'Run' method.
Alternatively, for interactive use, just call '<class>.Prompt()'
and a line will be read with readline completion, and then
executed.
Please see squires_test.py or example.py for simple usage.
"""
from __future__ import print_function
__version__ = '0.9.9'
import inspect
import os
import re
import shlex
import signal
import subprocess
import sys
import time
import traceback
import option_lib
import pipe
import readline
COMPLETE_SUFFIX = ''
SHOW_HIDDEN = False # Force display of hidden commands and options.
# Character used as a pipe to split command line
PIPE_CHAR = pipe.PIPE_CHAR
class Error(Exception):
pass
class AmbiguousError(Error):
"""Ambiguous option on command line."""
# TODO(bbuxton): Deal with ambiguous matches. eg, if the
# possible matches are ('on', 'off') and the command line
# contains 'o', then the matched value will be 'on', ie the
# first match.
pass
class NoMatchError(Error):
"""Subcommand did not match."""
class Command(dict):
"""An element on the command tree.
Keys are subcommand names, values are instances of Command.
Attributes:
name: A string, the name of this command.
help: A string, the help string for this command.
ancestors: A list of strings, ancestor command names.
root: A Command() object, the root of the tree.
parent: A Command() object, the parent command of this command.
runnable: A boolean, if this command can be run itself (ie doesnt require
subcommands). If true '<cr>' is shown in the completion list. If None
and a method is supplied, default to True, else False.
options: A list of option.Option() objects for this command.
hidden: A boolean. If True, the command does not show in tab completion.
command_line: A list of tokens in the current command line.
prompt: A string, the command prompt to display. Only valid for the top
level command.
histfile: A str, the filename to read/write history from.
method: A method, called from within Run(), unless Run() is overridden.
execute_command_string: A string, to display as '<cr>' help, if runnable.
orig_ancestors: A list of strings, ancestors of this command.
pipetree: A Command(), root of tree after a pipe. If none, there is
no piping.
meta: Any object type. Meta information that can be stored by the calling
program for reference later.
"""
def __init__(self, name='', help=None, runnable=None, method=None):
super(Command, self).__init__(self)
self.name = name
self.help = help or ''
self.root = self
self.parent = None
self.ancestors = []
self.options = Options()
self.options.command = self
self.command_line = []
self.hidden = False
self.prompt = '> '
self.method = method
self.pipetree = None
self.meta = None
self.histfile = None
self._completion_cache = (None, None)
if runnable is None:
# Set to 'True' if a method is supplied.
if self.method is not None:
runnable = True
else:
runnable = False
self.runnable = runnable
# Help string is method docstring by default.
if self.method is not None and help is None:
if hasattr(method, '__doc__'):
self.help = method.__doc__
self.execute_command_string = 'Execute this command'
self.orig_ancestors = []
def __str__(self):
attribs = ["'%s'" % self.name]
if self.help:
attribs.append("help='%s'" % self.help)
if self.method:
attribs.append("method='%s'" % self.method)
if self.hidden:
attribs.append('hidden=True')
opts = []
for option in self.options:
opts.append(str(option))
subs = []
for command in list(self.values()):
subs.append(str(command))
spaces = ' '*len(self.path)
return "COMMAND(%s):\n%s\n%s" % (
', '.join(attribs),
spaces + ('\n%s' % spaces).join(opts),
spaces + ('\n%s' % spaces).join(subs))
def PrepareReadline(self):
"""Prepares readline for our use. DEPRECATED."""
print('(Squires warning) PrepareReadline() is deprecated and now a NoOp.')
def AddCommand(self, name, help=None, runnable=None, method=None, pipe=None,
meta=None, hidden=False):
"""Convenience function to add a command to the tree.
Returns the new Command() object, already added to the tree. Options
can then be added.
Args:
name: A string, the full path of the command, eg 'set pager'. If
it already exists, the old command will be entirely overwritten.
help: A string. As per the Command() 'help' attribute.
runnable: A boolean, as per the Command() 'runnable' attribute.
method: A function which will be the new object's 'Run' method. This
method will be passed two arguments - the Command object ("self"), and
the entire command line as a list of strings.
pipe: A Command object, the pipe command tree. Currently unused.
meta: Any object. Meta information supplied by the squires user to
be used by it arbitrarily.
hidden: A boolean. If True, command does not show up in help or
tab completion.
Returns:
A Command() object.
"""
name = name.split()
command = Command(name=name[-1], help=help, runnable=runnable,
method=method)
command.ancestors = name[:-1]
command.meta = meta
command.hidden = hidden
self.root.Attach(command)
return command
def AddSubCommand(self, name, **kwargs):
"""Similar to AddCommand(), but added directly below the current Command.
Args:
name: A string, the command name relative to the Command() instance.
that AddSubCommand is called for. eg if this Command is 'set pager' and
name is 'terminal', the new command is 'set pager terminal'.
Returns:
A Command() object, the new command.
"""
ancestors = ' '.join(self.orig_ancestors) + ' %s %s' % (self.name, name)
return self.AddCommand(ancestors, **kwargs)
def __repr__(self):
return '<Command Object, Name "%s", SubCommands: %s>' % (
self.name, ','.join(list(self.keys())))
def GetPipeTree(self):
"""Get the best pipe tree, climbing the command tree.
Looks at this object's pipetree. If not none, returns it,
otherwise ascends the tree until it finds one.
Returns:
A Command() object, the matching pipe tree. None if there
is not any.
"""
if self.pipetree is not None:
return self.pipetree
if self.parent is not None:
return self.parent.GetPipeTree()
return None
def _ReadHistory(self):
"""Reads history file, if it exists."""
if self.histfile:
histfile = os.path.expanduser(self.histfile)
if os.path.exists(histfile):
readline.read_history_file(histfile)
def _WriteHistory(self):
"""Writes history file, creating if it doesnt exist."""
if self.histfile:
histfile = os.path.expanduser(self.histfile)
if not os.path.exists(histfile):
open(histfile, 'w')
readline.write_history_file(histfile)
@property
def path(self):
"""Fetches the path of this command.
Returns:
A list of str, the names of command objects from the root.
"""
path = []
cmd = self
while cmd.parent:
path.append(cmd.name)
cmd = cmd.parent
path.reverse()
return path
def WillPipe(self, line):
"""Returns whether this line will be piped.
Args:
line: A list of str, the command line.
Returns:
A boolean. If True, a pipe is present and will be processed.
"""
return self.GetPipeTree() is not None and PIPE_CHAR in line
def _ReadlinePrepare(self):
"""Prepare readline for use."""
readline.set_completion_display_matches_hook(
self.root.FormatCompleterOptions)
readline.set_completer(self.root.ReadlineCompleter)
readline.parse_and_bind('tab: complete')
readline.parse_and_bind('?: possible-completions')
readline.set_completer_delims(' \t')
self._old_delims = readline.get_completer_delims()
readline.set_completer_delims(' ')
def _ReadlineUnprepare(self):
"""Reset readline."""
readline.set_completer(None)
readline.set_completion_display_matches_hook(None)
readline.set_completer_delims(self._old_delims)
def Loop(self, prompt=None):
"""Main CLI loop.
Returns at the termination of the UI (either by the user exiting, or EOF)
Args:
prompt: A string, the prompt to display.
"""
self._ReadHistory()
while True:
try:
self.Prompt(prompt)
except KeyboardInterrupt:
print()
except EOFError:
print()
break
self._WriteHistory()
def _SplitCommandLine(self, command):
"""Split a command string into tokens.
Pipe character (|) will be split into its own token.
Backslash (\) is used as escape character.
Characters within (single and double) quotes will not be split.
Args:
command: A string, the command to split.
Returns:
A list of strings.
"""
lex = shlex.shlex(command, posix=True)
lex.commenters = '' # disable comments, just like shlex.split
# Includes all ASCII printable chars other than |\"'.
lex.wordchars += '!#$%&()*+,-./:;<=>?@[]^_`{}~'
return list(lex)
def Prompt(self, prompt=None):
"""Prompt user (with readline) then execute command.
Args:
prompt: A string, the prompt to display.
"""
if prompt is not None:
self.prompt = prompt
self._ReadlinePrepare()
if sys.version_info.major < 3:
line = raw_input(self.prompt)
else:
line = input(self.prompt)
self._ReadlineUnprepare()
try:
split_line = self._SplitCommandLine(line)
except ValueError as e:
print('%% %s' % e) # Unterminated quote or other parse error.
return
try:
self._SaveHistory()
self.Execute(split_line)
except (KeyboardInterrupt, EOFError) as e:
# Catch ctrl-c and eof and pass up.
raise
except IOError as e:
# Would only be seen by a pipe. Suppress useless error.
pass
except Exception as e:
# Other exceptions whilst running command have trace
# printed, then back to the prompt.
traceback.print_exc(file=sys.stdout)
finally:
self._RestoreHistory()
def ReadlineCompleter(self, word, state):
"""Readline completion handler.
This method is registered with readline to perform command
completion. It tidies up the current command line, before
passing to self.Completer.
See the Python readline documentation for more details on
this method.
Args:
word: A string, the current word under the cursor.
state: An int, the 'tab press number', indexed at zero.
Returns:
A string, the next possible completion, or None.
"""
if self._completion_cache[0] == word:
next_completion = self._completion_cache[1][state]
if next_completion is None:
self._completion_cache = (None, None)
return next_completion
initial_candidates = self.FindCurrentCandidates()
if not initial_candidates:
print('\nNo valid completions.')
print(self.prompt + readline.get_line_buffer(), end='')
sys.stdout.flush()
return None
candidates = []
for cand in sorted(initial_candidates):
candidates.append(cand + COMPLETE_SUFFIX) # Add space after the token.
if cand.startswith('<'):
# Make an additional dummy candidate to force
# display of these instead of auto-completing.
candidates.append('%@%@%@' + cand)
candidates.append(None) # readline expects None at the end.
self._completion_cache = (word, candidates)
return candidates[state]
def FormatCompleterOptions(self, unused_substitution, unused_matches,
unused_longest):
"""Displays the possible completions, with help text.
This function is registered in _ReadlinePrepare, and readline
calls it to display possible completions when needed.
See readline documentation for more details on this method.
Args:
unused_substitution: A str, the current word under the cursor.
unused_matches: A list, possible tokens that match, as returned by
ReadlineCompleter.
unused_longest: An int, the length of the longest match.
"""
print(u'\nValid completions:')
candidates = self.FindCurrentCandidates()
for candidate in sorted(candidates):
if not candidate.startswith('%@%@%@'):
# Print it unless its a dummy candidate (see above).
print(u' %-21s %s' % (candidate, candidates[candidate] or ''))
print(u'%s%s' % (self.prompt, readline.get_line_buffer()), end=u'')
sys.stdout.flush()
def FindCurrentCandidates(self):
"""Finds valid completions on the current line.
Returns:
A dict. Keys are valid next token, values are help text for each.
"""
try:
for quote in ('', "'", '"'):
# Auto close quotations to allow tab completion.
try:
current_line = shlex.split(readline.get_line_buffer() + quote)
break
except ValueError as e:
pass
else:
raise # Re-raise exception that our extra quotes couldnt stop.
if readline.get_line_buffer().endswith(' '):
current_line.append(' ')
return self.Completer(current_line)
except Exception:
print('\n%s' % traceback.format_exc())
return {}
def Completer(self, current_line):
"""Completion handler.
Takes the given line, and attempts to return valid
completions. If necessary, subcommand modules will be
called.
Completer is case insensitive - the supplied command is
matched regardless of case, however returned commands
may have case.
Tokens should be supplied pre-processed by shlex, so that
quoted parameters are set as one token. Ie, if a line
entered by the user is:
set interface ge-1/2/0 description "Some interface"
the argument to this function is:
['set', 'interface', 'ge-1/2/0', 'description', 'Some interface']
Args:
current_line: A list of strings, the line at this point.
Returns:
A dictionary of completions. Keyed by command, value is helpstring.
If the key starts with the character '<', then the entry is shown
in the completion candidate list, but is not actually used for
completion (eg, if one wants to show "<string> The string.")
"""
# First disambiguate as much as possible.
line = self.Disambiguate(current_line)
self.command_line = line
# If a pipe is present, tab complete down the pipeline.
if self.WillPipe(line):
return self.GetPipeTree().Completer(pipe.SplitByPipe(line)[1])
candidates = {}
# Examine subcommands for completions.
for name, subcommand in self.items():
if subcommand._Matches(line):
if len(line) > 1:
# A line with more elements here is passed to the first match.
return subcommand.Completer(line[1:])
elif not subcommand.hidden or SHOW_HIDDEN:
# Or add non-hidden commands to the help options.
candidates[name] = subcommand.help
# Add completions for options.
candidates.update(self.options.GetOptionCompletes(line))
return candidates
def _Matches(self, line):
"""Returns a boolean, whether the line matches this command.
A match is either the line's first element maching our name,
or the line being empty (meaning, ask for all possible commands).
Args:
line: A list of str, the current command line.
Returns:
A boolean, True if the line matches this command object.
"""
if line == [' '] or not line:
return True
return self.name.startswith(line[0].lower())
def AddOption(self, name, **kwargs):
"""Adds an option to this command.
Args:
name: The name of the option to add.
Returns:
A Command() object, this. This is done to make for convenient
chaining of AddOption calls to a single Command object.
"""
self.options.AddOption(name, **kwargs)
return self
def _GetCommonPrefix(self, words):
"""Returns the common prefix in the list of words.
Args:
words: A list of strings, words to match for.
Returns:
A string, the longest match prefix.
"""
common = ''
for i in range(len(words[0]) + 1):
prefix = words[0][:i]
for w in words:
if not w.startswith(prefix):
return common
common = prefix
return common
def Disambiguate(self, command, prefer_exact_match=False):
"""Disambiguates commands.
Attempts to expand all elements of the supplied command.
If a pipe is present, disambiguates both the main command
as well as the pipe, before recombining, else just
disambiguates this command.
Args:
command: A list of strings, tokens of current command line.
prefer_exact_match: A boolean, if there is an exact match then
return that instead of a common prefix.
Returns:
A list, where the tokens are disambiguated. If
ambiguous, returns the supplied 'command'
"""
expanded = []
if self.WillPipe(command):
first, last = pipe.SplitByPipe(command)
first_dis = self.Disambiguate(first, prefer_exact_match)
last_dis = self.GetPipeTree().Disambiguate(last, prefer_exact_match)
if first_dis:
expanded = first_dis + [PIPE_CHAR] + last_dis
else:
expanded = last_dis
else:
expanded = self._Disambiguate(command, prefer_exact_match)
return expanded
def _Disambiguate(self, command, prefer_exact_match=False):
"""Disambiguates a command, by expanding elements.
For example: ['sh'] -> ['show']
Also works recursively while not ambiguous:
Eg: ['sh', 'ver'] -> ['show', 'version']
however, if 'ver' is ambiguous (lets say 'version' or 'versed'),
then returns as much as possible:
['sh', 'ver'] -> ['show', 'vers']
Args:
command: A list of strings, tokens of current command line.
prefer_exact_match: A boolean, if there is an exact match then
return that instead of a common prefix.
Returns:
A list, where the tokens are disambiguated. If
ambiguous, returns the supplied 'command'
"""
if not command:
return []
matches = [] # List of candidate sub-commands.
# Attempt to look for valid subcommands.
for candidate in self:
if prefer_exact_match and candidate == command[0].lower():
# An exact match short-circuits the search.
matches = [candidate]
break
if candidate.startswith(command[0].lower()):
matches.append(candidate)
if len(matches) > 1:
# More than one, find common prefix, return that.
command[0] = self._GetCommonPrefix(matches)
return command
elif len(matches) == 1:
# One match, disambiguate subcommands.
if len(command) > 1:
submatches = self[matches[0]].Disambiguate(
command[1:], prefer_exact_match)
#if not submatches:
# return None
matches.extend(submatches)
return matches
else: # No match, try options. Exclude '<> completes.
return self.options.Disambiguate(list(command))
def _AddAncestors(self, command_object):
"""Ensure all ancestors are present in the command tree."""
command = self
for idx, key in enumerate(command_object.ancestors):
# Descend through all ancestors of the command_object. If
# any is missing, create a dummy command node and add it
# to the tree, then continue descending.
try:
command = command[key]
except KeyError:
# Ancestor not found, create it and add it.
cmd = Command(name=key, help=command_object.help)
cmd.ancestors = command_object.ancestors[:idx]
cmd._busy = True # Loop prevention.
if self.name != '<root>':
cmd.ancestors.extend(self.name)
self.Attach(cmd)
command = command[key]
def Attach(self, command_object):
"""Attaches a Command() object to the tree.
The ancestors attribute is used to determine whether to
anchor it to this object, or a descendent.
If a Command() at the specified tree position already exists,
then it will be overwritten except for the sub-commands.
Args:
command_object: A Command() object.
"""
ancestors = list(command_object.ancestors)
if not command_object.orig_ancestors:
command_object.orig_ancestors = ancestors
command_object.root = self.root
if not ancestors:
if command_object.name in self:
# If node already exists, merge sub-commands.
command_object.update(self[command_object.name])
self[command_object.name] = command_object
command_object.parent = self
return
# The '_busy' attribute is used for loop prevention when this method
# is called by _AddAncestors().
if self.name == '<root>' and not hasattr(command_object, '_busy'):
self._AddAncestors(command_object)
if len(ancestors) > 1:
command_object.ancestors = ancestors[1:]
else:
command_object.ancestors = []
self[ancestors[0]].Attach(command_object)
def GetOptionObject(self, option_name):
"""Fetches the named option object. See Options.GetOptionObject()."""
return self.options.GetOptionObject(option_name)
def GetOption(self, option_name):
"""Fetches an option from command line. See Options().GetOption()."""
if self.WillPipe(self.command_line):
return self.options.GetOption(
pipe.SplitByPipe(self.command_line)[0], option_name)
else:
return self.options.GetOption(self.command_line, option_name)
def GetGroupOption(self, group):
"""Fetches set options in a group. See Options().GetGroupOption()."""
if self.WillPipe(self.command_line):
return self.options.GetGroupOption(
pipe.SplitByPipe(self.command_line)[0], group)
else:
return self.options.GetGroupOption(self.command_line, group)
def GetCommand(self, cmdline):
"""Returns the command object for the given commandline.
Args:
cmdline: A list of str, the command line at this point.
Returns:
A command object, the command object for the given command.
"""
# Expand the command line out.
self.command_line = self.Disambiguate(cmdline, prefer_exact_match=True)
if self.WillPipe(cmdline) and cmdline[0] == PIPE_CHAR:
# Retain pipe at the start.
self.command_line = [PIPE_CHAR] + self.command_line
# If command line is empty at this point, or the next option
# is not a valid subcommand, we run it locally.
if len(self.command_line) < 1 or self.command_line[0] not in self:
return self
# First command line token is a subcommand, pass down.
return self[self.command_line[0]].GetCommand(self.command_line[1:])
def Execute(self, command, suppress_backspace=False):
"""Executes the command given.
'command' is the command at this point. eg, if this command
instance name is 'interface', and the user supplied command is
'show interface statistics detail', then 'command' might be
something like ['statistics', 'detail'].
If command len is > 1, and the first word is a subcommand,
it calls it if present (in this instance, 'statistics'). If not
present, or the 'command' is empty, it calls self.Run().
Args:
command: (list) The command to run, split into tokens..
suppress_backspace: (bool) If True, dont print
initial (realine workaround) backspace.
Returns:
The value returned by a command's 'Run' method. Else None.
"""
cmd = self.GetCommand(command)
if cmd.options.HasAllValidOptions(cmd.command_line, describe=True):
if not suppress_backspace:
print('\r', end='') # Backspace due to a readline quirk adding spurious space.
sys.stdout.flush()
if cmd.WillPipe(cmd.command_line):
retval = None
first, last = pipe.SplitByPipe(cmd.command_line)
# Run "<pipecmd> start" to init pipe.
if cmd.GetPipeTree().Execute(last + ['start']):
try:
retval = cmd.Run(first)
finally:
# Run "<pipecmd> stop" to close pipe.
cmd.GetPipeTree().Execute(last + ['stop'])
return retval
else:
return cmd.Run(cmd.command_line)
return False
def Run(self, command):
"""Run the given command."""
if self.method is not None:
return self.method(self, command)
print('%% Incomplete command.')
def _SaveHistory(self):
"""Save readline history then clear history."""
self._saved_history = []
for idx in range(1, readline.get_current_history_length()+1):
self._saved_history.append(readline.get_history_item(idx))
for idx in range(readline.get_current_history_length()):
readline.remove_history_item(0)
def _RestoreHistory(self):
"""Restore readline history saved in SaveHistory."""
for idx in range(readline.get_current_history_length()):
readline.remove_history_item(0)
for item in self._saved_history:
readline.add_history(item)
class ShellCommand(Command):
"""A type of Command that is useful for direct shell pipelines."""
def Completer(self, line):
"""Command line completer, for now just return a default placeholder."""
return {'<command>': 'Shell command to pipe output through'}
def Execute(self, command):
"""Called immediately before and immediately after the primary command."""
action = command[-1]
cmd = []
for token in command[:-1]:
if ' ' in token:
cmd.append('"%s"' % token)
else:
cmd.append(token)
pipe = ' '.join(cmd)
if len(cmd) < 1:
print('% Invalid pipe command.')
return False
if action == 'start': # Initialise shell pipe.
return self._StartPipe(pipe)
elif action == 'stop': # Terminate shell pipe.
return self._StopPipe()
else:
print('%% Should not get here! (%s)' % command)
return False
def _StartPipe(self, pipe):
"""Direct stdout to the supplied pipe."""
self._shell = subprocess.Popen(pipe, stdin=subprocess.PIPE, shell=True)
self._prevfd = os.dup(sys.stdout.fileno())
os.dup2(self._shell.stdin.fileno(), sys.stdout.fileno())
self._prev = sys.stdout
return True
def _StopPipe(self):
"""Restore stdout."""
self._shell.stdin.close()
os.dup2(self._prevfd, self._prev.fileno())
sys.stdout = self._prev
# .wait() can deadlock, so instead .poll() until terminated.
while self._shell.poll() is None:
time.sleep(0.05)
del self._shell
return True
class Options(list):
"""Represents all options for a command.
Attributes:
command: A Command() object, the associated command.
"""
def __init__(self, *args):
super(Options, self).__init__(*args)
self.command = None
def GetOptionObject(self, name):
"""Fetches the Option object for the given option name.
Args:
name: A str, the option name to fetch.
Returns:
An Option(), the option requested. None if the name is not found.
"""
for option in self:
if option.name == name:
return option
def remove(self, key):
"""Override parent remove.
Args:
key: A str or Option, the option to remove, or its name.
Raises:
ValueError, if the item is not present.
"""
new = []
if isinstance(key, option_lib.Option): # Remove by object.
key = key.name
for option in self:
if option.name != key:
new.append(option)
break
else:
raise ValueError('%s not in list' % key)
self[:] = new
def AddOption(self, name, **kwargs):
"""Adds an option to this command.
See Option() docstring for valid kwargs options.
Args:
name: A string, the name of the option.
Raises:
ValueError: An invalid option parameter combination was supplied.
"""
# First, see if this is a replacement for an existing option,
# and if so, remove it.
existing = self.GetOptionObject(name)
if existing is not None:
if existing.arg_val is not None:
# Also remove keyvalue "value" option.
self.remove(existing.arg_val.name)
self.remove(existing.name)
kwargs['name'] = name
default = kwargs.get('default')
if kwargs.get('keyvalue'):
# If the option is a keyvalue option, Squires handles this
# by creating another option to handle its argument.
#
# The two options are linked together so that the various
# completer methods can allow for this special case.
#
# The 'key' option is modified slightly to be a boolean option,
# and other parameters are passed to the 'value' option.
match = kwargs.pop('match', None)
path = kwargs.pop('is_path', None)
kwargs['boolean'] = True
if match is None and path is None:
raise ValueError(
'With "keyvalue", one of "match" or "is_path" must be set.')
option = option_lib.Option(**kwargs)
self.append(option)
if option.keyvalue:
kwargs['match'] = match
kwargs['boolean'] = False
kwargs['is_path'] = path
kwargs['name'] = '<' + option.name + '__arg>'
optionv = option_lib.Option(**kwargs)
optionv.arg_key = option
optionv.default = default
optionv.hidden = False # Always tab complete on the arg.
option.arg_val = optionv
option.default = default
self.append(optionv)
# Now re-order the options so that non-boolean/dict/list ones come last.
self.sort()
def GetOption(self, command_line, option_name):
"""Fetches an option from command line.
Fetches the current value of an option on the command
line. Assumes HasAllValidOptions() is true, otherwise
returned value may be incorrect.
Args:
command_line: A list, the command line of options.
option_name: A string, the name of the option.
Returns:
The option value, if set (string or True), else None.
"""
# Get options set on command line, return value if there's a match.
for option, value in self._FindOptions(command_line)[0].items():
if option.name == option_name:
return value
# Not on command line. Look for default (may be None).
option = self.GetOptionObject(option_name)
return option and option.default or None
def GetGroupOption(self, command_line, group):
"""Fetches set options in a group.
Fetches any options in a group which match in the
current command line.
Args:
command_line: A list of strings, the command line.
group: A string, the name of the group to fetch.
Returns:
A string. If any members of a group are set, will return
the name of the option (for bool options) or the matching
string, for non-bool options.
"""
for option in self:
if option.group == group:
value = self.GetOption(command_line, option.name)
if value:
if option.matcher.MATCH == 'boolean':
# Return the _name_ of boolean options.
return option.name
else: