當前位置: 首頁>>代碼示例>>Python>>正文


Python shlex.shlex方法代碼示例

本文整理匯總了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 
開發者ID:IronLanguages,項目名稱:ironpython2,代碼行數:18,代碼來源:win32rcparser.py

示例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 
開發者ID:IronLanguages,項目名稱:ironpython2,代碼行數:27,代碼來源:win32rcparser.py

示例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 
開發者ID:muhanzhang,項目名稱:D-VAE,代碼行數:26,代碼來源:configparser.py

示例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 
開發者ID:pypa,項目名稱:pipenv,代碼行數:27,代碼來源:delegator.py

示例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("'", "'\"'\"'") + "'" 
開發者ID:pypa,項目名稱:pipenv,代碼行數:23,代碼來源:lib.py

示例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('"', '"\'"\'"') + '"' 
開發者ID:pypa,項目名稱:pipenv,代碼行數:23,代碼來源:lib.py

示例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) 
開發者ID:nedbat,項目名稱:cog,代碼行數:18,代碼來源:cogapp.py

示例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 
開發者ID:TuuuNya,項目名稱:WebPocket,代碼行數:24,代碼來源:parsing.py

示例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 
開發者ID:Microvellum,項目名稱:Fluid-Designer,代碼行數:18,代碼來源:test_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 
開發者ID:Zheaoli,項目名稱:pipenv-sublime,代碼行數:27,代碼來源:delegator.py

示例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 
開發者ID:urinieto,項目名稱:msaf,代碼行數:25,代碼來源:configparser.py

示例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) 
開發者ID:apache,項目名稱:incubator-spot,代碼行數:16,代碼來源:streaming.py

示例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) 
開發者ID:apache,項目名稱:incubator-spot,代碼行數:14,代碼來源:bluecoat.py

示例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) 
開發者ID:tox-dev,項目名稱:tox,代碼行數:40,代碼來源:__init__.py

示例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) 
開發者ID:AtomLinter,項目名稱:linter-pylama,代碼行數:13,代碼來源:typecheck.py


注:本文中的shlex.shlex方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。