Browse Source

Add "Shell Commands" integration. Fixes #302

pull/307/head
Pēteris Caune 5 years ago
parent
commit
91c93b6a95
No known key found for this signature in database GPG Key ID: E28D7679E9A9EDE2
1 changed files with 38 additions and 0 deletions
  1. +38
    -0
      hc/lib/string.py

+ 38
- 0
hc/lib/string.py View File

@ -0,0 +1,38 @@
def replace(template, ctx):
"""Replace placeholders with their values and return the result.
Example:
>>> replace("$NAME is down", {"$NAME": "foo"})
foo is down
This function explicitly ignores "variable variables".
In this example, placeholder's value itself contains a placeholder:
>>> replace("Hello $FOO", {"$FOO": "$BAR", "$BAR": "World"})
Wrong: Hello World
Correct: Hello $BAR
>>> replace("Hello $$FOO", {"$FOO": "BAR", "$BAR": "World"})
Wrong: Hello World
Correct: Hello $BAR
In other words, this function only replaces placeholders that appear
in the original template. It ignores any placeholders that "emerge"
during string substitutions. This is done mainly to avoid unexpected
behavior when check names or tags contain dollar signs.
"""
parts = template.split("$")
result = [parts.pop(0)]
for part in parts:
part = "$" + part
for placeholder, value in ctx.items():
if part.startswith(placeholder):
part = part.replace(placeholder, value, 1)
break
result.append(part)
return "".join(result)

Loading…
Cancel
Save