mirror of
https://github.com/xonsh/xonsh.git
synced 2025-03-07 01:40:58 +01:00
Add support for path-yielding backticks
This commit is contained in:
parent
c685de9803
commit
d5d259b0db
3 changed files with 18 additions and 9 deletions
|
@ -154,16 +154,19 @@ def globsearch(s):
|
||||||
sort_result=glob_sorted)
|
sort_result=glob_sorted)
|
||||||
|
|
||||||
|
|
||||||
def pathsearch(func, s, pymode=False):
|
def pathsearch(func, s, pymode=False, pathobj=False):
|
||||||
"""
|
"""
|
||||||
Takes a string and returns a list of file paths that match (regex, glob,
|
Takes a string and returns a list of file paths that match (regex, glob,
|
||||||
or arbitrary search function).
|
or arbitrary search function). If pathobj=True, the return is a list of
|
||||||
|
pathlib.Path objects instead of strings.
|
||||||
"""
|
"""
|
||||||
if (not callable(func) or
|
if (not callable(func) or
|
||||||
len(inspect.signature(func).parameters) != 1):
|
len(inspect.signature(func).parameters) != 1):
|
||||||
error = "%r is not a known path search function"
|
error = "%r is not a known path search function"
|
||||||
raise XonshError(error % func)
|
raise XonshError(error % func)
|
||||||
o = func(s)
|
o = func(s)
|
||||||
|
if pathobj and pymode: #this doesn't make sense in subprocess mode
|
||||||
|
o = list(map(path_literal, o))
|
||||||
no_match = [] if pymode else [s]
|
no_match = [] if pymode else [s]
|
||||||
return o if len(o) != 0 else no_match
|
return o if len(o) != 0 else no_match
|
||||||
|
|
||||||
|
|
|
@ -129,15 +129,18 @@ def xonsh_pathsearch(pattern, pymode=False, lineno=None, col=None):
|
||||||
searchfunc, pattern = RE_SEARCHPATH.match(pattern).groups()
|
searchfunc, pattern = RE_SEARCHPATH.match(pattern).groups()
|
||||||
pattern = ast.Str(s=pattern, lineno=lineno,
|
pattern = ast.Str(s=pattern, lineno=lineno,
|
||||||
col_offset=col)
|
col_offset=col)
|
||||||
if searchfunc in {'r', ''}:
|
pathobj = False
|
||||||
func = '__xonsh_regexsearch__'
|
if searchfunc.startswith('@'):
|
||||||
elif searchfunc == 'g':
|
func = searchfunc[1:]
|
||||||
|
elif 'g' in searchfunc:
|
||||||
func = '__xonsh_globsearch__'
|
func = '__xonsh_globsearch__'
|
||||||
|
pathobj = 'p' in searchfunc
|
||||||
else:
|
else:
|
||||||
func = searchfunc[1:] # remove the '@' character
|
func = '__xonsh_regexsearch__'
|
||||||
|
pathobj = 'p' in searchfunc
|
||||||
func = ast.Name(id=func, ctx=ast.Load(), lineno=lineno,
|
func = ast.Name(id=func, ctx=ast.Load(), lineno=lineno,
|
||||||
col_offset=col)
|
col_offset=col)
|
||||||
return xonsh_call('__xonsh_pathsearch__', args=[func, pattern, pymode],
|
return xonsh_call('__xonsh_pathsearch__', args=[func, pattern, pymode, pathobj],
|
||||||
lineno=lineno, col=col)
|
lineno=lineno, col=col)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -221,7 +221,7 @@ String = group(StringPrefix + r"'[^\n'\\]*(?:\\.[^\n'\\]*)*'",
|
||||||
StringPrefix + r'"[^\n"\\]*(?:\\.[^\n"\\]*)*"')
|
StringPrefix + r'"[^\n"\\]*(?:\\.[^\n"\\]*)*"')
|
||||||
|
|
||||||
# Xonsh-specific Syntax
|
# Xonsh-specific Syntax
|
||||||
SearchPath = r"((?:[rg]|@\w*)?)`([^\n`\\]*(?:\\.[^\n`\\]*)*)`"
|
SearchPath = r"((?:[rgp]+|@\w*)?)`([^\n`\\]*(?:\\.[^\n`\\]*)*)`"
|
||||||
|
|
||||||
# Because of leftmost-then-longest match semantics, be sure to put the
|
# Because of leftmost-then-longest match semantics, be sure to put the
|
||||||
# longest operators first (e.g., if = came before ==, == would get
|
# longest operators first (e.g., if = came before ==, == would get
|
||||||
|
@ -275,8 +275,9 @@ endpats = {"'": Single, '"': Double,
|
||||||
"RB'''": Single3, 'RB"""': Double3,
|
"RB'''": Single3, 'RB"""': Double3,
|
||||||
"u'''": Single3, 'u"""': Double3,
|
"u'''": Single3, 'u"""': Double3,
|
||||||
"U'''": Single3, 'U"""': Double3,
|
"U'''": Single3, 'U"""': Double3,
|
||||||
|
"p'''": Single3, 'p"""': Double3,
|
||||||
'r': None, 'R': None, 'b': None, 'B': None,
|
'r': None, 'R': None, 'b': None, 'B': None,
|
||||||
'u': None, 'U': None}
|
'u': None, 'U': None, 'p': None}
|
||||||
|
|
||||||
triple_quoted = {}
|
triple_quoted = {}
|
||||||
for t in ("'''", '"""',
|
for t in ("'''", '"""',
|
||||||
|
@ -287,6 +288,7 @@ for t in ("'''", '"""',
|
||||||
"rb'''", 'rb"""', "rB'''", 'rB"""',
|
"rb'''", 'rb"""', "rB'''", 'rB"""',
|
||||||
"Rb'''", 'Rb"""', "RB'''", 'RB"""',
|
"Rb'''", 'Rb"""', "RB'''", 'RB"""',
|
||||||
"u'''", 'u"""', "U'''", 'U"""',
|
"u'''", 'u"""', "U'''", 'U"""',
|
||||||
|
"p'''", 'p""""',
|
||||||
):
|
):
|
||||||
triple_quoted[t] = t
|
triple_quoted[t] = t
|
||||||
single_quoted = {}
|
single_quoted = {}
|
||||||
|
@ -298,6 +300,7 @@ for t in ("'", '"',
|
||||||
"rb'", 'rb"', "rB'", 'rB"',
|
"rb'", 'rb"', "rB'", 'rB"',
|
||||||
"Rb'", 'Rb"', "RB'", 'RB"',
|
"Rb'", 'Rb"', "RB'", 'RB"',
|
||||||
"u'", 'u"', "U'", 'U"',
|
"u'", 'u"', "U'", 'U"',
|
||||||
|
"p'", 'p"',
|
||||||
):
|
):
|
||||||
single_quoted[t] = t
|
single_quoted[t] = t
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue