plane landing

This commit is contained in:
Anthony Scopatz 2015-02-17 23:51:33 -06:00
parent 743357af19
commit 4d7f870e46
6 changed files with 130 additions and 11 deletions

View file

@ -73,13 +73,13 @@ class CtxAwareTransformer(NodeTransformer):
del self.lines, self.contexts
return node
def ctxupdate(iterable):
def ctxupdate(self, iterable):
self.contexts[-1].update(iterable)
def ctxadd(value):
def ctxadd(self, value):
self.contexts[-1].add(value)
def ctxremove(value):
def ctxremove(self, value):
for ctx in self.contexts[::-1]:
if value in ctx:
ctx.remove(value)

69
xonsh/built_ins.py Normal file
View file

@ -0,0 +1,69 @@
"""The xonsh built-ins. Note that this module is named 'built_ins' so as
not to be confused with the special Python builtins module.
"""
import os
import builtins
import subprocess
from contextlib import contextmanager
from collections import MutableMapping
BUILTINS_LOADED = False
class Env(MutableMapping):
"""A xonsh environment, whose variables have limited typing
(unlike BASH). Most variables are, by default, strings (like BASH).
However, the following rules also apply based on variable-name:
* PATH: any variable whose name ends in PATH is a list of strings.
An Env instance may be converted to an untyped version suitable for
use in a subprocess.
"""
def __init__(self, *args, **kwargs):
"""If no initial environment is given, os.environ is used."""
if len(args) == 0 and len(kwargs) == 0:
args = (os.environ,)
super(Env, self).__init__(*args, **kwargs)
def detype():
pass
def load_builtins():
"""Loads the xonsh builtins into the Python builtins. Sets the
BUILTINS_LOADED variable to True.
"""
global BUILTINS_LOADED
builtins.__xonsh_env__ = {}
builtins.__xonsh_help__ = lambda x: x
builtins.__xonsh_superhelp__ = lambda x: x
builtins.__xonsh_regexpath__ = lambda x: []
builtins.__xonsh_subproc__ = subprocess
BUILTINS_LOADED = True
def unload_builtins():
"""Removes the xonsh builtins from the Python builins, if the
BUILTINS_LOADED is True, sets BUILTINS_LOADED to False, and returns.
"""
global BUILTINS_LOADED
if not BUILTINS_LOADED:
return
del (builtins.__xonsh_env__,
builtins.__xonsh_help__,
builtins.__xonsh_superhelp__,
builtins.__xonsh_regexpath__,
builtins.__xonsh_subproc__,
)
BUILTINS_LOADED = False
@contextmanager
def xonsh_builtins():
"""A context manager for using the xonsh builtins only in a limited
scope. Likely useful in testing.
"""
load_builtins()
yield
unload_builtins()

View file

@ -9,6 +9,7 @@ from collections import Iterable, Sequence, Mapping
from xonsh import ast
from xonsh.parser import Parser
from xonsh.tools import subproc_line
from xonsh.built_ins import load_builtins, unload_builtins
class Execer(object):
"""Executes xonsh code in a context."""
@ -29,6 +30,10 @@ class Execer(object):
self.filename = filename
self.debug_level = debug_level
self.ctxtransformer = ast.CtxAwareTransformer(self.parser)
load_builtins()
def __del__(self):
unload_builtins()
def parse(self, input, ctx):
"""Parses xonsh code in a context-aware fashion. For context-free

View file

@ -1,20 +1,23 @@
"""The main xonsh displaye."""
"""The main xonsh display."""
import urwid
from urwid.vterm import Terminal
from xonsh.shell_view import ShellView
from xonsh.shell import Shell
#from xonsh.shell_view import ShellView
class MainDisplay(object):
def __init__(self):
self.shell = shell = ShellView()
self.shell = shell = Shell()
self.shellview = shellview = Terminal(shell.cmdloop)
self.view = urwid.LineBox(
urwid.Pile([
('weight', 70, shell),
('weight', 70, shellview),
('fixed', 1, urwid.Filler(urwid.Edit('focus test edit: '))),
]),
)
urwid.connect_signal(shell, 'title', self.set_title)
urwid.connect_signal(shell, 'closed', self.quit)
urwid.connect_signal(shellview, 'title', self.set_title)
urwid.connect_signal(shellview, 'closed', self.quit)
def set_title(self, widget, title):
@ -32,7 +35,7 @@ class MainDisplay(object):
handle_mouse=False,
unhandled_input=self.handle_key)
loop.screen.set_terminal_properties(256)
self.loop = self.shell.main_loop = loop
self.loop = self.shellview.main_loop = loop
while True:
try:
self.loop.run()

29
xonsh/shell.py Normal file
View file

@ -0,0 +1,29 @@
"""The xonsh shell"""
import traceback
from cmd import Cmd
from xonsh.execer import Execer
class Shell(Cmd):
"""The xonsh shell."""
prompt = 'wakka? '
def __init__(self, completekey='tab', stdin=None, stdout=None, ctx=None):
super(Shell, self).__init__(completekey='tab', stdin=stdin,
stdout=stdout)
self.execer = Execer()
self.ctx = ctx or {}
def parseline(self, line):
"""Overridden to no-op."""
return None, None, line + '\n'
def default(self, line):
"""Implements parser."""
try:
self.execer.exec(line, glbs=None, locs=self.ctx)
except KeyboardInteruppt:
raise
except:
traceback.print_exc()

View file

@ -9,6 +9,7 @@ import struct
import signal
import atexit
import traceback
import code
try:
import pty
@ -18,7 +19,7 @@ except ImportError:
pass # windows
from urwid import Widget
from urwid.vterm import TermModes, TermCanvas
from urwid.vterm import TermModes, TermCanvas, Terminal
from urwid.display_common import AttrSpec, RealTerminal
class ShellView(Widget):
@ -320,3 +321,15 @@ class ShellView(Widget):
os.write(self.master, key)
class ShellView(Terminal):
#pass
def __init__(self):
cons = code.InteractiveConsole()
super(ShellView, self).__init__(cons.interact)
#def spawn(self):
# pass
#def terminate(self):
# pass