FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
sqlite3-kernel/sqlite3_kernel/kernel.py at master · brownan/sqlite3-kernel · GitHub
brownan
/
sqlite3-kernel
Public
Notifications
You must be signed in to change notification settings
Fork
13
Star
15
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
sqlite3-kernel
/
sqlite3_kernel
/
kernel.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
116 lines (93 loc) · 3.86 KB
Breadcrumbs
sqlite3-kernel
/
sqlite3_kernel
/
kernel.py
Copy path
File metadata and controls
116 lines (93 loc) · 3.86 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
from
ipykernel
.
kernelbase
import
Kernel
from
pexpect
import
replwrap
,
EOF
from
subprocess
import
check_output
import
re
import
signal
__version__
=
'0.2'
version_pat
=
re
.
compile
(
r'SQLite version (\d+(\.\d+)+)'
)
class
Sqlite3REPL
(
replwrap
.
REPLWrapper
):
def
__init__
(
self
):
super
().
__init__
(
"sqlite3"
,
"sqlite> "
,
prompt_change
=
None
,
continuation_prompt
=
" ...> "
)
# This is required to force suppression of command echo.
self
.
child
.
setecho
(
False
)
def
run_command
(
self
,
command
,
timeout
=
-
1
):
"""Same as parent class but on continuations, send a semicolon to
terminate the command"""
cmdlines
=
command
.
splitlines
()
if
command
.
endswith
(
'
\n
'
):
cmdlines
.
append
(
''
)
if
not
cmdlines
:
raise
ValueError
(
"No command was given"
)
res
=
[]
self
.
child
.
sendline
(
cmdlines
[
0
])
for
line
in
cmdlines
[
1
:]:
self
.
_expect_prompt
(
timeout
=
timeout
)
res
.
append
(
self
.
child
.
before
)
self
.
child
.
sendline
(
line
)
# Command was fully submitted, now wait for the next prompt
if
self
.
_expect_prompt
(
timeout
=
timeout
)
==
1
:
# We got the continuation prompt - command was incomplete
self
.
child
.
sendline
(
";"
)
if
self
.
_expect_prompt
(
timeout
=
timeout
)
==
1
:
raise
ValueError
(
"Continuation prompt found - input was incomplete:
\n
"
+
command
)
return
u''
.
join
(
res
+
[
self
.
child
.
before
])
class
Sqlite3Kernel
(
Kernel
):
implementation
=
'sqlite3_kernel'
implementation_version
=
__version__
@
property
def
language_version
(
self
):
m
=
version_pat
.
search
(
self
.
banner
)
return
m
.
group
(
1
)
_banner
=
None
@
property
def
banner
(
self
):
if
self
.
_banner
is
None
:
self
.
_banner
=
check_output
([
'sqlite3'
,
'--version'
]).
decode
(
'utf-8'
)
return
self
.
_banner
language_info
=
{
'name'
:
'sqlite3'
,
'codemirror_mode'
:
'sql'
,
'mimetype'
:
'text/x-sql'
,
'file_extension'
:
'.sql'
}
def
__init__
(
self
,
**
kwargs
):
Kernel
.
__init__
(
self
,
**
kwargs
)
self
.
_start_sqlite
()
def
_start_sqlite
(
self
):
# Signal handlers are inherited by forked processes, and we can't easily
# reset it from the subprocess. Since kernelapp ignores SIGINT except in
# message handlers, we need to temporarily reset the SIGINT handler here
# so that sqlite is interruptable
sig
=
signal
.
signal
(
signal
.
SIGINT
,
signal
.
SIG_DFL
)
try
:
self
.
sqlitewrapper
=
Sqlite3REPL
()
finally
:
signal
.
signal
(
signal
.
SIGINT
,
sig
)
def
do_execute
(
self
,
code
,
silent
,
store_history
=
True
,
user_expressions
=
None
,
allow_stdin
=
False
):
if
not
code
.
strip
():
return
{
'status'
:
'ok'
,
'execution_count'
:
self
.
execution_count
,
'payload'
: [],
'user_expressions'
: {}}
interrupted
=
False
try
:
output
=
self
.
sqlitewrapper
.
run_command
(
code
.
rstrip
())
except
KeyboardInterrupt
:
self
.
sqlitewrapper
.
child
.
sendintr
()
interrupted
=
True
self
.
sqlitewrapper
.
_expect_prompt
()
output
=
self
.
sqlitewrapper
.
child
.
before
except
EOF
:
output
=
self
.
sqlitewrapper
.
child
.
before
+
'Restarting SQLite3'
self
.
_start_sqlite
()
if
not
silent
:
# Send standard output
stream_content
=
{
'name'
:
'stdout'
,
'text'
:
output
}
self
.
send_response
(
self
.
iopub_socket
,
'stream'
,
stream_content
)
if
interrupted
:
return
{
'status'
:
'abort'
,
'execution_count'
:
self
.
execution_count
}
return
{
'status'
:
'ok'
,
'execution_count'
:
self
.
execution_count
,
'payload'
: [],
'user_expressions'
: {}}
Back
|
FazBrowse Home
|
New Git URL