mirror of
https://github.com/xonsh/xonsh.git
synced 2025-03-04 16:34:47 +01:00
Revert "Move on_transform_command into parser."
This reverts commit 5b01f80829
.
This commit is contained in:
parent
5b01f80829
commit
e5e2b08ed9
8 changed files with 51 additions and 61 deletions
|
@ -10,7 +10,7 @@ import pytest
|
|||
from xonsh.ast import AST, With, Pass
|
||||
from xonsh.parser import Parser
|
||||
|
||||
from tools import VER_FULL, skip_if_py34, nodes_equal, DummyEnv
|
||||
from tools import VER_FULL, skip_if_py34, nodes_equal
|
||||
|
||||
# a lot of col_offset data changed from Py v3.5.0 -> v3.5.1
|
||||
INC_ATTRS = (3, 5, 1) <= VER_FULL
|
||||
|
@ -42,8 +42,6 @@ def check_stmts(inp, run=True, mode='exec'):
|
|||
|
||||
def check_xonsh_ast(xenv, inp, run=True, mode='eval', debug_level=0,
|
||||
return_obs=False):
|
||||
if isinstance(xenv, dict):
|
||||
xenv = DummyEnv(xenv)
|
||||
__tracebackhide__ = True
|
||||
builtins.__xonsh_env__ = xenv
|
||||
obs = PARSER.parse(inp, debug_level=debug_level)
|
||||
|
|
|
@ -55,7 +55,7 @@ _foobar = 3
|
|||
ctx = xontrib_context('spameggs')
|
||||
assert ctx == {'spam': 1, '_foobar': 3}
|
||||
|
||||
def test_xshxontrib(tmpmod, xonsh_builtins):
|
||||
def test_xshxontrib(tmpmod):
|
||||
"""
|
||||
Test that .xsh xontribs are loadable
|
||||
"""
|
||||
|
|
|
@ -84,13 +84,9 @@ class DummyHistory:
|
|||
|
||||
|
||||
class DummyEnv(MutableMapping):
|
||||
DEFAULT = {
|
||||
'XONSH_DEBUG': 1,
|
||||
}
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
self._d = self.DEFAULT.copy()
|
||||
self._d.update(dict(*args, **kwargs))
|
||||
self._d = dict(*args, **kwargs)
|
||||
|
||||
def detype(self):
|
||||
return {k: str(v) for k, v in self._d.items()}
|
||||
|
@ -110,9 +106,6 @@ class DummyEnv(MutableMapping):
|
|||
def __iter__(self):
|
||||
yield from self._d
|
||||
|
||||
def is_manually_set(self, key):
|
||||
return False
|
||||
|
||||
#
|
||||
# Execer tools
|
||||
#
|
||||
|
|
|
@ -15,6 +15,7 @@ from xonsh.codecache import (should_use_cache, code_cache_name,
|
|||
from xonsh.completer import Completer
|
||||
from xonsh.prompt.base import multiline_prompt, PromptFormatter
|
||||
from xonsh.events import events
|
||||
from xonsh.shell import transform_command
|
||||
|
||||
if ON_WINDOWS:
|
||||
import ctypes
|
||||
|
@ -307,8 +308,6 @@ class BaseShell(object):
|
|||
src, code = self.push(line)
|
||||
if code is None:
|
||||
return
|
||||
|
||||
events.on_precommand.fire(src)
|
||||
env = builtins.__xonsh_env__
|
||||
hist = builtins.__xonsh_history__ # pylint: disable=no-member
|
||||
ts1 = None
|
||||
|
@ -383,6 +382,7 @@ class BaseShell(object):
|
|||
if self.need_more_lines:
|
||||
return None, None
|
||||
src = ''.join(self.buffer)
|
||||
src = transform_command(src)
|
||||
return self.compile(src)
|
||||
|
||||
def compile(self, src):
|
||||
|
|
|
@ -1,10 +1,7 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
"""Implements the base xonsh parser."""
|
||||
import builtins
|
||||
import difflib
|
||||
import os
|
||||
import re
|
||||
import sys
|
||||
import time
|
||||
import textwrap
|
||||
from threading import Thread
|
||||
|
@ -22,8 +19,6 @@ from xonsh.platform import PYTHON_VERSION_INFO
|
|||
from xonsh.tokenize import SearchPath, StringPrefix
|
||||
from xonsh.lazyasd import LazyObject
|
||||
from xonsh.parsers.context_check import check_contexts
|
||||
from xonsh.events import events
|
||||
from xonsh.tools import print_exception
|
||||
|
||||
RE_SEARCHPATH = LazyObject(lambda: re.compile(SearchPath), globals(),
|
||||
'RE_SEARCHPATH')
|
||||
|
@ -31,46 +26,6 @@ RE_STRINGPREFIX = LazyObject(lambda: re.compile(StringPrefix), globals(),
|
|||
'RE_STRINGPREFIX')
|
||||
|
||||
|
||||
events.doc('on_transform_command', """
|
||||
on_command_transform(cmd: str) -> str
|
||||
|
||||
Fired to request xontribs to transform a command line. Return the transformed
|
||||
command, or the same command if no transformaiton occurs.
|
||||
|
||||
This may be fired multiple times per command, so design any handlers for this
|
||||
carefully.
|
||||
""")
|
||||
|
||||
|
||||
def transform_command(src, show_diff=True):
|
||||
"""Returns the results of firing the precommand handles."""
|
||||
i = 0
|
||||
limit = sys.getrecursionlimit()
|
||||
lst = ''
|
||||
raw = src
|
||||
while src != lst:
|
||||
lst = src
|
||||
srcs = events.on_transform_command.fire(src)
|
||||
for s in srcs:
|
||||
if s != lst:
|
||||
src = s
|
||||
break
|
||||
i += 1
|
||||
if i == limit:
|
||||
print_exception('Modifcations to source input took more than '
|
||||
'the recursion limit number of interations to '
|
||||
'converge.')
|
||||
debug_level = builtins.__xonsh_env__.get('XONSH_DEBUG')
|
||||
if show_diff and debug_level > 1 and src != raw:
|
||||
sys.stderr.writelines(difflib.unified_diff(
|
||||
raw.splitlines(keepends=True),
|
||||
src.splitlines(keepends=True),
|
||||
fromfile='before precommand event',
|
||||
tofile='after precommand event',
|
||||
))
|
||||
return src
|
||||
|
||||
|
||||
class Location(object):
|
||||
"""Location in a file."""
|
||||
|
||||
|
@ -372,7 +327,6 @@ class BaseParser(object):
|
|||
-------
|
||||
tree : AST
|
||||
"""
|
||||
s = transform_command(s)
|
||||
self.reset()
|
||||
self.xonsh_code = s
|
||||
self.lexer.fname = filename
|
||||
|
|
|
@ -9,6 +9,7 @@ from prompt_toolkit.keys import Keys
|
|||
|
||||
from xonsh.aliases import xonsh_exit
|
||||
from xonsh.tools import ON_WINDOWS, check_for_partial_string
|
||||
from xonsh.shell import transform_command
|
||||
|
||||
env = builtins.__xonsh_env__
|
||||
DEDENT_TOKENS = frozenset(['raise', 'return', 'pass', 'break', 'continue'])
|
||||
|
@ -71,6 +72,7 @@ def _is_blank(l):
|
|||
def can_compile(src):
|
||||
"""Returns whether the code can be compiled, i.e. it is valid xonsh."""
|
||||
src = src if src.endswith('\n') else src + '\n'
|
||||
src = transform_command(src, show_diff=False)
|
||||
src = src.lstrip()
|
||||
try:
|
||||
builtins.__xonsh_execer__.compile(src, mode='single', glbs=None,
|
||||
|
|
|
@ -21,6 +21,7 @@ from xonsh.ptk.history import PromptToolkitHistory
|
|||
from xonsh.ptk.key_bindings import load_xonsh_bindings
|
||||
from xonsh.ptk.shortcuts import Prompter
|
||||
from xonsh.events import events
|
||||
from xonsh.shell import transform_command
|
||||
|
||||
|
||||
events.transmogrify('on_ptk_create', 'LoadEvent')
|
||||
|
@ -110,6 +111,7 @@ class PromptToolkitShell(BaseShell):
|
|||
if self.need_more_lines:
|
||||
return None, code
|
||||
src = ''.join(self.buffer)
|
||||
src = transform_command(src)
|
||||
try:
|
||||
code = self.execer.compile(src,
|
||||
mode='single',
|
||||
|
|
|
@ -1,8 +1,10 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
"""The xonsh shell"""
|
||||
import os
|
||||
import sys
|
||||
import random
|
||||
import time
|
||||
import difflib
|
||||
import builtins
|
||||
import warnings
|
||||
|
||||
|
@ -11,11 +13,21 @@ from xonsh.environ import xonshrc_context
|
|||
from xonsh.execer import Execer
|
||||
from xonsh.platform import (best_shell_type, has_prompt_toolkit,
|
||||
ptk_version_is_supported)
|
||||
from xonsh.tools import XonshError, to_bool_or_int
|
||||
from xonsh.tools import XonshError, to_bool_or_int, print_exception
|
||||
from xonsh.events import events
|
||||
import xonsh.history.main as xhm
|
||||
|
||||
|
||||
events.doc('on_transform_command', """
|
||||
on_command_transform(cmd: str) -> str
|
||||
|
||||
Fired to request xontribs to transform a command line. Return the transformed
|
||||
command, or the same command if no transformaiton occurs.
|
||||
|
||||
This may be fired multiple times per command, so design any handlers for this
|
||||
carefully.
|
||||
""")
|
||||
|
||||
events.doc('on_precommand', """
|
||||
on_precommand(cmd: str) -> None
|
||||
|
||||
|
@ -29,6 +41,35 @@ Fires just after a command is executed.
|
|||
""")
|
||||
|
||||
|
||||
def transform_command(src, show_diff=True):
|
||||
"""Returns the results of firing the precommand handles."""
|
||||
i = 0
|
||||
limit = sys.getrecursionlimit()
|
||||
lst = ''
|
||||
raw = src
|
||||
while src != lst:
|
||||
lst = src
|
||||
srcs = events.on_transform_command.fire(src)
|
||||
for s in srcs:
|
||||
if s != lst:
|
||||
src = s
|
||||
break
|
||||
i += 1
|
||||
if i == limit:
|
||||
print_exception('Modifcations to source input took more than '
|
||||
'the recursion limit number of interations to '
|
||||
'converge.')
|
||||
debug_level = builtins.__xonsh_env__.get('XONSH_DEBUG')
|
||||
if show_diff and debug_level > 1 and src != raw:
|
||||
sys.stderr.writelines(difflib.unified_diff(
|
||||
raw.splitlines(keepends=True),
|
||||
src.splitlines(keepends=True),
|
||||
fromfile='before precommand event',
|
||||
tofile='after precommand event',
|
||||
))
|
||||
return src
|
||||
|
||||
|
||||
class Shell(object):
|
||||
"""Main xonsh shell.
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue