Last active
March 30, 2023 20:53
-
-
Save jcfr/fc2bbb1e6b30d08c06f56d403f10145c to your computer and use it in GitHub Desktop.
Rewrite tr-like function calls found in Slicer scripted modules to include the context as first argument
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# SPDX-FileCopyrightText: 2023 Jean-Christophe Fillion-Robin <[email protected]> | |
# SPDX-License-Identifier: BSD-3-Clause | |
import ast | |
import argparse | |
import errno | |
import os | |
import sys | |
import astor | |
class TrFunctionRewriter(ast.NodeTransformer): | |
def __init__(self, input_file): | |
self.current_context = os.path.splitext(os.path.basename(input_file))[0] | |
"""Replace tr to QT_TRANSLATE_NOOP.""" | |
def visit_Call(self, node): | |
self.generic_visit(node) | |
# Transform tr-like functions into 'QT_TRANSLATE_NOOP' """ | |
if (isinstance(node.func, ast.Name) and \ | |
(node.func.id in ["_", "tr", "translate"]) and \ | |
len(node.args) == 1): | |
# Insert the context | |
updated_args = [ast.Constant(self.current_context)] + node.args | |
call = ast.Call(func=ast.Name(id=node.func.id, ctx=ast.Load()), | |
args=updated_args, | |
keywords=[]) | |
ast.copy_location(call, node) | |
# Add lineno & col_offset to the nodes we created | |
ast.fix_missing_locations(call) | |
return call | |
# Return the original node if we don't want to change it. | |
return node | |
def mkdir_p(path): | |
"""Ensure directory ``path`` exists. If needed, parent directories | |
are created. | |
Adapted from http://stackoverflow.com/a/600612/1539918 | |
""" | |
try: | |
os.makedirs(path) | |
except OSError as exc: # Python >2.5 | |
if exc.errno == errno.EEXIST and os.path.isdir(path): | |
pass | |
else: # pragma: no cover | |
raise | |
def main(argv): | |
parser = argparse.ArgumentParser(description="slicer_lupdate_preprocess.py") | |
parser.add_argument("-i", "--ifile", type=str, help="input file") | |
parser.add_argument("-o", "--ofile", type=str, help="output file") | |
args = parser.parse_args() | |
if not args.ifile or not args.ofile: | |
parser.print_help() | |
sys.exit(2) | |
input_file = args.ifile | |
output_file = args.ofile | |
with open(input_file, "r") as source: | |
tree = ast.parse(source.read()) | |
tree_new = TrFunctionRewriter(input_file=input_file).visit(tree) | |
all_lines = astor.to_source(tree_new) | |
# if needed, create output directory | |
output_dir = os.path.dirname(output_file) | |
if output_dir: | |
mkdir_p(output_dir) | |
with open(output_file, "w") as destination: | |
for line in all_lines: | |
destination.write(line) | |
print(f"output_file [{output_file}]") | |
if __name__ == "__main__": | |
main(sys.argv[1:]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This script was created in the context of pull-request Slicer#6917 and is based of prior work done in the context of pull-request Slicer#6050 from 2021.
Input: MyModule.py
Usage