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
def meta_prevodi( fname, dict= dict, nomer_stoinost =0, prn =None, zaglavie ='meta_prevodi', razdelitel_v_nachaloto =False): if prn: prn( zaglavie +':', fname) meta_prevodi = dict() for l in eutf.readlines( fname): if not l or l[0]=='#': continue if razdelitel_v_nachaloto: razdelitel = l[0] l = l[1:] if not razdelitel.strip(): razdelitel = None else: razdelitel = None l = l.strip() kvv = l.split( razdelitel) k = kvv[ nomer_stoinost].strip() for v in kvv: meta_prevodi[ v.strip() ] = k #XXX ignorecase .lower if prn: prn( razdelitel, k, '=', *kvv ) return meta_prevodi
def __init__( az, fname, redove =None, origfname =None, isdir =True): az._init() fname = az.unicode_fname( fname) az.fname = fname az.isdir = isdir az.origfname = origfname or fname az.vse[ fname ] = az az.imena = dictOrder() #ezik:ime az.etiketi = az.DictAttrTrans() az.etiketi_papka = az.DictAttrTrans() az.etiketi_element = az.DictAttrTrans() az.papka_etiketi = Naslagvane( (az.etiketi_papka, az.etiketi), bool= True) az.element_etiketi = Naslagvane( (az.etiketi_element, az.etiketi), bool= True) az.prevodi = dictOrder_lower() #fname:(ime,grupa,orig) az.grupi = [] #група/име,допиме,дългоиме,преводи az.redove = [] az.komentari= [] if redove is not None: try: redove = list( eutf.readlines( redove)) az.procheti( redove) except: print( '????', fname) raise
def cheti_eutf( az, fl): from svd_util import eutf return az.cheti( eutf.readlines( fl))
#!/usr/bin/env python from __future__ import print_function import re,sys from svd_util import eutf enc = eutf.e_utf_stdout() and 'utf-8' or 'cp1251' r = sys.argv.pop(1).decode( enc) #print r, sys.argv rx = re.compile( r) for f in sys.argv[1:]: for a in eutf.readlines( file(f)): if rx.search( a): print( f,':',a)
def zaredi_danni( klas, args): ''' - прочита всички описи - при преводи, обикаля всички директории и запомня кои имат преводи - тези от тях които нямат описи, им се правят нови - за всички описи - ако нямат име, се слага от преводи ако има - самопопълва звук/година от името-на-директорията - добавя поисканите етикети ''' options = klas.options ################# #зареждане на данни, заявки, и пр. - четене/оправяне на ОПИСИте if options.prevodi: if options.podrobno: prn( 'prevodi:', options.prevodi) for k,(v,o) in prevodi_file2ime( eutf.readlines( options.prevodi)): klas._nov_prevod( fname= k, ime= zaglavie(v), original= zaglavie(o), roditel= options.prevodi) if options.podrobno: prn( '%-40s = %s' % (k,v) ) if options.prevodi_meta: info.meta_prevodi.update( meta_prevodi( options.prevodi_meta, dict= dict_lower, prn= options.podrobno and prn, zaglavie= 'мета-преводи', razdelitel_v_nachaloto=True )) info.fenc = options.filename_enc or (eutf.e_utf_stdout() and 'utf-8' or locale.getpreferredencoding()) if options.podrobno: prn( 'filename_enc:', info.fenc) prn( 'stdout.enc:', sys.stdout.encoding, sys.stdout) prn( 'stderr.enc:', sys.stderr.encoding, sys.stderr) etiketi = {} for e in options.etiket or (): klas.procheti_etiket( e, etiketi) options.dirs = [] za_pravene = {} for path,dirs,files in klas.obikoli( args, klas.e_za_propuskane ): if options.prevodi: for dname in dirs: pname = join( path, dname) if not options.simvolni and os.path.islink( pname): continue p = klas.vse_prevodi.get( dname) if p: rpname = realpath( pname) za_pravene[ rpname] = p.ime, pname pname = realpath( path) for fname in files: if not e_opis( fname): continue i = klas( pname, redove= join( path, fname), origfname= path) if options.podrobno: prn( i.fname) if options.podrobno>1: prn( i.dump() ) for path,dirs,files in klas.obikoli( options.papka_s_opisi or () ): pname = realpath( path) for fname in files: if klas.e_za_propuskane( fname): continue i = klas( join( pname, fname), redove= join( path, fname), origfname= join( path, fname), isdir=False) if options.podrobno: prn( i.fname) for pname,(ime,fpath) in za_pravene.items(): if pname not in info.vse: i = klas( pname) i.slaga_ime( ime) for k,i in sorted( info.vse.items()): try: i.samopopylva_ot_fname() fname = basename( i.fname) p = klas.vse_prevodi.get( fname) if p: if not i.imeto or options.popravi_opisi: i.slaga_ime( p.ime) if p.original and not i.etiketi.original: i.slaga_etiket( i.stoinosti.original, p.original ) i.etiketi.update_pre( **etiketi) i.samopopylva_etiketi() razlika, t = i.zapis( naistina= options.zapis_opisi ) #if razlika: print '-----------' except: print( '????', fname) raise
setattr( options, optname, optvalue) print( optname,':', optvalue) return True allsize = 0 cutter = None for cutfile in args: print( 'new-cutfile=', cutfile) if cutfile=='-': options.inpath = '.' cutfile = sys.stdin else: options.inpath = dirname( cutfile) cutfile = open( cutfile) for c in eutf.readlines( cutfile): c = c.strip() if not c or c.startswith( '#'): continue if assign_params( c): continue if not c.replace( '=',''): #only ===== ; force filename follows if cutter: cutter.save() cutter = None #print '======' options.infile = None continue if sum( c.endswith( '.'+ext) for ext in 'flac wav mp3'.split()): if cutter: cutter.save() cutter = None print( 'new-infile=', c)
elif optz.o1251 or optz.cp1251: a = a.encode('cp1251') return a if optz.demo: print( oenc( hyphtext( txt, **cfg))) raise SystemExit,0 if args: for a in args: print( oenc( hyphtext( ienc(a), **cfg)), end=' ') else: import sys tt = sys.stdin if optz.iguess: from svd_util import eutf tt = eutf.readlines(tt) def ienc(a): return a if 0: tt = tt.readlines() * 10 #profile started=False for l in tt: l = l.rstrip() if optz.start: if not started: if optz.start in l: started = True n,l = l.split( optz.start,1) n+= optz.start print( oenc( ienc(n)), end='') else: print( oenc( ienc(l)) )