-
-
Notifications
You must be signed in to change notification settings - Fork 107
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
feat: Implement a prepare_connection plugin hook #573
Conversation
@@ -1,6 +1,6 @@ | |||
from .db import Database | |||
from .utils import suggest_column_types | |||
from .hookspecs import hookimpl | |||
from .hookspecs import hookspec |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had to move the import Database
statement to below hookspecs
because of some weird circular errors:
Traceback (most recent call last):
File "/Users/alex/.local/share/virtualenvs/sqlite-utils-XlVivqEy/bin/sqlite-utils", line 33, in <module>
sys.exit(load_entry_point('sqlite-utils', 'console_scripts', 'sqlite-utils')())
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/alex/.local/share/virtualenvs/sqlite-utils-XlVivqEy/bin/sqlite-utils", line 25, in importlib_load_entry_point
return next(matches).load()
^^^^^^^^^^^^^^^^^^^^
File "/usr/local/Cellar/python@3.11/3.11.3/Frameworks/Python.framework/Versions/3.11/lib/python3.11/importlib/metadata/__init__.py", line 202, in load
module = import_module(match.group('module'))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/Cellar/python@3.11/3.11.3/Frameworks/Python.framework/Versions/3.11/lib/python3.11/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<frozen importlib._bootstrap>", line 1206, in _gcd_import
File "<frozen importlib._bootstrap>", line 1178, in _find_and_load
File "<frozen importlib._bootstrap>", line 1128, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
File "<frozen importlib._bootstrap>", line 1206, in _gcd_import
File "<frozen importlib._bootstrap>", line 1178, in _find_and_load
File "<frozen importlib._bootstrap>", line 1149, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 940, in exec_module
File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
File "/Users/alex/projects/sqlite-utils/sqlite_utils/__init__.py", line 2, in <module>
from .db import Database
File "/Users/alex/projects/sqlite-utils/sqlite_utils/db.py", line 40, in <module>
from .plugins import pm
File "/Users/alex/projects/sqlite-utils/sqlite_utils/plugins.py", line 10, in <module>
pm.load_setuptools_entrypoints("sqlite_utils")
File "/Users/alex/.local/share/virtualenvs/sqlite-utils-XlVivqEy/lib/python3.11/site-packages/pluggy/_manager.py", line 364, in load_setuptools_entrypoints
plugin = ep.load()
^^^^^^^^^
File "/usr/local/Cellar/python@3.11/3.11.3/Frameworks/Python.framework/Versions/3.11/lib/python3.11/importlib/metadata/__init__.py", line 202, in load
module = import_module(match.group('module'))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/Cellar/python@3.11/3.11.3/Frameworks/Python.framework/Versions/3.11/lib/python3.11/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/alex/.local/share/virtualenvs/sqlite-utils-XlVivqEy/lib/python3.11/site-packages/sqlite_utils_hello_world.py", line 4, in <module>
@sqlite_utils.hookimpl
^^^^^^^^^^^^^^^^^^^^^
AttributeError: partially initialized module 'sqlite_utils' has no attribute 'hookimpl' (most likely due to a circular import)
Not sure if there's something better to do here...
I was literally seconds away from shipping version 3.34 but I this looks good so I'm going to try and get it in there. |
Codecov ReportPatch coverage:
Additional details and impacted files@@ Coverage Diff @@
## main #573 +/- ##
==========================================
+ Coverage 96.09% 96.12% +0.03%
==========================================
Files 8 8
Lines 2794 2816 +22
==========================================
+ Hits 2685 2707 +22
Misses 109 109
☔ View full report in Codecov by Sentry. |
![]() Glitch in the rendered documentation from https://sqlite-utils--573.org.readthedocs.build/en/573/plugins.html#prepare-connection-conn |
Just like the Datasette prepare_connection hook, this PR adds a similar hook for the
sqlite-utils
plugin system.The sole argument is
conn
, since I don't believe adatabase
ordatasette
argument would be relevant here.I want to do this so I can release
sqlite-utils
plugins for my SQLite extensions, similar to the Datasette plugins I've release for them.An example plugin: https://gist.github.com/asg017/d7cdf0d56e2be87efda28cebee27fa3c
Refs:
prepare_connection()
plugin hook #574📚 Documentation preview 📚: https://sqlite-utils--573.org.readthedocs.build/en/573/