diff --git a/tools/icu/icutrim.py b/tools/icu/icutrim.py index a825b18ac0c8c3..25bd99d9cb260d 100755 --- a/tools/icu/icutrim.py +++ b/tools/icu/icutrim.py @@ -163,17 +163,15 @@ def runcmd(tool, cmd, doContinue=False): config=json.load(fi) fi.close() -if (options.locales): - if not config.has_key("variables"): - config["variables"] = {} - if not config["variables"].has_key("locales"): - config["variables"]["locales"] = {} - config["variables"]["locales"]["only"] = options.locales.split(',') - -if (options.verbose > 6): +if options.locales: + config["variables"] = config.get("variables", {}) + config["variables"]["locales"] = config["variables"].get("locales", {}) + config["variables"]["locales"]["only"] = options.locales.split(',') + +if options.verbose > 6: print(config) -if(config.has_key("comment")): +if "comment" in config: print("%s: %s" % (options.filterfile, config["comment"])) ## STEP 1 - copy the data file, swapping endianness @@ -186,53 +184,47 @@ def runcmd(tool, cmd, doContinue=False): listfile = os.path.join(options.tmpdir,"icudata.lst") runcmd("icupkg", "-l %s > %s""" % (outfile, listfile)) -fi = open(listfile, 'rb') -items = fi.readlines() -items = [items[i].strip() for i in range(len(items))] -fi.close() - +with open(listfile, 'rb') as fi: + items = [line.strip() for line in fi.read().decode("utf-8").splitlines()] itemset = set(items) -if (options.verbose>1): - print("input file: %d items" % (len(items))) +if options.verbose > 1: + print("input file: %d items" % len(items)) # list of all trees trees = {} RES_INDX = "res_index.res" remove = None # remove - always remove these -if config.has_key("remove"): +if "remove" in config: remove = set(config["remove"]) else: remove = set() # keep - always keep these -if config.has_key("keep"): +if "keep" in config: keep = set(config["keep"]) else: keep = set() def queueForRemoval(tree): global remove - if not config.has_key("trees"): - # no config - return - if not config["trees"].has_key(tree): + if tree not in config.get("trees", {}): return mytree = trees[tree] - if(options.verbose>0): + if options.verbose > 0: print("* %s: %d items" % (tree, len(mytree["locs"]))) # do varible substitution for this tree here if isinstance(config["trees"][tree], basestring): treeStr = config["trees"][tree] - if(options.verbose>5): + if options.verbose > 5: print(" Substituting $%s for tree %s" % (treeStr, tree)) - if(not config.has_key("variables") or not config["variables"].has_key(treeStr)): + if treeStr not in config.get("variables", {}): print(" ERROR: no variable: variables.%s for tree %s" % (treeStr, tree)) sys.exit(1) config["trees"][tree] = config["variables"][treeStr] myconfig = config["trees"][tree] - if(options.verbose>4): + if options.verbose > 4: print(" Config: %s" % (myconfig)) # Process this tree if(len(myconfig)==0 or len(mytree["locs"])==0): @@ -240,7 +232,7 @@ def queueForRemoval(tree): print(" No processing for %s - skipping" % (tree)) else: only = None - if myconfig.has_key("only"): + if "only" in myconfig: only = set(myconfig["only"]) if (len(only)==0) and (mytree["treeprefix"] != ""): thePool = "%spool.res" % (mytree["treeprefix"]) @@ -297,7 +289,7 @@ def addTreeByType(tree, mytree): treeitems = fi.readlines() trees[tree]["locs"] = [treeitems[i].strip() for i in range(len(treeitems))] fi.close() - if(not config.has_key("trees") or not config["trees"].has_key(tree)): + if tree not in config.get("trees", {}): print(" Warning: filter file %s does not mention trees.%s - will be kept as-is" % (options.filterfile, tree)) else: queueForRemoval(tree) @@ -315,10 +307,8 @@ def removeList(count=0): oldcount = len(remove) hackerrfile=os.path.join(options.tmpdir, "REMOVE.err") removefile = os.path.join(options.tmpdir, "REMOVE.lst") - fi = open(removefile, 'wb') - for i in remove: - print(i, file=fi) - fi.close() + with open(removefile, 'wb') as fi: + fi.write('\n'.join(remove).encode("utf-8") + b'\n') rc = runcmd("icupkg","-r %s %s 2> %s" % (removefile,outfile,hackerrfile),True) if rc != 0: if(options.verbose>5): @@ -352,7 +342,7 @@ def removeList(count=0): # now, fixup res_index, one at a time for tree in trees: # skip trees that don't have res_index - if not trees[tree].has_key("hasIndex"): + if "hasIndex" not in trees[tree]: continue treebunddir = options.tmpdir if(trees[tree]["treeprefix"]):