kitir/_libs/file_utils.py
def replace_content_in_file(filepath, replacements, raise_on_fail=True, **kwargs):
    """
    replaces content in a file,
    :param filepath: the path to the file for replacements
    :param replacements: replacements should be a dictionary of {target: replacement}
    :param raise_on_fail:
    :param kwargs:
    :return:
    """
    filemode_read = kwargs.pop('filemode_read', 'r')
    filemode_write = kwargs.pop('filemode_write', 'w')
    log.debug('Modifying file in-place: filepath={}'.format(filepath))
    backup_file = kwargs.pop('backup_file', None)
    return_success = kwargs.pop('return_success', True)
    new_content = []  # will contain the final content of the conf file
    successful_replacements = []

    # make a backup
    if backup_file:
        smart_copy(filepath, backup_file)

    # read conf
    contents = read_file(filepath, filemode=filemode_read)

    for idx, line in enumerate(contents[:]):
        # make new line so we can modify it
        new_line = line
        # normal line (configuration / not section)
        for key in replacements.keys():
            if key not in new_line:
                continue  # line is not relevant
            log.trace('replace content in file: filepath={} index={} key={} value={}'.format(
                filepath, idx, key, replacements[key]))
            # replace and save (supports multiple per line)
            new_line = new_line.replace(key, replacements[key])
            successful_replacements.append(key)
        # add the new_line to the new_content (it may be unchanged)
        new_content.append(new_line)

    # write conf
    write_file(filepath, new_content, filemode=filemode_write)

    # return success
    if return_success:
        return bool(all([k in successful_replacements for k in replacements.keys()]))
    # todo: raise on fail ?
    # return the replacements that were done
    return successful_replacements

    replaces content in a file,
    :param filepath: the path to the file for replacements
    :param replacements: replacements should be a dictionary of {target: replacement}
    :param raise_on_fail:
    :param kwargs:
    :return:
    
replaces content in a file.
replaces content in a file .
def replace_content_in_file(filepath, replacements, raise_on_fail=True, **kwargs):
    filemode_read = kwargs.pop('filemode_read', 'r')
    filemode_write = kwargs.pop('filemode_write', 'w')
    log.debug('Modifying file in-place: filepath={}'.format(filepath))
    backup_file = kwargs.pop('backup_file', None)
    return_success = kwargs.pop('return_success', True)
    new_content = []  
    successful_replacements = []
    if backup_file:
        smart_copy(filepath, backup_file)
    contents = read_file(filepath, filemode=filemode_read)
    for idx, line in enumerate(contents[:]):
        new_line = line
        for key in replacements.keys():
            if key not in new_line:
                continue  
            log.trace('replace content in file: filepath={} index={} key={} value={}'.format(
                filepath, idx, key, replacements[key]))
            new_line = new_line.replace(key, replacements[key])
            successful_replacements.append(key)
        new_content.append(new_line)
    write_file(filepath, new_content, filemode=filemode_write)
    if return_success:
        return bool(all([k in successful_replacements for k in replacements.keys()]))
    return successful_replacements
def replace_content_in_file ( filepath , replacements , raise_on_fail = True , ** kwargs ) : filemode_read = kwargs . pop ( 'filemode_read' , 'r' ) filemode_write = kwargs . pop ( 'filemode_write' , 'w' ) log . debug ( 'Modifying file in-place: filepath={}' . format ( filepath ) ) backup_file = kwargs . pop ( 'backup_file' , None ) return_success = kwargs . pop ( 'return_success' , True ) new_content = [ ] successful_replacements = [ ] if backup_file : smart_copy ( filepath , backup_file ) contents = read_file ( filepath , filemode = filemode_read ) for idx , line in enumerate ( contents [ : ] ) : new_line = line for key in replacements . keys ( ) : if key not in new_line : continue log . trace ( 'replace content in file: filepath={} index={} key={} value={}' . format ( filepath , idx , key , replacements [ key ] ) ) new_line = new_line . replace ( key , replacements [ key ] ) successful_replacements . append ( key ) new_content . append ( new_line ) write_file ( filepath , new_content , filemode = filemode_write ) if return_success : return bool ( all ( [ k in successful_replacements for k in replacements . keys ( ) ] ) ) return successful_replacements
replaces content in a file,
replaces content in a file
"""
    replaces content in a file,
    :param filepath: the path to the file for replacements
    :param replacements: replacements should be a dictionary of {target: replacement}
    :param raise_on_fail:
    :param kwargs:
    :return:
    """ # will contain the final content of the conf file # make a backup # read conf # make new line so we can modify it # normal line (configuration / not section) # line is not relevant # replace and save (supports multiple per line) # add the new_line to the new_content (it may be unchanged) # write conf # return success # todo: raise on fail ? # return the replacements that were done
None
filepath
the path to the file for replacements
the path to the file for replacements
replacements
replacements should be a dictionary of {target: replacement}
replacements should be a dictionary of { target : replacement }
raise_on_fail
None
kwargs
None