Steve Piercy
2017-10-22 4567204570eff25408278fd01919c1b048b9f7f1
commit | author | age
b731b5 1 .. _qtut_ini:
PE 2
b1b922 3 =================================================
PE 4 03: Application Configuration with ``.ini`` Files
5 =================================================
6
7 Use Pyramid's ``pserve`` command with a ``.ini`` configuration file for
8 simpler, better application running.
9
8e6520 10
b1b922 11 Background
PE 12 ==========
13
8e6520 14 Pyramid has a first-class concept of :ref:`configuration <configuration_narr>`
SP 15 distinct from code. This approach is optional, but its presence makes it
16 distinct from other Python web frameworks. It taps into Python's ``setuptools``
17 library, which establishes conventions for installing and providing "entry
18 points" for Python projects. Pyramid uses an entry point to let a Pyramid
19 application know where to find the WSGI app.
20
b1b922 21
PE 22 Objectives
23 ==========
24
8e6520 25 - Modify our ``setup.py`` to have an entry point telling Pyramid the location
SP 26   of the WSGI app.
b1b922 27
8e6520 28 - Create an application driven by an ``.ini`` file.
b1b922 29
8e6520 30 - Start the application with Pyramid's ``pserve`` command.
b1b922 31
8e6520 32 - Move code into the package's ``__init__.py``.
SP 33
b1b922 34
PE 35 Steps
36 =====
37
38 #. First we copy the results of the previous step:
39
40    .. code-block:: bash
41
187104 42     $ cd ..; cp -r package ini; cd ini
b1b922 43
8e6520 44 #. Our ``ini/setup.py`` needs a setuptools "entry point" in the ``setup()``
SP 45    function:
b1b922 46
PE 47    .. literalinclude:: ini/setup.py
48     :linenos:
49
8e6520 50 #. We can now install our project, thus generating (or re-generating) an "egg"
SP 51    at ``ini/tutorial.egg-info``:
b1b922 52
PE 53    .. code-block:: bash
54
21fd51 55     $ $VENV/bin/pip install -e .
b1b922 56
PE 57 #. Let's make a file ``ini/development.ini`` for our configuration:
58
59    .. literalinclude:: ini/development.ini
60     :language: ini
61     :linenos:
62
8e6520 63 #. We can refactor our startup code from the previous step's ``app.py`` into
SP 64    ``ini/tutorial/__init__.py``:
b1b922 65
PE 66    .. literalinclude:: ini/tutorial/__init__.py
67     :linenos:
68
69 #. Now that ``ini/tutorial/app.py`` isn't used, let's remove it:
70
71    .. code-block:: bash
72
187104 73     $ rm tutorial/app.py
b1b922 74
PE 75 #. Run your Pyramid application with:
76
77    .. code-block:: bash
78
187104 79     $ $VENV/bin/pserve development.ini --reload
b1b922 80
d749bf 81 #. Open http://localhost:6543/.
b1b922 82
PE 83 Analysis
84 ========
85
8e6520 86 Our ``development.ini`` file is read by ``pserve`` and serves to bootstrap our
SP 87 application. Processing then proceeds as described in the Pyramid chapter on
4042c7 88 :ref:`application startup <startup_chapter>`:
b1b922 89
8e6520 90 - ``pserve`` looks for ``[app:main]`` and finds ``use = egg:tutorial``.
b1b922 91
456720 92 - The projects's ``setup.py`` has defined an "entry point" (lines 10-13) for the
8e6520 93   project's "main" entry point of ``tutorial:main``.
b1b922 94
8e6520 95 - The ``tutorial`` package's ``__init__`` has a ``main`` function.
b1b922 96
8e6520 97 - This function is invoked, with the values from certain ``.ini`` sections
SP 98   passed in.
b1b922 99
PE 100 The ``.ini`` file is also used for two other functions:
101
456720 102 - *Configuring the WSGI server*. ``[server:main]`` wires up the choice
SP 103   of which WSGI *server* for your WSGI *application*. In this case, we
104   are using ``waitress`` which was specified in
105   ``tutorial/setup.py``. It also wires up the *port number*:
106   ``listen = localhost:6543`` tells ``waitress`` to listen on host
107   ``localhost`` at port ``6543``.
b1b922 108
3c6ea2 109 - *Configuring Python logging*. Pyramid uses Python standard logging, which
TS 110   needs a number of configuration values. The ``.ini`` serves this function.
b1b922 111   This provides the console log output that you see on startup and each
PE 112   request.
113
114 We moved our startup code from ``app.py`` to the package's
8e6520 115 ``tutorial/__init__.py``. This isn't necessary, but it is a common style in
SP 116 Pyramid to take the WSGI app bootstrapping out of your module's code and put it
117 in the package's ``__init__.py``.
b1b922 118
8e6520 119 The ``pserve`` application runner has a number of command-line arguments and
SP 120 options. We are using ``--reload`` which tells ``pserve`` to watch the
121 filesystem for changes to relevant code (Python files, the INI file, etc.) and,
122 when something changes, restart the application. Very handy during development.
123
b1b922 124
65687f 125 Extra credit
b1b922 126 ============
PE 127
8e6520 128 #. If you don't like configuration and/or ``.ini`` files, could you do this
SP 129    yourself in Python code?
b1b922 130
8e6520 131 #. Can we have multiple ``.ini`` configuration files for a project? Why might
SP 132    you want to do that?
b1b922 133
8e6520 134 #. The entry point in ``setup.py`` didn't mention ``__init__.py`` when it
SP 135    declared ``tutorial:main`` function. Why not?
b1b922 136
24c406 137 #. What is the purpose of ``**settings``? What does the ``**`` signify?
SP 138
b1b922 139 .. seealso::
4042c7 140    :ref:`project_narr`,
b52d1a 141    :ref:`cookiecutters`,
4042c7 142    :ref:`what_is_this_pserve_thing`,
PE 143    :ref:`environment_chapter`,
144    :ref:`paste_chapter`