Platinum Partner

Transliterate Filenames From Cyrillic

Recursively traverse provided directories and transliterate file and directory names from cyrillic into latin.


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

from os import walk, rename, unlink, mkdir
from os.path import isdir, exists
from sys import argv, exit, getfilesystemencoding
from shutil import copyfile
import shutil

conversion = {
        u'а' : 'a',
        u'б' : 'b',
        u'в' : 'v',
        u'г' : 'g',
        u'д' : 'd',
        u'е' : 'e',
        u'Ñ‘' : 'e',
        u'ж' : 'zh',
        u'з' : 'z',
        u'и' : 'i',
        u'й' : 'j',
        u'к' : 'k',
        u'л' : 'l',
        u'м' : 'm',
        u'н' : 'n',
        u'о' : 'o',
        u'п' : 'p',
        u'Ñ€' : 'r',
        u'с' : 's',
        u'Ñ‚' : 't',
        u'у' : 'u',
        u'Ñ„' : 'f',
        u'Ñ…' : 'h',
        u'ц' : 'c',
        u'ч' : 'ch',
        u'ш' : 'sh',
        u'щ' : 'sch',
        u'ь' : "'",
        u'Ñ‹' : 'y',
        u'ь' : "'",
        u'э' : 'e',
        u'ÑŽ' : 'ju',
        u'я' : 'ja',
        u'А' : 'A',
        u'Б' : 'B',
        u'Ð’' : 'V',
        u'Г' : 'G',
        u'Д' : 'D',
        u'Е' : 'E',
        u'Ё' : 'E',
        u'Ж' : 'ZH',
        u'З' : 'Z',
        u'И' : 'I',
        u'Й' : 'J',
        u'К' : 'K',
        u'Л' : 'L',
        u'М' : 'M',
        u'Н' : 'N',
        u'О' : 'O',
        u'П' : 'P',
        u'Р' : 'R',
        u'С' : 'S',
        u'Т' : 'T',
        u'У' : 'U',
        u'Ф' : 'F',
        u'Ð¥' : 'H',
        u'Ц' : 'C',
        u'Ч' : 'CH',
        u'Ш' : 'SH',
        u'Щ' : 'SCH',
        u'Ъ' : "'",
        u'Ы' : 'Y',
        u'Ь' : "'",
        u'Э' : 'E',
        u'Ю' : 'JU',
        u'Я' : 'JA',
        }

def cyr2lat(s):
    retval = ""
    for c in s:
        try:
            c = conversion[c]
        except KeyError:
            pass
        retval += c
    return retval
    
if len(argv) == 1:
    print "Usage: %s " % argv[0]
    exit(-1)

processed = []

def recursive_walk(dir):
    # See http://docs.activestate.com/activepython/2.5/whatsnew/2.3/node6.html
    found = []
    dir = unicode(dir)
    for finfo in walk(dir, True):
        dirnames = finfo[1]
        fnames = finfo[2]
        for subdir in dirnames:
            subdir = "%s/%s" % (dir, subdir)
            if subdir in processed:
                continue
            for yield_val in recursive_walk(subdir):
                yield yield_val
        for fname in fnames:
            yield '%s/%s' % (dir, fname)
    raise StopIteration

if __name__ == "__main__":
    fs_enc = getfilesystemencoding()
    for dir in argv[1:]:
        for fpath in recursive_walk(dir):
            new_fpath = cyr2lat(fpath)
            print fpath.encode('utf-8')
            # First make dirs
            path_elts = new_fpath.split('/')
            for idx in range(len(path_elts))[1:]:
                subpath = '/'.join(path_elts[:idx])
                while True:
                    i = 0
                    if exists(subpath):
                        if not isdir(subpath):
                            print '%s exists but is not a directory, will try again' % subpath
                            subpath += str(i)
                            continue
                        else:
                            path_elts[idx - 1] = subpath.split('/')[-1]
                            break
                    else:
                        print 'Creating directory: %s' % subpath
                        mkdir(subpath)
                        break
            print 'Copying %s to %s' % (fpath.encode('utf-8'), new_fpath)
            shutil.copyfile(fpath, new_fpath)
{{ tag }}, {{tag}},

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}
{{ parent.authors[0].realName || parent.author}}

{{ parent.authors[0].tagline || parent.tagline }}

{{ parent.views }} ViewsClicks
Tweet

{{parent.nComments}}