from ipykernel.kernelbase import Kernelfrom pexpect import replwrap, EOFfrom subprocess import check_outputimport reimport 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 toterminate 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 promptif self._expect_prompt(timeout=timeout) == 1:# We got the continuation prompt - command was incompleteself.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__@propertydef language_version(self):m = version_pat.search(self.banner)return m.group(1)_banner = None@propertydef banner(self):if self._banner is None:self._banner = check_output(['sqlite3', '--version']).decode('utf-8')return self._bannerlanguage_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 interruptablesig = 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 = Falsetry:output = self.sqlitewrapper.run_command(code.rstrip())except KeyboardInterrupt:self.sqlitewrapper.child.sendintr()interrupted = Trueself.sqlitewrapper._expect_prompt()output = self.sqlitewrapper.child.beforeexcept EOF:output = self.sqlitewrapper.child.before + 'Restarting SQLite3'self._start_sqlite()if not silent:# Send standard outputstream_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': {}}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。