|
@@ -0,0 +1,97 @@
|
|
|
+
|
|
|
+
|
|
|
+"""
|
|
|
+ jupyter-mysql-kernel
|
|
|
+ author:rabin
|
|
|
+"""
|
|
|
+import json
|
|
|
+import os
|
|
|
+import re
|
|
|
+import signal
|
|
|
+
|
|
|
+from ipykernel.kernelbase import Kernel
|
|
|
+import pexpect
|
|
|
+
|
|
|
+class MysqlKernel(Kernel):
|
|
|
+ implementation = 'Mysql'
|
|
|
+ implementation_version = '1.0'
|
|
|
+ language = 'mysql'
|
|
|
+ language_version = 'm1.0'
|
|
|
+ language_info = {
|
|
|
+ 'name': 'mysql'
|
|
|
+ ,'mimetype': 'text/plain'
|
|
|
+ ,'file_extension': '.sql'
|
|
|
+ }
|
|
|
+ banner = 'Mysql kernel'
|
|
|
+
|
|
|
+ mysql_setting_file = os.path.join(os.path.expanduser('~'), '.local/config/mysql_config.json')
|
|
|
+ mysql_config = {
|
|
|
+ 'prompt' : '>'
|
|
|
+ ,'user' : 'root'
|
|
|
+ ,'host' : '192.168.15.10'
|
|
|
+ ,'port' : '3309'
|
|
|
+ ,'charset' : 'utf8'
|
|
|
+ ,'password' : '123456'
|
|
|
+ }
|
|
|
+
|
|
|
+ def __init__(self, *args, **kwargs):
|
|
|
+ super(MysqlKernel, self).__init__(*args, **kwargs)
|
|
|
+ if os.path.exists(self.mysql_setting_file):
|
|
|
+ with open(self.mysql_setting_file,"r") as f:
|
|
|
+ self.mysql_config.update(json.load(f))
|
|
|
+ self.prompt = self.mysql_config["prompt"]
|
|
|
+ self.start()
|
|
|
+
|
|
|
+ def start(self):
|
|
|
+ if 'password' in self.mysql_config:
|
|
|
+ self.process = pexpect.spawn('mysql -A -h {host} -P {port} -u {user} -p'.format(**self.mysql_config))
|
|
|
+ self.process.expect(':')
|
|
|
+ self.process.sendline(self.mysql_config['password'])
|
|
|
+ else:
|
|
|
+ self.process = pexpect.spawn('mysql -A -h {host} -P {port} -u {user}'.format(**self.mysql_config))
|
|
|
+ if self.process:
|
|
|
+ self.process.expect(self.prompt)
|
|
|
+
|
|
|
+
|
|
|
+ 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': {}
|
|
|
+ }
|
|
|
+
|
|
|
+ msg = ''
|
|
|
+ for c in code.split("\n"):
|
|
|
+ c = c.strip()
|
|
|
+ if len(c) > 0:
|
|
|
+ if c[0] == '#':
|
|
|
+ continue
|
|
|
+ msg += c + ';'
|
|
|
+ msg = msg.strip()
|
|
|
+
|
|
|
+ self.process.sendline(msg)
|
|
|
+ self.process.expect(self.prompt)
|
|
|
+ output = self.process.before
|
|
|
+ result = output.decode(self.mysql_config['charset'])
|
|
|
+ self.out(result)
|
|
|
+ return
|
|
|
+ {
|
|
|
+ 'status': 'ok',
|
|
|
+ 'execution_count': self.execution_count,
|
|
|
+ 'payload': [],
|
|
|
+ 'user_expressions': {}
|
|
|
+ }
|
|
|
+
|
|
|
+ def out(self, content):
|
|
|
+ stream_content = {'name': 'stdout', 'text': result}
|
|
|
+ self.send_response(self.iopub_socket, 'stream', stream_content)
|
|
|
+ def err(self, content):
|
|
|
+ stream_content = {'name': 'stderr', 'text': result}
|
|
|
+ self.send_response(self.iopub_socket, 'stream', stream_content)
|
|
|
+
|
|
|
+if __name__ == '__main__':
|
|
|
+ from ipykernel.kernelapp import IPKernelApp
|
|
|
+ IPKernelApp.launch_instance(kernel_class=MysqlKernel)
|