shlex: Some lint cleanup

This commit is contained in:
Florian Bruhin 2014-11-03 21:43:34 +01:00
parent b8d9f3b041
commit 9180a8b0bb
2 changed files with 17 additions and 17 deletions

View File

@ -72,5 +72,3 @@ class SplitTests(unittest.TestCase):
"""Test split with an unfinished escape and quotes..""" """Test split with an unfinished escape and quotes.."""
items = split.split('one "two\\') items = split.split('one "two\\')
self.assertEqual(items, ['one', 'two\\']) self.assertEqual(items, ['one', 'two\\'])

View File

@ -19,23 +19,24 @@
"""Our own fork of shlex.split with some added and removed features.""" """Our own fork of shlex.split with some added and removed features."""
import sys
from collections import deque from collections import deque
from io import StringIO from io import StringIO
class ShellLexer: class ShellLexer:
"A lexical analyzer class for simple shell-like syntaxes."
def __init__(self, instream=None, infile=None): """A lexical analyzer class for simple shell-like syntaxes.
if isinstance(instream, str):
instream = StringIO(instream) Based on Python's shlex, but cleaned up, removed some features, and added
if instream is not None: some features useful for qutebrowser.
self.instream = instream
self.infile = infile Attributes:
else: FIXME
self.instream = sys.stdin """
self.infile = None
def __init__(self, s):
self.instream = StringIO(s)
self.eof = None self.eof = None
self.wordchars = ('abcdfeghijklmnopqrstuvwxyz' self.wordchars = ('abcdfeghijklmnopqrstuvwxyz'
'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_') 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_')
@ -74,13 +75,14 @@ class ShellLexer:
return raw return raw
def read_token(self): def read_token(self):
"""Read a raw token from the input stream."""
quoted = False quoted = False
escapedstate = ' ' escapedstate = ' '
while True: while True:
nextchar = self.instream.read(1) nextchar = self.instream.read(1)
if self.debug >= 3: if self.debug >= 3:
print("shlex: in state", repr(self.state), \ print("shlex: in state", repr(self.state), "I see character:",
"I see character:", repr(nextchar)) repr(nextchar))
if self.state is None: if self.state is None:
self.token = '' # past end of file self.token = '' # past end of file
break break
@ -157,8 +159,8 @@ class ShellLexer:
elif nextchar in self.escape: elif nextchar in self.escape:
escapedstate = 'a' escapedstate = 'a'
self.state = nextchar self.state = nextchar
elif nextchar in self.wordchars or nextchar in self.quotes \ elif (nextchar in self.wordchars or nextchar in self.quotes or
or self.whitespace_split: self.whitespace_split):
self.token = self.token + nextchar self.token = self.token + nextchar
else: else:
self.pushback.appendleft(nextchar) self.pushback.appendleft(nextchar)