Skip to content

Commit

Permalink
refactor(magic): create singlestoredb.magics module
Browse files Browse the repository at this point in the history
  • Loading branch information
nunogoncalves03 committed Dec 9, 2024
1 parent 7b46bd9 commit c89f411
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 67 deletions.
4 changes: 0 additions & 4 deletions run_personal/__init__.py

This file was deleted.

4 changes: 0 additions & 4 deletions run_shared/__init__.py

This file was deleted.

34 changes: 34 additions & 0 deletions singlestoredb/magics/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from IPython.core.interactiveshell import InteractiveShell

from .run_personal import RunPersonalMagic
from .run_shared import RunSharedMagic

# In order to actually use these magics, we must register them with a
# running IPython.


def load_ipython_extension(ip: InteractiveShell) -> None:
"""
Any module file that define a function named `load_ipython_extension`
can be loaded via `%load_ext module.path` or be configured to be
autoloaded by IPython at startup time.
"""

# Load jupysql extension
# This is necessary for jupysql to initialize internal state
# required to render messages
assert ip.extension_manager is not None
result = ip.extension_manager.load_extension('sql')
if result == 'no load function':
raise RuntimeError('Could not load sql extension. Is jupysql installed?')

# Check if %run magic command is defined
if ip.find_line_magic('run') is None:
raise RuntimeError(
'%run magic command is not defined. '
'Is it available in your IPython environment?',
)

# Register run_personal and run_shared
ip.register_magics(RunPersonalMagic(ip))
ip.register_magics(RunSharedMagic(ip))
30 changes: 0 additions & 30 deletions run_personal/magic.py → singlestoredb/magics/run_personal.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,33 +55,3 @@ def run_personal(self, line: str, local_ns: Any = None) -> Any:
# Delete the local file after running it
if os.path.exists(local_filename):
os.remove(local_filename)


# In order to actually use these magics, you must register them with a
# running IPython.


def load_ipython_extension(ip: InteractiveShell) -> None:
"""
Any module file that define a function named `load_ipython_extension`
can be loaded via `%load_ext module.path` or be configured to be
autoloaded by IPython at startup time.
"""

# Load jupysql extension
# This is necessary for jupysql to initialize internal state
# required to render messages
assert ip.extension_manager is not None
result = ip.extension_manager.load_extension('sql')
if result == 'no load function':
raise RuntimeError('Could not load sql extension. Is jupysql installed?')

# Check if %run magic command is defined
if ip.find_line_magic('run') is None:
raise RuntimeError(
'%run magic command is not defined. '
'Is it available in your IPython environment?',
)

# Register run_personal
ip.register_magics(RunPersonalMagic(ip))
29 changes: 0 additions & 29 deletions run_shared/magic.py → singlestoredb/magics/run_shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,32 +53,3 @@ def run_shared(self, line: str, local_ns: Any = None) -> Any:
# Delete the local file after running it
if os.path.exists(local_filename):
os.remove(local_filename)

# In order to actually use these magics, you must register them with a
# running IPython.


def load_ipython_extension(ip: InteractiveShell) -> None:
"""
Any module file that define a function named `load_ipython_extension`
can be loaded via `%load_ext module.path` or be configured to be
autoloaded by IPython at startup time.
"""

# Load jupysql extension
# This is necessary for jupysql to initialize internal state
# required to render messages
assert ip.extension_manager is not None
result = ip.extension_manager.load_extension('sql')
if result == 'no load function':
raise RuntimeError('Could not load sql extension. Is jupysql installed?')

# Check if %run magic command is defined
if ip.find_line_magic('run') is None:
raise RuntimeError(
'%run magic command is not defined. '
'Is it available in your IPython environment?',
)

# Register run_shared
ip.register_magics(RunSharedMagic(ip))

0 comments on commit c89f411

Please sign in to comment.