File: genlang.py

package info (click to toggle)
wxpython4.0 4.2.3%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 221,752 kB
  • sloc: cpp: 962,555; python: 230,573; ansic: 170,731; makefile: 51,756; sh: 9,342; perl: 1,564; javascript: 584; php: 326; xml: 200
file content (244 lines) | stat: -rwxr-xr-x 7,055 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#!/usr/bin/env python

# Run this script from top-level wxWidgets directory to update the contents of
# include/wx/intl.h and src/common/intl.cpp using information from langtabl.txt
#
# Warning: error detection and reporting here is rudimentary, check if the
# files were updated correctly with "git diff" before committing them!

import os
import string
import sys

def ReadScriptTable():
    scripttable = []
    try:
        f = open('misc/languages/scripttabl.txt')
    except:
        print("Did you run the script from top-level wxWidgets directory?")
        raise

    for i in f.readlines():
        ispl = i.split()
        scripttable.append((ispl[0], ispl[1]))
    f.close()
    return scripttable


def ReadTable():
    table = []
    try:
        f = open('misc/languages/langtabl.txt')
    except:
        print("Did you run the script from top-level wxWidgets directory?")
        raise

    for i in f.readlines():
        ispl = i.split()
        table.append((ispl[0], ispl[1], ispl[2], ispl[3], ispl[4], ispl[5], ispl[6], string.join(ispl[7:])))
    f.close()
    return table


def WriteEnum(f, table, scripttable):
   f.write("""
/**
    The languages supported by wxLocale.

    This enum is generated by misc/languages/genlang.py
    When making changes, please put them into misc/languages/langtabl.txt
*/
enum wxLanguage
{
    /// User's default/preferred language as got from OS.
    wxLANGUAGE_DEFAULT,

    /// Unknown language, returned if wxLocale::GetSystemLanguage fails.
    wxLANGUAGE_UNKNOWN,

""");
   knownLangs = []
   for i in table:
       if i[0] not in knownLangs:
          f.write('    %s,\n' % i[0])
          knownLangs.append(i[0])
   f.write("""
    /// For custom, user-defined languages.
    wxLANGUAGE_USER_DEFINED,

    /// Synonyms.
    wxLANGUAGE_AZERI = wxLANGUAGE_AZERBAIJANI,
    wxLANGUAGE_AZERI_CYRILLIC = wxLANGUAGE_AZERBAIJANI_CYRILLIC,
    wxLANGUAGE_AZERI_LATIN = wxLANGUAGE_AZERBAIJANI_LATIN,
    wxLANGUAGE_BENGALI = wxLANGUAGE_BANGLA,
    wxLANGUAGE_BENGALI_BANGLADESH = wxLANGUAGE_BANGLA_BANGLADESH,
    wxLANGUAGE_BENGALI_INDIA = wxLANGUAGE_BANGLA_INDIA,
    wxLANGUAGE_BHUTANI = wxLANGUAGE_DZONGKHA,
    wxLANGUAGE_CHINESE_SIMPLIFIED = wxLANGUAGE_CHINESE_CHINA,
    wxLANGUAGE_CHINESE_TRADITIONAL = wxLANGUAGE_CHINESE_TAIWAN,
    wxLANGUAGE_CHINESE_MACAU = wxLANGUAGE_CHINESE_MACAO,
    wxLANGUAGE_KERNEWEK = wxLANGUAGE_CORNISH,
    wxLANGUAGE_MALAY_BRUNEI_DARUSSALAM = wxLANGUAGE_MALAY_BRUNEI,
    wxLANGUAGE_ORIYA = wxLANGUAGE_ODIA,
    wxLANGUAGE_ORIYA_INDIA = wxLANGUAGE_ODIA_INDIA,
    wxLANGUAGE_SPANISH_MODERN = wxLANGUAGE_SPANISH,

    /// Obsolete synonym.
    wxLANGUAGE_CAMBODIAN = wxLANGUAGE_KHMER
};

""")


def WriteTable(f, table, scripttable):
   sctable = ''
   for i in scripttable:
       scname = '"%s"' % i[0]
       scalias = '"%s"' % i[1]
       sctable += '    { %s, %s },\n' % (scname, scalias)

   lngtable = ''

   for i in table:
       ibcp47 = '"%s"' % i[1]
       ican = '"%s"' % i[2]
       if ican == '"-"': ican = '""'
       icanbase = '"%s"' % i[3]
       if icanbase == '"-"': icanbase = '""'
       ilang = i[4]
       if ilang == '-': ilang = '0'
       isublang = i[5]
       if isublang == '-': isublang = '0'
       if (i[6] == "LTR") :
           ilayout = "wxLayout_LeftToRight"
       elif (i[6] == "RTL"):
           ilayout = "wxLayout_RightToLeft"
       else:
           print "ERROR: Invalid value for the layout direction";
       lngtable += '    { %-60s %-17s, %-28s, %-15s, %-4s, %-4s, %s, %s },\n' % \
                     ((i[0]+','), ibcp47, ican, icanbase, ilang, isublang, ilayout, i[7])

   f.write("""
// The following data tables are generated by misc/languages/genlang.py
// When making changes, please put them into misc/languages/langtabl.txt

#if !defined(__WIN32__)

#define SETWINLANG(info,lang,sublang)

#else

#define SETWINLANG(info,lang,sublang) \\
    info.WinLang = tabLangData[j].lang; \\
    info.WinSublang = tabLangData[j].sublang;

#endif // __WIN32__

// Data table for known languages
static const struct langData_t
{
    int   wxlang;
    const char* bcp47tag;
    const char* canonical;
    const char* canonicalref;
    wxUint32 winlang;
    wxUint32 winsublang;
    wxLayoutDirection layout;
    const char* desc;
    const char* descnative;
}
tabLangData[] =
{
%s
    { 0, NULL, NULL, NULL, 0, 0, wxLayout_Default, NULL, NULL }
};

// Data table for known language scripts
static const struct scriptData_t
{
    const char* scname;
    const char* scalias;
}
tabScriptData[] =
{
%s
    { NULL, NULL }
};

void wxUILocale::InitLanguagesDB()
{
    wxLanguageInfo info;
    int j;

    // Known languages
    for (j = 0; tabLangData[j].wxlang != 0; ++j)
    {
      info.Language = tabLangData[j].wxlang;
      info.LocaleTag = tabLangData[j].bcp47tag;
      info.CanonicalName = tabLangData[j].canonical;
      info.CanonicalRef = tabLangData[j].canonicalref;
      info.LayoutDirection = tabLangData[j].layout;
      info.Description = wxString::FromUTF8(tabLangData[j].desc);
      info.DescriptionNative = wxString::FromUTF8(tabLangData[j].descnative);
      SETWINLANG(info, winlang, winsublang)
      AddLanguage(info);
    }

    // Known language scripts
    for (j = 0; tabScriptData[j].scname; ++j)
    {
      gs_scmap_name2alias[tabScriptData[j].scname] = tabScriptData[j].scalias;
      gs_scmap_alias2name[tabScriptData[j].scalias] = tabScriptData[j].scname;
    }
}

""" % (lngtable,sctable))


def ReplaceGeneratedPartOfFile(fname, func):
    """
        Replaces the part of file marked with the special comments with the
        output of func.

        fname is the name of the input file and func must be a function taking
        a file and language table on input and writing the appropriate chunk to
        this file, e.g. WriteEnum or WriteTable.
    """
    fin = open(fname, 'rt')
    fnameNew = fname + '.new'
    fout = open(fnameNew, 'wt')
    betweenBeginAndEnd = 0
    afterEnd = 0
    for l in fin.readlines():
        if l == '// --- --- --- generated code begins here --- --- ---\n':
            if betweenBeginAndEnd or afterEnd:
                print 'Unexpected starting comment.'
            betweenBeginAndEnd = 1
            fout.write(l)
            func(fout, table, scripttable)
        elif l == '// --- --- --- generated code ends here --- --- ---\n':
            if not betweenBeginAndEnd:
                print 'End comment found before the starting one?'
                break

            betweenBeginAndEnd = 0
            afterEnd = 1

        if not betweenBeginAndEnd:
            fout.write(l)

    if not afterEnd:
        print 'Failed to process %s.' % fname
        os.remove(fnameNew)
        sys.exit(1)

    fout.close()
    fin.close()
    os.remove(fname)
    os.rename(fnameNew, fname)

table = ReadTable()
scripttable = ReadScriptTable()
ReplaceGeneratedPartOfFile('include/wx/language.h', WriteEnum)
ReplaceGeneratedPartOfFile('interface/wx/language.h', WriteEnum)
ReplaceGeneratedPartOfFile('src/common/languageinfo.cpp', WriteTable)