def premesti_v_0( fime, dirime): log() dir = dirime + '/0' mkdir( dir) bezext,ext = ospath.splitext( fime) if 0: #stari import time sega = str( time.mktime( time.localtime()) ) dirstari = join( dir, sega) for f in glob( globescape( join( dir, basename( bezext))) +'.*'): makedirs( dirstari) rename( f, join( dirstari, basename( f) )) #novi davai = optz.premesti for f in glob( globescape( bezext) +'.*'): of = join( dir, basename( f)) if not exists( of): if davai: rename( f, of) else: link( f, of) else: assert ospath.samefile( f, of), of if davai: os.remove( f)
def e_opis( fname): for pat in OPISIpat: pat = globescape( pat) if fnmatch.fnmatch( fname, pat): return True return False return fname in OPISI return fname.split('.')[0] == OPIS
def prevedi_element( az, f ): k = az.fname assert isinstance( f, unicode) fpath,fname = os.path.split( f) if fpath != k: if exists( join( fpath, OPIS)): return fname = f[ len(k):].lstrip('/') if az.e_za_propuskane( dirname( fname) ): return svoistva = () aname = None for ime_bez_ext in az.bez_ext1x1( fname): if ime_bez_ext in info.vse_prevodi: break else: svoistva,aname = az.svoistva_ot_fname( ime_bez_ext) aname = aname.rstrip('.') if aname not in info.vse_prevodi: for aname in az.bez_ext1x1( aname): if aname in info.vse_prevodi: break else: if az.etiketi.sfx and aname.endswith( az.etiketi.sfx): aname = aname[ :-len(az.etiketi.sfx)] if aname not in info.vse_prevodi: if az.options.podrobno: prn( '!!! няма превод:', f, aname) return prevod = az.prevod_po_shablon( aname or ime_bez_ext) if svoistva: prevod = '.'.join( [prevod] + svoistva ) oname = join( k, ime_bez_ext) lodir = len(k)+1 loname= len(oname) assert isinstance( oname, unicode), repr(oname) for s in sorted( glob( globescape( oname)+'*')): staro = s[ lodir:] kk,ext = os.path.splitext( staro) if fname.startswith( kk): novo = prevod + ext else: #това се скапва за приказка с :автор novo = prevod+ s[ loname:] assert isinstance( novo, unicode), repr(novo) if staro == novo: novo = None if staro not in az.file_prevodi: az.file_prevodi[ staro ] = novo #АБ.В.Г е вече там когато минава през АБ.В if not az.vse_file_prevodi.get( staro) and novo: az.vse_file_prevodi[ staro ] = novo
def prevedi_elementi( az): r = az.file_prevodi = {} k = az.fname k = globescape( k) for ext in az.exts: #така че АБ.В.Г минава преди АБ.В XXX по-дългите първи щото иначе а-б.avi не минава преди а.avi for f in reversed( sorted( glob( k+'/*.'+ext) + glob( k+'/*/*.'+ext) #./grupa1/file1 , key= lambda x: (len(x),x) ) ): az.prevedi_element( f ) if az.options.podrobno and r: prn( '---------file_prevodi-----') prn( '\n'.join( '%-50s = %s' % (k,v) for k,v in sorted( r.items())))
def go( fime, fdir, nedei =False, command =None, move =True, decode =False): class Ddf( DictAttr): def __missing__( az, k): return '' ot_ime = Ddf( razglobi( fime)) ot_ime.setdefault( 'ime', '_') cnomer = ot_ime.get( 'nomer') opis = ''' име: {ime} етикети: {zagolemi} {dok} издание: радио откъде: {rubrika} {data} '''.format( **ot_ime ) + '\n'.join( v+': '+str(ot_ime[k]) for k,v in dict( godina = 'година', nomer = '#част', avtori_plus = 'автор', opisanie= 'опис', ).items() if ot_ime.get(k) ) opis = opis.strip() print( opis) opis += ''' срез: участници: редактор: превод: драматизация: изпълнение: музика: запис: з.реж: з.оп: з.оф: м.оф: режисьор: съдържание: описание: # vim:ts=4:sw=4:expandtab:ft=yaml''' dirname = ot_ime.get( 'dirname', fime ) nomer = cnomer print( '-----'*4) if nedei: return dir = join( fdir, dirname) orgdir = join( dir, '0') osextra.makedirs( orgdir, exist_ok= True) fnomer = nomer and '.'+str( nomer) or '' if not command: ext = '.wav' else: ext = ot_ime.ext wime = join( dir, dirname + fnomer + ext) print( ' > ', dir) print( ' >>', wime) if command: command( fime, wime) return fopis = join( dir, 'opis') while exists( fopis): fopis+='1' with eutf.filew_utf( fopis ) as f: print( opis, file= f) print( move and 'mv' or 'ln', fime, orgdir) bezext,ext = splitext( fime) ima_wav = False for f in glob( osextra.globescape( bezext)+'.*'): bf = basename( f) if f.endswith( '.wav'): ima_wav = True if cnomer: bf = str(cnomer)+'.'+bf (move and os.rename or os.link)( f, join( orgdir, bf )) #os.rename( fime, nime) if not ima_wav and optz.decode: bf = basename( fime) nime = join( orgdir, bf) if cnomer: bf = str(cnomer)+'.'+bf wwime = splitext( join( orgdir, bf))[0]+'.wav' if nime.endswith('.flac'): cmds = [ 'flac', '-d', '-o', wwime, nime ] print( cmds) subprocess.call( cmds) elif nime.endswith('.mp3'): cmds = 'lame --nohist -h -v -V 0 -S --decode'.split() + [ nime, wwime ] print( cmds) subprocess.call( cmds)
def diropis2srez( dirime, opis, optz): log() fdirime = (not dirime or dirime.rstrip('/')=='.') and '_' or dirime srezove = [] def dobavi( **k): return srezove.append( DictAttr( **k)) parcheta = dai( opis, *cyr._parcheta) #for p in [ cyr.chasti ] + [ cyr.parcheta[:i] for i in range( 3, 1+len(cyr.parcheta))]: # parcheta = opis.get(p) # if parcheta: break if parcheta: for fime, parche in parcheta.items(): fime = str(fime) pp = isinstance( parche, str) and [ dict(srez=parche) ] or not isinstance( parche, (list,tuple) ) and [ parche ] or parche for n,p in enumerate( pp,1): ss = dai_srezove( p, mnogo= True) if not ss: continue if not isinstance( ss, (list,tuple)): ss = [ss] for s in ss: if not s: continue print( ' ', fime, s) dirimena = [ dirime+'/'+fime, dirime+'/0/'+fime ] fimena = dirimena + multiglob( [ globescape( d)+'.*wav' for d in dirimena]) if len(pp)==1: n=None for f in fimena: if exists( f): try: nomer = razglobi_ime( fime).get('nomer') except: mnomer = re.search( '(\d+)', fime) if mnomer: nomer = int( mnomer.group(1) ) else: nomer = fime ime = dai_ime(p) dobavi( fime= f, nomer= p.get( cyr.chast, nomer) or n, srez = ime and { ime: s } or s, ) break else: print( '! липсва парче:', fime) else: ss = dai_srezove( opis, mnogo= True) if isinstance( ss, dict): for fime, srez in ss.items(): dobavi( fime= fime, srez= srez) elif ss: if not isinstance( ss, (list,tuple)): ss = [ss] dirimena = [ globescape( dirime+ ex)+ '*wav' for ex in ['/', '/0/'] ] fimena = multiglob( dirimena ) if len(fimena)==2: if basename( fimena[0]).replace( '.1.c.wav', '.wav' ) == basename( fimena[-1]): del fimena[0] if len( fimena) == 1: for srez in ss: dobavi( fime= fimena[0], srez= srez) elif not fimena: print( '! липсва', dirimena) else: print( '!!!! няколко:') for i in fimena: print( ' ?', i) for p in srezove: assert exists( p.fime) # ако срезове != стари срезове или звуковия файл е по-нов от дир/опис: режи, и парчетата към дир/ if 1: #newer( fime, fopis): izvadi( p.fime, p.srez, fdirime, nomer= p.get('nomer'), ime= dai_ime(p))