PageRenderTime 71ms CodeModel.GetById 51ms app.highlight 15ms RepoModel.GetById 2ms app.codeStats 0ms

/win32/reggen.py

https://bitbucket.org/tortoisehg/hgtk/
Python | 108 lines | 97 code | 4 blank | 7 comment | 5 complexity | a7fdf35fc80953904c9751cf2b42945f MD5 | raw file
  1# reggen.py - registry file generator for Windows shell context menus
  2#
  3# Copyright 2009 Yuki KODAMA <endflow.net@gmail.com>
  4#
  5# This software may be used and distributed according to the terms of the
  6# GNU General Public License version 2, incorporated herein by reference.
  7
  8import sys
  9import os.path
 10import glob
 11import re
 12import codecs
 13
 14# based on 'nautilus-thg.py' and 'hgtk'
 15def _thg_path():
 16    pfile = __file__
 17    if pfile.endswith('.pyc'):
 18        pfile = pfile[:-1]
 19    path = os.path.dirname(os.path.dirname(os.path.realpath(pfile)))
 20    thgpath = os.path.normpath(path)
 21    testpath = os.path.join(thgpath, 'tortoisehg')
 22    if os.path.isdir(testpath) and thgpath not in sys.path:
 23        sys.path.insert(0, thgpath)
 24_thg_path()
 25
 26from mercurial import demandimport
 27demandimport.ignore.append('win32com.shell')
 28demandimport.enable()
 29from tortoisehg.util.menuthg import thgcmenu
 30
 31regkeytmpl = u'[HKEY_CURRENT_USER\\Software\\TortoiseHg\\CMenu\\%s\\%s]'
 32regheaders = (  u'Windows Registry Editor Version 5.00',
 33                u'',
 34                u'[HKEY_CURRENT_USER\\Software\\TortoiseHg]',
 35                u'"CMenuLang"="%(lang)s"',
 36                u'',
 37                u'[HKEY_CURRENT_USER\\Software\\TortoiseHg\\CMenu]',
 38                u'',
 39                u'[HKEY_CURRENT_USER\\Software\\TortoiseHg\\CMenu\\%(lang)s]')
 40
 41# regex patterns used to extract strings from PO files
 42pat_id = re.compile(u'^msgid "([^\\"]+)"')
 43pat_str = re.compile(u'^msgstr "([^\\"]+)"')
 44def lookup(file):
 45    def stripmsg(line, pat):
 46        m = pat.match(line)
 47        if m:
 48            return m.group(1)
 49    # acquire all translatable strings
 50    # and set fallback messages
 51    i18n = {}
 52    msgids = []
 53    for cmenu in thgcmenu.values():
 54        label = cmenu['label']['id'].decode('utf-8')
 55        msgids.append(label)
 56        i18n[label] = label
 57        help = cmenu['help']['id'].decode('utf-8')
 58        msgids.append(help)
 59        i18n[help] = help
 60    # lookup PO file
 61    if file:
 62        foundmsgid = False
 63        f = codecs.open(file, 'r', 'utf-8')
 64        for line in f.readlines():
 65            line = line.rstrip(u'\r\n')
 66            if foundmsgid:
 67                msgstr = stripmsg(line, pat_str)
 68                if msgstr:
 69                    i18n[msgid] = msgstr
 70                foundmsgid = False
 71            else:
 72                msgid = stripmsg(line, pat_id)
 73                if msgid and msgid in msgids:
 74                    foundmsgid = True
 75        f.close()
 76    return i18n
 77
 78def wopen(path):
 79    newfile = codecs.open(path, 'w','utf-16-le')
 80    newfile.write(codecs.BOM_UTF16_LE.decode('utf-16-le'))
 81    def write(lines, newlines=2):
 82        if isinstance(lines, (str, unicode)):
 83            buf = lines
 84        else:
 85            buf = u'\r\n'.join(lines)
 86        buf = (buf + (u'\r\n' * newlines))
 87        newfile.write(buf)
 88    def close():
 89        newfile.close()
 90    return write, close
 91
 92# enumerate available languages
 93langinfo = [{'code': u'en_US', 'file': None}]
 94lang_pat = re.compile(u'([^\\.]+)\\.po$')
 95for file in glob.glob(u'../i18n/tortoisehg/*.po'):
 96    m = lang_pat.match(os.path.basename(file))
 97    langinfo.append({'code': m.group(1), 'file': os.path.abspath(file)})
 98
 99# output REG files
100for lang in langinfo:
101    write, close = wopen(u'thg-cmenu-%s.reg' % lang['code'])
102    write([h % {'lang': lang['code']} for h in regheaders])
103    i18n = lookup(lang['file'])
104    for hgcmd, cmenu in thgcmenu.items():
105        write(regkeytmpl % (lang['code'], hgcmd.decode('utf-8')), 1)
106        write((u'"menuText"="%s"' % i18n[cmenu['label']['id'].decode('utf-8')],
107               u'"helpText"="%s"' % i18n[cmenu['help']['id'].decode('utf-8')]))
108    close()