Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/anonr.TX.../opt/imh-pyth.../lib/python2....
File: shlex.py
# -*- coding: iso-8859-1 -*-
[0] Fix | Delete
"""A lexical analyzer class for simple shell-like syntaxes."""
[1] Fix | Delete
[2] Fix | Delete
# Module and documentation by Eric S. Raymond, 21 Dec 1998
[3] Fix | Delete
# Input stacking and error message cleanup added by ESR, March 2000
[4] Fix | Delete
# push_source() and pop_source() made explicit by ESR, January 2001.
[5] Fix | Delete
# Posix compliance, split(), string arguments, and
[6] Fix | Delete
# iterator interface by Gustavo Niemeyer, April 2003.
[7] Fix | Delete
[8] Fix | Delete
import os.path
[9] Fix | Delete
import sys
[10] Fix | Delete
from collections import deque
[11] Fix | Delete
[12] Fix | Delete
try:
[13] Fix | Delete
from cStringIO import StringIO
[14] Fix | Delete
except ImportError:
[15] Fix | Delete
from StringIO import StringIO
[16] Fix | Delete
[17] Fix | Delete
__all__ = ["shlex", "split"]
[18] Fix | Delete
[19] Fix | Delete
class shlex:
[20] Fix | Delete
"A lexical analyzer class for simple shell-like syntaxes."
[21] Fix | Delete
def __init__(self, instream=None, infile=None, posix=False):
[22] Fix | Delete
if isinstance(instream, basestring):
[23] Fix | Delete
instream = StringIO(instream)
[24] Fix | Delete
if instream is not None:
[25] Fix | Delete
self.instream = instream
[26] Fix | Delete
self.infile = infile
[27] Fix | Delete
else:
[28] Fix | Delete
self.instream = sys.stdin
[29] Fix | Delete
self.infile = None
[30] Fix | Delete
self.posix = posix
[31] Fix | Delete
if posix:
[32] Fix | Delete
self.eof = None
[33] Fix | Delete
else:
[34] Fix | Delete
self.eof = ''
[35] Fix | Delete
self.commenters = '#'
[36] Fix | Delete
self.wordchars = ('abcdfeghijklmnopqrstuvwxyz'
[37] Fix | Delete
'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_')
[38] Fix | Delete
if self.posix:
[39] Fix | Delete
[40] Fix | Delete
[41] Fix | Delete
self.whitespace = ' \t\r\n'
[42] Fix | Delete
self.whitespace_split = False
[43] Fix | Delete
self.quotes = '\'"'
[44] Fix | Delete
self.escape = '\\'
[45] Fix | Delete
self.escapedquotes = '"'
[46] Fix | Delete
self.state = ' '
[47] Fix | Delete
self.pushback = deque()
[48] Fix | Delete
self.lineno = 1
[49] Fix | Delete
self.debug = 0
[50] Fix | Delete
self.token = ''
[51] Fix | Delete
self.filestack = deque()
[52] Fix | Delete
self.source = None
[53] Fix | Delete
if self.debug:
[54] Fix | Delete
print 'shlex: reading from %s, line %d' \
[55] Fix | Delete
% (self.instream, self.lineno)
[56] Fix | Delete
[57] Fix | Delete
def push_token(self, tok):
[58] Fix | Delete
"Push a token onto the stack popped by the get_token method"
[59] Fix | Delete
if self.debug >= 1:
[60] Fix | Delete
print "shlex: pushing token " + repr(tok)
[61] Fix | Delete
self.pushback.appendleft(tok)
[62] Fix | Delete
[63] Fix | Delete
def push_source(self, newstream, newfile=None):
[64] Fix | Delete
"Push an input source onto the lexer's input source stack."
[65] Fix | Delete
if isinstance(newstream, basestring):
[66] Fix | Delete
newstream = StringIO(newstream)
[67] Fix | Delete
self.filestack.appendleft((self.infile, self.instream, self.lineno))
[68] Fix | Delete
self.infile = newfile
[69] Fix | Delete
self.instream = newstream
[70] Fix | Delete
self.lineno = 1
[71] Fix | Delete
if self.debug:
[72] Fix | Delete
if newfile is not None:
[73] Fix | Delete
print 'shlex: pushing to file %s' % (self.infile,)
[74] Fix | Delete
else:
[75] Fix | Delete
print 'shlex: pushing to stream %s' % (self.instream,)
[76] Fix | Delete
[77] Fix | Delete
def pop_source(self):
[78] Fix | Delete
"Pop the input source stack."
[79] Fix | Delete
self.instream.close()
[80] Fix | Delete
(self.infile, self.instream, self.lineno) = self.filestack.popleft()
[81] Fix | Delete
if self.debug:
[82] Fix | Delete
print 'shlex: popping to %s, line %d' \
[83] Fix | Delete
% (self.instream, self.lineno)
[84] Fix | Delete
self.state = ' '
[85] Fix | Delete
[86] Fix | Delete
def get_token(self):
[87] Fix | Delete
"Get a token from the input stream (or from stack if it's nonempty)"
[88] Fix | Delete
if self.pushback:
[89] Fix | Delete
tok = self.pushback.popleft()
[90] Fix | Delete
if self.debug >= 1:
[91] Fix | Delete
print "shlex: popping token " + repr(tok)
[92] Fix | Delete
return tok
[93] Fix | Delete
# No pushback. Get a token.
[94] Fix | Delete
raw = self.read_token()
[95] Fix | Delete
# Handle inclusions
[96] Fix | Delete
if self.source is not None:
[97] Fix | Delete
while raw == self.source:
[98] Fix | Delete
spec = self.sourcehook(self.read_token())
[99] Fix | Delete
if spec:
[100] Fix | Delete
(newfile, newstream) = spec
[101] Fix | Delete
self.push_source(newstream, newfile)
[102] Fix | Delete
raw = self.get_token()
[103] Fix | Delete
# Maybe we got EOF instead?
[104] Fix | Delete
while raw == self.eof:
[105] Fix | Delete
if not self.filestack:
[106] Fix | Delete
return self.eof
[107] Fix | Delete
else:
[108] Fix | Delete
self.pop_source()
[109] Fix | Delete
raw = self.get_token()
[110] Fix | Delete
# Neither inclusion nor EOF
[111] Fix | Delete
if self.debug >= 1:
[112] Fix | Delete
if raw != self.eof:
[113] Fix | Delete
print "shlex: token=" + repr(raw)
[114] Fix | Delete
else:
[115] Fix | Delete
print "shlex: token=EOF"
[116] Fix | Delete
return raw
[117] Fix | Delete
[118] Fix | Delete
def read_token(self):
[119] Fix | Delete
quoted = False
[120] Fix | Delete
escapedstate = ' '
[121] Fix | Delete
while True:
[122] Fix | Delete
nextchar = self.instream.read(1)
[123] Fix | Delete
if nextchar == '\n':
[124] Fix | Delete
self.lineno = self.lineno + 1
[125] Fix | Delete
if self.debug >= 3:
[126] Fix | Delete
print "shlex: in state", repr(self.state), \
[127] Fix | Delete
"I see character:", repr(nextchar)
[128] Fix | Delete
if self.state is None:
[129] Fix | Delete
self.token = '' # past end of file
[130] Fix | Delete
break
[131] Fix | Delete
elif self.state == ' ':
[132] Fix | Delete
if not nextchar:
[133] Fix | Delete
self.state = None # end of file
[134] Fix | Delete
break
[135] Fix | Delete
elif nextchar in self.whitespace:
[136] Fix | Delete
if self.debug >= 2:
[137] Fix | Delete
print "shlex: I see whitespace in whitespace state"
[138] Fix | Delete
if self.token or (self.posix and quoted):
[139] Fix | Delete
break # emit current token
[140] Fix | Delete
else:
[141] Fix | Delete
continue
[142] Fix | Delete
elif nextchar in self.commenters:
[143] Fix | Delete
self.instream.readline()
[144] Fix | Delete
self.lineno = self.lineno + 1
[145] Fix | Delete
elif self.posix and nextchar in self.escape:
[146] Fix | Delete
escapedstate = 'a'
[147] Fix | Delete
self.state = nextchar
[148] Fix | Delete
elif nextchar in self.wordchars:
[149] Fix | Delete
self.token = nextchar
[150] Fix | Delete
self.state = 'a'
[151] Fix | Delete
elif nextchar in self.quotes:
[152] Fix | Delete
if not self.posix:
[153] Fix | Delete
self.token = nextchar
[154] Fix | Delete
self.state = nextchar
[155] Fix | Delete
elif self.whitespace_split:
[156] Fix | Delete
self.token = nextchar
[157] Fix | Delete
self.state = 'a'
[158] Fix | Delete
else:
[159] Fix | Delete
self.token = nextchar
[160] Fix | Delete
if self.token or (self.posix and quoted):
[161] Fix | Delete
break # emit current token
[162] Fix | Delete
else:
[163] Fix | Delete
continue
[164] Fix | Delete
elif self.state in self.quotes:
[165] Fix | Delete
quoted = True
[166] Fix | Delete
if not nextchar: # end of file
[167] Fix | Delete
if self.debug >= 2:
[168] Fix | Delete
print "shlex: I see EOF in quotes state"
[169] Fix | Delete
# XXX what error should be raised here?
[170] Fix | Delete
raise ValueError, "No closing quotation"
[171] Fix | Delete
if nextchar == self.state:
[172] Fix | Delete
if not self.posix:
[173] Fix | Delete
self.token = self.token + nextchar
[174] Fix | Delete
self.state = ' '
[175] Fix | Delete
break
[176] Fix | Delete
else:
[177] Fix | Delete
self.state = 'a'
[178] Fix | Delete
elif self.posix and nextchar in self.escape and \
[179] Fix | Delete
self.state in self.escapedquotes:
[180] Fix | Delete
escapedstate = self.state
[181] Fix | Delete
self.state = nextchar
[182] Fix | Delete
else:
[183] Fix | Delete
self.token = self.token + nextchar
[184] Fix | Delete
elif self.state in self.escape:
[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 escape state"
[188] Fix | Delete
# XXX what error should be raised here?
[189] Fix | Delete
raise ValueError, "No escaped character"
[190] Fix | Delete
# In posix shells, only the quote itself or the escape
[191] Fix | Delete
# character may be escaped within quotes.
[192] Fix | Delete
if escapedstate in self.quotes and \
[193] Fix | Delete
nextchar != self.state and nextchar != escapedstate:
[194] Fix | Delete
self.token = self.token + self.state
[195] Fix | Delete
self.token = self.token + nextchar
[196] Fix | Delete
self.state = escapedstate
[197] Fix | Delete
elif self.state == 'a':
[198] Fix | Delete
if not nextchar:
[199] Fix | Delete
self.state = None # end of file
[200] Fix | Delete
break
[201] Fix | Delete
elif nextchar in self.whitespace:
[202] Fix | Delete
if self.debug >= 2:
[203] Fix | Delete
print "shlex: I see whitespace in word state"
[204] Fix | Delete
self.state = ' '
[205] Fix | Delete
if self.token or (self.posix and quoted):
[206] Fix | Delete
break # emit current token
[207] Fix | Delete
else:
[208] Fix | Delete
continue
[209] Fix | Delete
elif nextchar in self.commenters:
[210] Fix | Delete
self.instream.readline()
[211] Fix | Delete
self.lineno = self.lineno + 1
[212] Fix | Delete
if self.posix:
[213] Fix | Delete
self.state = ' '
[214] Fix | Delete
if self.token or (self.posix and quoted):
[215] Fix | Delete
break # emit current token
[216] Fix | Delete
else:
[217] Fix | Delete
continue
[218] Fix | Delete
elif self.posix and nextchar in self.quotes:
[219] Fix | Delete
self.state = nextchar
[220] Fix | Delete
elif self.posix and nextchar in self.escape:
[221] Fix | Delete
escapedstate = 'a'
[222] Fix | Delete
self.state = nextchar
[223] Fix | Delete
elif nextchar in self.wordchars or nextchar in self.quotes \
[224] Fix | Delete
or self.whitespace_split:
[225] Fix | Delete
self.token = self.token + nextchar
[226] Fix | Delete
else:
[227] Fix | Delete
self.pushback.appendleft(nextchar)
[228] Fix | Delete
if self.debug >= 2:
[229] Fix | Delete
print "shlex: I see punctuation in word state"
[230] Fix | Delete
self.state = ' '
[231] Fix | Delete
if self.token:
[232] Fix | Delete
break # emit current token
[233] Fix | Delete
else:
[234] Fix | Delete
continue
[235] Fix | Delete
result = self.token
[236] Fix | Delete
self.token = ''
[237] Fix | Delete
if self.posix and not quoted and result == '':
[238] Fix | Delete
result = None
[239] Fix | Delete
if self.debug > 1:
[240] Fix | Delete
if result:
[241] Fix | Delete
print "shlex: raw token=" + repr(result)
[242] Fix | Delete
else:
[243] Fix | Delete
print "shlex: raw token=EOF"
[244] Fix | Delete
return result
[245] Fix | Delete
[246] Fix | Delete
def sourcehook(self, newfile):
[247] Fix | Delete
"Hook called on a filename to be sourced."
[248] Fix | Delete
if newfile[0] == '"':
[249] Fix | Delete
newfile = newfile[1:-1]
[250] Fix | Delete
# This implements cpp-like semantics for relative-path inclusion.
[251] Fix | Delete
if isinstance(self.infile, basestring) and not os.path.isabs(newfile):
[252] Fix | Delete
newfile = os.path.join(os.path.dirname(self.infile), newfile)
[253] Fix | Delete
return (newfile, open(newfile, "r"))
[254] Fix | Delete
[255] Fix | Delete
def error_leader(self, infile=None, lineno=None):
[256] Fix | Delete
"Emit a C-compiler-like, Emacs-friendly error-message leader."
[257] Fix | Delete
if infile is None:
[258] Fix | Delete
infile = self.infile
[259] Fix | Delete
if lineno is None:
[260] Fix | Delete
lineno = self.lineno
[261] Fix | Delete
return "\"%s\", line %d: " % (infile, lineno)
[262] Fix | Delete
[263] Fix | Delete
def __iter__(self):
[264] Fix | Delete
return self
[265] Fix | Delete
[266] Fix | Delete
def next(self):
[267] Fix | Delete
token = self.get_token()
[268] Fix | Delete
if token == self.eof:
[269] Fix | Delete
raise StopIteration
[270] Fix | Delete
return token
[271] Fix | Delete
[272] Fix | Delete
def split(s, comments=False, posix=True):
[273] Fix | Delete
lex = shlex(s, posix=posix)
[274] Fix | Delete
lex.whitespace_split = True
[275] Fix | Delete
if not comments:
[276] Fix | Delete
lex.commenters = ''
[277] Fix | Delete
return list(lex)
[278] Fix | Delete
[279] Fix | Delete
if __name__ == '__main__':
[280] Fix | Delete
if len(sys.argv) == 1:
[281] Fix | Delete
lexer = shlex()
[282] Fix | Delete
else:
[283] Fix | Delete
file = sys.argv[1]
[284] Fix | Delete
lexer = shlex(open(file), file)
[285] Fix | Delete
while 1:
[286] Fix | Delete
tt = lexer.get_token()
[287] Fix | Delete
if tt:
[288] Fix | Delete
print "Token: " + repr(tt)
[289] Fix | Delete
else:
[290] Fix | Delete
break
[291] Fix | Delete
[292] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function