kitir/_libs/file_utils.py
def format_file(filepath, raise_on_fail=True, **kwargs):
    """
    format a files contents with given kwargs using pythons string.format()
    :param filepath: filepath to format
    :param raise_on_fail: raise exceptions
    :param kwargs:
    :return: returns True if success else False
    """
    filemode_read = kwargs.pop('filemode_read', 'r')
    filemode_write = kwargs.pop('filemode_write', 'w')
    try:
        with open(filepath, filemode_read) as fr:
            content_before = fr.readlines()
    except Exception as exc:
        log.error('Exception reading file before format: filepath={} exc={}'.format(filepath, exc))
        if raise_on_fail:
            raise
        return False
    else:
        content_after = []
        for idx, line in enumerate(content_before):
            # todo: harden {} rules
            if not any(k in line for k in kwargs.keys()):
                new_line = line
            else:
                try:
                    new_line = line.format(**kwargs)
                except Exception as exc:
                    log.error('Exception formatting file line: filepath={} idx={} exc={}'.format(filepath, idx, exc))
                    if raise_on_fail:
                        raise
                    return False
            content_after.append(new_line)
        try:
            with open(filepath, filemode_write) as fw:
                fw.writelines(content_after)
        except Exception as exc:
            log.error('Exception writing file after format: filepath={} exc={}'.format(filepath, exc))
            if raise_on_fail:
                raise
            return False
        else:
            return True

    format a files contents with given kwargs using pythons string.format()
    :param filepath: filepath to format
    :param raise_on_fail: raise exceptions
    :param kwargs:
    :return: returns True if success else False
    
format a files contents with given kwargs using pythons string.format()
format a files contents with given kwargs using pythons string . format ()
def format_file(filepath, raise_on_fail=True, **kwargs):
    filemode_read = kwargs.pop('filemode_read', 'r')
    filemode_write = kwargs.pop('filemode_write', 'w')
    try:
        with open(filepath, filemode_read) as fr:
            content_before = fr.readlines()
    except Exception as exc:
        log.error('Exception reading file before format: filepath={} exc={}'.format(filepath, exc))
        if raise_on_fail:
            raise
        return False
    else:
        content_after = []
        for idx, line in enumerate(content_before):
            if not any(k in line for k in kwargs.keys()):
                new_line = line
            else:
                try:
                    new_line = line.format(**kwargs)
                except Exception as exc:
                    log.error('Exception formatting file line: filepath={} idx={} exc={}'.format(filepath, idx, exc))
                    if raise_on_fail:
                        raise
                    return False
            content_after.append(new_line)
        try:
            with open(filepath, filemode_write) as fw:
                fw.writelines(content_after)
        except Exception as exc:
            log.error('Exception writing file after format: filepath={} exc={}'.format(filepath, exc))
            if raise_on_fail:
                raise
            return False
        else:
            return True
def format_file ( filepath , raise_on_fail = True , ** kwargs ) : filemode_read = kwargs . pop ( 'filemode_read' , 'r' ) filemode_write = kwargs . pop ( 'filemode_write' , 'w' ) try : with open ( filepath , filemode_read ) as fr : content_before = fr . readlines ( ) except Exception as exc : log . error ( 'Exception reading file before format: filepath={} exc={}' . format ( filepath , exc ) ) if raise_on_fail : raise return False else : content_after = [ ] for idx , line in enumerate ( content_before ) : if not any ( k in line for k in kwargs . keys ( ) ) : new_line = line else : try : new_line = line . format ( ** kwargs ) except Exception as exc : log . error ( 'Exception formatting file line: filepath={} idx={} exc={}' . format ( filepath , idx , exc ) ) if raise_on_fail : raise return False content_after . append ( new_line ) try : with open ( filepath , filemode_write ) as fw : fw . writelines ( content_after ) except Exception as exc : log . error ( 'Exception writing file after format: filepath={} exc={}' . format ( filepath , exc ) ) if raise_on_fail : raise return False else : return True
format a files contents with given kwargs using pythons string.format()
format a files contents with given kwargs using pythons string . format ()
"""
    format a files contents with given kwargs using pythons string.format()
    :param filepath: filepath to format
    :param raise_on_fail: raise exceptions
    :param kwargs:
    :return: returns True if success else False
    """ # todo: harden {} rules
returns True if success else False
returns True if success else False
filepath
filepath to format
filepath to format
raise_on_fail
None
kwargs
None