Skip to content

Instantly share code, notes, and snippets.

@enven-omiomi
Last active May 9, 2023 05:59
Show Gist options
  • Save enven-omiomi/5962541d83072a4b0d42cc6393b7e473 to your computer and use it in GitHub Desktop.
Save enven-omiomi/5962541d83072a4b0d42cc6393b7e473 to your computer and use it in GitHub Desktop.
A Python script that replaces the placeholders in the YAML file with the contents of the other file

This Python script, replace_function_code.py, is designed to replace a placeholder in a YAML file with the content of a Python file.

The script requires three positional arguments:

  1. yaml_file: This is the YAML file to be processed.
  2. python_file: The Python file whose content will be inserted into the YAML file.
  3. placeholder: The string in the YAML file to be replaced.

The script also accepts two optional arguments:

  1. -o or --output: This allows you to specify the name of the output file. If not provided, the default output file name is "output.yaml".
  2. -w or --overwrite: If this option is set, the original YAML file will be overwritten with the processed content.

The script reads the content of both the YAML file and the Python file, and replaces the placeholder in the YAML file with the content of the Python file, properly indented. The processed content is then written to the output file and printed to the standard output.

To use the script, save it as replace_function_code.py and run it from the command line like this:

python replace_function_code.py template.yaml lambda_function.py FUNCTION_CODE -o output.yaml -w

In this command, template.yaml and lambda_function.py are the input files, FUNCTION_CODE is the placeholder to be replaced, output.yaml is the output file, and the -w flag indicates that the original YAML file should be overwritten.

import argparse
import re
def replace_function_code(yaml_file, python_file, placeholder, output_file, overwrite):
with open(yaml_file, "r") as yml:
yaml_content = yml.read()
with open(python_file, "r") as py:
python_code = py.read()
match = re.search(re.escape(placeholder), yaml_content)
if match:
indent = match.start() - match.string.rfind("\n", 0, match.start()) - 1
indented_code = "\n".join(
[
(" " * indent) + line if i > 0 and line.strip() else line
for i, line in enumerate(python_code.split("\n"))
]
)
yaml_content = yaml_content.replace(placeholder, indented_code)
if overwrite:
output_file = yaml_file
with open(output_file, "w") as output:
output.write(yaml_content)
with open(output_file, "r") as output:
print(output.read())
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Replace placeholder in YAML file with code from Python file."
)
parser.add_argument("yaml_file", help="The YAML file to be processed")
parser.add_argument(
"python_file", help="The Python file to be inserted into the YAML file"
)
parser.add_argument(
"placeholder", help="The placeholder to be replaced in the YAML file"
)
parser.add_argument(
"-o",
"--output",
default="output.yaml",
help='The output file name. Defaults to "output.yaml"',
)
parser.add_argument(
"-w",
"--overwrite",
action="store_true",
help="If set, the original YAML file will be overwritten",
)
args = parser.parse_args()
replace_function_code(
args.yaml_file, args.python_file, args.placeholder, args.output, args.overwrite
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment