Skip to content

Commit

Permalink
[修改]register magic
Browse files Browse the repository at this point in the history
  • Loading branch information
longfengpili committed Aug 12, 2024
1 parent e1a1154 commit bf3852c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 27 deletions.
23 changes: 1 addition & 22 deletions pydbapi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@
# @Author: chunyang.xu
# @Date: 2023-06-02 15:27:41
# @Last Modified by: longfengpili
# @Last Modified time: 2024-08-12 13:45:06
# @Last Modified time: 2024-08-12 13:49:40
# @github: https://github.com/longfengpili


import os
import logging.config
from pathlib import Path

from pydbapi.api.pydbmagics import PydbapiMagics
from pydbapi.conf import LOGGING_CONFIG
logging.config.dictConfig(LOGGING_CONFIG)

Expand All @@ -21,22 +19,3 @@

# __all__ = ['SqliteDB', 'RedshiftDB', 'MysqlDB', 'SnowflakeDB',
# 'SqlParse', 'SqlCompile', 'SqlFileParse', 'ColumnModel', 'ColumnsModel']


# 注册magic命令
def load_ipython_extension(ipython):
ipython.register_magics(PydbapiMagics)


def register_magic():
# IPython 启动目录及脚本
ipython_startup_dir = Path.home() / ".ipython" / "profile_default" / "startup"
startup_script_path = ipython_startup_dir / "00-pydbapi-startup.py"

# 确保启动目录存在
if not os.path.exists(ipython_startup_dir):
os.makedirs(ipython_startup_dir)

# 写入启动脚本
with open(startup_script_path, 'w') as f:
f.write("get_ipython().run_line_magic('load_ext', 'pydbapi')\n")
23 changes: 21 additions & 2 deletions pydbapi/api/pydbmagics.py → pydbapi/api/pydbapimagic.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
# @Author: longfengpili
# @Date: 2023-11-02 13:36:08
# @Last Modified by: longfengpili
# @Last Modified time: 2024-08-06 10:50:05
# @Last Modified time: 2024-08-12 13:48:05
# @github: https://github.com/longfengpili


from pathlib import Path
import pandas as pd

from IPython.core.error import UsageError
Expand All @@ -29,6 +29,25 @@
from pydbapi.api import SqliteDB, MysqlDB, RedshiftDB, TrinoDB


# 注册magic命令
def load_ipython_extension(ipython):
ipython.register_magics(PydbapiMagics)


def register_magic():
# IPython 启动目录及脚本
ipython_startup_dir = Path.home() / ".ipython" / "profile_default" / "startup"
startup_script_path = ipython_startup_dir / "00-pydbapi-startup.py"

# 确保启动目录存在
if not ipython_startup_dir.exists():
ipython_startup_dir.mkdir(parents=True)

# 写入启动脚本
with open(startup_script_path, 'w') as f:
f.write("get_ipython().run_line_magic('load_ext', 'pydbapi')\n")


@magics_class
class PydbapiMagics(Magics):
dbtype = Unicode('trino',
Expand Down
6 changes: 3 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# @Author: longfengpili
# @Date: 2023-06-02 15:27:41
# @Last Modified by: longfengpili
# @Last Modified time: 2024-08-12 13:45:34
# @Last Modified time: 2024-08-12 13:50:18
# @github: https://github.com/longfengpili


Expand Down Expand Up @@ -54,10 +54,10 @@ def run(self):
install_requires=requires,
entry_points={
'ipython.extensions': [
'pydbapi = pydbapi:load_ipython_extension',
'pydbapi = pydbapi.api.pydbapimagic:load_ipython_extension',
],
'console_scripts': [
'pydbapimagic = pydbapi:register_magic',
'pydbapimagic = pydbapi.api.pydbapimagic:register_magic',
],
},
classifiers=[
Expand Down

0 comments on commit bf3852c

Please sign in to comment.