App context inside Command

14 views
Skip to first unread message

Bastien Sevajol

unread,
Jan 28, 2016, 3:35:55 AM1/28/16
to TurboGears
Hi,

I can't access to app configuration inside my command:

# -*- coding: utf-8 -*-
from gearbox.command import Command
from tg import config

class CrawlCommand(Command):
   
def take_action(self, parsed_args):
       
print(config.get('sqlalchemy.url'))

produce:

> gearbox crawl        
None

I would like be able to read app config like in tgshell:

> gearbox tgshell
>>> from tg import config
>>> config.get('sqlalchemy.url')
'postgresql://pyjobs:pyjobs@localhost/pyjobs'

How to load environnement/config inside gearbox.command.Command ?

Thanks,
Bastien.

Alessandro Molina

unread,
Jan 28, 2016, 10:57:15 AM1/28/16
to TurboGears
The magic lines you are looking for are https://github.com/TurboGears/tg2devtools/blob/master/devtools/gearbox/tgshell.py#L50-L54

Performing a request to /_test_vars makes a fake request that forces application initialization and loads the TurboGears context in memory, so you will have tg.config, tg.request and so on available. Much of what is explained in http://turbogears.readthedocs.org/en/latest/turbogears/testing.html#testing-outside-controllers also applies perfectly to writing scripts (like gearbox commands) that use TG.


--
You received this message because you are subscribed to the Google Groups "TurboGears" group.
To unsubscribe from this group and stop receiving emails from it, send an email to turbogears+...@googlegroups.com.
To post to this group, send email to turbo...@googlegroups.com.
Visit this group at https://groups.google.com/group/turbogears.
For more options, visit https://groups.google.com/d/optout.

Bastien Sevajol

unread,
Jan 29, 2016, 6:13:29 AM1/29/16
to turbo...@googlegroups.com
Hi,

Perfect. I used it with success.

Thank you !
You received this message because you are subscribed to a topic in the Google Groups "TurboGears" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/turbogears/sQc_YvfOocU/unsubscribe.
To unsubscribe from this group and all its topics, send an email to turbogears+...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages