By default, pyramid is provided with a command that takes a configuration file as an argument when it is executed. (e.g. pserve, proutes ...) The existing commands do more than just read the contents of the config file with a parser.
I want to do the same when I create a new command myself. In such a case, use the function under pyramid.paster.bootstrap.
pyramid.paster.bootstrap
pyramid.paster.bootstrap takes the path of the configuration file as an argument and executes the function (usually main () of app) registered at the entry point written in the configuration file. Returns a dictionary with the return value app etc. after execution.
When development.ini is written as follows
[app:main]
use = egg:qiita
Because the entry point is registered in setup.py.
setup(name='qiita',
...
entry_points="""\
[paste.app_factory]
main = qiita:main
""",
...
)
qiita.main () is called.
How to create a command to read a configuration file with the following file structure.
$ tree
.
|-- development.ini
|-- production.ini
|-- qiita
| |-- __init__.py
| |-- scripts.py
| `-- views.py
`-- setup.py
Write the implementation of the command to be created in qiita / scripts.py. Created as follows using bootstrap. This time, the command just prints the return value of bootstrap.
# -*- coding:utf-8 -*-
from pyramid.paster import bootstrap
import sys
import pprint
def sample(args=sys.argv):
config_path = sys.argv[1]
env = bootstrap(config_path)
pprint.pprint(env)
Add the description of console_scripts to setup.py. I will register it with the name qiita-sample.
setup(name='qiita',
...
entry_points="""\
[paste.app_factory]
main = qiita:main
[console_scripts]
qiita-sample = qiita.scripts:sample
""",
...
)
After re-installing with setup.py, you can use the registered qiita-sample. If you specify the configuration file and execute qiita-sample, you can get the environment where the app is loaded.
$ python setup.py develop
$ qiita-sample development.ini
{'app': <pyramid.router.Router object at 0x10edd2990>,
'closer': <function prepare.<locals>.closer at 0x10f30e440>,
'registry': <Registry qiita>,
'request': <Request at 0x10e140d90 GET http://localhost/>,
'root': <pyramid.traversal.DefaultRootFactory object at 0x10ede9f10>,
'root_factory': <class 'pyramid.traversal.DefaultRootFactory'>}