Edit File by line
/home/barbar84/www/wp-conte.../plugins/sujqvwi/AnonR/smanonr..../lib64/python3....
File: shlex.py
"""A lexical analyzer class for simple shell-like syntaxes."""
[0] Fix | Delete
[1] Fix | Delete
# Module and documentation by Eric S. Raymond, 21 Dec 1998
[2] Fix | Delete
# Input stacking and error message cleanup added by ESR, March 2000
[3] Fix | Delete
# push_source() and pop_source() made explicit by ESR, January 2001.
[4] Fix | Delete
# Posix compliance, split(), string arguments, and
[5] Fix | Delete
# iterator interface by Gustavo Niemeyer, April 2003.
[6] Fix | Delete
# changes to tokenize more like Posix shells by Vinay Sajip, July 2016.
[7] Fix | Delete
[8] Fix | Delete
import os
[9] Fix | Delete
import re
[10] Fix | Delete
import sys
[11] Fix | Delete
from collections import deque
[12] Fix | Delete
[13] Fix | Delete
from io import StringIO
[14] Fix | Delete
[15] Fix | Delete
__all__ = ["shlex", "split", "quote", "join"]
[16] Fix | Delete
[17] Fix | Delete
class shlex:
[18] Fix | Delete
"A lexical analyzer class for simple shell-like syntaxes."
[19] Fix | Delete
def __init__(self, instream=None, infile=None, posix=False,
[20] Fix | Delete
punctuation_chars=False):
[21] Fix | Delete
if isinstance(instream, str):
[22] Fix | Delete
instream = StringIO(instream)
[23] Fix | Delete
if instream is not None:
[24] Fix | Delete
self.instream = instream
[25] Fix | Delete
self.infile = infile
[26] Fix | Delete
else:
[27] Fix | Delete
self.instream = sys.stdin
[28] Fix | Delete
self.infile = None
[29] Fix | Delete
self.posix = posix
[30] Fix | Delete
if posix:
[31] Fix | Delete
self.eof = None
[32] Fix | Delete
else:
[33] Fix | Delete
self.eof = ''
[34] Fix | Delete
self.commenters = '#'
[35] Fix | Delete
self.wordchars = ('abcdfeghijklmnopqrstuvwxyz'
[36] Fix | Delete
'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_')
[37] Fix | Delete
if self.posix:
[38] Fix | Delete
self.wordchars += ('ßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿ'
[39] Fix | Delete
'ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞ')
[40] Fix | Delete
self.whitespace = ' \t\r\n'
[41] Fix | Delete
self.whitespace_split = False
[42] Fix | Delete
self.quotes = '\'"'
[43] Fix | Delete
self.escape = '\\'
[44] Fix | Delete
self.escapedquotes = '"'
[45] Fix | Delete
self.state = ' '
[46] Fix | Delete
self.pushback = deque()
[47] Fix | Delete
self.lineno = 1
[48] Fix | Delete
self.debug = 0
[49] Fix | Delete
self.token = ''
[50] Fix | Delete
self.filestack = deque()
[51] Fix | Delete
self.source = None
[52] Fix | Delete
if not punctuation_chars:
[53] Fix | Delete
punctuation_chars = ''
[54] Fix | Delete
elif punctuation_chars is True:
[55] Fix | Delete
punctuation_chars = '();<>|&'
[56] Fix | Delete
self._punctuation_chars = punctuation_chars
[57] Fix | Delete
if punctuation_chars:
[58] Fix | Delete
# _pushback_chars is a push back queue used by lookahead logic
[59] Fix | Delete
self._pushback_chars = deque()
[60] Fix | Delete
# these chars added because allowed in file names, args, wildcards
[61] Fix | Delete
self.wordchars += '~-./*?='
[62] Fix | Delete
#remove any punctuation chars from wordchars
[63] Fix | Delete
t = self.wordchars.maketrans(dict.fromkeys(punctuation_chars))
[64] Fix | Delete
self.wordchars = self.wordchars.translate(t)
[65] Fix | Delete
[66] Fix | Delete
@property
[67] Fix | Delete
def punctuation_chars(self):
[68] Fix | Delete
return self._punctuation_chars
[69] Fix | Delete
[70] Fix | Delete
def push_token(self, tok):
[71] Fix | Delete
"Push a token onto the stack popped by the get_token method"
[72] Fix | Delete
if self.debug >= 1:
[73] Fix | Delete
print("shlex: pushing token " + repr(tok))
[74] Fix | Delete
self.pushback.appendleft(tok)
[75] Fix | Delete
[76] Fix | Delete
def push_source(self, newstream, newfile=None):
[77] Fix | Delete
"Push an input source onto the lexer's input source stack."
[78] Fix | Delete
if isinstance(newstream, str):
[79] Fix | Delete
newstream = StringIO(newstream)
[80] Fix | Delete
self.filestack.appendleft((self.infile, self.instream, self.lineno))
[81] Fix | Delete
self.infile = newfile
[82] Fix | Delete
self.instream = newstream
[83] Fix | Delete
self.lineno = 1
[84] Fix | Delete
if self.debug:
[85] Fix | Delete
if newfile is not None:
[86] Fix | Delete
print('shlex: pushing to file %s' % (self.infile,))
[87] Fix | Delete
else:
[88] Fix | Delete
print('shlex: pushing to stream %s' % (self.instream,))
[89] Fix | Delete
[90] Fix | Delete
def pop_source(self):
[91] Fix | Delete
"Pop the input source stack."
[92] Fix | Delete
self.instream.close()
[93] Fix | Delete
(self.infile, self.instream, self.lineno) = self.filestack.popleft()
[94] Fix | Delete
if self.debug:
[95] Fix | Delete
print('shlex: popping to %s, line %d' \
[96] Fix | Delete
% (self.instream, self.lineno))
[97] Fix | Delete
self.state = ' '
[98] Fix | Delete
[99] Fix | Delete
def get_token(self):
[100] Fix | Delete
"Get a token from the input stream (or from stack if it's nonempty)"
[101] Fix | Delete
if self.pushback:
[102] Fix | Delete
tok = self.pushback.popleft()
[103] Fix | Delete
if self.debug >= 1:
[104] Fix | Delete
print("shlex: popping token " + repr(tok))
[105] Fix | Delete
return tok
[106] Fix | Delete
# No pushback. Get a token.
[107] Fix | Delete
raw = self.read_token()
[108] Fix | Delete
# Handle inclusions
[109] Fix | Delete
if self.source is not None:
[110] Fix | Delete
while raw == self.source:
[111] Fix | Delete
spec = self.sourcehook(self.read_token())
[112] Fix | Delete
if spec:
[113] Fix | Delete
(newfile, newstream) = spec
[114] Fix | Delete
self.push_source(newstream, newfile)
[115] Fix | Delete
raw = self.get_token()
[116] Fix | Delete
# Maybe we got EOF instead?
[117] Fix | Delete
while raw == self.eof:
[118] Fix | Delete
if not self.filestack:
[119] Fix | Delete
return self.eof
[120] Fix | Delete
else:
[121] Fix | Delete
self.pop_source()
[122] Fix | Delete
raw = self.get_token()
[123] Fix | Delete
# Neither inclusion nor EOF
[124] Fix | Delete
if self.debug >= 1:
[125] Fix | Delete
if raw != self.eof:
[126] Fix | Delete
print("shlex: token=" + repr(raw))
[127] Fix | Delete
else:
[128] Fix | Delete
print("shlex: token=EOF")
[129] Fix | Delete
return raw
[130] Fix | Delete
[131] Fix | Delete
def read_token(self):
[132] Fix | Delete
quoted = False
[133] Fix | Delete
escapedstate = ' '
[134] Fix | Delete
while True:
[135] Fix | Delete
if self.punctuation_chars and self._pushback_chars:
[136] Fix | Delete
nextchar = self._pushback_chars.pop()
[137] Fix | Delete
else:
[138] Fix | Delete
nextchar = self.instream.read(1)
[139] Fix | Delete
if nextchar == '\n':
[140] Fix | Delete
self.lineno += 1
[141] Fix | Delete
if self.debug >= 3:
[142] Fix | Delete
print("shlex: in state %r I see character: %r" % (self.state,
[143] Fix | Delete
nextchar))
[144] Fix | Delete
if self.state is None:
[145] Fix | Delete
self.token = '' # past end of file
[146] Fix | Delete
break
[147] Fix | Delete
elif self.state == ' ':
[148] Fix | Delete
if not nextchar:
[149] Fix | Delete
self.state = None # end of file
[150] Fix | Delete
break
[151] Fix | Delete
elif nextchar in self.whitespace:
[152] Fix | Delete
if self.debug >= 2:
[153] Fix | Delete
print("shlex: I see whitespace in whitespace state")
[154] Fix | Delete
if self.token or (self.posix and quoted):
[155] Fix | Delete
break # emit current token
[156] Fix | Delete
else:
[157] Fix | Delete
continue
[158] Fix | Delete
elif nextchar in self.commenters:
[159] Fix | Delete
self.instream.readline()
[160] Fix | Delete
self.lineno += 1
[161] Fix | Delete
elif self.posix and nextchar in self.escape:
[162] Fix | Delete
escapedstate = 'a'
[163] Fix | Delete
self.state = nextchar
[164] Fix | Delete
elif nextchar in self.wordchars:
[165] Fix | Delete
self.token = nextchar
[166] Fix | Delete
self.state = 'a'
[167] Fix | Delete
elif nextchar in self.punctuation_chars:
[168] Fix | Delete
self.token = nextchar
[169] Fix | Delete
self.state = 'c'
[170] Fix | Delete
elif nextchar in self.quotes:
[171] Fix | Delete
if not self.posix:
[172] Fix | Delete
self.token = nextchar
[173] Fix | Delete
self.state = nextchar
[174] Fix | Delete
elif self.whitespace_split:
[175] Fix | Delete
self.token = nextchar
[176] Fix | Delete
self.state = 'a'
[177] Fix | Delete
else:
[178] Fix | Delete
self.token = nextchar
[179] Fix | Delete
if self.token or (self.posix and quoted):
[180] Fix | Delete
break # emit current token
[181] Fix | Delete
else:
[182] Fix | Delete
continue
[183] Fix | Delete
elif self.state in self.quotes:
[184] Fix | Delete
quoted = True
[185] Fix | Delete
if not nextchar: # end of file
[186] Fix | Delete
if self.debug >= 2:
[187] Fix | Delete
print("shlex: I see EOF in quotes state")
[188] Fix | Delete
# XXX what error should be raised here?
[189] Fix | Delete
raise ValueError("No closing quotation")
[190] Fix | Delete
if nextchar == self.state:
[191] Fix | Delete
if not self.posix:
[192] Fix | Delete
self.token += nextchar
[193] Fix | Delete
self.state = ' '
[194] Fix | Delete
break
[195] Fix | Delete
else:
[196] Fix | Delete
self.state = 'a'
[197] Fix | Delete
elif (self.posix and nextchar in self.escape and self.state
[198] Fix | Delete
in self.escapedquotes):
[199] Fix | Delete
escapedstate = self.state
[200] Fix | Delete
self.state = nextchar
[201] Fix | Delete
else:
[202] Fix | Delete
self.token += nextchar
[203] Fix | Delete
elif self.state in self.escape:
[204] Fix | Delete
if not nextchar: # end of file
[205] Fix | Delete
if self.debug >= 2:
[206] Fix | Delete
print("shlex: I see EOF in escape state")
[207] Fix | Delete
# XXX what error should be raised here?
[208] Fix | Delete
raise ValueError("No escaped character")
[209] Fix | Delete
# In posix shells, only the quote itself or the escape
[210] Fix | Delete
# character may be escaped within quotes.
[211] Fix | Delete
if (escapedstate in self.quotes and
[212] Fix | Delete
nextchar != self.state and nextchar != escapedstate):
[213] Fix | Delete
self.token += self.state
[214] Fix | Delete
self.token += nextchar
[215] Fix | Delete
self.state = escapedstate
[216] Fix | Delete
elif self.state in ('a', 'c'):
[217] Fix | Delete
if not nextchar:
[218] Fix | Delete
self.state = None # end of file
[219] Fix | Delete
break
[220] Fix | Delete
elif nextchar in self.whitespace:
[221] Fix | Delete
if self.debug >= 2:
[222] Fix | Delete
print("shlex: I see whitespace in word state")
[223] Fix | Delete
self.state = ' '
[224] Fix | Delete
if self.token or (self.posix and quoted):
[225] Fix | Delete
break # emit current token
[226] Fix | Delete
else:
[227] Fix | Delete
continue
[228] Fix | Delete
elif nextchar in self.commenters:
[229] Fix | Delete
self.instream.readline()
[230] Fix | Delete
self.lineno += 1
[231] Fix | Delete
if self.posix:
[232] Fix | Delete
self.state = ' '
[233] Fix | Delete
if self.token or (self.posix and quoted):
[234] Fix | Delete
break # emit current token
[235] Fix | Delete
else:
[236] Fix | Delete
continue
[237] Fix | Delete
elif self.state == 'c':
[238] Fix | Delete
if nextchar in self.punctuation_chars:
[239] Fix | Delete
self.token += nextchar
[240] Fix | Delete
else:
[241] Fix | Delete
if nextchar not in self.whitespace:
[242] Fix | Delete
self._pushback_chars.append(nextchar)
[243] Fix | Delete
self.state = ' '
[244] Fix | Delete
break
[245] Fix | Delete
elif self.posix and nextchar in self.quotes:
[246] Fix | Delete
self.state = nextchar
[247] Fix | Delete
elif self.posix and nextchar in self.escape:
[248] Fix | Delete
escapedstate = 'a'
[249] Fix | Delete
self.state = nextchar
[250] Fix | Delete
elif (nextchar in self.wordchars or nextchar in self.quotes
[251] Fix | Delete
or (self.whitespace_split and
[252] Fix | Delete
nextchar not in self.punctuation_chars)):
[253] Fix | Delete
self.token += nextchar
[254] Fix | Delete
else:
[255] Fix | Delete
if self.punctuation_chars:
[256] Fix | Delete
self._pushback_chars.append(nextchar)
[257] Fix | Delete
else:
[258] Fix | Delete
self.pushback.appendleft(nextchar)
[259] Fix | Delete
if self.debug >= 2:
[260] Fix | Delete
print("shlex: I see punctuation in word state")
[261] Fix | Delete
self.state = ' '
[262] Fix | Delete
if self.token or (self.posix and quoted):
[263] Fix | Delete
break # emit current token
[264] Fix | Delete
else:
[265] Fix | Delete
continue
[266] Fix | Delete
result = self.token
[267] Fix | Delete
self.token = ''
[268] Fix | Delete
if self.posix and not quoted and result == '':
[269] Fix | Delete
result = None
[270] Fix | Delete
if self.debug > 1:
[271] Fix | Delete
if result:
[272] Fix | Delete
print("shlex: raw token=" + repr(result))
[273] Fix | Delete
else:
[274] Fix | Delete
print("shlex: raw token=EOF")
[275] Fix | Delete
return result
[276] Fix | Delete
[277] Fix | Delete
def sourcehook(self, newfile):
[278] Fix | Delete
"Hook called on a filename to be sourced."
[279] Fix | Delete
if newfile[0] == '"':
[280] Fix | Delete
newfile = newfile[1:-1]
[281] Fix | Delete
# This implements cpp-like semantics for relative-path inclusion.
[282] Fix | Delete
if isinstance(self.infile, str) and not os.path.isabs(newfile):
[283] Fix | Delete
newfile = os.path.join(os.path.dirname(self.infile), newfile)
[284] Fix | Delete
return (newfile, open(newfile, "r"))
[285] Fix | Delete
[286] Fix | Delete
def error_leader(self, infile=None, lineno=None):
[287] Fix | Delete
"Emit a C-compiler-like, Emacs-friendly error-message leader."
[288] Fix | Delete
if infile is None:
[289] Fix | Delete
infile = self.infile
[290] Fix | Delete
if lineno is None:
[291] Fix | Delete
lineno = self.lineno
[292] Fix | Delete
return "\"%s\", line %d: " % (infile, lineno)
[293] Fix | Delete
[294] Fix | Delete
def __iter__(self):
[295] Fix | Delete
return self
[296] Fix | Delete
[297] Fix | Delete
def __next__(self):
[298] Fix | Delete
token = self.get_token()
[299] Fix | Delete
if token == self.eof:
[300] Fix | Delete
raise StopIteration
[301] Fix | Delete
return token
[302] Fix | Delete
[303] Fix | Delete
def split(s, comments=False, posix=True):
[304] Fix | Delete
"""Split the string *s* using shell-like syntax."""
[305] Fix | Delete
lex = shlex(s, posix=posix)
[306] Fix | Delete
lex.whitespace_split = True
[307] Fix | Delete
if not comments:
[308] Fix | Delete
lex.commenters = ''
[309] Fix | Delete
return list(lex)
[310] Fix | Delete
[311] Fix | Delete
[312] Fix | Delete
def join(split_command):
[313] Fix | Delete
"""Return a shell-escaped string from *split_command*."""
[314] Fix | Delete
return ' '.join(quote(arg) for arg in split_command)
[315] Fix | Delete
[316] Fix | Delete
[317] Fix | Delete
_find_unsafe = re.compile(r'[^\w@%+=:,./-]', re.ASCII).search
[318] Fix | Delete
[319] Fix | Delete
def quote(s):
[320] Fix | Delete
"""Return a shell-escaped version of the string *s*."""
[321] Fix | Delete
if not s:
[322] Fix | Delete
return "''"
[323] Fix | Delete
if _find_unsafe(s) is None:
[324] Fix | Delete
return s
[325] Fix | Delete
[326] Fix | Delete
# use single quotes, and put single quotes into double quotes
[327] Fix | Delete
# the string $'b is then quoted as '$'"'"'b'
[328] Fix | Delete
return "'" + s.replace("'", "'\"'\"'") + "'"
[329] Fix | Delete
[330] Fix | Delete
[331] Fix | Delete
def _print_tokens(lexer):
[332] Fix | Delete
while 1:
[333] Fix | Delete
tt = lexer.get_token()
[334] Fix | Delete
if not tt:
[335] Fix | Delete
break
[336] Fix | Delete
print("Token: " + repr(tt))
[337] Fix | Delete
[338] Fix | Delete
if __name__ == '__main__':
[339] Fix | Delete
if len(sys.argv) == 1:
[340] Fix | Delete
_print_tokens(shlex())
[341] Fix | Delete
else:
[342] Fix | Delete
fn = sys.argv[1]
[343] Fix | Delete
with open(fn) as f:
[344] Fix | Delete
_print_tokens(shlex(f, fn))
[345] Fix | Delete
[346] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function