Beispiel #1
0
def insert_in_file(needle, newcontent, file, inline):
    # read yaml file
    fp = open(file)
    filecontents = fp.read()
    fp.close()

    # parse yaml, find target line, inject new line
    data = YamlParser.load_yaml(filecontents)
    try:
        element = find_element(data, needle)
    except exceptions.InvalidSearchStringException:
        click.echo("ERROR: Invalid search string '%s' for file '%s'" %
                   (needle, file),
                   err=True)
        exit(1)

    updated_filecontents = insert_line(element.line_end, newcontent,
                                       filecontents)

    # write new content to file or stdout
    if inline:
        fp = open(file, "w")
        fp.write(updated_filecontents)
        fp.close()
    else:
        click.echo(updated_filecontents, nl=False)
Beispiel #2
0
def find_in_file(needle, file, format):
    # read yaml file
    fp = open(file)
    filecontents = fp.read()
    fp.close()

    # parse the file
    data = YamlParser.load_yaml(filecontents)
    try:
        element = find_element(data, needle)
    except exceptions.InvalidSearchStringException:
        # TODO (jroovers): we should deduplicate this code. Best done by moving the core business logic
        # (like find_element) out of this module into it's own module and then creating a wrapper function
        # here that deals with exception handling
        click.echo("ERROR: Invalid search string '%s' for file '%s'" % (needle, file), err=True)
        exit(1)

    return dumper.dump(file, filecontents, element, format)
Beispiel #3
0
def find_in_file(needle, file, format):
    # read yaml file
    fp = open(file)
    filecontents = fp.read()
    fp.close()

    # parse the file
    data = YamlParser.load_yaml(filecontents)
    try:
        element = find_element(data, needle)
    except exceptions.InvalidSearchStringException:
        # TODO (jroovers): we should deduplicate this code. Best done by moving the core business logic
        # (like find_element) out of this module into it's own module and then creating a wrapper function
        # here that deals with exception handling
        click.echo("ERROR: Invalid search string '%s' for file '%s'" % (needle, file), err=True)
        exit(1)

    return dumper.dump(file, filecontents, element, format)
Beispiel #4
0
def insert_in_file(needle, newcontent, file, inline):
    # read yaml file
    fp = open(file)
    filecontents = fp.read()
    fp.close()

    # parse yaml, find target line, inject new line
    data = YamlParser.load_yaml(filecontents)
    try:
        element = find_element(data, needle)
    except exceptions.InvalidSearchStringException:
        click.echo("ERROR: Invalid search string '%s' for file '%s'" % (needle, file), err=True)
        exit(1)

    updated_filecontents = insert_line(element.line_end, newcontent, filecontents)

    # write new content to file or stdout
    if inline:
        fp = open(file, "w")
        fp.write(updated_filecontents)
        fp.close()
    else:
        click.echo(updated_filecontents, nl=False)
Beispiel #5
0
def cli(needle, newtext, file):
    """ Simple tool for inserting new entries in yaml files while keeping the original structure and formatting  """
    newtext = newtext.replace("\\n", "\n").replace("\\t", "\t")
    data = YamlParser.load_yaml(file)
    element = find_element(data, needle)
    inject_line(element.line, newtext, file)
Beispiel #6
0
def cli(needle, newtext, file):
    """ Simple tool for inserting new entries in yaml files while keeping the original structure and formatting  """
    newtext = newtext.replace("\\n", "\n").replace("\\t", "\t")
    data = YamlParser.load_yaml(file)
    element = find_element(data, needle)
    inject_line(element.line, newtext, file)