Skip to content

Commit a123a20

Browse files
cclaussBridgeAR
authored andcommitted
tools: fix Python 3 issues in tools/icu/icutrim.py
PR-URL: #29213 Reviewed-By: Steven R Loomis <[email protected]> Reviewed-By: Jiawen Geng <[email protected]> Reviewed-By: Ben Noordhuis <[email protected]>
1 parent eceebd3 commit a123a20

File tree

1 file changed

+23
-33
lines changed

1 file changed

+23
-33
lines changed

tools/icu/icutrim.py

Lines changed: 23 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -163,17 +163,15 @@ def runcmd(tool, cmd, doContinue=False):
163163
config=json.load(fi)
164164
fi.close()
165165

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:
174172
print(config)
175173

176-
if(config.has_key("comment")):
174+
if "comment" in config:
177175
print("%s: %s" % (options.filterfile, config["comment"]))
178176

179177
## STEP 1 - copy the data file, swapping endianness
@@ -186,61 +184,55 @@ def runcmd(tool, cmd, doContinue=False):
186184
listfile = os.path.join(options.tmpdir,"icudata.lst")
187185
runcmd("icupkg", "-l %s > %s""" % (outfile, listfile))
188186

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()]
194189
itemset = set(items)
195190

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))
198193

199194
# list of all trees
200195
trees = {}
201196
RES_INDX = "res_index.res"
202197
remove = None
203198
# remove - always remove these
204-
if config.has_key("remove"):
199+
if "remove" in config:
205200
remove = set(config["remove"])
206201
else:
207202
remove = set()
208203

209204
# keep - always keep these
210-
if config.has_key("keep"):
205+
if "keep" in config:
211206
keep = set(config["keep"])
212207
else:
213208
keep = set()
214209

215210
def queueForRemoval(tree):
216211
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", {}):
221213
return
222214
mytree = trees[tree]
223-
if(options.verbose>0):
215+
if options.verbose > 0:
224216
print("* %s: %d items" % (tree, len(mytree["locs"])))
225217
# do varible substitution for this tree here
226218
if isinstance(config["trees"][tree], basestring):
227219
treeStr = config["trees"][tree]
228-
if(options.verbose>5):
220+
if options.verbose > 5:
229221
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", {}):
231223
print(" ERROR: no variable: variables.%s for tree %s" % (treeStr, tree))
232224
sys.exit(1)
233225
config["trees"][tree] = config["variables"][treeStr]
234226
myconfig = config["trees"][tree]
235-
if(options.verbose>4):
227+
if options.verbose > 4:
236228
print(" Config: %s" % (myconfig))
237229
# Process this tree
238230
if(len(myconfig)==0 or len(mytree["locs"])==0):
239231
if(options.verbose>2):
240232
print(" No processing for %s - skipping" % (tree))
241233
else:
242234
only = None
243-
if myconfig.has_key("only"):
235+
if "only" in myconfig:
244236
only = set(myconfig["only"])
245237
if (len(only)==0) and (mytree["treeprefix"] != ""):
246238
thePool = "%spool.res" % (mytree["treeprefix"])
@@ -297,7 +289,7 @@ def addTreeByType(tree, mytree):
297289
treeitems = fi.readlines()
298290
trees[tree]["locs"] = [treeitems[i].strip() for i in range(len(treeitems))]
299291
fi.close()
300-
if(not config.has_key("trees") or not config["trees"].has_key(tree)):
292+
if tree not in config.get("trees", {}):
301293
print(" Warning: filter file %s does not mention trees.%s - will be kept as-is" % (options.filterfile, tree))
302294
else:
303295
queueForRemoval(tree)
@@ -315,10 +307,8 @@ def removeList(count=0):
315307
oldcount = len(remove)
316308
hackerrfile=os.path.join(options.tmpdir, "REMOVE.err")
317309
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')
322312
rc = runcmd("icupkg","-r %s %s 2> %s" % (removefile,outfile,hackerrfile),True)
323313
if rc != 0:
324314
if(options.verbose>5):
@@ -352,7 +342,7 @@ def removeList(count=0):
352342
# now, fixup res_index, one at a time
353343
for tree in trees:
354344
# skip trees that don't have res_index
355-
if not trees[tree].has_key("hasIndex"):
345+
if "hasIndex" not in trees[tree]:
356346
continue
357347
treebunddir = options.tmpdir
358348
if(trees[tree]["treeprefix"]):

0 commit comments

Comments
 (0)