mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2026-11-06 19:01:54 +00:00
Removed leading `# filter_plugins/...py` / `# lookup_plugins/...py` comments that referenced the old plugin paths. The plugins now live under plugins/filter and plugins/lookup, so these headers are both inaccurate and redundant. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
50 lines
1.0 KiB
Python
50 lines
1.0 KiB
Python
# sed-safe escaping for replacement strings.
|
|
#
|
|
# Intended usage:
|
|
# sed "s<d>PATTERN<d>{{ value | sed_escape('<d>') }}<d>"
|
|
#
|
|
# Escapes:
|
|
# - backslash (\)
|
|
# - ampersand (&) → sed replacement reference
|
|
# - delimiter (default: |)
|
|
#
|
|
# Does NOT:
|
|
# - add quotes
|
|
# - perform shell escaping
|
|
#
|
|
# Safe for:
|
|
# - sed replacement part
|
|
# - passwords containing $, ', ", spaces, /, etc.
|
|
#
|
|
from __future__ import annotations
|
|
|
|
|
|
class FilterModule:
|
|
def filters(self):
|
|
return {
|
|
"sed_escape": self.sed_escape,
|
|
}
|
|
|
|
@staticmethod
|
|
def sed_escape(value, delimiter: str = "|") -> str:
|
|
"""
|
|
Escape a value for use as the *replacement* part of a sed s/// command.
|
|
|
|
Example:
|
|
{{ password | sed_escape('|') }}
|
|
|
|
"""
|
|
if value is None:
|
|
return ""
|
|
|
|
s = str(value)
|
|
|
|
# Escape order matters
|
|
s = s.replace("\\", "\\\\") # backslash
|
|
s = s.replace("&", "\\&") # sed match reference
|
|
|
|
if delimiter:
|
|
s = s.replace(delimiter, "\\" + delimiter)
|
|
|
|
return s
|