def swap_file_list()

in fog-updater/src/fog_update.py [0:0]


def swap_file_list(content, app, files, metrics_or_pings, library=False):
    """
    Replace the list of `metrics_files` or `ping_files` in `content` with `files`
    for the given app or library..
    Returns the changed content.

    All other content is left untouched.
    YAML syntax is assumed.
    File entries are correctly indented.
    """
    output = io.StringIO()
    state = None
    if library:
        app = f"- library_name: {app}"
    else:
        app = f"- app_name: {app}"
    indent = 0

    lines = content.split("\n")

    # Remove trailing newlines.
    while not lines[-1]:
        lines.pop()

    for line in lines:
        if state is None and line.strip() == app:
            state = "app"
        elif (
            state == "app"
            and metrics_or_pings == "metrics"
            and "metrics_files:" in line
        ):
            state = "files"
        elif state == "app" and metrics_or_pings == "pings" and "ping_files:" in line:
            state = "files"
        elif state == "files":
            if line.strip().startswith("-"):
                indent = line.find("-")
                continue
            else:
                for file in files:
                    print(" " * indent, file=output, end="")
                    print(f"- {file}\n", file=output, end="")
                state = None

        print(line, file=output)

    return output.getvalue()