scripts / switch-layout.py

#!/usr/bin/env python
# coding: utf-8
"""
конвертирует раскладку клавиатуры для ошибочно введенной строки
Andy Mikhailenko, 2007-10-05
"""
import sys


LAT_CHARS = u'''QWERTYUIOP{}|ASDFGHJKL:"ZXCVBNM<>?qwertyuiop[]\\asdfghjkl;'zxcvbnm,./'''
CYR_CHARS = u'''ЙЦУКЕНГШЩЗХЪ/ФЫВАПРОЛДЖЭЯЧСМИТЬБЮ,йцукенгшщзхъ\\фывапролджэячсмитьбю.'''


_pairs = tuple((LAT_CHARS[i], CYR_CHARS[i]) for i in range(len(LAT_CHARS)))

LAT2CYR = dict((a,b) for a,b in _pairs)
CYR2LAT = dict((b,a) for a,b in _pairs)


def convert(line):
    lat_chars_cnt = len(set(line).intersection(LAT_CHARS))
    cyr_chars_cnt = len(set(line).intersection(CYR_CHARS))
    idx = LAT2CYR if lat_chars_cnt > cyr_chars_cnt else CYR2LAT
    return ''.join(idx.get(char, char) for char in line)


if __name__ == '__main__':
    if len(sys.argv) > 1:
        line = ' '.join(sys.argv[1:]).decode('utf8')
        print convert(line)
    else:
        print '# Usage:\n#   python switch-layout.py hello world'
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.