Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ShExBy/shex_roo.../usr/lib64/python3....
File: cmd.py
"""A generic class to build line-oriented command interpreters.
[0] Fix | Delete
[1] Fix | Delete
Interpreters constructed with this class obey the following conventions:
[2] Fix | Delete
[3] Fix | Delete
1. End of file on input is processed as the command 'EOF'.
[4] Fix | Delete
2. A command is parsed out of each line by collecting the prefix composed
[5] Fix | Delete
of characters in the identchars member.
[6] Fix | Delete
3. A command `foo' is dispatched to a method 'do_foo()'; the do_ method
[7] Fix | Delete
is passed a single argument consisting of the remainder of the line.
[8] Fix | Delete
4. Typing an empty line repeats the last command. (Actually, it calls the
[9] Fix | Delete
method `emptyline', which may be overridden in a subclass.)
[10] Fix | Delete
5. There is a predefined `help' method. Given an argument `topic', it
[11] Fix | Delete
calls the command `help_topic'. With no arguments, it lists all topics
[12] Fix | Delete
with defined help_ functions, broken into up to three topics; documented
[13] Fix | Delete
commands, miscellaneous help topics, and undocumented commands.
[14] Fix | Delete
6. The command '?' is a synonym for `help'. The command '!' is a synonym
[15] Fix | Delete
for `shell', if a do_shell method exists.
[16] Fix | Delete
7. If completion is enabled, completing commands will be done automatically,
[17] Fix | Delete
and completing of commands args is done by calling complete_foo() with
[18] Fix | Delete
arguments text, line, begidx, endidx. text is string we are matching
[19] Fix | Delete
against, all returned matches must begin with it. line is the current
[20] Fix | Delete
input line (lstripped), begidx and endidx are the beginning and end
[21] Fix | Delete
indexes of the text being matched, which could be used to provide
[22] Fix | Delete
different completion depending upon which position the argument is in.
[23] Fix | Delete
[24] Fix | Delete
The `default' method may be overridden to intercept commands for which there
[25] Fix | Delete
is no do_ method.
[26] Fix | Delete
[27] Fix | Delete
The `completedefault' method may be overridden to intercept completions for
[28] Fix | Delete
commands that have no complete_ method.
[29] Fix | Delete
[30] Fix | Delete
The data member `self.ruler' sets the character used to draw separator lines
[31] Fix | Delete
in the help messages. If empty, no ruler line is drawn. It defaults to "=".
[32] Fix | Delete
[33] Fix | Delete
If the value of `self.intro' is nonempty when the cmdloop method is called,
[34] Fix | Delete
it is printed out on interpreter startup. This value may be overridden
[35] Fix | Delete
via an optional argument to the cmdloop() method.
[36] Fix | Delete
[37] Fix | Delete
The data members `self.doc_header', `self.misc_header', and
[38] Fix | Delete
`self.undoc_header' set the headers used for the help function's
[39] Fix | Delete
listings of documented functions, miscellaneous topics, and undocumented
[40] Fix | Delete
functions respectively.
[41] Fix | Delete
"""
[42] Fix | Delete
[43] Fix | Delete
import string, sys
[44] Fix | Delete
[45] Fix | Delete
__all__ = ["Cmd"]
[46] Fix | Delete
[47] Fix | Delete
PROMPT = '(Cmd) '
[48] Fix | Delete
IDENTCHARS = string.ascii_letters + string.digits + '_'
[49] Fix | Delete
[50] Fix | Delete
class Cmd:
[51] Fix | Delete
"""A simple framework for writing line-oriented command interpreters.
[52] Fix | Delete
[53] Fix | Delete
These are often useful for test harnesses, administrative tools, and
[54] Fix | Delete
prototypes that will later be wrapped in a more sophisticated interface.
[55] Fix | Delete
[56] Fix | Delete
A Cmd instance or subclass instance is a line-oriented interpreter
[57] Fix | Delete
framework. There is no good reason to instantiate Cmd itself; rather,
[58] Fix | Delete
it's useful as a superclass of an interpreter class you define yourself
[59] Fix | Delete
in order to inherit Cmd's methods and encapsulate action methods.
[60] Fix | Delete
[61] Fix | Delete
"""
[62] Fix | Delete
prompt = PROMPT
[63] Fix | Delete
identchars = IDENTCHARS
[64] Fix | Delete
ruler = '='
[65] Fix | Delete
lastcmd = ''
[66] Fix | Delete
intro = None
[67] Fix | Delete
doc_leader = ""
[68] Fix | Delete
doc_header = "Documented commands (type help <topic>):"
[69] Fix | Delete
misc_header = "Miscellaneous help topics:"
[70] Fix | Delete
undoc_header = "Undocumented commands:"
[71] Fix | Delete
nohelp = "*** No help on %s"
[72] Fix | Delete
use_rawinput = 1
[73] Fix | Delete
[74] Fix | Delete
def __init__(self, completekey='tab', stdin=None, stdout=None):
[75] Fix | Delete
"""Instantiate a line-oriented interpreter framework.
[76] Fix | Delete
[77] Fix | Delete
The optional argument 'completekey' is the readline name of a
[78] Fix | Delete
completion key; it defaults to the Tab key. If completekey is
[79] Fix | Delete
not None and the readline module is available, command completion
[80] Fix | Delete
is done automatically. The optional arguments stdin and stdout
[81] Fix | Delete
specify alternate input and output file objects; if not specified,
[82] Fix | Delete
sys.stdin and sys.stdout are used.
[83] Fix | Delete
[84] Fix | Delete
"""
[85] Fix | Delete
if stdin is not None:
[86] Fix | Delete
self.stdin = stdin
[87] Fix | Delete
else:
[88] Fix | Delete
self.stdin = sys.stdin
[89] Fix | Delete
if stdout is not None:
[90] Fix | Delete
self.stdout = stdout
[91] Fix | Delete
else:
[92] Fix | Delete
self.stdout = sys.stdout
[93] Fix | Delete
self.cmdqueue = []
[94] Fix | Delete
self.completekey = completekey
[95] Fix | Delete
[96] Fix | Delete
def cmdloop(self, intro=None):
[97] Fix | Delete
"""Repeatedly issue a prompt, accept input, parse an initial prefix
[98] Fix | Delete
off the received input, and dispatch to action methods, passing them
[99] Fix | Delete
the remainder of the line as argument.
[100] Fix | Delete
[101] Fix | Delete
"""
[102] Fix | Delete
[103] Fix | Delete
self.preloop()
[104] Fix | Delete
if self.use_rawinput and self.completekey:
[105] Fix | Delete
try:
[106] Fix | Delete
import readline
[107] Fix | Delete
self.old_completer = readline.get_completer()
[108] Fix | Delete
readline.set_completer(self.complete)
[109] Fix | Delete
readline.parse_and_bind(self.completekey+": complete")
[110] Fix | Delete
except ImportError:
[111] Fix | Delete
pass
[112] Fix | Delete
try:
[113] Fix | Delete
if intro is not None:
[114] Fix | Delete
self.intro = intro
[115] Fix | Delete
if self.intro:
[116] Fix | Delete
self.stdout.write(str(self.intro)+"\n")
[117] Fix | Delete
stop = None
[118] Fix | Delete
while not stop:
[119] Fix | Delete
if self.cmdqueue:
[120] Fix | Delete
line = self.cmdqueue.pop(0)
[121] Fix | Delete
else:
[122] Fix | Delete
if self.use_rawinput:
[123] Fix | Delete
try:
[124] Fix | Delete
line = input(self.prompt)
[125] Fix | Delete
except EOFError:
[126] Fix | Delete
line = 'EOF'
[127] Fix | Delete
else:
[128] Fix | Delete
self.stdout.write(self.prompt)
[129] Fix | Delete
self.stdout.flush()
[130] Fix | Delete
line = self.stdin.readline()
[131] Fix | Delete
if not len(line):
[132] Fix | Delete
line = 'EOF'
[133] Fix | Delete
else:
[134] Fix | Delete
line = line.rstrip('\r\n')
[135] Fix | Delete
line = self.precmd(line)
[136] Fix | Delete
stop = self.onecmd(line)
[137] Fix | Delete
stop = self.postcmd(stop, line)
[138] Fix | Delete
self.postloop()
[139] Fix | Delete
finally:
[140] Fix | Delete
if self.use_rawinput and self.completekey:
[141] Fix | Delete
try:
[142] Fix | Delete
import readline
[143] Fix | Delete
readline.set_completer(self.old_completer)
[144] Fix | Delete
except ImportError:
[145] Fix | Delete
pass
[146] Fix | Delete
[147] Fix | Delete
[148] Fix | Delete
def precmd(self, line):
[149] Fix | Delete
"""Hook method executed just before the command line is
[150] Fix | Delete
interpreted, but after the input prompt is generated and issued.
[151] Fix | Delete
[152] Fix | Delete
"""
[153] Fix | Delete
return line
[154] Fix | Delete
[155] Fix | Delete
def postcmd(self, stop, line):
[156] Fix | Delete
"""Hook method executed just after a command dispatch is finished."""
[157] Fix | Delete
return stop
[158] Fix | Delete
[159] Fix | Delete
def preloop(self):
[160] Fix | Delete
"""Hook method executed once when the cmdloop() method is called."""
[161] Fix | Delete
pass
[162] Fix | Delete
[163] Fix | Delete
def postloop(self):
[164] Fix | Delete
"""Hook method executed once when the cmdloop() method is about to
[165] Fix | Delete
return.
[166] Fix | Delete
[167] Fix | Delete
"""
[168] Fix | Delete
pass
[169] Fix | Delete
[170] Fix | Delete
def parseline(self, line):
[171] Fix | Delete
"""Parse the line into a command name and a string containing
[172] Fix | Delete
the arguments. Returns a tuple containing (command, args, line).
[173] Fix | Delete
'command' and 'args' may be None if the line couldn't be parsed.
[174] Fix | Delete
"""
[175] Fix | Delete
line = line.strip()
[176] Fix | Delete
if not line:
[177] Fix | Delete
return None, None, line
[178] Fix | Delete
elif line[0] == '?':
[179] Fix | Delete
line = 'help ' + line[1:]
[180] Fix | Delete
elif line[0] == '!':
[181] Fix | Delete
if hasattr(self, 'do_shell'):
[182] Fix | Delete
line = 'shell ' + line[1:]
[183] Fix | Delete
else:
[184] Fix | Delete
return None, None, line
[185] Fix | Delete
i, n = 0, len(line)
[186] Fix | Delete
while i < n and line[i] in self.identchars: i = i+1
[187] Fix | Delete
cmd, arg = line[:i], line[i:].strip()
[188] Fix | Delete
return cmd, arg, line
[189] Fix | Delete
[190] Fix | Delete
def onecmd(self, line):
[191] Fix | Delete
"""Interpret the argument as though it had been typed in response
[192] Fix | Delete
to the prompt.
[193] Fix | Delete
[194] Fix | Delete
This may be overridden, but should not normally need to be;
[195] Fix | Delete
see the precmd() and postcmd() methods for useful execution hooks.
[196] Fix | Delete
The return value is a flag indicating whether interpretation of
[197] Fix | Delete
commands by the interpreter should stop.
[198] Fix | Delete
[199] Fix | Delete
"""
[200] Fix | Delete
cmd, arg, line = self.parseline(line)
[201] Fix | Delete
if not line:
[202] Fix | Delete
return self.emptyline()
[203] Fix | Delete
if cmd is None:
[204] Fix | Delete
return self.default(line)
[205] Fix | Delete
self.lastcmd = line
[206] Fix | Delete
if line == 'EOF' :
[207] Fix | Delete
self.lastcmd = ''
[208] Fix | Delete
if cmd == '':
[209] Fix | Delete
return self.default(line)
[210] Fix | Delete
else:
[211] Fix | Delete
try:
[212] Fix | Delete
func = getattr(self, 'do_' + cmd)
[213] Fix | Delete
except AttributeError:
[214] Fix | Delete
return self.default(line)
[215] Fix | Delete
return func(arg)
[216] Fix | Delete
[217] Fix | Delete
def emptyline(self):
[218] Fix | Delete
"""Called when an empty line is entered in response to the prompt.
[219] Fix | Delete
[220] Fix | Delete
If this method is not overridden, it repeats the last nonempty
[221] Fix | Delete
command entered.
[222] Fix | Delete
[223] Fix | Delete
"""
[224] Fix | Delete
if self.lastcmd:
[225] Fix | Delete
return self.onecmd(self.lastcmd)
[226] Fix | Delete
[227] Fix | Delete
def default(self, line):
[228] Fix | Delete
"""Called on an input line when the command prefix is not recognized.
[229] Fix | Delete
[230] Fix | Delete
If this method is not overridden, it prints an error message and
[231] Fix | Delete
returns.
[232] Fix | Delete
[233] Fix | Delete
"""
[234] Fix | Delete
self.stdout.write('*** Unknown syntax: %s\n'%line)
[235] Fix | Delete
[236] Fix | Delete
def completedefault(self, *ignored):
[237] Fix | Delete
"""Method called to complete an input line when no command-specific
[238] Fix | Delete
complete_*() method is available.
[239] Fix | Delete
[240] Fix | Delete
By default, it returns an empty list.
[241] Fix | Delete
[242] Fix | Delete
"""
[243] Fix | Delete
return []
[244] Fix | Delete
[245] Fix | Delete
def completenames(self, text, *ignored):
[246] Fix | Delete
dotext = 'do_'+text
[247] Fix | Delete
return [a[3:] for a in self.get_names() if a.startswith(dotext)]
[248] Fix | Delete
[249] Fix | Delete
def complete(self, text, state):
[250] Fix | Delete
"""Return the next possible completion for 'text'.
[251] Fix | Delete
[252] Fix | Delete
If a command has not been entered, then complete against command list.
[253] Fix | Delete
Otherwise try to call complete_<command> to get list of completions.
[254] Fix | Delete
"""
[255] Fix | Delete
if state == 0:
[256] Fix | Delete
import readline
[257] Fix | Delete
origline = readline.get_line_buffer()
[258] Fix | Delete
line = origline.lstrip()
[259] Fix | Delete
stripped = len(origline) - len(line)
[260] Fix | Delete
begidx = readline.get_begidx() - stripped
[261] Fix | Delete
endidx = readline.get_endidx() - stripped
[262] Fix | Delete
if begidx>0:
[263] Fix | Delete
cmd, args, foo = self.parseline(line)
[264] Fix | Delete
if cmd == '':
[265] Fix | Delete
compfunc = self.completedefault
[266] Fix | Delete
else:
[267] Fix | Delete
try:
[268] Fix | Delete
compfunc = getattr(self, 'complete_' + cmd)
[269] Fix | Delete
except AttributeError:
[270] Fix | Delete
compfunc = self.completedefault
[271] Fix | Delete
else:
[272] Fix | Delete
compfunc = self.completenames
[273] Fix | Delete
self.completion_matches = compfunc(text, line, begidx, endidx)
[274] Fix | Delete
try:
[275] Fix | Delete
return self.completion_matches[state]
[276] Fix | Delete
except IndexError:
[277] Fix | Delete
return None
[278] Fix | Delete
[279] Fix | Delete
def get_names(self):
[280] Fix | Delete
# This method used to pull in base class attributes
[281] Fix | Delete
# at a time dir() didn't do it yet.
[282] Fix | Delete
return dir(self.__class__)
[283] Fix | Delete
[284] Fix | Delete
def complete_help(self, *args):
[285] Fix | Delete
commands = set(self.completenames(*args))
[286] Fix | Delete
topics = set(a[5:] for a in self.get_names()
[287] Fix | Delete
if a.startswith('help_' + args[0]))
[288] Fix | Delete
return list(commands | topics)
[289] Fix | Delete
[290] Fix | Delete
def do_help(self, arg):
[291] Fix | Delete
'List available commands with "help" or detailed help with "help cmd".'
[292] Fix | Delete
if arg:
[293] Fix | Delete
# XXX check arg syntax
[294] Fix | Delete
try:
[295] Fix | Delete
func = getattr(self, 'help_' + arg)
[296] Fix | Delete
except AttributeError:
[297] Fix | Delete
try:
[298] Fix | Delete
doc=getattr(self, 'do_' + arg).__doc__
[299] Fix | Delete
if doc:
[300] Fix | Delete
self.stdout.write("%s\n"%str(doc))
[301] Fix | Delete
return
[302] Fix | Delete
except AttributeError:
[303] Fix | Delete
pass
[304] Fix | Delete
self.stdout.write("%s\n"%str(self.nohelp % (arg,)))
[305] Fix | Delete
return
[306] Fix | Delete
func()
[307] Fix | Delete
else:
[308] Fix | Delete
names = self.get_names()
[309] Fix | Delete
cmds_doc = []
[310] Fix | Delete
cmds_undoc = []
[311] Fix | Delete
help = {}
[312] Fix | Delete
for name in names:
[313] Fix | Delete
if name[:5] == 'help_':
[314] Fix | Delete
help[name[5:]]=1
[315] Fix | Delete
names.sort()
[316] Fix | Delete
# There can be duplicates if routines overridden
[317] Fix | Delete
prevname = ''
[318] Fix | Delete
for name in names:
[319] Fix | Delete
if name[:3] == 'do_':
[320] Fix | Delete
if name == prevname:
[321] Fix | Delete
continue
[322] Fix | Delete
prevname = name
[323] Fix | Delete
cmd=name[3:]
[324] Fix | Delete
if cmd in help:
[325] Fix | Delete
cmds_doc.append(cmd)
[326] Fix | Delete
del help[cmd]
[327] Fix | Delete
elif getattr(self, name).__doc__:
[328] Fix | Delete
cmds_doc.append(cmd)
[329] Fix | Delete
else:
[330] Fix | Delete
cmds_undoc.append(cmd)
[331] Fix | Delete
self.stdout.write("%s\n"%str(self.doc_leader))
[332] Fix | Delete
self.print_topics(self.doc_header, cmds_doc, 15,80)
[333] Fix | Delete
self.print_topics(self.misc_header, list(help.keys()),15,80)
[334] Fix | Delete
self.print_topics(self.undoc_header, cmds_undoc, 15,80)
[335] Fix | Delete
[336] Fix | Delete
def print_topics(self, header, cmds, cmdlen, maxcol):
[337] Fix | Delete
if cmds:
[338] Fix | Delete
self.stdout.write("%s\n"%str(header))
[339] Fix | Delete
if self.ruler:
[340] Fix | Delete
self.stdout.write("%s\n"%str(self.ruler * len(header)))
[341] Fix | Delete
self.columnize(cmds, maxcol-1)
[342] Fix | Delete
self.stdout.write("\n")
[343] Fix | Delete
[344] Fix | Delete
def columnize(self, list, displaywidth=80):
[345] Fix | Delete
"""Display a list of strings as a compact set of columns.
[346] Fix | Delete
[347] Fix | Delete
Each column is only as wide as necessary.
[348] Fix | Delete
Columns are separated by two spaces (one was not legible enough).
[349] Fix | Delete
"""
[350] Fix | Delete
if not list:
[351] Fix | Delete
self.stdout.write("<empty>\n")
[352] Fix | Delete
return
[353] Fix | Delete
[354] Fix | Delete
nonstrings = [i for i in range(len(list))
[355] Fix | Delete
if not isinstance(list[i], str)]
[356] Fix | Delete
if nonstrings:
[357] Fix | Delete
raise TypeError("list[i] not a string for i in %s"
[358] Fix | Delete
% ", ".join(map(str, nonstrings)))
[359] Fix | Delete
size = len(list)
[360] Fix | Delete
if size == 1:
[361] Fix | Delete
self.stdout.write('%s\n'%str(list[0]))
[362] Fix | Delete
return
[363] Fix | Delete
# Try every row count from 1 upwards
[364] Fix | Delete
for nrows in range(1, len(list)):
[365] Fix | Delete
ncols = (size+nrows-1) // nrows
[366] Fix | Delete
colwidths = []
[367] Fix | Delete
totwidth = -2
[368] Fix | Delete
for col in range(ncols):
[369] Fix | Delete
colwidth = 0
[370] Fix | Delete
for row in range(nrows):
[371] Fix | Delete
i = row + nrows*col
[372] Fix | Delete
if i >= size:
[373] Fix | Delete
break
[374] Fix | Delete
x = list[i]
[375] Fix | Delete
colwidth = max(colwidth, len(x))
[376] Fix | Delete
colwidths.append(colwidth)
[377] Fix | Delete
totwidth += colwidth + 2
[378] Fix | Delete
if totwidth > displaywidth:
[379] Fix | Delete
break
[380] Fix | Delete
if totwidth <= displaywidth:
[381] Fix | Delete
break
[382] Fix | Delete
else:
[383] Fix | Delete
nrows = len(list)
[384] Fix | Delete
ncols = 1
[385] Fix | Delete
colwidths = [0]
[386] Fix | Delete
for row in range(nrows):
[387] Fix | Delete
texts = []
[388] Fix | Delete
for col in range(ncols):
[389] Fix | Delete
i = row + nrows*col
[390] Fix | Delete
if i >= size:
[391] Fix | Delete
x = ""
[392] Fix | Delete
else:
[393] Fix | Delete
x = list[i]
[394] Fix | Delete
texts.append(x)
[395] Fix | Delete
while texts and not texts[-1]:
[396] Fix | Delete
del texts[-1]
[397] Fix | Delete
for col in range(len(texts)):
[398] Fix | Delete
texts[col] = texts[col].ljust(colwidths[col])
[399] Fix | Delete
self.stdout.write("%s\n"%str(" ".join(texts)))
[400] Fix | Delete
[401] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function