PageRenderTime 39ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/win32/reggen.py

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