Running a script against Kotti

33 views
Skip to first unread message

Andreas Jung

unread,
Jan 18, 2016, 7:19:03 AM1/18/16
to Kotti
Hi there,

in Plone/Zope we have a mechanism for running a custom script against the database, something like

bin/instance run myscript.py

Inside the script we have access to the root application object 'app'.

Is there a similar way for doing the same in Kotti?

Is

http://kotti.readthedocs.org/en/latest/developing/advanced/as-a-library.html

relevant here?

The main purpose is being able to query the database on top of the configured
database connector and the DB model of Kotti.

Andreas

Andreas Kaiser

unread,
Jan 18, 2016, 7:44:22 AM1/18/16
to Kotti
On 18 Jan 2016, at 13:19, Andreas Jung wrote:

> in Plone/Zope we have a mechanism for running a custom script against
> the
> database, something like
>
> bin/instance run myscript.py
>
> Inside the script we have access to the root application object 'app'.
>
> Is there a similar way for doing the same in Kotti?

kotti.util.command
(https://github.com/Kotti/Kotti/blob/master/kotti/util.py#L338) can be
used to build custom command line scripts. It is used by Kotti for its
CLI scripts (for an example see
https://github.com/Kotti/Kotti/blob/master/kotti/workflow.py#L70).

If you like Click for writing CLI scripts, you can use that scaffold for
any Pyramid application (Kotti isn't special at all here):
https://gist.github.com/disko/38ccdd594a7aff16a386.

You can also use Pyramid's pshell <your.ini> for one time tasks, similar
to Plone's "bin/instance debug" (IIRC).


HTH,

Andreas


PS / Sidenode: I'm currently thinking about using Click for CLI scripts
in Kotti 2.x, as it has very nice features for building plugins. This
would allow to have a single 'kotti' command that does the application
setup and could be used by plugins (in Kotti itself as well as in
arbitrary add ons) to provide subcommands.

Andreas Jung

unread,
Jan 18, 2016, 9:17:41 AM1/18/16
to Kotti
Do you have a clue why


is spitting 
sqlalchemy.orm.exc.DetachedInstanceError: Parent instance <Document at 0x7f1d11128d90> is not bound to a Session; lazy load operation of attribute '_children' cannot proceed

here?

Andreas

Andreas Kaiser

unread,
Jan 18, 2016, 9:22:08 AM1/18/16
to Kotti
On 18 Jan 2016, at 15:17, Andreas Jung wrote:

> Do you have a clue why
>
> https://gist.github.com/zopyx/ad39ebfc0d6d48e92025
>
> is spitting
> sqlalchemy.orm.exc.DetachedInstanceError: Parent instance <Document at
> 0x7f1d11128d90> is not bound to a Session; lazy load operation of
> attribute
> '_children' cannot proceed
>
> here?

Yes. The transaction context manager starts a new transaction, so that
root isn't in the (new) SQLAlchemy session.

from kotti.resources import get_root

and

root = get_root()

within the transaction context manager scope should make that error
vanish.


Andreas

Andreas Jung

unread,
Jan 18, 2016, 9:37:31 AM1/18/16
to Kotti
Thanks, you're the best.

Andreas
Reply all
Reply to author
Forward
0 new messages