diff --git a/scripts/common.py b/scripts/common.py index dc69cb7..7facabe 100644 --- a/scripts/common.py +++ b/scripts/common.py @@ -47,29 +47,32 @@ class ExtractType(enum.IntEnum): class PropertyNameProcessor: remaps: Dict[str, str] = {} stop_words: Set[str] = set() + contexts: Dict[str, Dict[str, str]] = {} # See String::_camelcase_to_underscore(). capitalize_re = re.compile(r"(?<=\D)(?=\d)|(?<=\d)(?=\D([a-z]|\d))") def __init__(self): - remap_re = re.compile(r'^\t*capitalize_string_remaps\["(?P.+)"\] = (String::utf8\()?"(?P.+)"') + remap_re = re.compile(r'^\t*capitalize_string_remaps\["(?P.+)"\] = (String::utf8\(|U)?"(?P.+)"') stop_words_re = re.compile(r'^\t*"(?P.+)",') - is_inside_stop_words = False + contexts_re = re.compile(r'^\t*translation_contexts\["(?P.+)"\]\["(?P.+)"\] = (String::utf8\(|U)?"(?P.+)"') with open("editor/editor_property_name_processor.cpp") as f: for line in f: - if is_inside_stop_words: - m = stop_words_re.search(line) - if m: - self.stop_words.add(m.group("word")) - else: - is_inside_stop_words = False - else: - m = remap_re.search(line) - if m: - self.remaps[m.group("from")] = m.group("to") - - if not is_inside_stop_words and not self.stop_words: - is_inside_stop_words = "stop_words = " in line + m = remap_re.search(line) + if m: + self.remaps[m.group("from")] = m.group("to") + continue + + m = stop_words_re.search(line) + if m: + self.stop_words.add(m.group("word")) + continue + + m = contexts_re.search(line) + if m: + context_map = self.contexts.setdefault(m.group("message"), {}) + context_map[m.group("condition")] = m.group("context") + continue def process_name(self, name: str) -> str: # See EditorPropertyNameProcessor::process_name(). @@ -94,6 +97,18 @@ def process_name(self, name: str) -> str: return " ".join(capitalized_parts) + def get_context(self, name: str, property: str, klass: str) -> str: + # See EditorPropertyNameProcessor::_get_context(). + if not property and not klass: + return "" + context_map = self.contexts.get(name) + if not context_map: + return "" + context = context_map.get(property) + if not context and klass: + context = context_map.get(klass + "::" + property) + return context or "" + def get_source_files() -> List[str]: matches = [] diff --git a/scripts/extract_editor.py b/scripts/extract_editor.py index 6446657..32f9373 100644 --- a/scripts/extract_editor.py +++ b/scripts/extract_editor.py @@ -58,7 +58,6 @@ r'RTRN\(U?"(?P([^"\\]|\\.)*)", "(?P([^"\\]|\\.)*)",[^,)]+?(, "(?P([^"\\]|\\.)*)")?\)' ): ExtractType.TEXT, } -theme_property_patterns: Dict[re.Pattern[str], ExtractType] = {} def _is_block_translator_comment(translator_line): @@ -104,12 +103,7 @@ def process_file(f, fname): reading_translator_comment = False is_block_translator_comment = False translator_comment = "" - current_group = "" - current_subgroup = "" - patterns = message_patterns - if os.path.basename(fname) == "default_theme.cpp": - patterns = {**message_patterns, **theme_property_patterns} while l: @@ -141,37 +135,6 @@ def process_file(f, fname): if extract_type == ExtractType.TEXT: _add_message(msg, msg_plural, msgctx, location, translator_comment) - elif extract_type == ExtractType.PROPERTY_PATH: - if captures.get("usage") == "PROPERTY_USAGE_NO_EDITOR": - continue - - if current_subgroup: - if msg.startswith(current_subgroup): - msg = msg[len(current_subgroup) :] - elif current_subgroup.startswith(msg): - pass # Keep this as-is. See EditorInspector::update_tree(). - else: - current_subgroup = "" - elif current_group: - if msg.startswith(current_group): - msg = msg[len(current_group) :] - elif current_group.startswith(msg): - pass # Keep this as-is. See EditorInspector::update_tree(). - else: - current_group = "" - current_subgroup = "" - - if "." in msg: # Strip feature tag. - msg = msg.split(".", 1)[0] - for part in msg.split("/"): - _add_message(processor.process_name(part), msg_plural, msgctx, location, translator_comment) - elif extract_type == ExtractType.GROUP: - _add_message(msg, msg_plural, msgctx, location, translator_comment) - current_group = captures["prefix"] - current_subgroup = "" - elif extract_type == ExtractType.SUBGROUP: - _add_message(msg, msg_plural, msgctx, location, translator_comment) - current_subgroup = captures["prefix"] translator_comment = "" l = f.readline() diff --git a/scripts/extract_properties.py b/scripts/extract_properties.py index 4561281..59a93d8 100644 --- a/scripts/extract_properties.py +++ b/scripts/extract_properties.py @@ -69,8 +69,9 @@ re.compile(r'PNAME\("(?P[^"]+)"\)'): ExtractType.PROPERTY_PATH, } theme_property_patterns = { - re.compile(r'set_(constant|font|font_size|stylebox|color|icon)\("(?P[^"]+)", '): ExtractType.PROPERTY_PATH, + re.compile(r'set_(?Pconstant|font|font_size|stylebox|color|icon)\("(?P[^"]+)", '): ExtractType.PROPERTY_PATH, } +class_name_pattern = re.compile(r"^\w+ (?P\w+)::\w+\(") def _is_block_translator_comment(translator_line): @@ -118,12 +119,17 @@ def process_file(f, fname): translator_comment = "" current_group = "" current_subgroup = "" + current_class = "" patterns = message_patterns if os.path.basename(fname) == "default_theme.cpp": patterns = {**message_patterns, **theme_property_patterns} while l: + # Detect class name. + m = class_name_pattern.match(l) + if m: + current_class = m.group("class_name") # Detect translator comments. if not reading_translator_comment and l.find("TRANSLATORS:") != -1: @@ -169,6 +175,13 @@ def process_file(f, fname): if "PROPERTY_USAGE_DEFAULT" not in usages and "PROPERTY_USAGE_EDITOR" not in usages: continue + property_path = msg + theme_item = captures.get("theme_item") + if theme_item: + if theme_item == "stylebox": + theme_item = "style" + property_path = "theme_overrides_" + theme_item + "/" + property_path + if current_subgroup: if msg.startswith(current_subgroup): msg = msg[len(current_subgroup) :] @@ -188,6 +201,7 @@ def process_file(f, fname): if "." in msg: # Strip feature tag. msg = msg.split(".", 1)[0] for part in msg.split("/"): + msgctx = processor.get_context(part, property_path, current_class) _add_message(processor.process_name(part), msg_plural, msgctx, location, translator_comment) elif extract_type == ExtractType.GROUP: _add_message(msg, msg_plural, msgctx, location, translator_comment)