I18n: fixes to add-on message extraction
This commit fixes several issues in add-ons UI messages extraction code: - In multi-file modules, the script would crash because it tried to write to the dir instead of a `translations.py` file; - The add-on message extraction works by enabling the add-on, getting all messages; disabling the add-on, getting all messages; then comparing the two message sets. But often a bug happens where a class gets a description from somewhere else in memory. I couldn’t debug that, so a workaround is to check that the message isn’t a corrupted one before removing it; - `printf()` doesn't exist in Python and would crash the script; - `self.src[self.settings.PARSER_PY_ID]` can be replaced by `self.py_file` in class `I18n`, since a property exists to do that; - At one point a generator was printed instead of its values, so let's unpack the generator to get the values. Maybe the print could be deleted entirely; - Use SPDX license identifier instead of GPL license block, to be more in line with other scripts from the codebase. Reviewed By: mont29 Differential Revision: https://developer.blender.org/D15474
This commit is contained in:
@@ -962,7 +962,12 @@ def dump_addon_messages(module_name, do_checks, settings):
|
|||||||
# and make the diff!
|
# and make the diff!
|
||||||
for key in minus_msgs:
|
for key in minus_msgs:
|
||||||
if key != settings.PO_HEADER_KEY:
|
if key != settings.PO_HEADER_KEY:
|
||||||
|
if key in msgs:
|
||||||
del msgs[key]
|
del msgs[key]
|
||||||
|
else:
|
||||||
|
# This should not happen, but some messages seem to have
|
||||||
|
# leaked on add-on unregister and register?
|
||||||
|
print(f"Key not found in msgs: {key}")
|
||||||
|
|
||||||
if check_ctxt:
|
if check_ctxt:
|
||||||
_diff_check_ctxt(check_ctxt, minus_check_ctxt)
|
_diff_check_ctxt(check_ctxt, minus_check_ctxt)
|
||||||
|
|||||||
@@ -154,7 +154,7 @@ def get_po_files_from_dir(root_dir, langs=set()):
|
|||||||
else:
|
else:
|
||||||
continue
|
continue
|
||||||
if uid in found_uids:
|
if uid in found_uids:
|
||||||
printf("WARNING! {} id has been found more than once! only first one has been loaded!".format(uid))
|
print("WARNING! {} id has been found more than once! only first one has been loaded!".format(uid))
|
||||||
continue
|
continue
|
||||||
found_uids.add(uid)
|
found_uids.add(uid)
|
||||||
yield uid, po_file
|
yield uid, po_file
|
||||||
@@ -1240,8 +1240,8 @@ class I18n:
|
|||||||
return os.path.join(os.path.dirname(path), uid + ".po")
|
return os.path.join(os.path.dirname(path), uid + ".po")
|
||||||
elif kind == 'PY':
|
elif kind == 'PY':
|
||||||
if not path.endswith(".py"):
|
if not path.endswith(".py"):
|
||||||
if self.src.get(self.settings.PARSER_PY_ID):
|
if os.path.isdir(path):
|
||||||
return self.src[self.settings.PARSER_PY_ID]
|
return os.path.join(path, "translations.py")
|
||||||
return os.path.join(os.path.dirname(path), "translations.py")
|
return os.path.join(os.path.dirname(path), "translations.py")
|
||||||
return path
|
return path
|
||||||
|
|
||||||
@@ -1392,15 +1392,15 @@ class I18n:
|
|||||||
if langs set is void, all languages found are loaded.
|
if langs set is void, all languages found are loaded.
|
||||||
"""
|
"""
|
||||||
default_context = self.settings.DEFAULT_CONTEXT
|
default_context = self.settings.DEFAULT_CONTEXT
|
||||||
self.src[self.settings.PARSER_PY_ID], msgs = self.check_py_module_has_translations(src, self.settings)
|
self.py_file, msgs = self.check_py_module_has_translations(src, self.settings)
|
||||||
if msgs is None:
|
if msgs is None:
|
||||||
self.src[self.settings.PARSER_PY_ID] = src
|
self.py_file = src
|
||||||
msgs = ()
|
msgs = ()
|
||||||
for key, (sources, gen_comments), *translations in msgs:
|
for key, (sources, gen_comments), *translations in msgs:
|
||||||
if self.settings.PARSER_TEMPLATE_ID not in self.trans:
|
if self.settings.PARSER_TEMPLATE_ID not in self.trans:
|
||||||
self.trans[self.settings.PARSER_TEMPLATE_ID] = I18nMessages(self.settings.PARSER_TEMPLATE_ID,
|
self.trans[self.settings.PARSER_TEMPLATE_ID] = I18nMessages(self.settings.PARSER_TEMPLATE_ID,
|
||||||
settings=self.settings)
|
settings=self.settings)
|
||||||
self.src[self.settings.PARSER_TEMPLATE_ID] = self.src[self.settings.PARSER_PY_ID]
|
self.src[self.settings.PARSER_TEMPLATE_ID] = self.py_file
|
||||||
if key in self.trans[self.settings.PARSER_TEMPLATE_ID].msgs:
|
if key in self.trans[self.settings.PARSER_TEMPLATE_ID].msgs:
|
||||||
print("ERROR! key {} is defined more than once! Skipping re-definitions!")
|
print("ERROR! key {} is defined more than once! Skipping re-definitions!")
|
||||||
continue
|
continue
|
||||||
@@ -1416,7 +1416,7 @@ class I18n:
|
|||||||
for uid, msgstr, (is_fuzzy, user_comments) in translations:
|
for uid, msgstr, (is_fuzzy, user_comments) in translations:
|
||||||
if uid not in self.trans:
|
if uid not in self.trans:
|
||||||
self.trans[uid] = I18nMessages(uid, settings=self.settings)
|
self.trans[uid] = I18nMessages(uid, settings=self.settings)
|
||||||
self.src[uid] = self.src[self.settings.PARSER_PY_ID]
|
self.src[uid] = self.py_file
|
||||||
comment_lines = [self.settings.PO_COMMENT_PREFIX + c for c in user_comments] + common_comment_lines
|
comment_lines = [self.settings.PO_COMMENT_PREFIX + c for c in user_comments] + common_comment_lines
|
||||||
self.trans[uid].msgs[key] = I18nMessage(ctxt, [key[1]], [msgstr], comment_lines, False, is_fuzzy,
|
self.trans[uid].msgs[key] = I18nMessage(ctxt, [key[1]], [msgstr], comment_lines, False, is_fuzzy,
|
||||||
settings=self.settings)
|
settings=self.settings)
|
||||||
@@ -1479,7 +1479,7 @@ class I18n:
|
|||||||
if langs:
|
if langs:
|
||||||
translations &= langs
|
translations &= langs
|
||||||
translations = [('"' + lng + '"', " " * (len(lng) + 6), self.trans[lng]) for lng in sorted(translations)]
|
translations = [('"' + lng + '"', " " * (len(lng) + 6), self.trans[lng]) for lng in sorted(translations)]
|
||||||
print(k for k in keys.keys())
|
print(*(k for k in keys.keys()))
|
||||||
for key in keys.keys():
|
for key in keys.keys():
|
||||||
if ref.msgs[key].is_commented:
|
if ref.msgs[key].is_commented:
|
||||||
continue
|
continue
|
||||||
@@ -1565,25 +1565,9 @@ class I18n:
|
|||||||
# We completely replace the text found between start and end markers...
|
# We completely replace the text found between start and end markers...
|
||||||
txt = _gen_py(self, langs)
|
txt = _gen_py(self, langs)
|
||||||
else:
|
else:
|
||||||
printf("Creating python file {} containing translations.".format(dst))
|
print("Creating python file {} containing translations.".format(dst))
|
||||||
txt = [
|
txt = [
|
||||||
"# ***** BEGIN GPL LICENSE BLOCK *****",
|
"# SPDX-License-Identifier: GPL-2.0-or-later",
|
||||||
"#",
|
|
||||||
"# This program is free software; you can redistribute it and/or",
|
|
||||||
"# modify it under the terms of the GNU General Public License",
|
|
||||||
"# as published by the Free Software Foundation; either version 2",
|
|
||||||
"# of the License, or (at your option) any later version.",
|
|
||||||
"#",
|
|
||||||
"# This program is distributed in the hope that it will be useful,",
|
|
||||||
"# but WITHOUT ANY WARRANTY; without even the implied warranty of",
|
|
||||||
"# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the",
|
|
||||||
"# GNU General Public License for more details.",
|
|
||||||
"#",
|
|
||||||
"# You should have received a copy of the GNU General Public License",
|
|
||||||
"# along with this program; if not, write to the Free Software Foundation,",
|
|
||||||
"# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.",
|
|
||||||
"#",
|
|
||||||
"# ***** END GPL LICENSE BLOCK *****",
|
|
||||||
"",
|
"",
|
||||||
self.settings.PARSER_PY_MARKER_BEGIN,
|
self.settings.PARSER_PY_MARKER_BEGIN,
|
||||||
"",
|
"",
|
||||||
|
|||||||
Reference in New Issue
Block a user