@@ -163,17 +163,15 @@ def runcmd(tool, cmd, doContinue=False):
163
163
config = json .load (fi )
164
164
fi .close ()
165
165
166
- if (options .locales ):
167
- if not config .has_key ("variables" ):
168
- config ["variables" ] = {}
169
- if not config ["variables" ].has_key ("locales" ):
170
- config ["variables" ]["locales" ] = {}
171
- config ["variables" ]["locales" ]["only" ] = options .locales .split (',' )
172
-
173
- if (options .verbose > 6 ):
166
+ if options .locales :
167
+ config ["variables" ] = config .get ("variables" , {})
168
+ config ["variables" ]["locales" ] = config ["variables" ].get ("locales" , {})
169
+ config ["variables" ]["locales" ]["only" ] = options .locales .split (',' )
170
+
171
+ if options .verbose > 6 :
174
172
print (config )
175
173
176
- if ( config . has_key ( "comment" )) :
174
+ if "comment" in config :
177
175
print ("%s: %s" % (options .filterfile , config ["comment" ]))
178
176
179
177
## STEP 1 - copy the data file, swapping endianness
@@ -186,61 +184,55 @@ def runcmd(tool, cmd, doContinue=False):
186
184
listfile = os .path .join (options .tmpdir ,"icudata.lst" )
187
185
runcmd ("icupkg" , "-l %s > %s" "" % (outfile , listfile ))
188
186
189
- fi = open (listfile , 'rb' )
190
- items = fi .readlines ()
191
- items = [items [i ].strip () for i in range (len (items ))]
192
- fi .close ()
193
-
187
+ with open (listfile , 'rb' ) as fi :
188
+ items = [line .strip () for line in fi .read ().decode ("utf-8" ).splitlines ()]
194
189
itemset = set (items )
195
190
196
- if ( options .verbose > 1 ) :
197
- print ("input file: %d items" % ( len (items ) ))
191
+ if options .verbose > 1 :
192
+ print ("input file: %d items" % len (items ))
198
193
199
194
# list of all trees
200
195
trees = {}
201
196
RES_INDX = "res_index.res"
202
197
remove = None
203
198
# remove - always remove these
204
- if config . has_key ( "remove" ) :
199
+ if "remove" in config :
205
200
remove = set (config ["remove" ])
206
201
else :
207
202
remove = set ()
208
203
209
204
# keep - always keep these
210
- if config . has_key ( "keep" ) :
205
+ if "keep" in config :
211
206
keep = set (config ["keep" ])
212
207
else :
213
208
keep = set ()
214
209
215
210
def queueForRemoval (tree ):
216
211
global remove
217
- if not config .has_key ("trees" ):
218
- # no config
219
- return
220
- if not config ["trees" ].has_key (tree ):
212
+ if tree not in config .get ("trees" , {}):
221
213
return
222
214
mytree = trees [tree ]
223
- if ( options .verbose > 0 ) :
215
+ if options .verbose > 0 :
224
216
print ("* %s: %d items" % (tree , len (mytree ["locs" ])))
225
217
# do varible substitution for this tree here
226
218
if isinstance (config ["trees" ][tree ], basestring ):
227
219
treeStr = config ["trees" ][tree ]
228
- if ( options .verbose > 5 ) :
220
+ if options .verbose > 5 :
229
221
print (" Substituting $%s for tree %s" % (treeStr , tree ))
230
- if ( not config .has_key ("variables" ) or not config [ "variables" ]. has_key ( treeStr ) ):
222
+ if treeStr not in config .get ("variables" , {} ):
231
223
print (" ERROR: no variable: variables.%s for tree %s" % (treeStr , tree ))
232
224
sys .exit (1 )
233
225
config ["trees" ][tree ] = config ["variables" ][treeStr ]
234
226
myconfig = config ["trees" ][tree ]
235
- if ( options .verbose > 4 ) :
227
+ if options .verbose > 4 :
236
228
print (" Config: %s" % (myconfig ))
237
229
# Process this tree
238
230
if (len (myconfig )== 0 or len (mytree ["locs" ])== 0 ):
239
231
if (options .verbose > 2 ):
240
232
print (" No processing for %s - skipping" % (tree ))
241
233
else :
242
234
only = None
243
- if myconfig . has_key ( "only" ) :
235
+ if "only" in myconfig :
244
236
only = set (myconfig ["only" ])
245
237
if (len (only )== 0 ) and (mytree ["treeprefix" ] != "" ):
246
238
thePool = "%spool.res" % (mytree ["treeprefix" ])
@@ -297,7 +289,7 @@ def addTreeByType(tree, mytree):
297
289
treeitems = fi .readlines ()
298
290
trees [tree ]["locs" ] = [treeitems [i ].strip () for i in range (len (treeitems ))]
299
291
fi .close ()
300
- if ( not config .has_key ("trees" ) or not config [ "trees" ]. has_key ( tree ) ):
292
+ if tree not in config .get ("trees" , {} ):
301
293
print (" Warning: filter file %s does not mention trees.%s - will be kept as-is" % (options .filterfile , tree ))
302
294
else :
303
295
queueForRemoval (tree )
@@ -315,10 +307,8 @@ def removeList(count=0):
315
307
oldcount = len (remove )
316
308
hackerrfile = os .path .join (options .tmpdir , "REMOVE.err" )
317
309
removefile = os .path .join (options .tmpdir , "REMOVE.lst" )
318
- fi = open (removefile , 'wb' )
319
- for i in remove :
320
- print (i , file = fi )
321
- fi .close ()
310
+ with open (removefile , 'wb' ) as fi :
311
+ fi .write ('\n ' .join (remove ).encode ("utf-8" ) + b'\n ' )
322
312
rc = runcmd ("icupkg" ,"-r %s %s 2> %s" % (removefile ,outfile ,hackerrfile ),True )
323
313
if rc != 0 :
324
314
if (options .verbose > 5 ):
@@ -352,7 +342,7 @@ def removeList(count=0):
352
342
# now, fixup res_index, one at a time
353
343
for tree in trees :
354
344
# skip trees that don't have res_index
355
- if not trees [tree ]. has_key ( "hasIndex" ) :
345
+ if "hasIndex" not in trees [tree ]:
356
346
continue
357
347
treebunddir = options .tmpdir
358
348
if (trees [tree ]["treeprefix" ]):
0 commit comments