|
| 1 | +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). |
| 2 | +import re |
| 3 | +from odoo_module_migrate.base_migration_script import BaseMigrationScript |
| 4 | + |
| 5 | + |
| 6 | +def multi_value_translation_replacement_function(match, single_quote=True): |
| 7 | + format_string = match.group(1) |
| 8 | + dictionary_entries = match.group(2) |
| 9 | + |
| 10 | + formatted_entries = [] |
| 11 | + for entry in dictionary_entries.split(","): |
| 12 | + if ":" in entry: |
| 13 | + [key, value] = entry.split(":") |
| 14 | + formatted_entries.append( |
| 15 | + "{}={}".format(key.strip().strip("'").strip('"'), value.strip()) |
| 16 | + ) |
| 17 | + |
| 18 | + formatted_entries = ", ".join(formatted_entries) |
| 19 | + |
| 20 | + if single_quote: |
| 21 | + return f"_('{format_string}', {formatted_entries})" |
| 22 | + return f'_("{format_string}", {formatted_entries})' |
| 23 | + |
| 24 | + |
| 25 | +def format_parenthesis(match): |
| 26 | + format_string = match.group(1) |
| 27 | + dictionary_entries = match.group(2) |
| 28 | + |
| 29 | + if dictionary_entries.endswith(","): |
| 30 | + dictionary_entries = dictionary_entries[:-1] |
| 31 | + |
| 32 | + return f"_({format_string}, {dictionary_entries})" |
| 33 | + |
| 34 | + |
| 35 | +def format_replacement_function(match, single_quote=True): |
| 36 | + format_string = re.sub(r"\{\d*\}", "%s", match.group(1)) |
| 37 | + format_string = re.sub(r"{(\w+)}", r"%(\1)s", format_string) |
| 38 | + arguments = " ".join(match.group(2).split()) |
| 39 | + |
| 40 | + if arguments.endswith(","): |
| 41 | + arguments = arguments[:-1] |
| 42 | + |
| 43 | + if single_quote: |
| 44 | + return f"_('{format_string}', {arguments})" |
| 45 | + return f'_("{format_string}", {arguments})' |
| 46 | + |
| 47 | + |
| 48 | +def replace_translation_function( |
| 49 | + logger, module_path, module_name, manifest_path, migration_steps, tools |
| 50 | +): |
| 51 | + files_to_process = tools.get_files(module_path, (".py",)) |
| 52 | + |
| 53 | + replaces = { |
| 54 | + r'_\(\s*"([^"]+)"\s*\)\s*%\s*\{([^}]+)\}': lambda match: multi_value_translation_replacement_function( |
| 55 | + match, single_quote=False |
| 56 | + ), |
| 57 | + r"_\(\s*'([^']+)'\s*\)\s*%\s*\{([^}]+)\}": lambda match: multi_value_translation_replacement_function( |
| 58 | + match, single_quote=True |
| 59 | + ), |
| 60 | + r'_\(\s*(["\'].*?%[ds].*?["\'])\s*\)\s*%\s*\(\s*(.+)\s*\)': format_parenthesis, |
| 61 | + r'_\(\s*(["\'].*?%[ds].*?["\'])\s*\)\s*?%\s*?([^\s]+)': r"_(\1, \2)", |
| 62 | + r'_\(\s*"([^"]*)"\s*\)\.format\(\s*(\s*[^)]+)\)': lambda match: format_replacement_function( |
| 63 | + match, single_quote=False |
| 64 | + ), |
| 65 | + r"_\(\s*'([^']*)'\s*\)\.format\(\s*(\s*[^)]+)\)": lambda match: format_replacement_function( |
| 66 | + match, single_quote=True |
| 67 | + ), |
| 68 | + } |
| 69 | + |
| 70 | + for file in files_to_process: |
| 71 | + try: |
| 72 | + tools._replace_in_file( |
| 73 | + file, |
| 74 | + replaces, |
| 75 | + log_message=f"""Improve _() function: {file}""", |
| 76 | + ) |
| 77 | + except Exception as e: |
| 78 | + logger.error(f"Error processing file {file}: {str(e)}") |
| 79 | + |
| 80 | + |
| 81 | +class MigrationScript(BaseMigrationScript): |
| 82 | + |
| 83 | + _GLOBAL_FUNCTIONS = [replace_translation_function] |
0 commit comments