genlang.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #!/usr/bin/env python
  2. # Run this script from top-level wxWidgets directory to update the contents of
  3. # include/wx/intl.h and src/common/intl.cpp using information from langtabl.txt
  4. #
  5. # Warning: error detection and reporting here is rudimentary, check if the
  6. # files were updated correctly with "svn diff" before committing them!
  7. import os
  8. import string
  9. import sys
  10. def ReadTable():
  11. table = []
  12. try:
  13. f = open('misc/languages/langtabl.txt')
  14. except:
  15. print "Did you run the script from top-level wxWidgets directory?"
  16. raise
  17. for i in f.readlines():
  18. ispl = i.split()
  19. table.append((ispl[0], ispl[1], ispl[2], ispl[3], ispl[4], string.join(ispl[5:])))
  20. f.close()
  21. return table
  22. def WriteEnum(f, table):
  23. f.write("""
  24. /**
  25. The languages supported by wxLocale.
  26. This enum is generated by misc/languages/genlang.py
  27. When making changes, please put them into misc/languages/langtabl.txt
  28. */
  29. enum wxLanguage
  30. {
  31. /// User's default/preffered language as got from OS.
  32. wxLANGUAGE_DEFAULT,
  33. /// Unknown language, returned if wxLocale::GetSystemLanguage fails.
  34. wxLANGUAGE_UNKNOWN,
  35. """);
  36. knownLangs = []
  37. for i in table:
  38. if i[0] not in knownLangs:
  39. f.write(' %s,\n' % i[0])
  40. knownLangs.append(i[0])
  41. f.write("""
  42. /// For custom, user-defined languages.
  43. wxLANGUAGE_USER_DEFINED
  44. };
  45. """)
  46. def WriteTable(f, table):
  47. all_langs = []
  48. all_sublangs = []
  49. lngtable = ''
  50. ifdefs = ''
  51. for i in table:
  52. ican = '"%s"' % i[1]
  53. if ican == '"-"': ican = '""'
  54. ilang = i[2]
  55. if ilang == '-': ilang = '0'
  56. isublang = i[3]
  57. if isublang == '-': isublang = '0'
  58. if (i[4] == "LTR") :
  59. ilayout = "wxLayout_LeftToRight"
  60. elif (i[4] == "RTL"):
  61. ilayout = "wxLayout_RightToLeft"
  62. else:
  63. print "ERROR: Invalid value for the layout direction";
  64. lngtable += ' LNG(%-38s %-7s, %-15s, %-34s, %s, %s)\n' % \
  65. ((i[0]+','), ican, ilang, isublang, ilayout, i[5])
  66. if ilang not in all_langs: all_langs.append(ilang)
  67. if isublang not in all_sublangs: all_sublangs.append(isublang)
  68. for s in all_langs:
  69. if s != '0':
  70. ifdefs += '#ifndef %s\n#define %s (0)\n#endif\n' % (s, s)
  71. for s in all_sublangs:
  72. if s != '0' and s != 'SUBLANG_DEFAULT':
  73. ifdefs += '#ifndef %s\n#define %s SUBLANG_DEFAULT\n#endif\n' % (s, s)
  74. f.write("""
  75. // This table is generated by misc/languages/genlang.py
  76. // When making changes, please put them into misc/languages/langtabl.txt
  77. #if !defined(__WIN32__) || defined(__WXMICROWIN__)
  78. #define SETWINLANG(info,lang,sublang)
  79. #else
  80. #define SETWINLANG(info,lang,sublang) \\
  81. info.WinLang = lang, info.WinSublang = sublang;
  82. %s
  83. #endif // __WIN32__
  84. #define LNG(wxlang, canonical, winlang, winsublang, layout, desc) \\
  85. info.Language = wxlang; \\
  86. info.CanonicalName = wxT(canonical); \\
  87. info.LayoutDirection = layout; \\
  88. info.Description = wxT(desc); \\
  89. SETWINLANG(info, winlang, winsublang) \\
  90. AddLanguage(info);
  91. void wxLocale::InitLanguagesDB()
  92. {
  93. wxLanguageInfo info;
  94. %s
  95. }
  96. #undef LNG
  97. """ % (ifdefs, lngtable))
  98. def ReplaceGeneratedPartOfFile(fname, func):
  99. """
  100. Replaces the part of file marked with the special comments with the
  101. output of func.
  102. fname is the name of the input file and func must be a function taking
  103. a file and language table on input and writing the appropriate chunk to
  104. this file, e.g. WriteEnum or WriteTable.
  105. """
  106. fin = open(fname, 'rt')
  107. fnameNew = fname + '.new'
  108. fout = open(fnameNew, 'wt')
  109. betweenBeginAndEnd = 0
  110. afterEnd = 0
  111. for l in fin.readlines():
  112. if l == '// --- --- --- generated code begins here --- --- ---\n':
  113. if betweenBeginAndEnd or afterEnd:
  114. print 'Unexpected starting comment.'
  115. betweenBeginAndEnd = 1
  116. fout.write(l)
  117. func(fout, table)
  118. elif l == '// --- --- --- generated code ends here --- --- ---\n':
  119. if not betweenBeginAndEnd:
  120. print 'End comment found before the starting one?'
  121. break
  122. betweenBeginAndEnd = 0
  123. afterEnd = 1
  124. if not betweenBeginAndEnd:
  125. fout.write(l)
  126. if not afterEnd:
  127. print 'Failed to process %s.' % fname
  128. os.remove(fnameNew)
  129. sys.exit(1)
  130. os.remove(fname)
  131. os.rename(fnameNew, fname)
  132. table = ReadTable()
  133. ReplaceGeneratedPartOfFile('include/wx/language.h', WriteEnum)
  134. ReplaceGeneratedPartOfFile('interface/wx/language.h', WriteEnum)
  135. ReplaceGeneratedPartOfFile('src/common/languageinfo.cpp', WriteTable)