我们从Python开源项目中,提取了以下49个代码示例,用于说明如何使用shlex.shlex()。
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
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
def __init__(self, synfiledata): #init self.instructions = [] self.dnscache = {} lexer = list(shlex.shlex(synfiledata)) itr_ctr = 0 while len(lexer) > 0: token = lexer[0] #should be the start of a new line if (token.lower() == 'flow'): (flowdecl, lexer) = self.lex_flow(lexer[1:]) self.instructions.append(flowdecl) else: #treat as an event (eventdecl, lexer) = self.lex_event(lexer) self.instructions.append(eventdecl) itr_ctr = itr_ctr + 1
def __init__(self, data): self.shape = {} self.unit = {} self.fields = [] for line in data: line = line.replace('\n', '') s = shlex.shlex(line) s.whitespace_split = True s.commenters = '' s.quotes = '"' line = list(s) # select the keys list and default values array if line[0] in self._KEYS: key_list = self._KEYS[line[0]] values = line[1:] + ['' for n in range(len(key_list) - len(line[1:]))] if line[0] == 'S': self.shape = dict(zip(key_list,values)) elif line[0] == 'U': self.unit = dict(zip(key_list,values)) elif line[0][0] == 'F': key_list = self._F_KEYS values = line + ['' for n in range(len(key_list) - len(line))] self.fields.append(dict(zip(key_list,values)))
def parse_for_var(self, line): lexer = shlex.shlex(line) lexer.wordchars = self.wordchars varname = lexer.get_token() is_env_var = lexer.get_token() == '=' value = ''.join(lexer) if is_env_var: return (varname, value) raise CronVarError("Not a variable.")
def safe_split_line(inputline): parser = shlex.shlex(inputline, posix=True) parser.whitespace_split = True res = [] # track whether or not we're looking at quoted text -- it should suppress a # lot of types of completion if we see an open quote without a close quote quoted = False try: for val in parser: res.append(val) # No closing quotation except ValueError: quoted = True # grab the last token from the shlexer if parser.token: res.append(parser.token) return res, quoted
def rename_window(self, new_name): """Return :class:`Window` object ``$ tmux rename-window <new_name>``. :param new_name: name of the window :type new_name: str """ import shlex lex = shlex.shlex(new_name) lex.escape = ' ' lex.whitespace_split = False try: self.cmd( 'rename-window', new_name ) self['window_name'] = new_name except Exception as e: logger.error(e) self.server._update_windows() return self
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
def split_args(line): """Version of shlex.split that silently accept incomplete strings.""" lex = shlex.shlex(line, posix=True) lex.whitespace_split = True lex.commenters = '' res = [] try: while True: res.append(next(lex)) except ValueError: # No closing quotation pass except StopIteration: # End of loop pass if lex.token: res.append(lex.token) return res
def do_bash_complete(cli, prog_name): comp_words = os.environ['COMP_WORDS'] try: cwords = shlex.split(comp_words) quoted = False except ValueError: # No closing quotation cwords = split_args(comp_words) quoted = True cword = int(os.environ['COMP_CWORD']) args = cwords[1:cword] try: incomplete = cwords[cword] except IndexError: incomplete = '' choices = get_choices(cli, prog_name, args, incomplete) if quoted: echo('\t'.join(opt for opt, _ in choices), nl=False) else: echo('\t'.join(re.sub(r"""([\s\\"'])""", r'\\\1', opt) for opt, _ in choices), nl=False) return True
def _expand_args(command): """Parses command strings and returns a Popen-ready list.""" # Prepare arguments. if isinstance(command, STR_TYPES): 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
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
def _run_hook(self, hook_name: str): key = f"exec_{hook_name}" value = self.config[key] if value == "/usr/bin/true": return self.logger.verbose( f"Running {hook_name} hook for {self.humanreadable_name}" ) lex = shlex.shlex(value) # noqa: T484 lex.whitespace_split = True command = list(lex) return iocage.lib.helpers.exec( command, logger=self.logger, env=self.env )
def smartsplit(string, sep): """Split while allowing escaping. So far, this seems to do what I expect - split at the separator, allow escaping via \, and allow the backslash itself to be escaped. One problem is that it can raise a ValueError when given a backslash without a character to escape. I'd really like a smart splitter without manually scan the string. But maybe that is exactly what should be done. """ assert string is not None # or shlex will read from stdin if not six.PY3: # On 2.6, shlex fails miserably with unicode input is_unicode = isinstance(string, unicode) if is_unicode: string = string.encode('utf8') l = shlex.shlex(string, posix=True) l.whitespace += ',' l.whitespace_split = True l.quotes = '' if not six.PY3 and is_unicode: return map(lambda s: s.decode('utf8'), list(l)) else: return list(l)
def parse_binary(cls, string): r""" Parse a string for a binary (executable). Allow multiple arguments to indicate the binary (as parsed by shlex). Return a list of arguments suitable for passing to subprocess functions. >>> ExternalTool.parse_binary('/usr/bin/lessc') ['/usr/bin/lessc'] >>> ExternalTool.parse_binary('node node_modules/bin/lessc') ['node', 'node_modules/bin/lessc'] >>> ExternalTool.parse_binary('"binary with spaces"') ['binary with spaces'] >>> ExternalTool.parse_binary(r'binary\ with\ spaces') ['binary with spaces'] >>> ExternalTool.parse_binary('') [] """ return shlex.split(string)
def read_file(file_path): f = open(file_path) # remove every comments and empty lines from the file content to avoid # false positives file = shlex.shlex(f, False) #file = filter(None, re.sub("#.*[^\n]", "", f.read()).splitlines()) return file
def _parse_flink_line(line, final_flags): """private""" lexer = shlex.shlex(line, posix = True) lexer.whitespace_split = True t = lexer.get_token() tmp_flags = [] while t: t = _parse_flink_token(lexer, t, tmp_flags) final_flags.extend(tmp_flags) return final_flags
def shsplit(s): """ Returs original list from what shjoin returned """ lex = shlex.shlex(s, posix=True) lex.escapedquotes = b'"\'' lex.whitespace_split = True return [ x.decode('utf-8') for x in list(lex) ]
def parameter_string_to_list(par): import shlex parameter_string = shlex.shlex(par) parameter_string.quotes = '"' parameter_string.whitespace_split = True parameter_string.commenters = '' parameters = list(parameter_string) return parameters
def shlexSplitList(entity, dataDelimiter=u' ,'): import shlex lexer = shlex.shlex(entity, posix=True) lexer.whitespace = dataDelimiter lexer.whitespace_split = True return list(lexer)
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 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