def opts(): optz.help( '''%prog [опции] папка .. обхожда папките и зарежда данни от файлове именовани "opis", и изпълнява действия с тези данни''') gg = optz.grouparg( 'данни') optz.str( 'prevodi', help= 'файл-речник с преводи файл=заглавие или заглавие=файл (lat=cyr)', **gg) optz.str( 'prevodi_meta', help= 'файл-речник с преводи на понятия (lat=cyr) - хора, организации, ..', **gg) optz.str( 'filename_enc', help= 'кодировка на имената на файловете [или това от терминала]', **gg) gg = optz.grouparg( 'описи') optz.bool( 'zapis_opisi', help= '(пре)записва описите', **gg) optz.append( 'etiket', '-e',help= 'добавя етикета към _всички описи'+helplist, **gg) optz.bool( 'sort_prevodi', help= 'пренарежда преводите по азбучен ред', **gg) optz.bool( 'popravi_opisi', help= 'имената в речника с преводи имат превес над местните в описите', **gg) optz.bool( 'podravni_po_grupi', help= 'подравнява в описа всяка група за себе си; иначе подравнява всички заедно', **gg) optz.bool( 'mnogoredovi_etiketi', help= 'многоредовите се записват като 1 ключ:: с много редове, без пренасяне (иначе много ключ:реда)', **gg) optz.int( 'shirina_tekstove', default=80, help= 'ширина на пренасяне на текстове; подразбиране- %default', **gg) optz.bool( 'yaml') #ignore optz.bool( 'noyaml') #optz.bool( 'vinagi', '-f',help= 'записва независимо дали има разлики', **gg) gg = optz.grouparg( 'обхождане') optz.append( 'opisi', help= 'шаблон за името на описите ['+' '.join( OPISI)+']'+helplist, **gg) optz.bool( 'simvolni', '-L', help= 'обхожда и символни връзки', **gg) optz.append( 'bez', help= 'пропуска (папки) по дадения шаблон'+helplist, **gg) optz.append( 'samo', help= 'включва само (папки) по дадения шаблон'+helplist, **gg) optz.append( 'papka_s_opisi', help= 'счита всички файлове вътре за описи (и се прилагат горните шаблони)'+helplist, **gg) gg = optz.grouparg( 'действия с папки') optz.bool( 'preimenovai_papki', help= 'преименова+превежда папките на място', **gg) optz.str( 'prehvyrli_papki', help= 'прехвърля+превежда папки+съдържание към тук/', **gg) optz.bool( 'prehvyrli_simvolno', help= 'прехвърля като символни връзки (иначе са твърди)', **gg) optz.bool( 'davai', help= 'извършва промените', **gg) #разни optz.bool( 'stderr', help= 'грешки и съобщения към stderr') optz.count( 'podrobno', '-v', help= 'показва подробности')
#!/usr/bin/env python3 # -*- coding: utf-8 -*- import os,sys,re from svd_util import optz optz.bool( 'fake', help= 'do nothing') optz.bool( 'link', help= 'link instead of move/rename') optz.bool( 'dirfiles', '-r', help= 'dir and then files inside') #? optz.bool( 'upper', '-u', help= 'just upper-case, all args are filepaths') optz.bool( 'lower', '-l', help= 'just lower-case, all args are filepaths') optz.bool( 'insymlink', help= 'rename inside symlinks-text, ignore non-symlinks') optz.help( ''' %prog [options] regexp subst filepaths subst can also use $1..$6 for groups if filepath is - , reads stdin, one filepath per line ! put -- before args if any filepath/regexp/subst starts with - %prog -u/-l [options] filepaths renu* [options] filepaths renl* [options] filepaths '''.strip() ) optz,argz = optz.get() def renul( x, up): r = os.path.split(x) return os.path.join( r[0], r[1].upper() if up else r[1].lower()) prg = os.path.basename( sys.argv[0] ) if prg.startswith( 'renu'): optz.upper = True if prg.startswith( 'renl'): optz.lower = True if optz.upper: func = lambda x: renul(x,True) elif optz.lower: func = lambda x: renul(x,False)
#!/usr/bin/env python # -*- coding: utf-8 -*- from __future__ import print_function import os, fnmatch from os.path import isdir, basename, exists, join, dirname, realpath from svd_util import optz, osextra optz.help( ''' walklink sourceitem[s] target - mv/cp/ln that sees through dir+file symlinks if targetnotexists TODO if one source, rename it else - make targetdir, move sources into it if target.exists if target.isdir, move sources into it TODO elif target.isfile and one source.isfile and --force, overwrite target else error: multiple or non-file sources into existing target.file ''') optz.bool( 'force', '-f', help= 'force source file to overwrite target file if exists') optz.list( 'exclude', help= 'exclude filepattern (multiple)') optz.list( 'include', help= 'include filepattern (multiple)') optz.list( 'direxclude', help= 'exclude dirpattern (multiple)') optz.list( 'dirinclude', help= 'include dirpattern (multiple)') optz.bool( 'verbose', '-v') optz.simvolni = True #', '-L', help= 'обхожда и символни връзки') ops = { os.rename : 'mv move', os.link : 'ln link', # os.link : 'cp copy',
#!/usr/bin/env python #sdobrev 2007- from svd_util import optz optz.help( '''default use is filter: %prog [options] input_encoding output_encoding <input >output output_encoding default is utf8 ; use "utf2" as special input_encoding to double decode utf8 ''' .strip()) optz.bool( 'reverse', help= 'reverses each line' ) optz.text( 'input', '-i', default= '-', help= 'input filename, -/empty means stdin, default: stdin') optz.text( 'output', '-o', default= '-', help= 'output filename, = means overwrite input, -/empty means stdout, default: stdout') #optz.bool( 'overwrite') #optz.bool( 'unicodeio', help= 'i/o is utf, but encode2input+decode2output') optz, argz = optz.get() import codecs import sys import os stdin = sys.stdin stdout= sys.stdout _v3 = sys.version_info[0]>=3 if _v3: stdin= stdin.buffer stdout=stdout.buffer e_from = argz[0] e_to = (argz[1:] or ['utf8'])[0] utf2 = e_from == 'utf2' if utf2: e_from = 'utf8'
#!/usr/bin/env python # -*- coding: utf-8 -*- from __future__ import print_function #,unicode_literals import sys,os,re from svd_util import optz optz.help( 'find all video files under argument[s] and symlink as flat-filenames into . (replacing / with :)') optz.bool( 'real', '-y', help='do it') optz.bool( 'follow_symlinks', ) optz.str( 'exclude', help= 'regexp to exclude filepaths') optz,argz = optz.get() print( 'dirs:', argz) ignore = None if optz.exclude: print( 'excluding:', optz.exclude) ignore = re.compile( optz.exclude) exts = 'mov mkv mp4 avi 3gp'.lower().split() def walk( roots, followlinks =optz.follow_symlinks, exts =exts): for root in roots: for path,dirs,files in os.walk( root, followlinks= followlinks): for f in files: fpath = os.path.join( path, f) if ignore and ignore.search( fp): continue #'name', '.ext' if os.path.splitext(f)[-1][1:].lower() in exts: yield fpath def do( afrom, bto): print( afrom, ':>', bto)
#!/usr/bin/env python #sdobrev 2007- from svd_util import optz optz.help( '''use as filter: enc2enc [options] input_encoding output_encoding <input >output use utf2 as special input_encoding to double decode utf8 ''' ) optz.bool( 'reverse') #optz.bool( 'unicodeio', help= 'i/o is utf, but encode2input+decode2output') optz, argz = optz.get() import codecs import sys import os stdin = sys.stdin stdout= sys.stdout _v3 = sys.version_info[0]>=3 if _v3: stdin= stdin.buffer stdout=stdout.buffer e_from = argz[0] e_to = argz[1] utf2 = e_from == 'utf2' if utf2: e_from = 'utf8' fi = codecs.getreader( e_from)( stdin, errors='replace') if utf2:
f = open( fname, 'x', newline='') except FileExistsError: print( '! file exists', fname) return print( '>', fname) with f: #open( fname, 'w', newline='') as f: c = csv.writer(f, **(delimiter and dict( delimiter=delimiter) or {})) for row in array: c.writerow( row ) if __name__ == '__main__': from svd_util import optz, osextra optz.help( ''' extract excel sheet(s) into .csv args-in-any-order: file.xlsx [sheetname] [sheetname] .. unless --allsheets, no sheet-names means first if only one, else shows all sheet-names and stops examples: ... path/to/somename.xls --all --dir --path . -->> dumps all sheets as ./somename/sheetname*.csv '''.strip()) optz.str( 'csv', help= 'name of output file.csv ; used as prefix if allsheets; defaults to path+name of file.xlsx + .sheetname-if-specified') optz.str( 'path', help= 'output path, default is whereever .xls is') optz.bool( 'allsheets', help= 'save all sheets, named csvname.sheetname.csv') optz.bool( 'dir_from_name', help= 'create outname as folder, i.e. csvname/sheetname.csv') # optz.bool( 'nostrip', help= 'do not strip empty columns/rows at right/bottom - see also --strip_top_left') optz.str( 'strip', help= 'strip empty columns/rows at where - left,top,bottom,right, comma separated; default [%default]', default= 'right,bottom') optz.str( 'columns', help= 'columns to extract, comma separated (index-0-based, or excel letter A-...) ; default to all') #name if first row of result .csv contain names) ; may be multiple optz.str( 'delimiter', help= '.csv column delimiter, default is comma ","') oparser = optz.oparser optz,argz = optz.get() fcsv = optz.csv sheets = []
#!/usr/bin/env python #svd'2k2-7 'better view onto python profiler results' #XXX BEWARE That 2.4 - 2.5 - 2.6 have diff.file-format try: from svd_util import optz except: import os.path, sys p = os.path.realpath( __file__).split('/') sys.path.insert( 0, '/'.join( p[:-2]) ) import optz #hope it lives in .. optz.help( '%prog [options] [numberlines] infile') optz.bool( 'hotshot', help= 'load data as hotshot') optz.text( 'strip', help= 'regexp to strip from filenames; replaced with .#') optz.text( 'rootstrip', default= '.*/(site-packages|python\d\.\d+)/', help= 'regexp to strip from filenames; replaced with ##; defaults to %defaults', ) optz.bool( 'withcalls', help= 'include call-stacks') options,args = optz.get() import re import pstats def func_strip_path(func_name): filename, line, name = func_name if options.rootstrip: filename = re.sub( options.rootstrip, '##', filename) if options.strip: filename = re.sub( options.strip, '.#', filename) return filename, line, name return func_name
for b in da.* ne.*; do md -p $P/$b ln -nsf ../../$a/org/$b $P/$b/$a done cd - fi done ''' import sys,os from glob import glob from os.path import join, split, isdir, realpath, exists, lexists, basename from instr import meta_prevodi from svd_util.lowercase import dict_lower from svd_util import optz optz.help( '%prog [опции] папка-изход папки-входящи..') optz.str( 'prevodi_meta', help= 'файл-речник с преводи на понятия (lat=cyr) - хора..') optz,args = optz.get() out = args[0] all = args[1:] meta_prevodi = optz.prevodi_meta and meta_prevodi( optz.prevodi_meta, dict= dict_lower, nomer_stoinost=1) or {} def ignored( f, org): if not isdir( f): return True if isdir( org): return False if glob( f+'/*.mp3'): return False return True count = 0 for f in all:
#!/usr/bin/env python # -*- coding: utf-8 -*- from __future__ import print_function import os join = os.path.join from svd_util import optz optz.help( 'apply moves-around-dirs/deletes from template/ into .') optz.bool( 'real', '-y', ) optz.text( 'exclude', '-x', help= 'regexp',) optz.list( 'ignore' , '-i', help= 'file/dir-names', default= []) optz.bool( 'alsodirs' , help= 'check moving of dirs too') optz.bool( 'dup2ignore' , help= 'ignore duplicates') optz.text( 'deldir' , default='del', help= 'folder to move deleted items into [%default]') optz,args = optz.get() none = not optz.real template = args[0].rstrip('/') from collections import defaultdict tree = {} paths = set() try: os.mkdir( 'del') except Exception as e: print( e) #optz.exclude = [ a.rstrip('/') for a in optz.exclude ] exclude = None if optz.exclude: import re
cfg = dict( hyphen= '~', minsize= 4) print( cfg) def tst( res, exp): if res != exp: print( '\n %(res)s ?=\n %(exp)s' % locals()) assert res == exp for w,w1 in zip( t0.split(), t1.split() ): tst( hyphtext( w, **cfg), w1 ) tst( hyphtext( t0, **cfg), t1 ) print( t1) if __name__ =='__main__': from svd_util import optz optz.help( u''' %prog [options] <infile >outfile %prog [options] думи за пренасяне ... '''.rstrip()) optz.text( 'hyphen',help= u'"тире" за отбелязване на пренасянето (меко/скрито-тире); подразбира се "%default"', default= config.hyphen ) optz.bool( 'html', help= u'ползва HTML-меко-тире %(HTML_HYPHEN)r (вместо горното)' % locals() ) optz.str( 'start', help= u'започва да променя след този текст' ) optz.bool( 'htmlpre', help= u'пропуска съдържанието на <pre>..</pre> групи' ) optz.int( 'minsize', help= u'думи под тази дължина не се пренасят; подразбира се %default', default =config.minsize ) optz.bool( 'utf', help= u'вх/изх utf8' ) optz.bool( 'iutf', help= u'вх utf8' ) optz.bool( 'iguess',help= u'вх познай кодировката' ) optz.bool( 'outf', help= u'изх utf8' ) optz.bool( 'i1251', help= u'вх cp1251' ) optz.bool( 'o1251', help= u'изх cp1251' ) optz.bool( 'cp1251',help= u'вх/изх cp1251' ) optz.bool( 'test', help= u'самопроверка' ) optz.bool( 'demo', help= u'демо' )
- взема ПЪТ и ИМЕ (от след _nradio/) - извлича ЧАСТ# от описа преименува описа в ИМЕ-ЧАСТ.opis на място, и го слага също и в (papka+)ПЪТ (създавайки лисващите нива) ''' import os, subprocess from svd_util import optz from svd_util import lat2cyr from svd_util import osextra optz.bool( 'nedei', '-n') optz.text( 'papka', '-p', help= 'начална коренна папка') optz.text( 'cmd', help= 'изпълни вместо преименоване тази команда с 2 аргумента') optz.help( __doc__) #rec2dir def c2l( x): return lat2cyr.zvuchene.cyr2lat( x).lower() join = os.path.join optz,argz = optz.get() for a in argz: o = open(a).readlines() http = [ l.strip('#') for l in o if 'http://' in l and '/_nradio' in l] if not http: print( '??? http-къде', a) continue chast= [ l.split(':',1)[-1] for l in o if l.startswith( '#част:') ] chast = chast and chast[0].strip() or '' pyt = http[0].strip().split('/_nradio/')[-1]