This commit is contained in:
Vitor Santos Costa
2019-02-27 04:23:21 +00:00
parent 5d7c6378e7
commit 2d65d0463c
30 changed files with 527 additions and 424 deletions

View File

@@ -761,31 +761,33 @@ bool python_release_GIL(term_t curBlock) {
}
install_t install_pypreds(void) {
PL_register_foreign("python_builtin_eval", 3, python_builtin_eval, 0);
PL_register_foreign("python_builtin", 1, python_builtin, 0);
PL_register_foreign("python_import", 2, python_import, 0);
PL_register_foreign("python_to_rhs", 2, python_to_rhs, 0);
PL_register_foreign("python_len", 2, python_len, 0);
PL_register_foreign("python_is", 2, python_is, 0);
PL_register_foreign("python_dir", 2, python_dir, 0);
PL_register_foreign("python_apply", 4, python_apply, 0);
PL_register_foreign("python_index", 3, python_index, 0);
PL_register_foreign("python_field", 3, python_field, 0);
PL_register_foreign("python_assign", 2, assign_python, 0);
PL_register_foreign("python_represents", 2, python_represent, 0);
PL_register_foreign("python_export", 2, python_export, 0);
PL_register_foreign("python_function", 1, python_function, 0);
PL_register_foreign("python_slice", 4, python_slice, 0);
PL_register_foreign("python_run_file", 1, python_run_file, 0);
PL_register_foreign("python_proc", 1, python_proc, 0);
PL_register_foreign("python_run_command", 1, python_run_command, 0);
PL_register_foreign("python_run_script", 2, python_run_script, 0);
PL_register_foreign("python_main_module", 1, python_main_module, 0);
PL_register_foreign("python_import", 2, python_import, 0);
PL_register_foreign("python_access", 3, python_access, 0);
PL_register_foreign("python_threaded", 0, p_python_threaded, 0);
PL_register_foreign("python_clear_errors", 0, python_clear_errors, 0);
PL_register_foreign("python_string_to", 1, python_string_to, 0);
PL_register_foreign_in_module("python", "python_builtin_eval", 3, python_builtin_eval, 0);
PL_register_foreign_in_module("python", "python_builtin", 1, python_builtin, 0);
PL_register_foreign_in_module("python", "python_import", 2, python_import, 0);
PL_register_foreign_in_module("python", "python_to_rhs", 2, python_to_rhs, 0);
PL_register_foreign_in_module("python", "python_len", 2, python_len, 0);
PL_register_foreign_in_module("python", "python_is", 2, python_is, 0);
PL_register_foreign_in_module("python", "python_dir", 2, python_dir, 0);
PL_register_foreign_in_module("python", "python_apply", 4, python_apply, 0);
PL_register_foreign_in_module("python", "python_index", 3, python_index, 0);
PL_register_foreign_in_module("python", "python_field", 3, python_field, 0);
PL_register_foreign_in_module("python", "python_assign", 2, assign_python, 0);
PL_register_foreign_in_module("python", "python_represents", 2, python_represent, 0);
PL_register_foreign_in_module("python", "python_export", 2, python_export, 0);
PL_register_foreign_in_module("python", "python_function", 1, python_function, 0);
PL_register_foreign_in_module("python", "python_slice", 4, python_slice, 0);
PL_register_foreign_in_module("python", "python_run_file", 1, python_run_file, 0);
PL_register_foreign_in_module("python", "python_proc", 1, python_proc, 0);
PL_register_foreign_in_module("python", "python_run_command", 1, python_run_command, 0);
PL_register_foreign_in_module("python", "python_run_script", 2, python_run_script, 0);
PL_register_foreign_in_module("python", "python_main_module", 1, python_main_module, 0);
PL_register_foreign_in_module("python", "python_import", 2, python_import, 0);
PL_register_foreign_in_module("python", "python_access", 3, python_access, 0);
PL_register_foreign_in_module("python", "python_threaded", 0, p_python_threaded, 0);
PL_register_foreign_in_module("python", "python_clear_errors", 0, python_clear_errors, 0);
PL_register_foreign_in_module("python", "python_string_to", 1, python_string_to, 0);
init_python_vfs();
}

View File

@@ -43,13 +43,18 @@
op(50, yf, []),
op(50, yf, '()'),
op(100, xfy, '.'),
op(100, fy, '.')
op(100, fy, '.'),
(:=)/2,
(:=)/1,
% (<-)/1,
% (<-)/2,
'()'/1, '{}'/1, dot_qualified_goal/1, import_arg/1
]).
/** @defgroup Py4YAP A C-based Prolog interface to python.
@ingroup python
b
@{
@author Vitor Santos Costa
@@ -96,7 +101,7 @@ similar as possible.
Python interface
Data types are
Data types arebb
Python Prolog
string atoms
@@ -115,34 +120,38 @@ Data types are
:- use_module(library(charsio)).
:- dynamic python_mref_cache/2, python_obj_cache/2.
:- multifile user:(:=)/2,
user:(:=)/1,
% user:(<-)/1,
% user:(<-)/2,
user:'()'/1, user:'{}'/1, user:dot_qualified_goal/1, user:import_arg/1.
:- op(100,fy,'$'),
op(950,fy,:=),
op(950,yfx,:=),
% op(950,fx,<-),
% op(950,yfx,<-),
op(50, yf, []),
op(50, yf, '()'),
op(100, xfy, '.'),
op(100, fy, '.').
:- multifile (<-)/1, (<-)/2,
'()'/1, '{}'/1,
dot_qualified_goal/1,
import_arg/1.
import( F ) :- catch( python:python_import(F), _, fail ).
user:dot_qualified_goal(Fs) :- catch( python:python_proc(Fs), _, fail ).
dot_qualified_goal(Fs) :- catch( python:python_proc(Fs), _, fail ).
user:F() :-
catch( python:python_proc(F() ), _, fail ).
'()'(F) :-
catch( python_proc(()(F) ), _, fail ).
user(P1,P2) :- !,
:= (P1,P2) :- !,
:= P1,
:= P2.
:= F :- catch( python:python_proc(F), _, fail ).
:= (P1,P2) :- !,
:= P1,
:= P2.
user:(:= F) :- catch( python:python_proc(F), _, fail ).
user:( V := F ) :-
V := F :-
python:python_assign(F, V).
/*
@@ -153,15 +162,15 @@ user:(V <- F) :-
V := F.
*/
python:python_import(Module) :-
python:python_import(Module, _).
python_import(Module) :-
python_import(Module, _).
python(Exp, Out) :-
Out := Exp.
python_command(Cmd) :-
python:python_run_command(Cmd).
python_run_command(Cmd).
start_python :-
python:python_import('inspect', _),

View File

@@ -2,27 +2,27 @@
%% @file yapi.yap
%% @brief support yap shell
%%
%:- start_low_level_trace.
%% :- module(yapi, [
%% python_ouput/0,
%% show_answer/2,
%% show_answer/3,
%% yap_query/4,
%% python_query/2,
%% python_query/3,
%% python_import/1,
%% yapi_query/2
%% ]).
:- yap_flag(verbose, silent).
:- module(yapi, [
python_ouput/0,
show_answer/2,
show_answer/3,
yap_query/4,
python_query/2,
python_query/3,
python_import/1,
yapi_query/2
]).
:- use_module(library(python)).
%:- yap_flag(verbose, silent).
:- reexport(library(python)).
:- use_module( library(lists) ).
:- use_module( library(maplist) ).
:- use_module( library(rbtrees) ).
:- use_module( library(terms) ).
:- python_import(yap4py.yapi).
:- python_import(json).
@@ -46,9 +46,6 @@ yapi_query( VarNames, Self ) :-
set_preds :-
fail,
current_predicate(P, Q),
functor(Q,P,A),
current_predicate(P, Q),
functor(Q,P,A),
atom_string(P,S),
@@ -69,36 +66,55 @@ fail,
set_preds.
argi(N,I,I1) :-
atomic_concat(`A`,I,N),
atomic_concat('A',I,N),
I1 is I+1.
python_query( Caller, String ) :-
python_query( Caller, String ) :-
python_query( Caller, String, _Bindings).
python_query( Caller, String, Bindings ) :-
atomic_to_term( String, Goal, VarNames ),
query_to_answer( Goal, _, Status, VarNames, Bindings),
query_to_answer( Goal, VarNames, Status, Bindings),
Caller.port := Status,
output(Caller, Bindings).
output( _, Bindings ) :-
write_query_answer( Bindings ),
fail.
output( Caller, Bindings ) :-
answer := {},
Answer := {},
% start_low_level_trace,
foldl(ground_dict(answer), Bindings, [], Ts),
term_variables( Ts, Hidden),
foldl(bv, Hidden , 0, _),
maplist(into_dict(answer),Ts),
Caller.answer := json.dumps(answer),
S := Caller.answer,
format(user_error, '~nor ~s~n~n',S),
fail.
maplist(into_dict(Answer),Ts),
Caller.answer := Answer,
fail.
output( _, Bindings ) :-
write_query_answer( Bindings ),
fail.
output(_Caller, _Bindings).
bv(V,I,I1) :-
atomic_concat(['__',I],V),
I1 is I+1.
into_dict(D,V0=T) :-
python_represents(D[V0], T).
atom(T),
!,
D[V0] := T.
into_dict(D,V0=T) :-
integer(T),
writeln((D[V0]:=T)),
!,
D[V0] := T,
:= print(D).
into_dict(D,V0=T) :-
string(T),
!,
D[V0] := T.
into_dict(D,V0=T) :-
python_represents(T1,T),
D[V0] := T1.
/**
*

View File

@@ -69,7 +69,7 @@ elif platform.system() == 'Darwin':
win_libs = []
local_libs = ['Py4YAP']
elif platform.system() == 'Linux':
my_extra_link_args = ['-L','..','-Wl,-rpath,'+abspath(join(sysconfig.get_path('platlib'),'yap4py')),'-Wl,-rpath,@CMAKE_INSTALL_FULL_LIBDIR@','-Wl,-rpath,'+join('@CMAKE_INSTALL_FULL_LIBDIR@','..'),'-Wl,-rpath,../yap4py']
my_extra_link_args = ['-L','..','-Wl,-rpath,'+abspath(join(sysconfig.get_path('platlib'),'yap4py')),'-L','@CMAKE_INSTALL_FULL_LIBDIR@','-Wl,-rpath,@CMAKE_INSTALL_FULL_LIBDIR@','-Wl,-rpath,'+join('@CMAKE_INSTALL_FULL_LIBDIR@','..'),'-Wl,-rpath,../yap4py']
win_libs = []
local_libs = ['Py4YAP']

View File

@@ -5,44 +5,75 @@
* @brief JUpyter support.
*/
:- yap_flag(gc_trace,verbose).
/*
%:- yap_flag(gc_trace,verbose).
:- module( jupyter,
[jupyter_queryl/3,
[jupyter_query/3,
jupyter_query/4,
op(100,fy,('$')),
op(950,fy,:=),
op(950,yfx,:=),
% op(950,fx,<-),
% op(950,yfx,<-),
op(50, yf, []),
op(50, yf, '()'),
op(100, xfy, '.'),
op(100, fy, '.'),
blank/1,
streams/2
streams/1
]
).
*/
:- use_module(library(hacks)).
:- use_module(library(lists)).
:- use_module(library(maplist)).
%% :- reexport(library(python)).
%% :- reexport(library(yapi)).
%% :- reexport(library(complete)).
%% :- reexport(library(verify)).
:- use_module(library(python)).
:- use_module(library(yapi)).
:- use_module(library(complete)).
:- use_module(library(verify)).
:- python_import(sys).
jupyter_query(Caller, Cell, Line, Bindings ) :-
gated_call(
streams(true),
jupyter_cell(Caller, Cell, Line, Bindings),
Port,
next_streams( Caller, Port, Bindings )
).
jupyter_query(Caller, Cell, Line ) :-
jupyter_cell(Caller, Cell, Line).
jupyter_query( Caller, Cell, Line, _Bindings ).
jupyter_cell(_Caller, Cell, _Line) :-
jupyter_consult(Cell), %stack_dump,
next_streams( _Caller, exit, _Bindings ) :-
% Caller.answer := Bindings,
!.
next_streams( _Caller, answer, _Bindings ) :-
% Caller.answer := Bindings,
!.
next_streams(_, redo, _ ) :-
streams(true),
!.
next_streams( _, _, _ ) :-
streams(false).
jupyter_cell(_Caller, Cell, _Line, _) :-
jupyter_consult(Cell), %stack_dump,
fail.
jupyter_cell( _Caller, _, ¨¨ ) :- !.
jupyter_cell( _Caller, _, Line ) :-
jupyter_cell( _Caller, _, ¨¨ , _) :- !.
jupyter_cell( _Caller, _, Line , _) :-
blank( Line ),
!.
jupyter_cell(Caller, _, Line ) :-
jupyter_cell(Caller, _, Line, Bindings ) :-
Query = Caller,
catch(
python_query(Query,Line),
python_query(Query,Line, Bindings),
error(A,B),
system_error(A,B)
).
@@ -58,6 +89,8 @@ restreams(!).
restreams(external_exception(_)).
restreams(exception).
%:- meta_predicate
jupyter_consult(Text) :-
blank( Text ),
!.
@@ -89,7 +122,7 @@ blank(Text) :-
maplist( code_type(space), L).
streams(false) :-
streams(false) :-
close(user_input),
close(user_output),
close(user_error).

View File

@@ -16,7 +16,7 @@
:- use_module(library(lists)).
:- use_module(library(maplist)).
:- use_module(library(python)).
%% :- use_module(library(python)).
%% :- use_module(library(yapi)).
:- dynamic jupyter/1.
@@ -25,7 +25,7 @@ jupyter( []).
ready( Engine, Query) :-
errors( Engine , Query ),
Es := Engine.errors,
not Es == [].
Es \== [].

View File

@@ -1,19 +1,19 @@
"""
Shim to maintain backwards compatibility with old yap_ipython.terminal.console imports.
Shim to maintain backwards compatibility with old IPython.terminal.console imports.
"""
# Copyright (c) yap_ipython Development Team.
# Copyright (c) IPython Development Team.
# Distributed under the terms of the Modified BSD License.
import sys
from warnings import warn
from yap_ipython.utils.shimmodule import ShimModule, ShimWarning
from IPython.utils.shimmodule import ShimModule, ShimWarning
warn("The `yap_ipython.terminal.console` package has been deprecated since yap_ipython 4.0. "
warn("The `IPython.terminal.console` package has been deprecated since IPython 4.0. "
"You should import from jupyter_console instead.", ShimWarning)
# Unconditionally insert the shim into sys.modules so that further import calls
# trigger the custom attribute access above
sys.modules['yap_ipython.terminal.console'] = ShimModule(
src='yap_ipython.terminal.console', mirror='jupyter_console')
sys.modules['IPython.terminal.console'] = ShimModule(
src='IPython.terminal.console', mirror='jupyter_console')

View File

@@ -1,18 +1,18 @@
import signal
import sys
from yap_ipython.core.debugger import Pdb
from IPython.core.debugger import Pdb
from yap_ipython.core.completer import IPCompleter
from IPython.core.completer import IPCompleter
from .ptutils import IPythonPTCompleter
from .shortcuts import suspend_to_bg, cursor_in_leading_ws
from prompt_toolkit.enums import DEFAULT_BUFFER
from prompt_toolkit.filters import (Condition, HasFocus, HasSelection,
ViInsertMode, EmacsInsertMode)
from prompt_toolkit.keys import Keys
from prompt_toolkit.key_binding.manager import KeyBindingManager
from prompt_toolkit.filters import (Condition, has_focus, has_selection,
vi_insert_mode, emacs_insert_mode)
from prompt_toolkit.key_binding import KeyBindings
from prompt_toolkit.key_binding.bindings.completion import display_completions_like_readline
from pygments.token import Token
from prompt_toolkit.shortcuts.prompt import PromptSession
from prompt_toolkit.enums import EditingMode
from prompt_toolkit.formatted_text import PygmentsTokens
@@ -58,6 +58,7 @@ class TerminalPdb(Pdb):
complete_style=self.shell.pt_complete_style,
style=self.shell.style,
inputhook=self.shell.inputhook,
color_depth=self.shell.color_depth,
)
def cmdloop(self, intro=None):
@@ -107,7 +108,7 @@ def set_trace(frame=None):
if __name__ == '__main__':
import pdb
# yap_ipython.core.debugger.Pdb.trace_dispatch shall not catch
# IPython.core.debugger.Pdb.trace_dispatch shall not catch
# bdb.BdbQuit. When started through __main__ and an exception
# happened after hitting "c", this is needed in order to
# be able to quit the debugging session (see #9950).

View File

@@ -1,27 +1,27 @@
# encoding: utf-8
"""
An embedded yap_ipython shell.
An embedded IPython shell.
"""
# Copyright (c) yap_ipython Development Team.
# Copyright (c) IPython Development Team.
# Distributed under the terms of the Modified BSD License.
import sys
import warnings
from yap_ipython.core import ultratb, compilerop
from yap_ipython.core import magic_arguments
from yap_ipython.core.magic import Magics, magics_class, line_magic
from yap_ipython.core.interactiveshell import DummyMod, InteractiveShell
from yap_ipython.terminal.interactiveshell import TerminalInteractiveShell
from yap_ipython.terminal.ipapp import load_default_config
from IPython.core import ultratb, compilerop
from IPython.core import magic_arguments
from IPython.core.magic import Magics, magics_class, line_magic
from IPython.core.interactiveshell import DummyMod, InteractiveShell
from IPython.terminal.interactiveshell import TerminalInteractiveShell
from IPython.terminal.ipapp import load_default_config
from traitlets import Bool, CBool, Unicode
from yap_ipython.utils.io import ask_yes_no
from IPython.utils.io import ask_yes_no
class KillEmbedded(Exception):pass
# kept for backward compatibility as yap_ipython 6 was released with
# kept for backward compatibility as IPython 6 was released with
# the typo. See https://github.com/ipython/ipython/pull/10706
KillEmbeded = KillEmbedded
@@ -38,10 +38,10 @@ class EmbeddedMagics(Magics):
@magic_arguments.argument('-y', '--yes', action='store_true',
help='Do not ask confirmation')
def kill_embedded(self, parameter_s=''):
"""%kill_embedded : deactivate for good the current embedded yap_ipython
"""%kill_embedded : deactivate for good the current embedded IPython
This function (after asking for confirmation) sets an internal flag so
that an embedded yap_ipython will never activate again for the given call
that an embedded IPython will never activate again for the given call
location. This is useful to permanently disable a shell that is being
called inside a loop: once you've figured out what you needed from it,
you may then kill it and the program will then continue to run without
@@ -59,7 +59,7 @@ class EmbeddedMagics(Magics):
.. note::
This was the default behavior before yap_ipython 5.2
This was the default behavior before IPython 5.2
"""
@@ -74,7 +74,7 @@ class EmbeddedMagics(Magics):
kill = True
if kill:
self.shell._disable_init_location()
print("This embedded yap_ipython instance will not reactivate anymore "
print("This embedded IPython instance will not reactivate anymore "
"once you exit.")
else:
if not args.yes:
@@ -84,7 +84,7 @@ class EmbeddedMagics(Magics):
kill = True
if kill:
self.shell.embedded_active = False
print("This embedded yap_ipython call location will not reactivate anymore "
print("This embedded IPython call location will not reactivate anymore "
"once you exit.")
if args.exit:
@@ -97,9 +97,9 @@ class EmbeddedMagics(Magics):
def exit_raise(self, parameter_s=''):
"""%exit_raise Make the current embedded kernel exit and raise and exception.
This function sets an internal flag so that an embedded yap_ipython will
raise a `yap_ipython.terminal.embed.KillEmbedded` Exception on exit, and then exit the current I. This is
useful to permanently exit a loop that create yap_ipython embed instance.
This function sets an internal flag so that an embedded IPython will
raise a `IPython.terminal.embed.KillEmbedded` Exception on exit, and then exit the current I. This is
useful to permanently exit a loop that create IPython embed instance.
"""
self.shell.should_raise = True
@@ -148,7 +148,7 @@ class InteractiveShellEmbed(TerminalInteractiveShell):
def __init__(self, **kw):
if kw.get('user_global_ns', None) is not None:
raise DeprecationWarning(
"Key word argument `user_global_ns` has been replaced by `user_module` since yap_ipython 4.0.")
"Key word argument `user_global_ns` has been replaced by `user_module` since IPython 4.0.")
clid = kw.pop('_init_location_id', None)
if not clid:
@@ -166,7 +166,7 @@ class InteractiveShellEmbed(TerminalInteractiveShell):
def init_sys_modules(self):
"""
Explicitly overwrite :mod:`yap_ipython.core.interactiveshell` to do nothing.
Explicitly overwrite :mod:`IPython.core.interactiveshell` to do nothing.
"""
pass
@@ -234,12 +234,12 @@ class InteractiveShellEmbed(TerminalInteractiveShell):
print(self.exit_msg)
if self.should_raise:
raise KillEmbedded('Embedded yap_ipython raising error, as user requested.')
raise KillEmbedded('Embedded IPython raising error, as user requested.')
def mainloop(self, local_ns=None, module=None, stack_depth=0,
display_banner=None, global_ns=None, compile_flags=None):
"""Embeds yap_ipython into a running python program.
"""Embeds IPython into a running python program.
Parameters
----------
@@ -265,10 +265,10 @@ class InteractiveShellEmbed(TerminalInteractiveShell):
"""
if (global_ns is not None) and (module is None):
raise DeprecationWarning("'global_ns' keyword argument is deprecated, and has been removed in yap_ipython 5.0 use `module` keyword argument instead.")
raise DeprecationWarning("'global_ns' keyword argument is deprecated, and has been removed in IPython 5.0 use `module` keyword argument instead.")
if (display_banner is not None):
warnings.warn("The display_banner parameter is deprecated since yap_ipython 4.0", DeprecationWarning)
warnings.warn("The display_banner parameter is deprecated since IPython 4.0", DeprecationWarning)
# Get locals and globals from caller
if ((local_ns is None or module is None or compile_flags is None)
@@ -323,7 +323,7 @@ class InteractiveShellEmbed(TerminalInteractiveShell):
with self.builtin_trap, self.display_trap:
self.interact()
# now, purge out the local namespace of yap_ipython's hidden variables.
# now, purge out the local namespace of IPython's hidden variables.
if local_ns is not None:
local_ns.update({k: v for (k, v) in self.user_ns.items() if k not in self.user_ns_hidden.keys()})
@@ -335,7 +335,7 @@ class InteractiveShellEmbed(TerminalInteractiveShell):
def embed(**kwargs):
"""Call this to embed yap_ipython at the current point in your program.
"""Call this to embed IPython at the current point in your program.
The first invocation of this will create an :class:`InteractiveShellEmbed`
instance and then call it. Consecutive calls just call the already
@@ -343,12 +343,12 @@ def embed(**kwargs):
If you don't want the kernel to initialize the namespace
from the scope of the surrounding function,
and/or you want to load full yap_ipython configuration,
you probably want `yap_ipython.start_ipython()` instead.
and/or you want to load full IPython configuration,
you probably want `IPython.start_ipython()` instead.
Here is a simple example::
from yap_ipython import embed
from IPython import embed
a = 10
b = 20
embed(header='First time')

View File

@@ -1,15 +1,15 @@
"""yap_ipython terminal interface using prompt_toolkit"""
"""IPython terminal interface using prompt_toolkit"""
import os
import sys
import warnings
from warnings import warn
from yap_ipython.core.interactiveshell import InteractiveShell, InteractiveShellABC
from yap_ipython.utils import io
from yap_ipython.utils.py3compat import input
from yap_ipython.utils.terminal import toggle_set_term_title, set_term_title
from yap_ipython.utils.process import abbrev_cwd
from IPython.core.interactiveshell import InteractiveShell, InteractiveShellABC
from IPython.utils import io
from IPython.utils.py3compat import input
from IPython.utils.terminal import toggle_set_term_title, set_term_title
from IPython.utils.process import abbrev_cwd
from traitlets import (
Bool, Unicode, Dict, Integer, observe, Instance, Type, default, Enum, Union,
Any, validate
@@ -98,8 +98,8 @@ class TerminalInteractiveShell(InteractiveShell):
simple_prompt = Bool(_use_simple_prompt,
help="""Use `raw_input` for the REPL, without completion and prompt colors.
Useful when controlling yap_ipython as a subprocess, and piping STDIN/OUT/ERR. Known usage are:
yap_ipython own testing machinery, and emacs inferior-shell integration through elpy.
Useful when controlling IPython as a subprocess, and piping STDIN/OUT/ERR. Known usage are:
IPython own testing machinery, and emacs inferior-shell integration through elpy.
This mode default to `True` if the `IPY_TEST_SIMPLE_PROMPT`
environment variable is set, or the current terminal is not a tty."""
@@ -111,7 +111,7 @@ class TerminalInteractiveShell(InteractiveShell):
confirm_exit = Bool(True,
help="""
Set to confirm when you try to exit yap_ipython with an EOF (Control-D
Set to confirm when you try to exit IPython with an EOF (Control-D
in Unix, Control-Z/Enter in Windows). By typing 'exit' or 'quit',
you can force a direct exit without any confirmation.""",
).tag(config=True)
@@ -147,7 +147,8 @@ class TerminalInteractiveShell(InteractiveShell):
@observe('editing_mode')
def _editing_mode(self, change):
u_mode = change.new.upper()
self.pt_app.editing_mode = u_mode
if self.pt_app:
self.pt_app.editing_mode = u_mode
@observe('highlighting_style')
@observe('colors')
@@ -170,7 +171,7 @@ class TerminalInteractiveShell(InteractiveShell):
).tag(config=True)
editor = Unicode(get_default_editor(),
help="Set the editor used by yap_ipython (default to $EDITOR/vi/notepad)."
help="Set the editor used by IPython (default to $EDITOR/vi/notepad)."
).tag(config=True)
prompts_class = Type(Prompts, help='Class used to generate Prompt token for prompt_toolkit').tag(config=True)
@@ -193,7 +194,7 @@ class TerminalInteractiveShell(InteractiveShell):
help="Automatically set the terminal title"
).tag(config=True)
term_title_format = Unicode("yap_ipython: {cwd}",
term_title_format = Unicode("IPython: {cwd}",
help="Customize the terminal title format. This is a python format string. " +
"Available substitutions are: {cwd}."
).tag(config=True)
@@ -224,6 +225,10 @@ class TerminalInteractiveShell(InteractiveShell):
help="Allows to enable/disable the prompt toolkit history search"
).tag(config=True)
prompt_includes_vi_mode = Bool(True,
help="Display the current vi mode (when using vi editing mode)."
).tag(config=True)
@observe('term_title')
def init_term_title(self, change=None):
# Enable or disable the terminal title.
@@ -257,7 +262,7 @@ class TerminalInteractiveShell(InteractiveShell):
# Set up keyboard shortcuts
key_bindings = create_ipython_shortcuts(self)
# Pre-populate history from yap_ipython's history database
# Pre-populate history from IPython's history database
history = InMemoryHistory()
last_cell = u""
for __, ___, cell in self.history_manager.get_tail(self.history_load_length,
@@ -283,12 +288,12 @@ class TerminalInteractiveShell(InteractiveShell):
include_default_pygments_style=False,
mouse_support=self.mouse_support,
enable_open_in_editor=self.extra_open_editor_shortcuts,
color_depth=(ColorDepth.TRUE_COLOR if self.true_color else None),
color_depth=self.color_depth,
**self._extra_prompt_options())
def _make_style_from_name_or_cls(self, name_or_cls):
"""
Small wrapper that make an yap_ipython compatible style from a style name
Small wrapper that make an IPython compatible style from a style name
We need that to add style for prompt ... etc.
"""
@@ -360,6 +365,10 @@ class TerminalInteractiveShell(InteractiveShell):
'readlinelike': CompleteStyle.READLINE_LIKE,
}[self.display_completions]
@property
def color_depth(self):
return (ColorDepth.TRUE_COLOR if self.true_color else None)
def _extra_prompt_options(self):
"""
Return the current layout option for the current Terminal InteractiveShell
@@ -442,7 +451,7 @@ class TerminalInteractiveShell(InteractiveShell):
# need direct access to the console in a way that we can't emulate in
# GUI or web frontend
if os.name == 'posix':
for cmd in ['clear', 'more', 'less', 'man']:
for cmd in ('clear', 'more', 'less', 'man'):
self.alias_manager.soft_define_alias(cmd, cmd)
@@ -462,7 +471,7 @@ class TerminalInteractiveShell(InteractiveShell):
def interact(self, display_banner=DISPLAY_BANNER_DEPRECATED):
if display_banner is not DISPLAY_BANNER_DEPRECATED:
warn('interact `display_banner` argument is deprecated since yap_ipython 5.0. Call `show_banner()` if needed.', DeprecationWarning, stacklevel=2)
warn('interact `display_banner` argument is deprecated since IPython 5.0. Call `show_banner()` if needed.', DeprecationWarning, stacklevel=2)
self.keep_running = True
while self.keep_running:

View File

@@ -1,11 +1,11 @@
#!/usr/bin/env python
# encoding: utf-8
"""
The :class:`~yap_ipython.core.application.Application` object for the command
The :class:`~IPython.core.application.Application` object for the command
line :command:`ipython` program.
"""
# Copyright (c) yap_ipython Development Team.
# Copyright (c) IPython Development Team.
# Distributed under the terms of the Modified BSD License.
@@ -16,24 +16,24 @@ import warnings
from traitlets.config.loader import Config
from traitlets.config.application import boolean_flag, catch_config_error
from yap_ipython.core import release
from yap_ipython.core import usage
from yap_ipython.core.completer import IPCompleter
from yap_ipython.core.crashhandler import CrashHandler
from yap_ipython.core.formatters import PlainTextFormatter
from yap_ipython.core.history import HistoryManager
from yap_ipython.core.application import (
ProfileDir, BaseYAPApplication, base_flags, base_aliases
from IPython.core import release
from IPython.core import usage
from IPython.core.completer import IPCompleter
from IPython.core.crashhandler import CrashHandler
from IPython.core.formatters import PlainTextFormatter
from IPython.core.history import HistoryManager
from IPython.core.application import (
ProfileDir, BaseIPythonApplication, base_flags, base_aliases
)
from yap_ipython.core.magics import (
from IPython.core.magics import (
ScriptMagics, LoggingMagics
)
from yap_ipython.core.shellapp import (
from IPython.core.shellapp import (
InteractiveShellApp, shell_flags, shell_aliases
)
from yap_ipython.extensions.storemagic import StoreMagics
from IPython.extensions.storemagic import StoreMagics
from .interactiveshell import TerminalInteractiveShell
from yap_ipython.paths import get_ipython_dir
from IPython.paths import get_ipython_dir
from traitlets import (
Bool, List, default, observe, Type
)
@@ -52,7 +52,7 @@ ipython --profile=foo # start with profile foo
ipython profile create foo # create profile foo w/ default config files
ipython help profile # show the help for the profile subcmd
ipython locate # print the path to the yap_ipython directory
ipython locate # print the path to the IPython directory
ipython locate profile foo # print the path to the directory for profile `foo`
"""
@@ -61,7 +61,7 @@ ipython locate profile foo # print the path to the directory for profile `foo`
#-----------------------------------------------------------------------------
class IPAppCrashHandler(CrashHandler):
"""sys.excepthook for yap_ipython itself, leaves a detailed report on disk."""
"""sys.excepthook for IPython itself, leaves a detailed report on disk."""
def __init__(self, app):
contact_name = release.author
@@ -106,12 +106,12 @@ addflag('simple-prompt', 'TerminalInteractiveShell.simple_prompt',
"Use a rich interactive prompt with prompt_toolkit",
)
addflag('banner', 'Terminalyap_ipythonApp.display_banner',
"Display a banner upon starting yap_ipython.",
"Don't display a banner upon starting yap_ipython."
addflag('banner', 'TerminalIPythonApp.display_banner',
"Display a banner upon starting IPython.",
"Don't display a banner upon starting IPython."
)
addflag('confirm-exit', 'TerminalInteractiveShell.confirm_exit',
"""Set to confirm when you try to exit yap_ipython with an EOF (Control-D
"""Set to confirm when you try to exit IPython with an EOF (Control-D
in Unix, Control-Z/Enter in Windows). By typing 'exit' or 'quit',
you can force a direct exit without any confirmation.""",
"Don't prompt the user when exiting."
@@ -123,7 +123,7 @@ addflag('term-title', 'TerminalInteractiveShell.term_title',
classic_config = Config()
classic_config.InteractiveShell.cache_size = 0
classic_config.PlainTextFormatter.pprint = False
classic_config.TerminalInteractiveShell.prompts_class='yap_ipython.terminal.prompts.ClassicPrompts'
classic_config.TerminalInteractiveShell.prompts_class='IPython.terminal.prompts.ClassicPrompts'
classic_config.InteractiveShell.separate_in = ''
classic_config.InteractiveShell.separate_out = ''
classic_config.InteractiveShell.separate_out2 = ''
@@ -132,7 +132,7 @@ classic_config.InteractiveShell.xmode = 'Plain'
frontend_flags['classic']=(
classic_config,
"Gives yap_ipython a similar feel to the classic Python prompt."
"Gives IPython a similar feel to the classic Python prompt."
)
# # log doesn't make so much sense this way anymore
# paa('--log','-l',
@@ -141,12 +141,12 @@ frontend_flags['classic']=(
#
# # quick is harder to implement
frontend_flags['quick']=(
{'Terminalyap_ipythonApp' : {'quick' : True}},
{'TerminalIPythonApp' : {'quick' : True}},
"Enable quick startup with no config files."
)
frontend_flags['i'] = (
{'Terminalyap_ipythonApp' : {'force_interact' : True}},
{'TerminalIPythonApp' : {'force_interact' : True}},
"""If running code from the command line, become interactive afterwards.
It is often useful to follow this with `--` to treat remaining flags as
script arguments.
@@ -162,11 +162,11 @@ aliases.update(shell_aliases)
#-----------------------------------------------------------------------------
class Locateyap_ipythonApp(BaseYAPApplication):
description = """print the path to the yap_ipython dir"""
class LocateIPythonApp(BaseIPythonApplication):
description = """print the path to the IPython dir"""
subcommands = dict(
profile=('yap_ipython.core.profileapp.ProfileLocate',
"print the path to an yap_ipython profile directory",
profile=('IPython.core.profileapp.ProfileLocate',
"print the path to an IPython profile directory",
),
)
def start(self):
@@ -176,7 +176,7 @@ class Locateyap_ipythonApp(BaseYAPApplication):
print(self.ipython_dir)
class Terminalyap_ipythonApp(BaseYAPApplication, InteractiveShellApp):
class TerminalIPythonApp(BaseIPythonApplication, InteractiveShellApp):
name = u'ipython'
description = usage.cl_usage
crash_handler_class = IPAppCrashHandler
@@ -194,7 +194,7 @@ class Terminalyap_ipythonApp(BaseYAPApplication, InteractiveShellApp):
@default('classes')
def _classes_default(self):
"""This has to be in a method, for Terminalyap_ipythonApp to be available."""
"""This has to be in a method, for TerminalIPythonApp to be available."""
return [
InteractiveShellApp, # ShellApp comes before TerminalApp, because
self.__class__, # it will also affect subclasses (e.g. QtConsole)
@@ -210,41 +210,41 @@ class Terminalyap_ipythonApp(BaseYAPApplication, InteractiveShellApp):
deprecated_subcommands = dict(
qtconsole=('qtconsole.qtconsoleapp.JupyterQtConsoleApp',
"""DEPRECATED, Will be removed in yap_ipython 6.0 : Launch the Jupyter Qt Console."""
"""DEPRECATED, Will be removed in IPython 6.0 : Launch the Jupyter Qt Console."""
),
notebook=('notebook.notebookapp.NotebookApp',
"""DEPRECATED, Will be removed in yap_ipython 6.0 : Launch the Jupyter HTML Notebook Server."""
"""DEPRECATED, Will be removed in IPython 6.0 : Launch the Jupyter HTML Notebook Server."""
),
console=('jupyter_console.app.ZMQTerminalyap_ipythonApp',
"""DEPRECATED, Will be removed in yap_ipython 6.0 : Launch the Jupyter terminal-based Console."""
console=('jupyter_console.app.ZMQTerminalIPythonApp',
"""DEPRECATED, Will be removed in IPython 6.0 : Launch the Jupyter terminal-based Console."""
),
nbconvert=('nbconvert.nbconvertapp.NbConvertApp',
"DEPRECATED, Will be removed in yap_ipython 6.0 : Convert notebooks to/from other formats."
"DEPRECATED, Will be removed in IPython 6.0 : Convert notebooks to/from other formats."
),
trust=('nbformat.sign.TrustNotebookApp',
"DEPRECATED, Will be removed in yap_ipython 6.0 : Sign notebooks to trust their potentially unsafe contents at load."
"DEPRECATED, Will be removed in IPython 6.0 : Sign notebooks to trust their potentially unsafe contents at load."
),
kernelspec=('jupyter_client.kernelspecapp.KernelSpecApp',
"DEPRECATED, Will be removed in yap_ipython 6.0 : Manage Jupyter kernel specifications."
"DEPRECATED, Will be removed in IPython 6.0 : Manage Jupyter kernel specifications."
),
)
subcommands = dict(
profile = ("yap_ipython.core.profileapp.ProfileApp",
"Create and manage yap_ipython profiles."
profile = ("IPython.core.profileapp.ProfileApp",
"Create and manage IPython profiles."
),
kernel = ("ipykernel.kernelapp.IPKernelApp",
"Start a kernel without an attached frontend."
),
locate=('yap_ipython.terminal.ipapp.Locateyap_ipythonApp',
Locateyap_ipythonApp.description
locate=('IPython.terminal.ipapp.LocateIPythonApp',
LocateIPythonApp.description
),
history=('yap_ipython.core.historyapp.HistoryApp',
"Manage the yap_ipython history database."
history=('IPython.core.historyapp.HistoryApp',
"Manage the IPython history database."
),
)
deprecated_subcommands['install-nbextension'] = (
"notebook.nbextensions.InstallNBExtensionApp",
"DEPRECATED, Will be removed in yap_ipython 6.0 : Install Jupyter notebook extension files"
"DEPRECATED, Will be removed in IPython 6.0 : Install Jupyter notebook extension files"
)
subcommands.update(deprecated_subcommands)
@@ -252,7 +252,7 @@ class Terminalyap_ipythonApp(BaseYAPApplication, InteractiveShellApp):
auto_create=Bool(True)
# configurables
quick = Bool(False,
help="""Start yap_ipython quickly by skipping the loading of config files."""
help="""Start IPython quickly by skipping the loading of config files."""
).tag(config=True)
@observe('quick')
def _quick_changed(self, change):
@@ -260,7 +260,7 @@ class Terminalyap_ipythonApp(BaseYAPApplication, InteractiveShellApp):
self.load_config_file = lambda *a, **kw: None
display_banner = Bool(True,
help="Whether to display a banner upon starting yap_ipython."
help="Whether to display a banner upon starting IPython."
).tag(config=True)
# if there is code of files to run from the cmd line, don't interact
@@ -300,12 +300,12 @@ class Terminalyap_ipythonApp(BaseYAPApplication, InteractiveShellApp):
" Use `--matplotlib <backend>` and import pylab manually.")
argv[idx] = '--pylab'
return super(Terminalyap_ipythonApp, self).parse_command_line(argv)
return super(TerminalIPythonApp, self).parse_command_line(argv)
@catch_config_error
def initialize(self, argv=None):
"""Do actions after construct, but before starting the app."""
super(Terminalyap_ipythonApp, self).initialize(argv)
super(TerminalIPythonApp, self).initialize(argv)
if self.subapp is not None:
# don't bother initializing further, starting subapp
return
@@ -352,10 +352,10 @@ class Terminalyap_ipythonApp(BaseYAPApplication, InteractiveShellApp):
return self.subapp.start()
# perform any prexec steps:
if self.interact:
self.log.debug("Starting yap_ipython's mainloop...")
self.log.debug("Starting IPython's mainloop...")
self.shell.mainloop()
else:
self.log.debug("yap_ipython not interactive...")
self.log.debug("IPython not interactive...")
if not self.shell.last_execution_succeeded:
sys.exit(1)
@@ -368,12 +368,12 @@ def load_default_config(ipython_dir=None):
ipython_dir = get_ipython_dir()
profile_dir = os.path.join(ipython_dir, 'profile_default')
app = Terminalyap_ipythonApp()
app = TerminalIPythonApp()
app.config_file_paths.append(profile_dir)
app.load_config_file()
return app.config
launch_new_instance = Terminalyap_ipythonApp.launch_instance
launch_new_instance = TerminalIPythonApp.launch_instance
if __name__ == '__main__':

View File

@@ -1,6 +1,6 @@
"""Extra magics for terminal use."""
# Copyright (c) yap_ipython Development Team.
# Copyright (c) IPython Development Team.
# Distributed under the terms of the Modified BSD License.
@@ -8,11 +8,11 @@ from logging import error
import os
import sys
from yap_ipython.core.error import TryNext, UsageError
from yap_ipython.core.magic import Magics, magics_class, line_magic
from yap_ipython.lib.clipboard import ClipboardEmpty
from yap_ipython.utils.text import SList, strip_email_quotes
from yap_ipython.utils import py3compat
from IPython.core.error import TryNext, UsageError
from IPython.core.magic import Magics, magics_class, line_magic
from IPython.lib.clipboard import ClipboardEmpty
from IPython.utils.text import SList, strip_email_quotes
from IPython.utils import py3compat
def get_pasted_lines(sentinel, l_input=py3compat.input, quiet=False):
""" Yield pasted lines until the user enters the given sentinel value.
@@ -109,7 +109,7 @@ class TerminalMagics(Magics):
Just press enter and type -- (and press enter again) and the block
will be what was just pasted.
yap_ipython statements (magics, shell escapes) are not supported (yet).
IPython statements (magics, shell escapes) are not supported (yet).
See also
--------
@@ -162,7 +162,7 @@ class TerminalMagics(Magics):
-q: quiet mode: do not echo the pasted text back to the terminal.
yap_ipython statements (magics, shell escapes) are not supported (yet).
IPython statements (magics, shell escapes) are not supported (yet).
See also
--------

View File

@@ -3,7 +3,7 @@
from pygments.token import Token
import sys
from yap_ipython.core.displayhook import DisplayHook
from IPython.core.displayhook import DisplayHook
from prompt_toolkit.formatted_text import fragment_list_width, PygmentsTokens
from prompt_toolkit.shortcuts import print_formatted_text
@@ -14,9 +14,8 @@ class Prompts(object):
self.shell = shell
def vi_mode(self):
if not hasattr(self.shell.pt_app, 'editing_mode'):
return ''
if self.shell.pt_app.editing_mode == 'VI':
if (getattr(self.shell.pt_app, 'editing_mode', None) == 'VI'
and self.shell.prompt_includes_vi_mode):
return '['+str(self.shell.pt_app.app.vi_state.input_mode)[3:6]+'] '
return ''

View File

@@ -19,12 +19,16 @@ from prompt_toolkit.lexers import PygmentsLexer
from prompt_toolkit.patch_stdout import patch_stdout
import pygments.lexers as pygments_lexers
import os
_completion_sentinel = object()
def _elide(string, *, min_elide=30):
"""
If a string is long enough, and has at least 2 dots,
If a string is long enough, and has at least 3 dots,
replace the middle part with ellipses.
If a string naming a file is long enough, and has at least 3 slashes,
replace the middle part with ellipses.
If three consecutive dots, or two consecutive dots are encountered these are
@@ -36,16 +40,20 @@ def _elide(string, *, min_elide=30):
if len(string) < min_elide:
return string
parts = string.split('.')
object_parts = string.split('.')
file_parts = string.split(os.sep)
if len(parts) <= 3:
return string
if len(object_parts) > 3:
return '{}.{}\N{HORIZONTAL ELLIPSIS}{}.{}'.format(object_parts[0], object_parts[1][0], object_parts[-2][-1], object_parts[-1])
return '{}.{}\N{HORIZONTAL ELLIPSIS}{}.{}'.format(parts[0], parts[1][0], parts[-2][-1], parts[-1])
elif len(file_parts) > 3:
return ('{}' + os.sep + '{}\N{HORIZONTAL ELLIPSIS}{}' + os.sep + '{}').format(file_parts[0], file_parts[1][0], file_parts[-2][-1], file_parts[-1])
return string
def _adjust_completion_text_based_on_context(text, body, offset):
if text.endswith('=') and len(body) > offset and body[offset] is '=':
if text.endswith('=') and len(body) > offset and body[offset] == '=':
return text[:-1]
else:
return text

View File

@@ -17,6 +17,9 @@ from IPython.core.inputtransformer import *
from IPython.core.interactiveshell import *
from ipython_genutils.py3compat import builtin_mod
import copy
import json
from yap_kernel.displayhook import ZMQShellDisplayHook
import traceback
@@ -229,6 +232,7 @@ class YAPInputSplitter(InputSplitter):
transformed_lines_list.append(transformed)
if transformed_lines_list:
transformed_lines = '\n'.join(transformed_lines_list)
else:
# Got nothing back from transformers - they must be waiting for
# more input.
@@ -542,7 +546,7 @@ class YAPRun(InteractiveShell):
# construct a self.queryuery from a one-line string
# self.query is opaque to Python
try:
program,squery,_ ,howmany = self.prolog_cell(s)
program,squery,_ ,howmany = self.prolog_cell(s)
# sys.settrace(tracefunc)
if self.query and self.os == (program,squery):
howmany += self.iterations
@@ -551,22 +555,29 @@ class YAPRun(InteractiveShell):
self.query.close()
self.query = None
self.answers = []
result.result = []
self.os = (program,squery)
self.iterations = 0
pg = jupyter_query(self.engine,program,squery)
self.query = Query(self.engine, pg)
self.answers = []
for answer in self.query:
self.answers += [answer]
print( answer )
self.answers += [copy.deepcopy(answer)]
self.iterations += 1
self.os = None
self.query.close()
self.query = None
if self.answers:
sys.stderr.write('Completed, with '+str(self.answers)+'\n')
result.result = self.answers
sys.stderr.write('\n'+'[ ' +str(len(self.answers))+' answer(s): ]\n[ ')
print( self.answers )
result.result = json.dumps(self.answers)
sys.stderr.write(result.result+' ]\n\n')
else:
result.result = []
return result.result
except Exception as e:
sys.stderr.write('Exception '+str(e)+'in query '+ str(self.query)+