def shlexSplitList(entity, dataDelimiter=u' ,'):
import shlex
lexer = shlex.shlex(entity, posix=True)
lexer.whitespace = dataDelimiter
lexer.whitespace_split = True
return list(lexer)
python类shlex()的实例源码
def open(self, rcstream):
self.lex = shlex.shlex(rcstream)
self.lex.commenters = "//#"
def _parse_cmd_string(self, cmd_string):
lex = shlex.shlex(cmd_string, posix=True)
lex.whitespace_split = True
lex.commenters = ''
lex.wordchars += '.'
try:
return list(lex)
except ValueError:
podutils.raise_('Incorrect cmd string')
def onecmd(self, line):
"""Override onecmd.
1 - So we don't have to have a do_EOF method.
2 - So we can strip comments
3 - So we can track line numbers
"""
if DEBUG:
print('Executing "%s"' % line)
self.line_num += 1
if line == "EOF":
if cmd.Cmd.use_rawinput:
# This means that we printed a prompt, and we'll want to
# print a newline to pretty things up for the caller.
self.print('')
return True
# Strip comments
comment_idx = line.find("#")
if comment_idx >= 0:
line = line[0:comment_idx]
line = line.strip()
# search multiple commands on the same line
lexer = shlex.shlex(line)
lexer.whitespace = ''
for issemicolon, group in itertools.groupby(lexer, lambda x: x == ";"):
if not issemicolon:
self.onecmd_exec("".join(group))
def split(value):
lex = shlex.shlex(value)
lex.quotes = '"'
lex.whitespace_split = True
lex.commenters = ''
return list(lex)
def arg_split(s, posix=False, strict=True):
"""Split a command line's arguments in a shell-like manner.
This is a modified version of the standard library's shlex.split()
function, but with a default of posix=False for splitting, so that quotes
in inputs are respected.
if strict=False, then any errors shlex.split would raise will result in the
unparsed remainder being the last element of the list, rather than raising.
This is because we sometimes use arg_split to parse things other than
command-line args.
"""
lex = shlex.shlex(s, posix=posix)
lex.whitespace_split = True
# Extract tokens, ensuring that things like leaving open quotes
# does not cause this to raise. This is important, because we
# sometimes pass Python source through this (e.g. %timeit f(" ")),
# and it shouldn't raise an exception.
# It may be a bad idea to parse things that are not command-line args
# through this function, but we do, so let's be safe about it.
lex.commenters='' #fix for GH-1269
tokens = []
while True:
try:
tokens.append(next(lex))
except StopIteration:
break
except ValueError:
if strict:
raise
# couldn't parse, get remaining blob as last token
tokens.append(lex.token)
break
return tokens
def splitTest(self, data, comments):
for i in range(len(data)):
l = shlex.split(data[i][0], comments=comments)
self.assertEqual(l, data[i][1:],
"%s: %s != %s" %
(data[i][0], l, data[i][1:]))
def oldSplit(self, s):
ret = []
lex = shlex.shlex(io.StringIO(s))
tok = lex.get_token()
while tok:
ret.append(tok)
tok = lex.get_token()
return ret
def testCompat(self):
"""Test compatibility interface"""
for i in range(len(self.data)):
l = self.oldSplit(self.data[i][0])
self.assertEqual(l, self.data[i][1:],
"%s: %s != %s" %
(self.data[i][0], l, self.data[i][1:]))
# Allow this test to be used with old shlex.py
def split(text: str, maxsplit: int=-1):
""" Split a string with shlex when possible, and add support for maxsplit.
:param text: Text to split.
:param maxsplit: Number of times to split. The rest is returned without splitting.
:return: list: split text.
"""
# Generate a shlex object for eventually splitting manually
split_object = shlex.shlex(text, posix=True)
split_object.quotes = '"`'
split_object.whitespace_split = True
split_object.commenters = ""
# When the maxsplit is disabled, return the entire split object
if maxsplit == -1:
try:
return list(split_object)
except ValueError: # If there is a problem with quotes, use the regular split method
return text.split()
# Create a list for the following split keywords
maxsplit_object = []
splits = 0
# Split until we've reached the limit
while splits < maxsplit:
maxsplit_object.append(next(split_object))
splits += 1
# Add any following text without splitting
maxsplit_object.append(split_object.instream.read())
return maxsplit_object
def show_window_options(self, option=None, g=False):
"""Return a dict of options for the window.
For familiarity with tmux, the option ``option`` param forwards to pick
a single option, forwarding to :meth:`Window.show_window_option`.
:param option: optional. show a single option.
:type option: str
:param g: Pass ``-g`` flag for global variable
:type g: bool
:rtype: :py:obj:`dict`
"""
tmux_args = tuple()
if g:
tmux_args += ('-g',)
if option:
return self.show_window_option(option, g=g)
else:
tmux_args += ('show-window-options',)
cmd = self.cmd(
*tmux_args
).stdout
# The shlex.split function splits the args at spaces, while also
# retaining quoted sub-strings.
# shlex.split('this is "a test"') => ['this', 'is', 'a test']
cmd = [tuple(shlex.split(item)) for item in cmd]
window_options = dict(cmd)
for key, value in window_options.items():
if value.isdigit():
window_options[key] = int(value)
return window_options
def show_window_option(self, option, g=False):
"""Return a list of options for the window.
todo: test and return True/False for on/off string
:param option: option to return.
:type option: str
:param g: Pass ``-g`` flag, global.
:type g: bool
:rtype: str, int
:raises: :exc:`exc.OptionError`, :exc:`exc.UnknownOption`,
:exc:`exc.InvalidOption`, :exc:`exc.AmbiguousOption`
"""
tmux_args = tuple()
if g:
tmux_args += ('-g',)
tmux_args += (option,)
cmd = self.cmd(
'show-window-options', *tmux_args
)
if len(cmd.stderr):
handle_option_error(cmd.stderr[0])
if not len(cmd.stdout):
return None
option = [shlex.split(item) for item in cmd.stdout][0]
if option[1].isdigit():
option = (option[0], int(option[1]))
return option[1]
def splitTest(self, data, comments):
for i in range(len(data)):
l = shlex.split(data[i][0], comments=comments)
self.assertEqual(l, data[i][1:],
"%s: %s != %s" %
(data[i][0], l, data[i][1:]))
def oldSplit(self, s):
ret = []
lex = shlex.shlex(StringIO(s))
tok = lex.get_token()
while tok:
ret.append(tok)
tok = lex.get_token()
return ret
def testEmptyStringHandling(self):
"""Test that parsing of empty strings is correctly handled."""
# see Issue #21999
expected = ['', ')', 'abc']
s = shlex.shlex("'')abc", posix=True)
slist = list(s)
self.assertEqual(slist, expected)
expected = ["''", ')', 'abc']
s = shlex.shlex("'')abc")
self.assertEqual(list(s), expected)
# Allow this test to be used with old shlex.py
def splitTest(self, data, comments):
for i in range(len(data)):
l = shlex.split(data[i][0], comments=comments)
self.assertEqual(l, data[i][1:],
"%s: %s != %s" %
(data[i][0], l, data[i][1:]))
def oldSplit(self, s):
ret = []
lex = shlex.shlex(StringIO(s))
tok = lex.get_token()
while tok:
ret.append(tok)
tok = lex.get_token()
return ret
def testEmptyStringHandling(self):
"""Test that parsing of empty strings is correctly handled."""
# see Issue #21999
expected = ['', ')', 'abc']
s = shlex.shlex("'')abc", posix=True)
slist = list(s)
self.assertEqual(slist, expected)
expected = ["''", ')', 'abc']
s = shlex.shlex("'')abc")
self.assertEqual(list(s), expected)
# Allow this test to be used with old shlex.py
def splitTest(self, data, comments):
for i in range(len(data)):
l = shlex.split(data[i][0], comments=comments)
self.assertEqual(l, data[i][1:],
"%s: %s != %s" %
(data[i][0], l, data[i][1:]))