Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Add new CLI command: ckan shell#6919

Merged
Zharktas merged 13 commits into
ckan:masterfrom
pdelboca:add-new-shell-command
Aug 9, 2022
Merged

Add new CLI command: ckan shell#6919
Zharktas merged 13 commits into
ckan:masterfrom
pdelboca:add-new-shell-command

Conversation

@pdelboca
Copy link
Copy Markdown
Member

@pdelboca pdelboca commented Jun 17, 2022

This new command is inspired by Flask's flask shell and Django's manage.py shell commands. The main goal is to execute a IPython session with the application loaded for easy debugging and dynamic coding.

This provides several advantages like direct access to the model or logic layer to interact directly with all the services configured in our config files.

There are three variables already populated into the namespace of the shell:

  • app containing the Flask application
  • config containing the CKAN config dictrionary
  • model module to access to the database using SQLAlchemy syntax

Example 1:

~/Repos/ckan (add-new-shell-command)$ ckan shell
Python 3.9.13 (main, May 23 2022, 21:57:12) 
Type 'copyright', 'credits' or 'license' for more information
IPython 8.2.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: model.User.all()
Out[1]: 
[<User id=f48287e2-6fac-41a9-9170-fc25ddbcc2d7 name=default password=$pbkdf2-sha512$25000$4rzXure2NkYoBeA8h5DyHg$yKMLOBZCtY.bA5XYq/qhzXfNCO7QOHGuRSkvCjkE2wThE.km/2L6GwQbY4p4lFXyyRMYXnACLxXvR27rVDq/yw fullname=None email=None apikey=46a0b1cc-28f3-4f96-9cf2-f0479fd3f200 created=2022-06-08 12:54:20.344765 reset_key=None about=None last_active=None activity_streams_email_notifications=False sysadmin=True state=active image_url=None plugin_extras=None>,
 <User id=f0c04c11-4369-4cf1-9da4-69d9aae06a2e name=admin password=$pbkdf2-sha512$25000$0dp7L4UQQoiRsjYmhHBOaQ$FHgZEstFVp6WgR6qZSqqHk6kGGleLIRPijserkkKNoqGKcDFOq2tjf7IPKVI.YNIQfl4KUMhcLMYjZQ6CW3BSw fullname=My Cool Admin email=admin@ckan.org apikey=None created=2022-06-08 12:54:20.971423 reset_key=None about= last_active=2022-06-17 13:30:19.456468 activity_streams_email_notifications=False sysadmin=True state=active image_url= plugin_extras=None>,
 <User id=38b4b316-e0d6-461e-b38e-ab31491c16e9 name=test_user password=$pbkdf2-sha512$25000$GsOY8x4jpNT6P0fo3bvXug$wu8z18RGGWbFi4LHAbTewqZZXaB.KCQ7qybYKwWlqxhiuH2zpuU6kM7JE/JvcNKxspXLGaVjWQFaz3W6VMmFKg fullname=None email=test_user@ckan.org apikey=None created=2022-06-08 12:55:00.139126 reset_key=None about=None last_active=2022-06-16 12:47:40.288937 activity_streams_email_notifications=False sysadmin=False state=active image_url=None plugin_extras=None>]

In [2]: from ckan.logic.action.get import package_show

In [3]: package_show({"model": model}, {"id": "api-package-1"})
Out[3]: 
{'author': None,
 'author_email': None,
 'creator_user_id': 'f0c04c11-4369-4cf1-9da4-69d9aae06a2e',
 'id': '922f3a91-c9ed-4e19-a722-366671b7d72c',
 'isopen': False,
 'license_id': None,
 'license_title': None,
 'maintainer': None,
 'maintainer_email': None,
 'metadata_created': '2022-06-16T14:13:37.736125',
 'metadata_modified': '2022-06-16T14:20:19.639665',
 'name': 'api-package-1',
 'notes': 'Update from API:10000',
 'num_resources': 0,
 'num_tags': 0,
 'organization': None,
 'owner_org': None,
 'private': False,
 'state': 'active',
 'title': 'api-package-1',
 'type': 'dataset',
 'url': None,
 'version': None,
 'resources': [],
 'tags': [],
 'extras': [],
 'groups': [],
 'relationships_as_subject': [],
 'relationships_as_object': []}

Example 2:

I can also do some quick benchmarking of functions to test out new implementations:

In [7]: from ckanext.activity.logic import action

In [8]: before = datetime.fromisoformat('2022-06-16T14:14:00.627446').timestamp()

In [9]: %timeit action.package_activity_list({}, {'id': 'api-package-1', 'before': before})
3.17 ms ± 11.9 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

In [10]: %timeit action.package_activity_list({}, {'id': 'api-package-1', 'offset': 9000})
25.3 ms ± 504 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)

I think this command will improve the CKAN development experience.

@pdelboca pdelboca changed the title Add new shell CLI command Add new CLI command: ckan shell Jun 17, 2022
@amercader
Copy link
Copy Markdown
Member

Yes!! Pylons used to have this and it was super useful

Comment thread ckan/cli/shell.py Outdated
@pdelboca
Copy link
Copy Markdown
Member Author

I've implemented the feedback @Zharktas ! Let me know if anything else is needed :)

@amercader amercader added this to the CKAN 2.10 milestone Jun 29, 2022
@Zharktas
Copy link
Copy Markdown
Member

Zharktas commented Aug 9, 2022

@pdelboca want to add changelog entry for this, I think otherwise this is good to go.

@Zharktas
Copy link
Copy Markdown
Member

Zharktas commented Aug 9, 2022

Failing lint and tests are in master, i'll merge this once everything else is green :)

@Zharktas Zharktas merged commit da539e9 into ckan:master Aug 9, 2022
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.

3 participants