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

feat: Implement a prepare_connection plugin hook #573

Merged
merged 5 commits into from
Jul 22, 2023

Conversation

asg017
Copy link
Contributor

@asg017 asg017 commented Jul 22, 2023

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 a database or datasette 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

$ sqlite-utils install https://gist.github.com/asg017/d7cdf0d56e2be87efda28cebee27fa3c/archive/5f5ad549a40860787629c69ca120a08c32519e99.zip

$ sqlite-utils memory 'select hello("alex") as response'
[{"response": "Hello, alex!"}]

Refs:


📚 Documentation preview 📚: https://sqlite-utils--573.org.readthedocs.build/en/573/

@@ -1,6 +1,6 @@
from .db import Database
from .utils import suggest_column_types
from .hookspecs import hookimpl
from .hookspecs import hookspec
Copy link
Contributor Author

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...

@simonw
Copy link
Owner

simonw commented Jul 22, 2023

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
Copy link

codecov bot commented Jul 22, 2023

Codecov Report

Patch coverage: 100.00% and project coverage change: +0.03 🎉

Comparison is base (86a352f) 96.09% compared to head (faf398f) 96.12%.

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              
Impacted Files Coverage Δ
sqlite_utils/__init__.py 100.00% <100.00%> (ø)
sqlite_utils/db.py 97.36% <100.00%> (+0.01%) ⬆️
sqlite_utils/hookspecs.py 100.00% <100.00%> (ø)

... and 1 file with indirect coverage changes

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

@simonw
Copy link
Owner

simonw commented Jul 22, 2023

@simonw simonw linked an issue Jul 22, 2023 that may be closed by this pull request
@simonw
Copy link
Owner

simonw commented Jul 22, 2023

@simonw simonw merged commit 3f80a02 into simonw:main Jul 22, 2023
34 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

prepare_connection() plugin hook
2 participants