Beispiel #1
0
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)
Beispiel #2
0
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)
Beispiel #3
0
                pathdeeper = levels( path)[ srcdepth:]
                targdeeper = join( target, *pathdeeper )
                mkdir = False
                for f in files:
                    if not included( f, optz.include, optz.exclude ): continue
                    fsrc = realpath( join( path, f))
                    if not mkdir:
                        yield None, targdeeper
                        mkdir = True
                    yield fsrc, join( targdeeper, f )

op = getop( optz)
for s,t in walk( argz, target):
    try:
        if not s:
            if not op or optz.verbose: print( '>>', t+'/' )
            if op: osextra.makedirs( t)
        else:
            if not op or optz.verbose: print( '--', s, '\n->', t)
            if op:
                if exists( t) and optz.force: os.remove( t)
                op( s, t)
    except OSError as e:
        print( str(e), '\n  --', s, '\n  ->', t)
    except Exception as e:
        print( str(e), '\n  --', s, '\n  ->', t)
        raise

# vim:ts=4:sw=4:expandtab
Beispiel #4
0
def gotovo( pyt, opis, kym =None, ime =None):
    d = pyt
    assert ospath.isdir( kym), kym
    ime = ime or dai_ime( opis)
    avtori  = dai( opis, 'ав*тор', 'awtor')
    izdanie = dai( opis, 'изд*ание', 'издания').lower()
    izdaniecyr= l2c( izdanie.lower())
    izdaniecyr2= re.sub( '^б([аеоскхнт][анмк])', r'в\1', izdaniecyr)
    if 'lat':
        izdanie = cyr2lat( izdaniecyr2)
        if izdaniecyr2 != izdaniecyr:
            izdanie = (izdanie
            ).replace( 'v','b'
            ).replace( 'h','x'
            ).replace( 'n','h'
            )
    etik    = dai( opis, 'ет*икети') or ''
    avtori = (avtori or '').split()
    stihove = ime.lower() == 'стихове' or 'стих' in etik
    izp = []
    if stihove:
        izp = (dai( dai( opis, 'уч*астници') or opis, 'изп*ълнение') or ()) #.split()
        if izp and isinstance( izp, str): izp = [ izp]
        if len(izp)==1:
            izp = [ i.rstrip('?') for i in izp ]
            #avtori += [ i.rstrip('?') for i in izp ]
        else: izp = []
    from abbr import razdeli_kamila2
    def imena( l):
        return '-'.join( [ razdeli_kamila2(a).replace(' ','.') for a in l])

    if stihove: # and izdaniecyr == 'радио':
        fname = [ imena( avtori), ime, imena( izp) ]
    else:
        fname = [ ime, imena( avtori + izp) ]
    fname = '--'.join( f.strip() for f in fname if f)
    fname = fname.replace('..','.')
    #mau ROOT=d/
    fname = fname.replace(' ', '_')
    fname = fname.replace('-_', '-')
    fname = fname.replace('_-', '-')
    fname_lat = cyr2lat( fname.lower())
    if izdaniecyr:
        fname     += '--'+izdaniecyr
        fname_lat += '--'+izdanie #cyrre.sub( '--v([aeoskhnt]a)$','--b\1', fname_lat)
    fname     = fname.replace( '?','')
    fname_lat = fname_lat.replace( '?','')
    print( ' ', fname_lat, '///', fname )

    elementi = glob( join( d, '*.wav'))
    if not elementi: elementi = glob( join( d, '*.mp3'))
    if len(elementi)==1:
        mkdir( join( d, '0'))
        def kym0( e, d, fname, ext):
            if exists( e+ext):
                rename( e+ext, join( d, '0', fname+ext))
        e,_ext = ospath.splitext( elementi[0])
        kym0( e,d, fname, '.wav')
        kym0( e,d, fname, '.flac')
        rename( e+'.mp3', join( d, fname+'.mp3'))
        print( 'ok:', join( d, fname+'.mp3'))
    else:
        print( ' ??', elementi)

    cel = join( kym, fname_lat)
    if d.rstrip('/') == '.':
        print( 'md + mv * >>', cel)
        makedirs( cel)
        for g in glob( join( d, '*')):
            if g == cel: continue
            rename( g, join( cel, basename(g)))
    else:
        iztrij = None
        if ospath.islink(d):
            iztrij = d
            d = ospath.realpath( d)
        rename( d, cel)
        if iztrij: remove( iztrij)
    #cd ~/azcom/zdetski/`basename $gotovo`
    #m lnfrom ; m sym2; m
    #m zl
    # . e3*
    print( '')
Beispiel #5
0
def cron( items, o ):
    '''
cron/crontab plain:  # m h dom mon dow  command
cron.d/crontab direct: # m h dom mon dow (user) command
'''
    tyrsach = nalichni_imena( o)

    for x in sorted( items, key= lambda a: (a.today,a.time) ):
        h,m = x.time

        #print( '#', x.time, x.title)
        endtime = x.get( 'endtime')
        if endtime:
            eh,em = endtime
            eoffs = eh*60+ em
            soffs = h*60+ m
            if eoffs < soffs: eoffs += 24*60
            sizemins = eoffs - soffs
        else: sizemins = ''
        t = datetime.datetime( x.today.year, x.today.month, x.today.day, h,m)

        dni = x.get('dni')
        if dni: #a,b,c-f
            r = set()
            for d in dni.split(','):
                dd = d.split('-')
                if len(dd)==2:
                    r.update( range( int(dd[0]), int(dd[1])+1) )
                else: r.add( int(d))
            if t.weekday()+1 not in r:
                continue    #skip

        channel = x.channel
        if not channel:
            if x.stream:
                channel = x.stream.split('://')[-1].replace('/','_')
        if channel in bnr_kanali:
            channel = bnr_kanali[ channel ].abbr


        fname = str( channel)
        fname_kanal = fname
        dati = '{t.year:04d}-{t.month:02d}{t.day:02d}-{t.hour:02d}{t.minute:02d}'.format( **locals())
        if not o.cron_fname_notime: fname += dati
        fname_kanal_vreme = fname + dati

        if x.get('title'): fname += '+'+x.title
        if x.get('text'):
            DOT = '\u2022' #'•'
            tx = x.text.replace('Предаването', 'Пр.'
                      ).replace('посветено', 'посв.'
                      ).replace('годишнина', 'год.'
                      ).replace('години', 'г.'
                      ).replace( DOT,'-'
                      )
            fname += '+'+tx
            x.text = x.text.replace( DOT, '\n--')

        def sykr( x):
            x = re.sub( rec2dir.requo, '', x.strip()
                    ).replace( '\u2013','-'     #-
                    ).replace( '\u0406','I'     #І
                    #).replace( '\u0425','X' cyrХ latX ?
                    ).replace( 'x','х'
                    ).replace('  ',' '
                    ).replace(' -','-'
                    ).replace('- ','-'
                    ##).replace('  ',' '
                    ).replace(' ','_'
                    ).replace('__','_'
                    #).replace('„',''
                    #).replace('”',''
                    ).replace('Документално_студио','Док.ст.'
                    ).replace('Радиоколекция',      'Ркц'
                    ).replace('Радиотеатър',        'Рт'
                    ).replace('радиотеатър',        'Рт'
                    ).replace('Радотеатър',         'Рт'
                    ).replace('Време_за_приказка',  'ВзаП'
                    ).replace('Ваканционна_програма',   'Вкц'
                    ).replace('Избрано_от_', ''
                    ).replace('фонда_на_редакция',''
                    ).replace('фонда_на_',   ''
                    ).replace('_на_БНР',''
                    ).replace('Запазена_марка',''
                    ).replace('Запазна_марка',''
                    ).replace('Семейно_радио', '' #Сем
                    ).replace('Голямата_къща_на_смешните_хора', 'ГКСХ'
                    ).replace('Салон_за_класифицирана_словесност', 'Салон_словесност'
                    ).replace('Съвременна',     'Съвр.'
                    ).replace('Драматургични',  'драм.'
                    ).replace('драматургия',    'драм.'
                    ).replace('Незабравими_български_спектакли_във_фонда', 'бълг.др.'
                    ).replace('българска',  'бълг.'
                    ).replace('български',  'бълг.'
                    )
            x = re.sub( rec2dir.rlatcyr( '([Дд])окументал(ен|н(а|о|и))'), r'\1ок.', x)
            x = re.sub( rec2dir.rezlf, rec2dir.zlf, x)
            x = re.sub( rec2dir.reakm, rec2dir.akomika, x)
            x = re.sub( rec2dir.rehs, rec2dir.hs, x)
            x = x.strip( rec2dir.rend+':')
            return x

        dosave = 10#True
        danni = DictAttr(
            rubrika = x.get('title') or '',
            data = '{t.year:04d}{t.month:02d}{t.day:02d}'.format( **locals()),
            opisanie = x.get('text') or '',
        )
        danni.opisanie = '\n'.join( d.strip() for d in danni.opisanie.split('\n') if d.strip())
        danni.rubrika_kysa = sykr( danni.rubrika)
        z = danni.razglobeno = rec2dir.razglobi_imena(
            imena= danni.opisanie.replace(' ','_'),
            rubrika= danni.rubrika or x.get('ime') or '',
            #rubrika_kysa = danni.rubrika_kysa,
            data = danni.data,
            dirname = None
        )
        z = attr2item( z, default='')

        op = danni.opisanie.lower()

        opis = dictOrder()
        s = attr2item( opis)
        s.име = z.ime or '??'
        s.автор     = z.avtori_plus
        s.откъде    = [ danni.rubrika_kysa, danni.data ]
        s.издание   = 'радио'
        s.етикети   = [ z.zagolemi, z.dok,
                            'радиоколекция' in z.rubrika.lower() and 'стихове' not in op and 'прочит',
                            'стихове' in op and 'стихове',
                            ]
        s.година    = z.godina
        s['#част']  = z.nomer
        s['#продължителност'] = sizemins and sizemins*60
        s.вид       = [ k for k in 'разказ стихове'.split() if k in op]
        s.описание  = (z.opisanie or '').replace('_', ' ')
        s['#автори_отделни'] = z.avtori
        if danni.opisanie != s.описание:
            s.ориг_описание = danni.opisanie
        s.ориг_рубрика  = danni.rubrika
        #s.dirname = z.dirname

        if tyrsach and not z.bez_ime and z.ime:
            ime = z.ime
            if z.avtori_plus: ime += ' : '+z.avtori_plus
            s['#подобни'] = '\n# '.join( ['']+[str(t) for t in tyrsach( ime )])

        for k,v in list( opis.items()):
            if isinstance( v, (tuple,list)):
                v = ' '.join( str(x) for x in v if x )
            if not v: del opis[k]
            else:
                if isinstance( v,str) and v.isdigit(): v = int(v)
                opis[ k ] = v

        def filtr( fname ):
            return ''.join((fname
                    ).replace( '._', '.'
                    ).replace( '__', '_'
                    ).replace( '+_', '+'
                    ).replace( '_+', '+'

                    ).replace( '"',  ''
                    ).replace( "'",  ''
                    ).replace( '(',  '['   #sh
                    ).replace( ')',  ']'   #sh
                    ).replace( ':',  ''    #mplayer,make
                    ).replace( ',',  ''    #mplayer
                    #).replace( rI+'V'+p, '.4'
                    #).replace( 'V'+p,    '.5'
                    #).replace( 3*rI+p,'.3'
                    #).replace( 2*rI+p,'.2'
                    #).replace( 1*rI+p,'.2'
                    ##).replace( '\xA0',' '
                    ).split())

        dirname = fname_kanal+'/'+fname_kanal_vreme
        makedirs( dirname)
        ldirname = z.get( 'dirname_cyr','').rsplit('--радио')[0]
        if ldirname in ('радио', danni.rubrika_kysa, danni.rubrika, danni.rubrika.replace(' ','_'), z.get('rubrika_') ):
            ldirname = ''
        if danni.rubrika_kysa:
            rubr = rec2dir.filt_er( danni.rubrika)
            if ldirname.startswith( rubr):
                ldirname = ldirname[ len( rubr):].lstrip('-')
        #print( '33333333333333333', ldirname, danni.rubrika, file= sys.stderr)
        ldirname = '+'.join( n for n in [ fname_kanal_vreme,
                danni.rubrika_kysa or x.get('ime'),
                ldirname[:60] ]
                if n )

        for d in glob.glob( fname_kanal_vreme+'*'):
            try: os.remove( d)
            except: pass
        try:
            os.symlink( dirname, ldirname)
        except: pass

        fname = (z.dirname or fname_kanal)[:60]
        fname = filtr( sykr( fname))
        fname = dirname + '/' + fname
        usability.Dumper.force_block = '>'
        usability.Dumper.shorten_width = 15
        komentari = [ (k + ': ' +str(opis.pop(k))) for k in list( opis.keys()) if k[0]=='#' ]
        VIMtail = '# v' + 'im:ts=4:sw=4:expandtab:ft=yaml' #separated!
        r = usability.dump( opis)
        r += '\n'+'\n'.join( komentari)
        r += '''

срез:
участници:
 редактор:
 превод:
 драматизация:
 адаптация:
 изпълнение:
 музика:
 зв.реж:
 зв.оп:
 зв.оформ:
 муз.оформ:
 запис:
 режисьор:
съдържание:

''' + 0*'''
#излишните полета може да се оставят празни или да се изтрият
#срез:
# 12:34.5 - 890.7
#или
# 722 -
# - 900.2
#или
# име.на.парче1
# от-до
# име.на.парче2
# от-до
# ...

''' + VIMtail
        fopis0 = os.path.join( dirname, 'opis')
        fopis = fopis0 + '1'
        while os.path.exists( fopis):
            ro = open( fopis).read()
            if ro == r: break
            fopis += '1'
        else:
            with fopen( fopis) as f:
                f.write( r)

        #opis = link( opis1)
        if os.path.exists( fopis0):
            s = os.stat( fopis0)
            if s.st_nlink >1:
                os.remove( fopis0)
            else:
                f0 = fopis0
                while os.path.exists( f0):
                    f0 += '0'
                os.rename( fopis0, f0)
        os.link( fopis, fopis0)

        if o.cron_earlier_minutes:
            t -= datetime.timedelta( minutes= o.cron_earlier_minutes)

        if sizemins:
            if o.cron_later_percent:
                sizemins += (sizemins * o.cron_later_percent) // 100
            if o.cron_earlier_minutes: sizemins += o.cron_earlier_minutes
            if o.cron_later_minutes:
                sizemins += o.cron_later_minutes

        print( t.minute, t.hour, t.day, t.month, '*',
                o.cron_user or '',
                o.cron,
                sizemins and '-m '+str(sizemins) or '',
                '--fname', fname,
                '--stream', x.stream,
             )
Beispiel #6
0
def mkdir( dir):
    log()
    makedirs( dir, exist_ok =True)
Beispiel #7
0
                mkdir = False
                for f in files:
                    if not included(f, optz.include, optz.exclude):
                        continue
                    fsrc = realpath(join(path, f))
                    if not mkdir:
                        yield None, targdeeper
                        mkdir = True
                    yield fsrc, join(targdeeper, f)


op = op2op.get(optz.op)
for s, t in walk(argz, target):
    try:
        if not s:
            if op:
                osextra.makedirs(t)
            else:
                print(">>", t + "/")
        elif op:
            if exists(t) and optz.force:
                os.remove(t)
            op(s, t)
        else:
            print("--", s, "\n->", t)
    except:
        print("--", s, "\n->", t)
        raise

# vim:ts=4:sw=4:expandtab
Beispiel #8
0
def makedirs( *a):
    if verbose or 1: dbg( 'makedirs', a)
    if not optz.dont: osextra.makedirs( *a)
Beispiel #9
0
    def write( me,
            maketoc =False,
            scale   =1,
            fps     =1,
            offset  =0,
            do_nothing  =False,
            verbose     =False,
            makedir = False,
            path    = '',
            ofile   = None,
            ofile_as_sfx =None, #priority over ofile
            infile  = None,
            inpath  = '.',
            **kargs_ignore
            ):
        options = DictAttr( locals())

        if not infile.startswith( '/') :
            infile = join( inpath, infile)

        if options.verbose:
            print( infile)
            for c in me.cuts: print( ' '.join( k+'='+str(v) for k,v in c.items()))

        for a in infile, splitext( infile)[0]:
            a += '.wav'
            if exists( a):
                infile = a
                break
        else:
            print( '!!! nema go', a)
            return

        import wave
        i = wave.open( infile, 'r')
        cur = 0
        p = DictAttr()
        params = (p.nchannels, p.sampwidth, p.framerate, p.nframes, p.comptype, p.compname) = i.getparams()
        #size = p.nframes/float(p.framerate)
        #print nframes, framerate
        if verbose: print( '%sHz %s x %sbit' % (p.framerate, p.nchannels, p.sampwidth*8))

        def sec2frames(sec):
            return int(sec * p.framerate)

        ofile = basename( ofile or infile)    #no dirs
        for a in 'wav avi flac mp3'.split():
            if ofile.endswith( '.'+a): ofile = ofile[:-1-len(a)]
        if ofile_as_sfx: ofile += ofile_as_sfx
        opath = options.path
        if opath:
            makedirs( opath)
            ofile = join( opath, ofile)
        if options.makedir:
            makedirs( ofile)
            ofile += '/'
        else:
            ofile += '.'

        nfmt = len(me.cuts)>=10 and '%02d' or '%d'
        n=0
        oname = ''
        for rec in me.cuts:
            #print '> %(name)r : %(start)s - %(end)s  %(nopause)s' % rec
            nopause = rec.nopause and 'nopause' or ''

            if not rec.name:
                assert oname
                #oname = oname + '.x'   #има номерация и без това
            else:
                oname = rec.name
            ss = sum( point2sec( x, options.scale, options.fps ) for x in rec.start.split('+'))
            se = sum( point2sec( x, options.scale, options.fps ) for x in rec.end.split('+'))
            if options.offset:
                ss += options.offset
                se += options.offset
            fs,fe = [sec2frames(x) for x in (ss,se) ]
            print( ' ', oname, '>', prnsec(ss),':', prnsec(se), nopause)

            n+=1
            ooname = ofile + '.'.join( [ nfmt % n, oname, 'wav'] )
            print( '      ', ooname)

            if options.do_nothing and (not maketoc or fe <= p.nframes): continue

            skip = fs-cur
            while skip:
                sk = min( 9*60, skip)    #11mb/min
                i.readframes( sk)
                skip -= sk
            data = i.readframes( fe - fs )
            assert data
            cur = fe

            size = se-ss if fe <= p.nframes else len(data)/float( p.nchannels * p.sampwidth * p.framerate)
            me.add2toc( nomer= n, src= ofile, name= oname, fname= ooname, nopause= nopause, size= size,)

            if options.do_nothing: continue

            o = wave.open( ooname, 'w')
            o.setparams( params)
            o.writeframes( data)
            o.close()
Beispiel #10
0
        if a.lower().endswith( '.xls') or a.lower().endswith( '.xlsx'):
            fxls = a
        else: sheets.append( a )

    if not fxls:
        oparser.error( 'needs .xls or .xlsx file')
    if optz.allsheets:
        if sheets: oparser.error( 'needs either --allsheets or sheetnames, not both')
        if fcsv:  print( 'auto-appending sheetnames to .csv name')
        sheets = ALLSHEETS

    from os.path import join,basename
    if not fcsv:
        fcsv = osextra.withoutext( fxls, '.xlsx', '.xls', ignorecase=True)
    else:
        fcsv = osextra.withoutext( fcsv, '.csv', ignorecase=True)
    if optz.path:
        osextra.makedirs( optz.path)
        fcsv = join( optz.path, basename( fcsv))
    if optz.dir_from_name:
        osextra.makedirs( fcsv)
    for sheetname, data in read( fxls, sheets):
        if optz.strip:      strip( data, optz.strip.split(',') )
        if optz.columns:    select_columns( data, optz.columns.split(','))
        afcsv = fcsv
        if sheets:
            afcsv += ('/' if optz.dir_from_name else '.') + sheetname
        write( afcsv + '.csv', data, delimiter= optz.delimiter)

# vim:ts=4:sw=4:expandtab
Beispiel #11
0
    chast = chast and chast[0].strip() or ''

    pyt = http[0].strip().split('/_nradio/')[-1]
    papka = pyt.strip('/').split('/')[-1]
    ime = papka.split('+')
    if ime[0].startswith('hb'):
        ime = ime[-1]
    else:
        assert ime[-1].startswith('hb'), papka
        ime = ime[-2]
    ime_lat = c2l( ime)
    if chast: ime_lat += '-'+chast
    fime = ime_lat+'.opis'
    print( a, ':', fime, '->', pyt)
    if optz.nedei: continue

    if optz.papka:
        pyt = join( optz.papka, pyt)
    osextra.makedirs( pyt, exist_ok= True)
    pyt_ime = join( pyt, fime)
    try:
        os.link( a, pyt_ime )
    except FileExistsError:
        print( '?? FileExistsError', pyt_ime)
    if optz.cmd:
        subprocess.call( optz.cmd.split() + [ a, fime])
    else:
        os.rename( a, fime)

# vim:ts=4:sw=4:expandtab