Skip to content

Commit

Permalink
Document load file regex (#5173)
Browse files Browse the repository at this point in the history
* update load_file_regex documentation

* improve load_file_regex documentation

* Punctuation and verb tense consistency

* Apply suggestions from code review

Co-authored-by: Katherine Kinnaman <kkinnaman@anaconda.com>

---------

Co-authored-by: Katherine Kinnaman <kkinnaman@anaconda.com>
  • Loading branch information
dholth and kathatherine authored Feb 9, 2024
1 parent c4c3449 commit 3a4e576
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions docs/source/resources/define-metadata.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1756,11 +1756,11 @@ retrieve a fully rendered ``meta.yaml``, use the
Loading data from other files
-----------------------------

There are several additional functions available to Jinja2 which can be used
There are several additional functions available to Jinja2, which can be used
to load data from other files. These are ``load_setup_py_data``, ``load_file_regex``,
``load_file_data``, and ``load_str_data``.

* ``load_setup_py_data``: Loads data from a ``setup.py`` file. This can be useful to
* ``load_setup_py_data``: Load data from a ``setup.py`` file. This can be useful to
obtain metadata such as the version from a project's ``setup.py`` file. For example::

{% set data = load_setup_py_data() %}
Expand All @@ -1769,16 +1769,23 @@ to load data from other files. These are ``load_setup_py_data``, ``load_file_reg
name: foo
version: {{ version }}

* ``load_file_regex``: Searches a file for a regular expression and returns the
first match as a Python ``re.Match object``. For example::
* ``load_file_regex``: Search a file for a regular expression returning the
first match as a Python `re.Match
<https://docs.python.org/3/library/re.html#match-objects>`_ object.

For example, using ``load_file_regex(load_file, regex_pattern, from_recipe_dir=False) -> re.Match | None``::

{% set version_match = load_file_regex(
load_file="conda_package_streaming/__init__.py",
regex_pattern='^__version__ = "(.+)"') %}
{% set version = version_match[1] %}

{% set readme_heading = load_file_regex(load_file='README.rst', regex_pattern=r'^# (\S+)') %}
package:
name: {{ readme_heading.string }}
version: {{ version }}

* ``load_file_data``: You can also parse JSON, TOML, or YAML files and load data
from them. For example you can use this to load poetry configurations from
``pyproject.toml``. This is especially useful as ``setup.py`` is no longer the
* ``load_file_data``: Parse JSON, TOML, or YAML files and load data
from them. For example, you can use this to load poetry configurations from
``pyproject.toml``. This is especially useful, as ``setup.py`` is no longer the
only standard way to define project metadata (see
`PEP 517 <https://peps.python.org/pep-0517>`_ and
`PEP 518 <https://peps.python.org/pep-0518>`_)::
Expand All @@ -1789,7 +1796,7 @@ to load data from other files. These are ``load_setup_py_data``, ``load_file_reg
name: {{ poetry.get('name') }}
version: {{ poetry.get('version') }}

* ``load_str_data``: Loads and parses data from a string. This is similar to
* ``load_str_data``: Load and parse data from a string. This is similar to
``load_file_data``, but it takes a string instead of a file as an argument.
This may seem pointless at first, but you can use this to pass more complex
data structures by environment variables. For example::
Expand Down

0 comments on commit 3a4e576

Please sign in to comment.