Merge branch 'master' into first-class-paths

This commit is contained in:
Gordon Ball 2016-10-19 15:05:42 +02:00
commit 7d79644612
9 changed files with 93 additions and 47 deletions

View file

@ -1,14 +1,17 @@
doc-warnings: yes
test-warnings: yes
strictness: veryhigh
max-line-length: 100
max-line-length: 90
autodetect: yes
ignore-paths:
- docs/conf.py
- xonsh/ply
python-targets:
- 3
pylint:
disable:
- syntax-error # have to do this since landscape doesn't support py3k
- super-on-old-class
- old-style-class
- global-variable-not-assigned
@ -28,5 +31,5 @@ pylint:
mccabe:
disable:
- MC0000 # again, not Py3k compatabile
- MC0000 # not Py3k compatabile
- MC0001 # silly cyclomatic complexity

View file

@ -953,40 +953,36 @@ The following example shows the form of these functions:
['aa', 'aaa', 'aab', 'aabb']
Path Objects
============
Path Output
-----------
Shells spend a lot of their time manipulating paths, so you may find it useful
to handle paths as objects instead of string. A shorthand is provided to
instantiate ``pathlib.Path`` objects:
.. code-block:: xonshcon
>>> p'/foo/bar'
Path('/foo/bar')
>>> p'/foo/bar'.stem
'bar'
This also applies to using backticks for path searching. The ``p`` modifier to
a search causes it to return a list of path objects instead of strings (but only
in python mode). This can be combined with other modifiers.
Using the ``p`` modifier with either regex or glob backticks changes the
return type from a list of strings to a list of :class:`pathlib.Path` objects:
.. code-block:: xonshcon
>>> p`.*`
[Path('foo.py'), Path('bar.py')]
>>> pg`*.py`
[Path('foo.py'), Path('bar.py')]
>>> for f in pg`**`:
... print(f.absolute())
[Path('foo'), Path('bar')]
>>> [x for x in pg`**` if x.is_symlink()]
[Path('a_link')]
Path objects can be used without further conversion in subprocess mode:
Path Literals
-------------
Path objects can be instantiated directly using *p-string* syntax. Path objects
can be converted back to plain strings with `str()`, and this conversion is
handled implicitly in subprocess mode.
.. code-block:: xonshcon
>>> x = p"some/path"
>>> echo @(x)
some/path
>>> mypath = p'/foo/bar'
>>> mypath
Path('/foo/bar')
>>> mypath.stem
'bar'
>>> echo @(mypath)
/foo/bar
Help & Superhelp with ``?`` & ``??``

13
news/history-help.rst Normal file
View file

@ -0,0 +1,13 @@
**Added:** None
**Changed:**
* ``history`` help messages to reflect subcommand usage
**Deprecated:** None
**Removed:** None
**Fixed:** None
**Security:** None

13
news/landscape.rst Normal file
View file

@ -0,0 +1,13 @@
**Added:** None
**Changed:**
* landscape.io linting now ignores ply directory
**Deprecated:** None
**Removed:** None
**Fixed:** None
**Security:** None

14
news/path-backticks.rst Normal file
View file

@ -0,0 +1,14 @@
**Added:**
* Backticks for regex or glob searches now support an additional modifier
``p``, which causes them to return Path objects instead of strings.
**Changed:** None
**Deprecated:** None
**Removed:** None
**Fixed:** None
**Security:** None

View file

@ -1619,14 +1619,14 @@ def test_ls_glob():
def test_gbacktick():
check_xonsh_ast({}, 'print(g`.*`)', False)
def test_pbacktick():
def test_pbacktrick():
check_xonsh_ast({}, 'print(p`.*`)', False)
def test_pgbacktick():
check_xonsh_ast({}, 'print(pg`.*`)', False)
def test_prbacktick():
check_xonsh_ast({}, 'print(rp`.*`)', False)
check_xonsh_ast({}, 'print(pr`.*`)', False)
def test_ls_glob_octothorpe():
check_xonsh_ast({}, '$(ls g`#[Ff]+i*LE` -l)', False)

View file

@ -13,6 +13,7 @@ import types
import shlex
import signal
import atexit
import pathlib
import inspect
import pathlib
import builtins
@ -165,7 +166,7 @@ def pathsearch(func, s, pymode=False, pathobj=False):
error = "%r is not a known path search function"
raise XonshError(error % func)
o = func(s)
if pathobj and pymode: #this doesn't make sense in subprocess mode
if pathobj and pymode:
o = list(map(path_literal, o))
no_match = [] if pymode else [s]
return o if len(o) != 0 else no_match

View file

@ -161,7 +161,8 @@ class CommandsCache(cabc.Mapping):
cached = next((cmd for cmd in possibilities if cmd in self._cmds_cache),
None)
if cached:
return self._cmds_cache[cached][0]
(path, is_alias) = self._cmds_cache[cached]
return path if not is_alias else None
elif os.path.isfile(name) and name != pathbasename(name):
return name

View file

@ -336,15 +336,16 @@ def _bash_hist_parser(location=None, **kwargs):
def _hist_create_parser():
"""Create a parser for the "history" command."""
p = argparse.ArgumentParser(prog='history',
description='Tools for dealing with history')
subp = p.add_subparsers(title='action', dest='action')
description="try 'history <command> --help' "
'for more info')
subp = p.add_subparsers(title='commands', dest='action')
# session action
show = subp.add_parser('show', prefix_chars='-+',
help='displays session history, default action')
help='display history of a session, default command')
show.add_argument('-r', dest='reverse', default=False,
action='store_true', help='reverses the direction')
show.add_argument('-n', dest='numerate', default=False, action='store_true',
help='numerate each command')
show.add_argument('-n', dest='numerate', default=False,
action='store_true', help='numerate each command')
show.add_argument('-t', dest='timestamp', default=False,
action='store_true', help='show command timestamps')
show.add_argument('-T', dest='end_time', default=None,
@ -352,26 +353,30 @@ def _hist_create_parser():
show.add_argument('+T', dest='start_time', default=None,
help='show only commands after timestamp')
show.add_argument('-f', dest='datetime_format', default=None,
help='the datetime format to be used for filtering and printing')
show.add_argument('session', nargs='?', choices=_HIST_SESSIONS.keys(), default='session',
help='Choose a history session, defaults to current session')
show.add_argument('slices', nargs='*', default=None,
help='display history entries or range of entries')
help='the datetime format to be used for'
'filtering and printing')
show.add_argument('session', nargs='?', choices=_HIST_SESSIONS.keys(),
default='session',
metavar='session',
help='{} (default: current session, all is an alias for xonsh)'
''.format(', '.join(map(repr, _HIST_SESSIONS.keys()))))
show.add_argument('slices', nargs='*', default=None, metavar='slice',
help='integer or slice notation')
# 'id' subcommand
subp.add_parser('id', help='displays the current session id')
subp.add_parser('id', help='display the current session id')
# 'file' subcommand
subp.add_parser('file', help='displays the current history filename')
subp.add_parser('file', help='display the current history filename')
# 'info' subcommand
info = subp.add_parser('info', help=('displays information about the '
info = subp.add_parser('info', help=('display information about the '
'current history'))
info.add_argument('--json', dest='json', default=False,
action='store_true', help='print in JSON format')
# diff
diff = subp.add_parser('diff', help='diffs two xonsh history files')
diff = subp.add_parser('diff', help='diff two xonsh history files')
_dh_create_parser(p=diff)
# replay, dynamically
from xonsh import replay
rp = subp.add_parser('replay', help='replays a xonsh history file')
rp = subp.add_parser('replay', help='replay a xonsh history file')
replay._rp_create_parser(p=rp)
_HIST_MAIN_ACTIONS['replay'] = replay._rp_main_action
# gc