find_tagged_line

Operation for finding a line in a file containing a text “tag”.

Example

The following example inserts text to a file named settings.py by first searching for a string matching the tag "# qwikstart: middleware":

examples/operations/find_tagged_line.yml
steps:
    "Find middleware tag in settings":
        name: find_tagged_line
        file_path: "examples/data/settings.py"
        tag: "# qwikstart: middleware"
    "Insert new middleware under tag":
        name: insert_text
        file_path: "examples/data/settings.py"
        text: '"django.contrib.auth.middleware.AuthenticationMiddleware",'

The settings.py file has the following text:

MIDDLEWARE = [
    "django.middleware.security.SecurityMiddleware",
    "django.contrib.sessions.middleware.SessionMiddleware",
    # qwikstart: middleware
]

The find_tagged_line operation adds the line number and column number to the global context. The names of these values match inputs expected by the insert_text operation, which will insert text below the tagged line.

Required context

file_path

Path to file relative to the current working directory.

tag

Text used as a placeholder for detecting where to insert text. For example:

# qwikstart: inject-line-below

Output

line

Line number where the text was found.

column

Column where the start of the matching text was found.

See also