-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsync-localization-compat.py
More file actions
150 lines (113 loc) · 4.65 KB
/
Copy pathsync-localization-compat.py
File metadata and controls
150 lines (113 loc) · 4.65 KB
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
#
# (c) Morthimer McMare a.k.a. JSO_x, 2023-2026
#
# Base files by PROPHESSOR and Morthimer McMare:
# https://github.com/DRRP-Team/DRRP/blob/master/tools/updateOldLocalization.py
# https://github.com/LLDM-Doom-Modding/ZChecker/blob/master/SyncLocalization.py
#
import csv
import sys
from pathlib import Path
def escape_incompatible_sequences( text: str ) -> str:
return text.replace( '"', '\\"' ).replace( '\n', '\\n' ).replace( "ё", "е" ).replace( "—", "-" )
def parse_csv( csvrows ) -> tuple[dict[str, list[str]], list[str]]:
outparsed = {}
outheader = []
header = next( csvrows )
headeridskip = None
# Most of default manpages CSV language headers are in format:
# "Identifier, Remark, Identifier, default, lang1, lang2, ..."
#
# GZDoom will skip first "Identifier" column in this case, so I use this
# behaviour to add a Remark field before the ID.
if ( header[ 0 ] == 'Identifier' and 'Identifier' in header[ 1 : ] ):
headeridskip = 0
print( f" Skipped dummy column 'Identifier' at index {headeridskip}." )
columns = [[] for _ in header]
# Convert CSV to the list of columns:
for row in csvrows:
for col, cell in enumerate( row ):
if col == headeridskip:
continue
#print( col, cell )
columns[ col ].append( cell )
if headeridskip is not None:
outheader = header[ : headeridskip ] + header[ headeridskip + 1 : ]
columns = columns[ : headeridskip ] + columns[ headeridskip + 1 : ]
#print( f'outheader: {outheader} (len {len(outheader)})' )
else:
outheader = header
#print( "\n=========" )
# Convert columns to `dict<str('Header'), list>`:
for idx, i in enumerate( outheader ):
#print( f"parsed[ {i} ] = columns[ {idx} ]: {columns[idx]} (len {len(columns[idx])})", end='\n\n' )
outparsed[ i ] = columns[ idx ]
return outparsed, outheader
pass # of def parse_csv( csvrows )
csvlangfiles = list( Path( "." ).glob( "LANGUAGE.*.csv" ) )
#csvlangfiles = [ "LANGUAGE.manpages_general.csv" ]
parsedcolumns = {}
for csvfileidx, csvfile in enumerate( csvlangfiles ):
curparsedcolumns = {}
curheader = []
print( f'Parsing CSV file "{csvfile}"...' )
with open( str(csvfile), mode='r', encoding='utf-8' ) as infile:
(curparsedcolumns, curheader) = parse_csv( csv.reader( infile ) )
for i in curheader:
if not parsedcolumns.get( i ):
if csvfileidx == 0:
print( f" Added new column '{i}'." )
parsedcolumns[ i ] = list()
else:
print( f" Error: extra column with header '{i}' in file '{csvfile}'." )
exit( 1 )
# Adds everything to the global columns:
for k, v in curparsedcolumns.items():
parsedcolumns[ k ] += v
if not parsedcolumns.get( 'Identifier' ) or not parsedcolumns.get( 'Remark' ):
print( "No column 'Identifier' or 'Remark' in parsedcolumns." )
exit( 1 ) # Wrong format?
compat_filename_remapping = {
"ru": "rus",
"default": "enu"
}
compat_headers_remapping = {
"ru": "ru",
"default": "enu default"
}
compat_encoding_remapping = {
"ru": "CP1251"
}
print( "Resulting parsed columns: " + str( [i[0] for i in parsedcolumns.items()] ) )
for column in parsedcolumns.items():
colname = column[ 0 ]
if colname == 'Identifier' or colname == 'Remark':
print( " Skipped non-language column '" + colname + "'." )
continue
# Configuring output file:
compatfilesuffix = compat_filename_remapping.get( colname, colname )
compatfileheader = compat_headers_remapping.get( colname, colname )
compatfileencoding = compat_encoding_remapping.get( colname, 'CP1252' )
compatfilename = "LANGUAGE.gzdoom330." + compatfilesuffix
print( f' Creating LANGUAGE lump "{compatfilename}" for \'{colname}\' with encoding "{compatfileencoding}" and header [{compatfileheader}].' )
# Write to current output language file:
with open( compatfilename, mode='w', encoding=compatfileencoding ) as outfile:
#if lfprint is not None:
# del lfprint
def lwrite( *args, sep=' ', end='\n' ):
outfile.write( sep.join( map( str, args ) ) + end )
# Write headers:
lwrite( f"// This file is automatically generated by {sys.argv[0]}." )
lwrite( "// Update localization lines in \"LANGUAGE.*.csv\" instead.", end = '\n\n' )
lwrite( "[" + compatfileheader + "]\n\n" )
#print( "\nparsedcolumns['Identifier'], column, parsedcolumns['Remark'] ==", len(parsedcolumns['Identifier']), len(column), len(parsedcolumns['Remark']), '\n' )
for (lid, text, remark) in zip( parsedcolumns[ 'Identifier' ], column[ 1 ], parsedcolumns[ 'Remark' ], strict = True ):
if not lid:
if remark:
lwrite( "// %s" % (remark) )
else:
lwrite()
else:
filteredtext = escape_incompatible_sequences( text )
lwrite( "%-37s = \"%s\";" % (lid, filteredtext), end = f' // {remark}\n' if remark else '\n' )
print( "Updated successfully." )