1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
|
Pystache
========
|ci| |conda| |coverage| |bandit| |release|
|pre| |cov| |pylint|
|tag| |license| |python|
This updated fork of Pystache is currently tested on Python 3.8+ and in
Conda, on Linux, Macos, and Windows.
|logo|
`Pystache <https://github.com/PennyDreadfulMTG/pystache>`__ is a Python
implementation of `Mustache <https://github.com/mustache/mustache/>`__.
Mustache is a framework-agnostic, logic-free templating system inspired
by `ctemplate <https://code.google.com/p/google-ctemplate/>`__ and
et. Like ctemplate, Mustache "emphasizes separating logic from presentation:
it is impossible to embed application logic in this template language."
The `mustache(5) <https://mustache.github.io/mustache.5.html>`__ man
page provides a good introduction to Mustache's syntax. For a more
complete (and more current) description of Mustache's behavior, see the
official `Mustache spec <https://github.com/mustache/spec>`__.
Pystache is `semantically versioned <https://semver.org>`__ and older
versions can still be found on `PyPI <https://pypi.python.org/pypi/pystache>`__.
This version of Pystache now passes all tests in `version 1.1.3
<https://github.com/mustache/spec/tree/v1.1.3>`__ of the spec.
Requirements
============
Pystache is tested with:
- Python 3.8
- Python 3.9
- Python 3.10
- Python 3.11
- Python 3.12
- Python 3.13
- Conda (py38 and py310)
JSON support is needed only for the command-line interface and to run
the spec tests; PyYAML can still be used (see the Develop section).
Official support for Python 2 has ended with Pystache version 0.6.0.
.. note:: This project uses setuptools_scm_ to generate and maintain the
version file, which only gets included in the sdist/wheel
packages. In a fresh clone, running any of the tox_ commands
should generate the current version file.
.. _setuptools_scm: https://github.com/pypa/setuptools_scm
.. _tox: https://github.com/tox-dev/tox
Quick Start
===========
Be sure to get the latest release from either Pypi or Github.
Install It
----------
From Pypi::
$ pip install pystache
Or Github::
$ pip install -U pystache -f https://github.com/PennyDreadfulMTG/pystache/releases/
And test it::
$ pystache-test
To install and test from source (e.g. from GitHub), see the Develop
section.
Use It
------
Open a python console::
>>> import pystache
>>> print(pystache.render('Hi {{person}}!', {'person': 'Mom'}))
Hi Mom!
You can also create dedicated view classes to hold your view logic.
Here's your view class (in ../pystache/tests/examples/readme.py):
::
class SayHello(object):
def to(self):
return "Pizza"
Instantiating like so:
::
>>> from pystache.tests.examples.readme import SayHello
>>> hello = SayHello()
Then your template, say_hello.mustache (by default in the same directory
as your class definition):
::
Hello, {{to}}!
Pull it together:
::
>>> renderer = pystache.Renderer()
>>> print(renderer.render(hello))
Hello, Pizza!
For greater control over rendering (e.g. to specify a custom template
directory), use the ``Renderer`` class like above. One can pass
attributes to the Renderer class constructor or set them on a Renderer
instance. To customize template loading on a per-view basis, subclass
``TemplateSpec``. See the docstrings of the
`Renderer <https://github.com/PennyDreadfulMTG/pystache/blob/master/pystache/renderer.py>`__
class and
`TemplateSpec <https://github.com/PennyDreadfulMTG/pystache/blob/master/pystache/template_spec.py>`__
class for more information.
You can also pre-parse a template:
::
>>> parsed = pystache.parse(u"Hey {{#who}}{{.}}!{{/who}}")
>>> print(parsed)
['Hey ', _SectionNode(key='who', index_begin=12, index_end=18, parsed=[_EscapeNode(key='.'), '!'])]
And then:
::
>>> print(renderer.render(parsed, {'who': 'Pops'}))
Hey Pops!
>>> print(renderer.render(parsed, {'who': 'you'}))
Hey you!
Unicode
-------
This section describes how Pystache handles unicode, strings, and
encodings.
Internally, Pystache uses `only unicode strings`_ (``str`` in Python 3).
For input, Pystache accepts byte strings (``bytes`` in Python 3).
For output, Pystache's template rendering methods return only unicode.
.. _only unicode strings: https://docs.python.org/howto/unicode.html#tips-for-writing-unicode-aware-programs
Pystache's ``Renderer`` class supports a number of attributes to control
how Pystache converts byte strings to unicode on input. These include
the ``file_encoding``, ``string_encoding``, and ``decode_errors`` attributes.
The ``file_encoding`` attribute is the encoding the renderer uses to
convert to unicode any files read from the file system. Similarly,
``string_encoding`` is the encoding the renderer uses to convert any other
byte strings encountered during the rendering process into unicode (e.g.
context values that are encoded byte strings).
The ``decode_errors`` attribute is what the renderer passes as the
``errors`` argument to Python's built-in unicode-decoding function
(``str()`` in Python 3). The valid values for this argument are
``strict``, ``ignore``, and ``replace``.
Each of these attributes can be set via the ``Renderer`` class's
constructor using a keyword argument of the same name. See the Renderer
class's docstrings for further details. In addition, the ``file_encoding``
attribute can be controlled on a per-view basis by subclassing the
``TemplateSpec`` class. When not specified explicitly, these attributes
default to values set in Pystache's ``defaults`` module.
Develop
=======
To test from a source distribution (without installing)::
$ python test_pystache.py
To test Pystache with multiple versions of Python (with a single
command!) and different platforms, you can use [tox](https://pypi.python.org/pypi/tox)::
$ pip install tox
$ tox -e py
To run tests on multiple versions with coverage, run::
$ tox -e py38-linux,py39-linux # for example
(substitute your platform above, eg, macos or windows)
The source distribution tests also include doctests and tests from the
Mustache spec. To include tests from the Mustache spec in your test
runs::
$ git submodule update --init
The test harness parses the spec's (more human-readable) yaml files if
`PyYAML <http://pypi.python.org/pypi/PyYAML>`__ is present. Otherwise,
it parses the json files. To install PyYAML::
$ pip install pyyaml # note this is installed automatically by tox
Once the submodule is available, you can run the full test set with::
$ tox -e setup -- ext/spec/specs
Making Changes & Contributing
-----------------------------
We use the gitchangelog_ action to generate our github Release page, as
well as the gitchangelog message format to help it categorize/filter
commits for a tidier release page. Please use the appropriate ACTION
modifiers in any Pull Requests.
This repo is also pre-commit_ enabled for various linting and format
checks. The checks run automatically on commit and will fail the
commit (if not clean) with some checks performing simple file corrections.
If other checks fail on commit, the failure display should explain the error
types and line numbers. Note you must fix any fatal errors for the
commit to succeed; some errors should be fixed automatically (use
``git status`` and ``git diff`` to review any changes).
Note ``pylint`` is the primary check that requires your own input, as well
as a decision as to the appropriate fix action. You must fix any ``pylint``
warnings (relative to the baseline config score) for the commit to succeed.
See the following pages for more information on gitchangelog and pre-commit.
.. inclusion-marker-1
* generate-changelog_
* pre-commit-config_
* pre-commit-usage_
.. _generate-changelog: docs/source/dev/generate-changelog.rst
.. _pre-commit-config: docs/source/dev/pre-commit-config.rst
.. _pre-commit-usage: docs/source/dev/pre-commit-usage.rst
.. inclusion-marker-2
You will need to install pre-commit before contributing any changes;
installing it using your system's package manager is recommended,
otherwise install with pip into your usual virtual environment using
something like::
$ sudo emerge pre-commit --or--
$ pip install pre-commit
then install it into the repo you just cloned::
$ git clone https://github.com/PennyDreadfulMTG/pystache
$ cd pystache/
$ pre-commit install
It's usually a good idea to update the hooks to the latest version::
pre-commit autoupdate
.. _gitchangelog: https://github.com/sarnold/gitchangelog-action
.. _pre-commit: https://pre-commit.com/
Credits
=======
>>> import pystache
>>> context = { 'author': 'Chris Wanstrath', 'maintainer': 'Chris Jerdonek','refurbisher': 'Steve Arnold', 'new_maintainer': 'Thomas David Baker' }
>>> print(pystache.render("Author: {{author}}\nMaintainer: {{maintainer}}\nRefurbisher: {{refurbisher}}\nNew maintainer: {{new_maintainer}}", context))
Author: Chris Wanstrath
Maintainer: Chris Jerdonek
Refurbisher: Steve Arnold
New maintainer: Thomas David Baker
Pystache logo by `David Phillips <http://davidphillips.us/>`__ is
licensed under a `Creative Commons Attribution-ShareAlike 3.0 Unported
License <https://creativecommons.org/licenses/by-sa/3.0/deed.en_US>`__.
|ccbysa|
.. |ci| image:: https://github.com/PennyDreadfulMTG/pystache/actions/workflows/ci.yml/badge.svg
:target: https://github.com/PennyDreadfulMTG/pystache/actions/workflows/ci.yml
:alt: CI Status
.. |conda| image:: https://github.com/PennyDreadfulMTG/pystache/actions/workflows/conda.yml/badge.svg
:target: https://github.com/PennyDreadfulMTG/pystache/actions/workflows/conda.yml
:alt: Conda Status
.. |coverage| image:: https://github.com/PennyDreadfulMTG/pystache/actions/workflows/coverage.yml/badge.svg
:target: https://github.com/PennyDreadfulMTG/pystache/actions/workflows/coverage.yml
:alt: Coverage workflow
.. |bandit| image:: https://github.com/PennyDreadfulMTG/pystache/actions/workflows/bandit.yml/badge.svg
:target: https://github.com/PennyDreadfulMTG/pystache/actions/workflows/bandit.yml
:alt: Security check - Bandit
.. |release| image:: https://github.com/PennyDreadfulMTG/pystache/actions/workflows/release.yml/badge.svg
:target: https://github.com/PennyDreadfulMTG/pystache/actions/workflows/release.yml
:alt: Release Status
.. |cov| image:: https://raw.githubusercontent.com/PennyDreadfulMTG/pystache/badges/master/test-coverage.svg
:target: https://github.com/PennyDreadfulMTG/pystache/
:alt: Test coverage
.. |pylint| image:: https://raw.githubusercontent.com/PennyDreadfulMTG/pystache/badges/master/pylint-score.svg
:target: https://github.com/PennyDreadfulMTG/pystache/actions/workflows/pylint.yml
:alt: Pylint Score
.. |license| image:: https://img.shields.io/github/license/PennyDreadfulMTG/pystache
:target: https://github.com/PennyDreadfulMTG/pystache/blob/master/LICENSE
:alt: License
.. |tag| image:: https://img.shields.io/github/v/tag/PennyDreadfulMTG/pystache?color=green&include_prereleases&label=latest%20release
:target: https://github.com/PennyDreadfulMTG/pystache/releases
:alt: GitHub tag
.. |python| image:: https://img.shields.io/badge/python-3.6+-blue.svg
:target: https://www.python.org/downloads/
:alt: Python
.. |pre| image:: https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit&logoColor=white
:target: https://github.com/pre-commit/pre-commit
:alt: pre-commit
.. |logo| image:: gh/images/logo_phillips_small.png
.. |ccbysa| image:: https://i.creativecommons.org/l/by-sa/3.0/88x31.png
|