Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#7130 groovy magic expanded to other languages #7202

Merged
merged 2 commits into from
Apr 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions beakerx/beakerx/install.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,13 @@ def _install_magics():
with open(os.path.join(dir_path, 'ipython_config.py'), 'w+') as ipython_config:
ipython_config.write("c = get_config()\n")
ipython_config.write("c.InteractiveShellApp.extensions = ["
"'beakerx_magics.magic_kernel_comm',\n"
"'beakerx_magics.groovy_magic'\n"
"'beakerx_magics.kernel_magic',\n"
"'beakerx_magics.groovy_magic',\n"
"'beakerx_magics.clojure_magic',\n"
"'beakerx_magics.kotlin_magic',\n"
"'beakerx_magics.scala_magic',\n"
"'beakerx_magics.sql_magic',\n"
"'beakerx_magics.java_magic'\n"
"]\n")

def _set_conf_privileges():
Expand Down
7 changes: 6 additions & 1 deletion beakerx/beakerx_magics/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,10 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from .kernel_magic import *
from .clojure_magic import *
from .groovy_magic import *
from .magic_kernel_comm import *
from .java_magic import *
from .kotlin_magic import *
from .scala_magic import *
from .sql_magic import *
39 changes: 39 additions & 0 deletions beakerx/beakerx_magics/clojure_magic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Copyright 2017 TWO SIGMA OPEN SOURCE, LLC #
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from IPython import get_ipython
from IPython.core.magic import (magics_class, cell_magic)
from .kernel_magic import KernelMagics


@magics_class
class ClojureMagics(KernelMagics):

def __init__(self, shell):
super(ClojureMagics, self).__init__(shell)

def start(self):
super(ClojureMagics, self).start('clojure')

@cell_magic
def clojure(self, line, cell):
return self.run_cell(line, cell)


def load_ipython_extension(ipython):
ipython.register_magics(ClojureMagics)


if __name__ == '__main__':
ip = get_ipython()
ip.register_magics(ClojureMagics)
65 changes: 4 additions & 61 deletions beakerx/beakerx_magics/groovy_magic.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,76 +11,19 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from queue import Empty
from IPython import get_ipython
from IPython.core.magic import (Magics, magics_class, cell_magic)
from jupyter_client.manager import KernelManager
import atexit
from IPython.core.magic import (magics_class, cell_magic)
from .kernel_magic import KernelMagics


@magics_class
class GroovyMagics(Magics):
_execution_count = 1

def stop_kernel(self):
self.kc.stop_channels()
self.km.shutdown_kernel(now=True)
class GroovyMagics(KernelMagics):

def __init__(self, shell):
super(GroovyMagics, self).__init__(shell)
self.km = None
self.kc = None
self.comms = []

def start(self):
self.km = KernelManager()
self.km.kernel_name = 'groovy'
self.km.start_kernel()
atexit.register(self.stop_kernel)
self.kc = self.km.client()
self.kc.start_channels()
try:
self.kc.wait_for_ready()
print("Groovy started successfully\n")
except AttributeError:
self._wait_for_ready_backport()

def run_cell(self, line, code):
if not self.km:
self.start()
self.kc.execute(code, allow_stdin=True)
reply = self.kc.get_shell_msg()
self._handle_iopub_messages()

def _handle_iopub_messages(self):
while True:
try:
msg = self.kc.get_iopub_msg(timeout=1)
except Empty:
break
comm_id = msg['content'].get('comm_id')
if comm_id and comm_id not in self.comms:
self.comms.append(comm_id)
self.shell.kernel.session.send(self.shell.kernel.iopub_socket, msg['msg_type'],
msg['content'],
metadata=msg['metadata'],
parent=self.shell.kernel._parent_header,
ident=msg.get('comm_id'),
buffers=msg['buffers'],
)

def pass_message(self, msg_raw):
comm_id = msg_raw['content'].get('comm_id')
if comm_id in self.comms:
content = msg_raw['content']
msg = self.kc.session.msg(msg_raw['msg_type'], content)
self.kc.shell_channel.send(msg)
self._handle_iopub_messages()
else:
self.log.warn("No such comm: %s", comm_id)
if self.log.isEnabledFor(logging.DEBUG):
# don't create the list of keys if debug messages aren't enabled
self.log.debug("Current comms: %s", list(self.comms.keys()))
super(GroovyMagics, self).start('groovy')

@cell_magic
def groovy(self, line, cell):
Expand Down
39 changes: 39 additions & 0 deletions beakerx/beakerx_magics/java_magic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Copyright 2017 TWO SIGMA OPEN SOURCE, LLC #
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from IPython import get_ipython
from IPython.core.magic import (magics_class, cell_magic)
from .kernel_magic import KernelMagics


@magics_class
class JavaMagics(KernelMagics):

def __init__(self, shell):
super(JavaMagics, self).__init__(shell)

def start(self):
super(JavaMagics, self).start('java')

@cell_magic
def java(self, line, cell):
return self.run_cell(line, cell)


def load_ipython_extension(ipython):
ipython.register_magics(JavaMagics)


if __name__ == '__main__':
ip = get_ipython()
ip.register_magics(JavaMagics)
110 changes: 110 additions & 0 deletions beakerx/beakerx_magics/kernel_magic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
# Copyright 2017 TWO SIGMA OPEN SOURCE, LLC #
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from queue import Empty
from IPython import get_ipython
from IPython.core.magic import (Magics, magics_class, cell_magic)
from jupyter_client.manager import KernelManager
import atexit
import logging

@magics_class
class KernelMagics(Magics):
_execution_count = 1

def stop_kernel(self):
self.kc.stop_channels()
self.km.shutdown_kernel(now=True)

def __init__(self, shell):
super(KernelMagics, self).__init__(shell)
self.km = None
self.kc = None
self.comms = []

def start(self, kernel_name):
self.km = KernelManager()
self.km.kernel_name = kernel_name
self.km.start_kernel()
atexit.register(self.stop_kernel)
self.kc = self.km.client()
self.kc.start_channels()
try:
self.kc.wait_for_ready()
print("{} started successfully\n".format(kernel_name.capitalize()))
except AttributeError:
self._wait_for_ready_backport()

def run_cell(self, line, code):
if not self.km:
self.start()
self.kc.execute(code, allow_stdin=True)
reply = self.kc.get_shell_msg()
self._handle_iopub_messages()

def _handle_iopub_messages(self):
while True:
try:
msg = self.kc.get_iopub_msg(timeout=1)
except Empty:
break
comm_id = msg['content'].get('comm_id')
if comm_id and comm_id not in self.comms:
self.comms.append(comm_id)
self.shell.kernel.session.send(self.shell.kernel.iopub_socket, msg['msg_type'],
msg['content'],
metadata=msg['metadata'],
parent=self.shell.kernel._parent_header,
ident=msg.get('comm_id'),
buffers=msg['buffers'],
)

def pass_message(self, msg_raw):
comm_id = msg_raw['content'].get('comm_id')
if comm_id in self.comms:
content = msg_raw['content']
msg = self.kc.session.msg(msg_raw['msg_type'], content)
self.kc.shell_channel.send(msg)
self._handle_iopub_messages()
else:
self.log.warn("No such comm: %s", comm_id)
if self.log.isEnabledFor(logging.DEBUG):
# don't create the list of keys if debug messages aren't enabled
self.log.debug("Current comms: %s", list(self.comms.keys()))

def comm_msg(stream, ident, msg):
content = msg['content']
comm_id = content['comm_id']
comm_manager = get_ipython().kernel.comm_manager
comm = comm_manager.comms.get(comm_id)
if comm is None:
magic_registry = comm_manager.kernel.shell.magics_manager.registry
for magic in magic_registry.values():
if (hasattr(magic, 'pass_message') and comm_id in magic.comms):
try:
magic.pass_message(msg)
return
except Exception:
comm_manager.log.error('Exception in comm_msg for %s', comm_id, exc_info=True)
comm_manager.log.warn("No such comm: %s", comm_id)
if comm_manager.log.isEnabledFor(logging.DEBUG):
comm_manager.log.debug("Current comms: %s", list(comm_manager.comms.keys()))
else:
try:
comm.handle_msg(msg)
except Exception:
comm_manager.log.error('Exception in comm_msg for %s', comm_id, exc_info=True)


def load_ipython_extension(ipython):
ipython.kernel.shell_handlers['comm_msg'] = comm_msg
39 changes: 39 additions & 0 deletions beakerx/beakerx_magics/kotlin_magic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Copyright 2017 TWO SIGMA OPEN SOURCE, LLC #
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from IPython import get_ipython
from IPython.core.magic import (magics_class, cell_magic)
from .kernel_magic import KernelMagics


@magics_class
class KotlinMagics(KernelMagics):

def __init__(self, shell):
super(KotlinMagics, self).__init__(shell)

def start(self):
super(KotlinMagics, self).start('kotlin')

@cell_magic
def kotlin(self, line, cell):
return self.run_cell(line, cell)


def load_ipython_extension(ipython):
ipython.register_magics(KotlinMagics)


if __name__ == '__main__':
ip = get_ipython()
ip.register_magics(KotlinMagics)
42 changes: 0 additions & 42 deletions beakerx/beakerx_magics/magic_kernel_comm.py

This file was deleted.

Loading