|
@@ -7,6 +7,7 @@ from pexpect import replwrap, EOF
|
|
|
import pexpect
|
|
|
|
|
|
from subprocess import check_output
|
|
|
+from .parser import MysqlParser
|
|
|
import os.path
|
|
|
|
|
|
import re
|
|
@@ -21,14 +22,12 @@ class MysqlWrapper(replwrap.REPLWrapper):
|
|
|
def __init__(self, cmd_or_spawn, orig_prompt, prompt_change,
|
|
|
extra_init_cmd=None, line_output_callback=None):
|
|
|
self.line_output_callback = line_output_callback
|
|
|
- replwrap.REPLWrapper.__init__(self, cmd_or_spawn, orig_prompt,
|
|
|
- prompt_change, extra_init_cmd=extra_init_cmd)
|
|
|
+ replwrap.REPLWrapper.__init__(self, cmd_or_spawn, orig_prompt, prompt_change, extra_init_cmd=extra_init_cmd)
|
|
|
|
|
|
def _expect_prompt(self, timeout=-1):
|
|
|
if timeout == None:
|
|
|
while True:
|
|
|
- pos = self.child.expect_exact([self.prompt, self.continuation_prompt, u'\r\n'],
|
|
|
- timeout=None)
|
|
|
+ pos = self.child.expect_exact([self.prompt, self.continuation_prompt, u'\r\n'], timeout=None)
|
|
|
if pos == 2:
|
|
|
self.line_output_callback(self.child.before + '\n')
|
|
|
else:
|
|
@@ -67,7 +66,7 @@ class MysqlKernel(Kernel):
|
|
|
,'user' : 'root'
|
|
|
,'host' : '192.168.15.10'
|
|
|
,'port' : '3309'
|
|
|
- ,'charset' : 'utf8'
|
|
|
+ ,'charset' : 'utf-8'
|
|
|
,'password' : '123456'
|
|
|
}
|
|
|
|
|
@@ -83,20 +82,29 @@ class MysqlKernel(Kernel):
|
|
|
sig = signal.signal(signal.SIGINT, signal.SIG_DFL)
|
|
|
try:
|
|
|
if 'password' in self.mysql_config:
|
|
|
- child = pexpect.spawn('mysql -A -h {host} -P {port} -u {user} -p{password}'.format(**self.mysql_config), echo=False, encoding='utf-8', codec_errors='replace')
|
|
|
+ child = pexpect.spawn('mysql -A -h {host} -P {port} -u {user} -p{password}'.format(**self.mysql_config), echo=False, encoding=self.mysql_config['charset'], codec_errors='replace')
|
|
|
else:
|
|
|
child = pexpect.spawn('mysql -A -h {host} -P {port} -u {user}'.format(**self.mysql_config))
|
|
|
prompt_change = None
|
|
|
init_cmd = None
|
|
|
- self.wrapper = MysqlWrapper(child, u'>', prompt_change=prompt_change, extra_init_cmd=init_cmd, line_output_callback=self.process_output)
|
|
|
+ self.wrapper = MysqlWrapper(child, self.prompt, prompt_change=prompt_change, extra_init_cmd=init_cmd, line_output_callback=self.process_output)
|
|
|
finally:
|
|
|
signal.signal(signal.SIGINT, sig)
|
|
|
|
|
|
def process_output(self, output):
|
|
|
- if not self.silent:
|
|
|
-
|
|
|
- stream_content = {'name': 'stdout', 'text': output}
|
|
|
- self.send_response(self.iopub_socket, 'stream', stream_content)
|
|
|
+ if not self.silent and '|' in output:
|
|
|
+ output = output.decode(self.mysql_config['charset'])
|
|
|
+ output = MysqlParser(output)
|
|
|
+
|
|
|
+
|
|
|
+ display_content = {
|
|
|
+ 'source': 'kernel',
|
|
|
+ 'data': {
|
|
|
+ 'text/plain': output.text(),
|
|
|
+ 'text/html': output.html()
|
|
|
+ }, 'metadata': {}
|
|
|
+ }
|
|
|
+ self.send_response(self.iopub_socket, 'display_data', display_content)
|
|
|
|
|
|
def do_execute(self, code, silent, store_history=True,
|
|
|
user_expressions=None, allow_stdin=False):
|