Ejemplo n.º 1
0
def save_if_diff( fname, r, naistina =False, enc ='utf8', podrobno =True, makedirs= True, prepend_py_enc =False, prezapis =False):
    try:
        if isinstance( r, str): r = r.strip().split( '\n')
        r = [ x.rstrip() for x in r ]
        if prepend_py_enc:
            tenc = '# -*- coding: {enc} -*-'.format( **locals())
            if tenc not in r[:2]:
                r.insert( 0, tenc )
        txt = '\n'.join( r)

        if exists( fname):
            org = list( eutf.readlines( fname))
            org = [ x.rstrip() for x in org ]
        else:
            org = []

        razlika = (r != org)
        if razlika:
            if podrobno:
                if not org:
                    prn( 'ново:', fname )
                    prn( txt)
                else:
                    prn( 'разлика:', fname )
                    df = diff( org, r, 'старо', 'ново', lineterm='')
                    ima=0
                    for l in df:
                        ima=1
                        if l.startswith('---') or l.startswith('+++'): continue
                        prn( l)
                    if not ima:
                        for a,b in zip( org,r):
                            if a!=b:
                                prn( '?<'+repr(a))
                                prn( '?>'+repr(b))
        if razlika or prezapis:
            if naistina:
                if makedirs:
                    import os.path
                    fpath = os.path.dirname( fname)
                    if fpath and not os.path.exists( fpath): os.makedirs( fpath )
                prn( '>>>', fname)
                with eutf.filew( enc, fname ) as of:
                    of.write( txt+'\n')
        return razlika, txt
    except:
        prn( '??', fname)
        raise
Ejemplo n.º 2
0
 def fopen( fn):
     if optz.oenc: f = filew( optz.oenc, fn)
     else: f = open( fn, 'w')
     return f