本文整理汇总了Python中shlex.shlex方法的典型用法代码示例。如果您正苦于以下问题:Python shlex.shlex方法的具体用法?Python shlex.shlex怎么用?Python shlex.shlex使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类shlex
的用法示例。
在下文中一共展示了shlex.shlex方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: currentQuotedString
# 需要导入模块: import shlex [as 别名]
# 或者: from shlex import shlex [as 别名]
def currentQuotedString(self):
# Handle quoted strings - pity shlex doesn't handle it.
assert self.token.startswith('"'), self.token
bits = [self.token]
while 1:
tok = self.getToken()
if not tok.startswith('"'):
self.ungetToken()
break
bits.append(tok)
sval = "".join(bits)[1:-1] # Remove end quotes.
# Fixup quotes in the body, and all (some?) quoted characters back
# to their raw value.
for i, o in ('""', '"'), ("\\r", "\r"), ("\\n", "\n"), ("\\t", "\t"):
sval = sval.replace(i, o)
return sval
示例2: parseH
# 需要导入模块: import shlex [as 别名]
# 或者: from shlex import shlex [as 别名]
def parseH(self, file):
lex = shlex.shlex(file)
lex.commenters = "//"
token = " "
while token is not None:
token = lex.get_token()
if token == "" or token is None:
token = None
else:
if token=='define':
n = lex.get_token()
i = int(lex.get_token())
self.ids[n] = i
if i in self.names:
# Dupe ID really isn't a problem - most consumers
# want to go from name->id, and this is OK.
# It means you can't go from id->name though.
pass
# ignore AppStudio special ones
#if not n.startswith("_APS_"):
# print "Duplicate id",i,"for",n,"is", self.names[i]
else:
self.names[i] = n
if self.next_id<=i:
self.next_id = i+1
示例3: parse_config_string
# 需要导入模块: import shlex [as 别名]
# 或者: from shlex import shlex [as 别名]
def parse_config_string(config_string, issue_warnings=True):
"""
Parses a config string (comma-separated key=value components) into a dict.
"""
config_dict = {}
my_splitter = shlex.shlex(config_string, posix=True)
my_splitter.whitespace = ','
my_splitter.whitespace_split = True
for kv_pair in my_splitter:
kv_pair = kv_pair.strip()
if not kv_pair:
continue
kv_tuple = kv_pair.split('=', 1)
if len(kv_tuple) == 1:
if issue_warnings:
TheanoConfigWarning.warn(
("Config key '%s' has no value, ignoring it"
% kv_tuple[0]),
stacklevel=1)
else:
k, v = kv_tuple
# subsequent values for k will override earlier ones
config_dict[k] = v
return config_dict
示例4: _expand_args
# 需要导入模块: import shlex [as 别名]
# 或者: from shlex import shlex [as 别名]
def _expand_args(command):
"""Parses command strings and returns a Popen-ready list."""
# Prepare arguments.
if isinstance(command, STR_TYPES):
if sys.version_info[0] == 2:
splitter = shlex.shlex(command.encode("utf-8"))
elif sys.version_info[0] == 3:
splitter = shlex.shlex(command)
else:
splitter = shlex.shlex(command.encode("utf-8"))
splitter.whitespace = "|"
splitter.whitespace_split = True
command = []
while True:
token = splitter.get_token()
if token:
command.append(token)
else:
break
command = list(map(shlex.split, command))
return command
示例5: single_quote
# 需要导入模块: import shlex [as 别名]
# 或者: from shlex import shlex [as 别名]
def single_quote(s):
"""Escape a string with single quotes in order to be parsed as a single element by shlex
Parameters
----------
s : str
The string to quote
Returns
-------
str
The quoted string
"""
if not s:
return "''"
if find_unsafe(s) is None:
return s
# use single quotes, and put single quotes into double quotes
# the string $'b is then quoted as '$'"'"'b'
return "'" + s.replace("'", "'\"'\"'") + "'"
示例6: double_quote
# 需要导入模块: import shlex [as 别名]
# 或者: from shlex import shlex [as 别名]
def double_quote(s):
"""Escape a string with double quotes in order to be parsed as a single element by shlex
Parameters
----------
s : str
The string to quote
Returns
-------
str
The quoted string
"""
if not s:
return '""'
if find_unsafe(s) is None:
return s
# use double quotes, and put double quotes into single quotes
# the string $"b is then quoted as "$"'"'"b"
return '"' + s.replace('"', '"\'"\'"') + '"'
示例7: processFileList
# 需要导入模块: import shlex [as 别名]
# 或者: from shlex import shlex [as 别名]
def processFileList(self, sFileList):
""" Process the files in a file list.
"""
flist = self.openInputFile(sFileList)
lines = flist.readlines()
flist.close()
for l in lines:
# Use shlex to parse the line like a shell.
lex = shlex.shlex(l, posix=True)
lex.whitespace_split = True
lex.commenters = '#'
# No escapes, so that backslash can be part of the path
lex.escape = ''
args = list(lex)
if args:
self.processArguments(args)
示例8: tokenize
# 需要导入模块: import shlex [as 别名]
# 或者: from shlex import shlex [as 别名]
def tokenize(self, line: str) -> List[str]:
"""Lex a string into a list of tokens.
Comments are removed, and shortcuts and aliases are expanded.
Raises ValueError if there are unclosed quotation marks.
"""
# strip C-style comments
# shlex will handle the python/shell style comments for us
line = re.sub(self.comment_pattern, self._comment_replacer, line)
# expand shortcuts and aliases
line = self._expand(line)
# split on whitespace
lexer = shlex.shlex(line, posix=False)
lexer.whitespace_split = True
# custom lexing
tokens = self._split_on_punctuation(list(lexer))
return tokens
示例9: testQuote
# 需要导入模块: import shlex [as 别名]
# 或者: from shlex import shlex [as 别名]
def testQuote(self):
safeunquoted = string.ascii_letters + string.digits + '@%_-+=:,./'
unicode_sample = '\xe9\xe0\xdf' # e + acute accent, a + grave, sharp s
unsafe = '"`$\\!' + unicode_sample
self.assertEqual(shlex.quote(''), "''")
self.assertEqual(shlex.quote(safeunquoted), safeunquoted)
self.assertEqual(shlex.quote('test file name'), "'test file name'")
for u in unsafe:
self.assertEqual(shlex.quote('test%sname' % u),
"'test%sname'" % u)
for u in unsafe:
self.assertEqual(shlex.quote("test%s'name'" % u),
"'test%s'\"'\"'name'\"'\"''" % u)
# Allow this test to be used with old shlex.py
示例10: _expand_args
# 需要导入模块: import shlex [as 别名]
# 或者: from shlex import shlex [as 别名]
def _expand_args(command):
"""Parses command strings and returns a Popen-ready list."""
# Prepare arguments.
if isinstance(command, STR_TYPES):
if sys.version_info[0] == 2:
splitter = shlex.shlex(command.encode('utf-8'))
elif sys.version_info[0] == 3:
splitter = shlex.shlex(command)
else:
splitter = shlex.shlex(command.encode('utf-8'))
splitter.whitespace = '|'
splitter.whitespace_split = True
command = []
while True:
token = splitter.get_token()
if token:
command.append(token)
else:
break
command = list(map(shlex.split, command))
return command
示例11: parse_config_string
# 需要导入模块: import shlex [as 别名]
# 或者: from shlex import shlex [as 别名]
def parse_config_string(config_string, issue_warnings=True):
"""
Parses a config string (comma-separated key=value components) into a dict.
"""
config_dict = {}
my_splitter = shlex.shlex(config_string, posix=True)
my_splitter.whitespace = ','
my_splitter.whitespace_split = True
for kv_pair in my_splitter:
kv_pair = kv_pair.strip()
if not kv_pair:
continue
kv_tuple = kv_pair.split('=', 1)
if len(kv_tuple) == 1:
if issue_warnings:
MsafConfigWarning.warn(
("Config key '%s' has no value, ignoring it" %
kv_tuple[0]), stacklevel=1)
else:
k, v = kv_tuple
# subsequent values for k will override earlier ones
config_dict[k] = v
return config_dict
示例12: _analyzer
# 需要导入模块: import shlex [as 别名]
# 或者: from shlex import shlex [as 别名]
def _analyzer(line):
'''
A lexical analyzer for simple shell-like syntaxes. Split given line into fields.
:param line: Line to split.
:returs : List of fields.
:rtype : ``list``
'''
lex = shlex.shlex(line)
lex.quotes = '"'
lex.whitespace_split = True
lex.commenters = ''
return list(lex)
示例13: split_log_entry
# 需要导入模块: import shlex [as 别名]
# 或者: from shlex import shlex [as 别名]
def split_log_entry(line):
"""
Split the given line into its fields.
:param line: line to split
:returns: list
"""
lex = shlex.shlex(line)
lex.quotes = '"'
lex.whitespace_split = True
lex.commenters = ''
return list(lex)
示例14: processcommand
# 需要导入模块: import shlex [as 别名]
# 或者: from shlex import shlex [as 别名]
def processcommand(cls, reader, command, replace=True):
posargs = getattr(reader, "posargs", "")
if sys.platform.startswith("win"):
posargs_string = list2cmdline([x for x in posargs if x])
else:
posargs_string = " ".join([shlex_quote(x) for x in posargs if x])
# Iterate through each word of the command substituting as
# appropriate to construct the new command string. This
# string is then broken up into exec argv components using
# shlex.
if replace:
newcommand = ""
for word in CommandParser(command).words():
if word == "{posargs}" or word == "[]":
newcommand += posargs_string
continue
elif word.startswith("{posargs:") and word.endswith("}"):
if posargs:
newcommand += posargs_string
continue
else:
word = word[9:-1]
new_arg = ""
new_word = reader._replace(word)
new_word = reader._replace(new_word)
new_word = new_word.replace("\\{", "{").replace("\\}", "}")
new_arg += new_word
newcommand += new_arg
else:
newcommand = command
# Construct shlex object that will not escape any values,
# use all values as is in argv.
shlexer = shlex.shlex(newcommand, posix=True)
shlexer.whitespace_split = True
shlexer.escape = ""
return list(shlexer)
示例15: open
# 需要导入模块: import shlex [as 别名]
# 或者: from shlex import shlex [as 别名]
def open(self):
# do this in open since config not fully initialized in __init__
# generated_members may contain regular expressions
# (surrounded by quote `"` and followed by a comma `,`)
# REQUEST,aq_parent,"[a-zA-Z]+_set{1,2}"' =>
# ('REQUEST', 'aq_parent', '[a-zA-Z]+_set{1,2}')
if isinstance(self.config.generated_members, six.string_types):
gen = shlex.shlex(self.config.generated_members)
gen.whitespace += ','
gen.wordchars += r'[]-+\.*?()|'
self.config.generated_members = tuple(tok.strip('"') for tok in gen)