0.027
thk thk Fri. 5 Mar. 2010 18:58 tags linux , python download (903 hits)
#rename_to greeklish.py
#renames filenames with greek characters to greeklish (latin equivalent)

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import shutil,os

map=[
     (["α","Α","ά","Ά"] ,"a") ,
     (["β","Β"] ,"b") ,
     (["γ","Γ"] ,"g") ,
     (["δ","Δ"] ,"d") ,
     (["ε","έ","Ε","Έ"] ,"e") ,
     (["ζ","Ζ"] ,"z") ,
     (["η","ή","Η","Ή"] ,"i") ,
     (["θ","Θ"] ,"th") ,
     (["ι","ί","ϊ","Ϊ","ί","Ί","Ι"] ,"i") ,
     (["κ","Κ"] ,"k") ,
     (["λ","Λ"] ,"l") ,
     (["μ","Μ"] ,"m") ,
     (["ν","Ν",] ,"n") ,
     (["ξ","Ξ"] ,"j") ,
     (["ο","Ο","ό","Ό"] ,"o") ,
     (["π","Π"] ,"p") ,
     (["ρ","Ρ"] ,"r") ,
     (["σ","Σ","ς"] ,"s") ,
     (["τ","Τ"] ,"t") ,
     (["υ","Υ","ύ" , "Ύ","ϋ" ,"Ϋ"] ,"y") ,
     (["φ","Φ"] ,"f") ,
     (["χ","Χ"] ,"x") ,
     (["ψ","Ψ"] ,"ps") ,
     (["ω","Ω","ώ","Ώ"] ,"o") ,

     ]
def conv(name):

    pa = name.replace(" ","_")
    pa=pa.lower()

    pa=transformText(pa,map)
    return pa



def transformText(t,map):
    for repl in map:
        togo = repl[0]
        replacer = repl[1]
        for s in togo:
            t=t.replace(s,replacer)
    return t



def convFilename(filepath):

    if os.path.exists(filepath):


        name, extension = os.path.splitext(filepath)
        name = os.path.basename(filepath)

        dir = os.path.dirname(filepath)

        newName=conv(name)
        newFilePath = os.path.join(dir,newName)

        if filepath == newFilePath:
            print (filepath  + " is ok . skipping.")
        else:
            print ("renaming " + filepath + " to " + newFilePath)
            shutil.move(filepath, newFilePath )

def convFilenames(filename=''):
    if filename:
        convFilename(filename)
    else:
        for root, dirs, files in os.walk('.',False):
            for f in files:
                p= os.path.join(root,f)
                convFilename(p)
            for d in dirs:
                p= os.path.join(root,d)
                convFilename(p)





if __name__ == '__main__':
    import sys
    if len(sys.argv)>1:
        convFilenames(sys.argv[1])
    else:
        ans=input('WARNING all filenames will be converted to greeklish.Continue? (y/n):')
        if ans=='y' or ans=='Y':
            convFilenames()
        else:
            print ("exiting..")