Merge pull request #1316 from pgoelz/sortglob

Sort results from globbing expressions
This commit is contained in:
Anthony Scopatz 2016-06-28 16:43:55 -04:00 committed by GitHub
commit 88b57b3198
7 changed files with 165 additions and 53 deletions

15
news/sortglob.rst Normal file
View file

@ -0,0 +1,15 @@
**Added:**
* The results of glob expressions are sorted if ``$GLOB_SORTED`` is set.
**Changed:**
* ``GLOB_SORTED`` is enabled by default.
**Deprecated:** None
**Removed:** None
**Fixed:** None
**Security:** None

View file

@ -1,9 +1,10 @@
# -*- coding: utf-8 -*-
"""Tests the xonsh lexer."""
"""Tests xonsh tools."""
import os
import pathlib
from tempfile import TemporaryDirectory
import stat
import builtins
import pytest
@ -15,7 +16,7 @@ from xonsh.tools import (
bool_or_int_to_str, bool_to_str, check_for_partial_string,
dynamic_cwd_tuple_to_str, ensure_int_or_slice, ensure_string,
env_path_to_str, escape_windows_cmd_string, executables_in,
expand_case_matching, find_next_break, is_bool, is_bool_or_int,
expand_case_matching, find_next_break, iglobpath, is_bool, is_bool_or_int,
is_callable, is_dynamic_cwd_width, is_env_path, is_float, is_int,
is_int_as_str, is_logfile_opt, is_slice_as_str, is_string,
is_string_or_callable, logfile_opt_to_str, str_to_env_path,
@ -25,6 +26,8 @@ from xonsh.tools import (
pathsep_to_upper_seq, seq_to_upper_pathsep,
)
from xonsh.commands_cache import CommandsCache
from xonsh.built_ins import expand_path
from xonsh.environ import Env
from tools import mock_xonsh_env
@ -317,6 +320,7 @@ def test_subexpr_from_unbalanced_parens():
obs = subexpr_from_unbalanced(expr, '(', ')')
assert exp == obs
def test_find_next_break():
cases = [
('ls && echo a', 0, 4),
@ -331,6 +335,41 @@ def test_find_next_break():
assert exp == obs
def test_iglobpath():
with TemporaryDirectory() as test_dir:
# Create files 00.test to 99.test in unsorted order
num = 18
for _ in range(100):
s = str(num).zfill(2)
path = os.path.join(test_dir, s + '.test')
with open(path, 'w') as file:
file.write(s + '\n')
num = (num + 37) % 100
# Create one file not matching the '*.test'
with open(os.path.join(test_dir, '07'), 'w') as file:
file.write('test\n')
with mock_xonsh_env(Env(EXPAND_ENV_VARS=False)):
builtins.__xonsh_expand_path__ = expand_path
paths = list(iglobpath(os.path.join(test_dir, '*.test'),
ignore_case=False, sort_result=False))
assert len(paths) == 100
paths = list(iglobpath(os.path.join(test_dir, '*'),
ignore_case=True, sort_result=False))
assert len(paths) == 101
paths = list(iglobpath(os.path.join(test_dir, '*.test'),
ignore_case=False, sort_result=True))
assert len(paths) == 100
assert paths == sorted(paths)
paths = list(iglobpath(os.path.join(test_dir, '*'),
ignore_case=True, sort_result=True))
assert len(paths) == 101
assert paths == sorted(paths)
def test_is_int():
cases = [
(42, True),
@ -389,7 +428,7 @@ def test_is_slice_as_str():
(None, False),
('42', False),
('-42', False),
(slice(1,2,3), False),
(slice(1, 2, 3), False),
([], False),
(False, False),
(True, False),
@ -527,8 +566,6 @@ def test_seq_to_upper_pathsep():
assert exp == obs
def test_is_env_path():
cases = [
('/home/wakka', False),
@ -566,8 +603,9 @@ def test_env_path_to_str():
def test_env_path():
# lambda to expand the expected paths
expand = lambda path: os.path.expanduser(os.path.expandvars(path))
def expand(path):
return os.path.expanduser(os.path.expandvars(path))
getitem_cases = [
('xonsh_dir', 'xonsh_dir'),
('.', '.'),
@ -604,11 +642,11 @@ def test_env_path():
# cases that involve pathlib.Path objects
pathlib_cases = [
(pathlib.Path('/home/wakka'), ['/home/wakka'.replace('/',os.sep)]),
(pathlib.Path('/home/wakka'), ['/home/wakka'.replace('/', os.sep)]),
(pathlib.Path('~/'), ['~']),
(pathlib.Path('.'), ['.']),
(['/home/wakka', pathlib.Path('/home/jakka'), '~/'],
['/home/wakka', '/home/jakka'.replace('/',os.sep), '~/']),
['/home/wakka', '/home/jakka'.replace('/', os.sep), '~/']),
(['/home/wakka', pathlib.Path('../'), '../'],
['/home/wakka', '..', '../']),
(['/home/wakka', pathlib.Path('~/'), '~/'],
@ -623,7 +661,8 @@ def test_env_path():
def test_env_path_slices():
# build os-dependent paths properly
mkpath = lambda *paths: os.sep + os.sep.join(paths)
def mkpath(*paths):
return os.sep + os.sep.join(paths)
# get all except the last element in a slice
slice_last = [
@ -790,6 +829,7 @@ def test_is_dynamic_cwd_width():
obs = is_dynamic_cwd_width(inp)
assert exp == obs
def test_is_logfile_opt():
cases = [
('throwback.log', True),
@ -808,6 +848,7 @@ def test_is_logfile_opt():
obs = is_logfile_opt(inp)
assert exp == obs
def test_to_logfile_opt():
cases = [
(True, None),
@ -823,6 +864,7 @@ def test_to_logfile_opt():
obs = to_logfile_opt(inp)
assert exp == obs
def test_logfile_opt_to_str():
cases = [
(None, ''),
@ -834,6 +876,7 @@ def test_logfile_opt_to_str():
obs = logfile_opt_to_str(inp)
assert exp == obs
def test_to_dynamic_cwd_tuple():
cases = [
('20', (20.0, 'c')),

View file

@ -38,9 +38,11 @@ skip_if_py35plus = pytest.mark.skipif(VER_MAJOR_MINOR < VER_3_5,
def sp(cmd):
return subprocess.check_output(cmd, universal_newlines=True)
class DummyStyler():
styles = defaultdict(None.__class__)
class DummyBaseShell(BaseShell):
def __init__(self):
@ -106,12 +108,14 @@ def mock_xonsh_env(xenv):
DEBUG_LEVEL = 0
EXECER = None
def execer_setup():
# only setup one parser
global EXECER
if EXECER is None:
EXECER = Execer(debug_level=DEBUG_LEVEL, login=False)
def check_exec(input, **kwargs):
with mock_xonsh_env(None):
if not input.endswith('\n'):
@ -119,8 +123,9 @@ def check_exec(input, **kwargs):
EXECER.debug_level = DEBUG_LEVEL
EXECER.exec(input, **kwargs)
def check_eval(input):
env = {'AUTO_CD': False, 'XONSH_ENCODING' :'utf-8',
env = {'AUTO_CD': False, 'XONSH_ENCODING': 'utf-8',
'XONSH_ENCODING_ERRORS': 'strict', 'PATH': []}
if ON_WINDOWS:
env['PATHEXT'] = ['.COM', '.EXE', '.BAT', '.CMD']
@ -128,6 +133,7 @@ def check_eval(input):
EXECER.debug_level = DEBUG_LEVEL
EXECER.eval(input)
def check_parse(input):
with mock_xonsh_env(None):
EXECER.debug_level = DEBUG_LEVEL

View file

@ -86,8 +86,7 @@ def superhelper(x, name=''):
def expand_path(s):
"""Takes a string path and expands ~ to home and environment vars."""
global ENV
if ENV.get('EXPAND_ENV_VARS'):
if builtins.__xonsh_env__.get('EXPAND_ENV_VARS'):
s = expandvars(s)
return os.path.expanduser(s)
@ -138,7 +137,9 @@ def regexsearch(s):
def globsearch(s):
csc = builtins.__xonsh_env__.get('CASE_SENSITIVE_COMPLETIONS')
return globpath(s, ignore_case=(not csc), return_empty=True)
glob_sorted = builtins.__xonsh_env__.get('GLOB_SORTED')
return globpath(s, ignore_case=(not csc), return_empty=True,
sort_result=glob_sorted)
def pathsearch(func, s, pymode=False):

View file

@ -105,9 +105,11 @@ def _add_cdpaths(paths, prefix):
"""Completes current prefix using CDPATH"""
env = builtins.__xonsh_env__
csc = env.get('CASE_SENSITIVE_COMPLETIONS')
glob_sorted = env.get('GLOB_SORTED')
for cdp in env.get('CDPATH'):
test_glob = os.path.join(cdp, prefix) + '*'
for s in iglobpath(test_glob, ignore_case=(not csc)):
for s in iglobpath(test_glob, ignore_case=(not csc),
sort_result=glob_sorted):
if os.path.isdir(s):
paths.add(os.path.basename(s))
@ -222,9 +224,10 @@ def _subsequence_match_iter(ref, typed):
def _expand_one(sofar, nextone, csc):
out = set()
glob_sorted = builtins.__xonsh_env__.get('GLOB_SORTED')
for i in sofar:
_glob = os.path.join(_joinpath(i), '*') if i is not None else '*'
for j in iglobpath(_glob):
for j in iglobpath(_glob, sort_result=glob_sorted):
j = os.path.basename(j)
if subsequence_match(j, nextone, csc):
out.add((i or ()) + (j, ))
@ -247,7 +250,9 @@ def complete_path(prefix, line, start, end, ctx, cdpath=True, filtfunc=None):
paths = set()
env = builtins.__xonsh_env__
csc = env.get('CASE_SENSITIVE_COMPLETIONS')
for s in iglobpath(prefix + '*', ignore_case=(not csc)):
glob_sorted = env.get('GLOB_SORTED')
for s in iglobpath(prefix + '*', ignore_case=(not csc),
sort_result=glob_sorted):
paths.add(s)
if len(paths) == 0 and env.get('SUBSEQUENCE_PATH_COMPLETION'):
# this block implements 'subsequence' matching, similar to fish and zsh.
@ -267,7 +272,8 @@ def complete_path(prefix, line, start, end, ctx, cdpath=True, filtfunc=None):
paths |= {_joinpath(i) for i in matches_so_far}
if len(paths) == 0 and env.get('FUZZY_PATH_COMPLETION'):
threshold = env.get('SUGGEST_THRESHOLD')
for s in iglobpath(os.path.dirname(prefix) + '*', ignore_case=(not csc)):
for s in iglobpath(os.path.dirname(prefix) + '*', ignore_case=(not csc),
sort_result=glob_sorted):
if levenshtein(prefix, s, threshold) < threshold:
paths.add(s)
if tilde in prefix:

View file

@ -100,6 +100,7 @@ DEFAULT_ENSURERS = {
dynamic_cwd_tuple_to_str),
'FORCE_POSIX_PATHS': (is_bool, to_bool, bool_to_str),
'FUZZY_PATH_COMPLETION': (is_bool, to_bool, bool_to_str),
'GLOB_SORTED': (is_bool, to_bool, bool_to_str),
'HISTCONTROL': (is_string_set, csv_to_set, set_to_csv),
'IGNOREEOF': (is_bool, to_bool, bool_to_str),
'INTENSIFY_COLORS_ON_WIN':(always_false, intensify_colors_on_win_setter,
@ -222,6 +223,7 @@ DEFAULT_VALUES = {
'EXPAND_ENV_VARS': True,
'FORCE_POSIX_PATHS': False,
'FUZZY_PATH_COMPLETION': True,
'GLOB_SORTED': True,
'HISTCONTROL': set(),
'IGNOREEOF': False,
'INDENT': ' ',
@ -379,6 +381,9 @@ DEFAULT_DOCS = {
"used as a fallback if no other completions succeed but can be used "
"as a way to adjust for typographical errors. If ``True``, then, e.g.,"
" ``xonhs`` will match ``xonsh``."),
'GLOB_SORTED': VarDocs(
"Toggles whether globbing results are manually sorted. If ``False``, "
"the results are returned in arbitrary order."),
'HISTCONTROL': VarDocs(
'A set of strings (comma-separated list in string form) of options '
'that determine what commands are saved to the history list. By '

View file

@ -38,10 +38,8 @@ from subprocess import CalledProcessError
# adding further imports from xonsh modules is discouraged to avoid circular
# dependencies
from xonsh.lazyasd import LazyObject, LazyDict
from xonsh.platform import (
has_prompt_toolkit, scandir,
DEFAULT_ENCODING, ON_LINUX, ON_WINDOWS, PYTHON_VERSION_INFO,
)
from xonsh.platform import (has_prompt_toolkit, scandir, DEFAULT_ENCODING,
ON_LINUX, ON_WINDOWS, PYTHON_VERSION_INFO)
@functools.lru_cache(1)
@ -145,8 +143,8 @@ class EnvPath(collections.MutableSequence):
# in order to be able to retrieve it later, for cases such as
# when a generator expression was passed as an argument
args = list(args)
if not all(isinstance(i, (str, bytes, pathlib.Path)) \
for i in args):
if not all(isinstance(i, (str, bytes, pathlib.Path))
for i in args):
# make TypeError's message as informative as possible
# when given an invalid initialization sequence
raise TypeError(
@ -222,7 +220,8 @@ def _is_not_lparen_and_rparen(lparens, rtok):
def find_next_break(line, mincol=0, lexer=None):
"""Returns the column number of the next logical break in subproc mode.
This function may be useful in finding the maxcol argument of subproc_toks().
This function may be useful in finding the maxcol argument of
subproc_toks().
"""
if mincol >= 1:
line = line[mincol:]
@ -409,13 +408,15 @@ def get_sep():
""" Returns the appropriate filepath separator char depending on OS and
xonsh options set
"""
return (os.altsep if ON_WINDOWS
and builtins.__xonsh_env__.get('FORCE_POSIX_PATHS') else
os.sep)
if ON_WINDOWS and builtins.__xonsh_env__.get('FORCE_POSIX_PATHS'):
return os.altsep
else:
return os.sep
def fallback(cond, backup):
"""Decorator for returning the object if cond is true and a backup if cond is false.
"""Decorator for returning the object if cond is true and a backup if cond
is false.
"""
def dec(obj):
return obj if cond else backup
@ -483,9 +484,10 @@ def _yield_accessible_unix_file_names(path):
def _executables_in_posix(path):
if PYTHON_VERSION_INFO < (3, 5, 0):
for fname in os.listdir(path):
fpath = os.path.join(path, fname)
if (os.path.exists(fpath) and os.access(fpath, os.X_OK) and \
(not os.path.isdir(fpath))):
fpath = os.path.join(path, fname)
if (os.path.exists(fpath) and
os.access(fpath, os.X_OK) and
(not os.path.isdir(fpath))):
yield fname
else:
yield from _yield_accessible_unix_file_names(path)
@ -559,9 +561,10 @@ def suggest_commands(cmd, env, aliases):
for path in filter(os.path.isdir, env.get('PATH')):
for _file in executables_in(path):
if _file not in suggested \
and levenshtein(_file.lower(), cmd, thresh) < thresh:
suggested[_file] = 'Command ({0})'.format(os.path.join(path, _file))
if (_file not in suggested and
levenshtein(_file.lower(), cmd, thresh) < thresh):
suggested[_file] = \
'Command ({0})'.format(os.path.join(path, _file))
suggested = collections.OrderedDict(
sorted(suggested.items(),
@ -656,6 +659,7 @@ def is_writable_file(filepath):
# and ensure that directory is writable instead
return os.access(os.path.dirname(filepath), os.W_OK)
# Modified from Public Domain code, by Magnus Lie Hetland
# from http://hetland.org/coding/python/levenshtein.py
def levenshtein(a, b, max_dist=float('inf')):
@ -755,6 +759,7 @@ def is_int(x):
"""Tests if something is an integer"""
return isinstance(x, int)
def is_int_as_str(x):
"""
Tests if something is an integer
@ -778,10 +783,12 @@ def is_string(x):
"""Tests if something is a string"""
return isinstance(x, str)
def is_slice(x):
"""Tests if something is a slice"""
return isinstance(x, slice)
def is_slice_as_str(x):
"""
Tests if a str is a slice
@ -800,6 +807,7 @@ def is_slice_as_str(x):
return True
return False
def is_callable(x):
"""Tests if something is callable"""
return callable(x)
@ -839,7 +847,9 @@ def str_to_env_path(x):
def env_path_to_str(x):
"""Converts an environment path to a string by joining on the OS separator."""
"""Converts an environment path to a string by joining on the OS
separator.
"""
return os.pathsep.join(x)
@ -855,8 +865,10 @@ def is_logfile_opt(x):
"""
if x is None:
return True
return False if not isinstance(x, str) else \
(is_writable_file(x) or x == '')
if not isinstance(x, str):
return False
else:
return (is_writable_file(x) or x == '')
def to_logfile_opt(x):
@ -903,7 +915,9 @@ def to_bool(x):
def bool_to_str(x):
"""Converts a bool to an empty string if False and the string '1' if True."""
"""Converts a bool to an empty string if False and the string '1' if
True.
"""
return '1' if x else ''
@ -1148,12 +1162,14 @@ HISTORY_UNITS = LazyObject(lambda: {
}, globals(), 'HISTORY_UNITS')
"""Maps lowercase unit names to canonical name and conversion utilities."""
def is_history_tuple(x):
"""Tests if something is a proper history value, units tuple."""
if isinstance(x, abc.Sequence) and len(x) == 2 and \
isinstance(x[0], (int, float)) and \
x[1].lower() in CANON_HISTORY_UNITS:
return True
if (isinstance(x, abc.Sequence) and
len(x) == 2 and
isinstance(x[0], (int, float)) and
x[1].lower() in CANON_HISTORY_UNITS):
return True
return False
@ -1161,8 +1177,10 @@ def is_dynamic_cwd_width(x):
""" Determine if the input is a valid input for the DYNAMIC_CWD_WIDTH
environement variable.
"""
return isinstance(x, tuple) and len(x) == 2 and isinstance(x[0], float) and \
(x[1] in set('c%'))
return (isinstance(x, tuple) and
len(x) == 2 and
isinstance(x[0], float) and
x[1] in set('c%'))
def to_dynamic_cwd_tuple(x):
@ -1191,6 +1209,7 @@ RE_HISTORY_TUPLE = LazyObject(
lambda: re.compile('([-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?)\s*([A-Za-z]*)'),
globals(), 'RE_HISTORY_TUPLE')
def to_history_tuple(x):
"""Converts to a canonincal history tuple."""
if not isinstance(x, (abc.Sequence, float, int)):
@ -1321,7 +1340,7 @@ _STRINGS = (_RE_STRING_TRIPLE_DOUBLE,
_RE_STRING_DOUBLE,
_RE_STRING_SINGLE)
RE_BEGIN_STRING = LazyObject(
lambda: re.compile("(" + _RE_STRING_START + \
lambda: re.compile("(" + _RE_STRING_START +
'(' + "|".join(_STRINGS) + '))'),
globals(), 'RE_BEGIN_STRING')
"""Regular expression matching the start of a string, including quotes and
@ -1417,6 +1436,7 @@ def _is_in_env(name):
ENV = builtins.__xonsh_env__
return name in ENV._d or name in ENV._defaults
def _get_env_string(name):
ENV = builtins.__xonsh_env__
value = ENV.get(name)
@ -1529,6 +1549,7 @@ def expandvars(path):
# File handling tools
#
def backup_file(fname):
"""Moves an existing file to a new name that has the current time right
before the extension.
@ -1593,26 +1614,41 @@ def expand_case_matching(s):
return ''.join(t)
def globpath(s, ignore_case=False, return_empty=False):
def globpath(s, ignore_case=False, return_empty=False, sort_result=None):
"""Simple wrapper around glob that also expands home and env vars."""
o, s = _iglobpath(s, ignore_case=ignore_case)
o, s = _iglobpath(s, ignore_case=ignore_case, sort_result=sort_result)
o = list(o)
no_match = [] if return_empty else [s]
return o if len(o) != 0 else no_match
def _iglobpath(s, ignore_case=False):
def _iglobpath(s, ignore_case=False, sort_result=None):
s = builtins.__xonsh_expand_path__(s)
if sort_result is None:
sort_result = builtins.__xonsh_env__.get('GLOB_SORTED')
if ignore_case:
s = expand_case_matching(s)
if sys.version_info > (3, 5):
if '**' in s and '**/*' not in s:
s = s.replace('**', '**/*')
# `recursive` is only a 3.5+ kwarg.
return glob.iglob(s, recursive=True), s
if sort_result:
paths = glob.glob(s, recursive=True)
paths.sort()
paths = iter(paths)
else:
paths = glob.iglob(s, recursive=True)
return paths, s
else:
return glob.iglob(s), s
if sort_result:
paths = glob.glob(s)
paths.sort()
paths = iter(paths)
else:
paths = glob.iglob(s)
return paths, s
def iglobpath(s, ignore_case=False):
def iglobpath(s, ignore_case=False, sort_result=None):
"""Simple wrapper around iglob that also expands home and env vars."""
return _iglobpath(s, ignore_case)[0]
return _iglobpath(s, ignore_case=ignore_case, sort_result=sort_result)[0]