[PYTHON] How to make a command to read the configuration file with pyramid

How to make a command to read the configuration file with pyramid

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.

Function called in bootstrap

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

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'>}

reference

Recommended Posts

How to make a command to read the configuration file with pyramid
How to read a CSV file with Python 2/3
How to switch the configuration file to be read by Python
How to output the output result of the Linux man command to a file
How to make a dictionary with a hierarchical structure.
[Python] How to read excel file with pandas
I read "How to make a hacking lab"
Save the object to a file with pickle
How to read a file in a different directory
How to write a GUI using the maya command
How to delete the specified string with the sed command! !! !!
Try to make a command standby tool with python
How to make a shooting game with toio (Part 1)
Zip-compress any file with the [shell] command to create a file and delete the original file.
How to make a 3D geometric figure with one click [From triangular pyramid to fractal]
How to make a Cisco Webex Teams BOT with Flask
How to put a hyperlink to "file: // hogehoge" with sphinx-> pdf
How to run a Python file at a Windows 10 command prompt
How to make a simple Flappy Bird game with pygame
A story about how to deal with the CORS problem
How to use a file other than .fabricrc as a configuration file
How to read an Excel file (.xlsx) with Pandas [Python]
How to make a Japanese-English translation
How to make a slack bot
How to read the SNLI dataset
How to make a crawler --Advanced
How to make a recursive function
How to make a deadman's switch
[Blender] How to make a Blender plugin
How to make a crawler --Basic
How to create a config file
[sh] How to store the command execution result in a variable
Make it possible to output a log to a file with go echo
[Introduction to Python] How to split a character string with the split function
How to make a surveillance camera (Security Camera) with Opencv and Python
Read a file in Python with a relative path from the program
How to monitor the execution status of sqlldr with the pv command
[ROS2] How to play a bag file with python format launch
How to send a request to the DMM (FANZA) API with python
How to upload a file to Cloud Storage using Python [Make a fixed point camera with Raspberry PI # 1]
How to add a package with PyCharm
[Python] How to make a class iterable
[Linux] How to use the echo command
How to use the Linux grep command
Let's read the RINEX file with Python ①
How to use CUT command (with sample)
How to make a Backtrader custom indicator
Try rewriting the file with the less command
How to make a Pelican site map
Read a character data file with numpy
How to read problem data with paiza
I made a configuration file with Python
Replace the directory name and the file name in the directory together with a Linux command.
Think about how to write a filter with the Shotgun API-Contact Versions
[Python] Explains how to use the range function with a concrete example
[Introduction to Python] How to sort the contents of a list efficiently with list sort
[Image recognition] How to read the result of automatic annotation with VoTT
[LPIC 101] How to specify the disk partition number in the GRUB configuration file
How to put a line number at the beginning of a CSV file
[Python] How to read a csv file (read_csv method of pandas module)
How to fix the initial population with a genetic algorithm using DEAP