[Bf-blender-cvs] [06ae2e3a609] master: i18n utils : Reduce dependency to Blender bpy API, step 2.

Bastien Montagne noreply at git.blender.org
Fri Dec 4 15:14:22 CET 2020


Commit: 06ae2e3a609fdc2081108e4163b7c62540618310
Author: Bastien Montagne
Date:   Fri Dec 4 15:08:11 2020 +0100
Branches: master
https://developer.blender.org/rB06ae2e3a609fdc2081108e4163b7c62540618310

i18n utils : Reduce dependency to Blender bpy API, step 2.

Remove some top imports of bpy, only import it in a few specific
functions that only make sense when used whithin Blender anyway.

===================================================================

M	release/scripts/modules/bl_i18n_utils/settings.py
M	release/scripts/modules/bl_i18n_utils/utils.py

===================================================================

diff --git a/release/scripts/modules/bl_i18n_utils/settings.py b/release/scripts/modules/bl_i18n_utils/settings.py
index e304ef5ea17..cfa4fcac17f 100644
--- a/release/scripts/modules/bl_i18n_utils/settings.py
+++ b/release/scripts/modules/bl_i18n_utils/settings.py
@@ -30,7 +30,11 @@ import os
 import sys
 import types
 
-import bpy
+try:
+    import bpy
+except ModuleNotFoundError:
+    print("Could not import bpy, some features are not available when not run from Blender.")
+    bpy = None
 
 ###############################################################################
 # MISC
@@ -98,8 +102,10 @@ LANGUAGES = (
     (47, "Slovak (SlovenĨina)", "sk_SK"),
 )
 
-# Default context, in py!
-DEFAULT_CONTEXT = bpy.app.translations.contexts.default
+# Default context, in py (keep in sync with `BLT_translation.h`)!
+if bpy is not None:
+    assert(bpy.app.translations.contexts.default == "*")
+DEFAULT_CONTEXT = "*"
 
 # Name of language file used by Blender to generate translations' menu.
 LANGUAGES_FILE = "languages"
diff --git a/release/scripts/modules/bl_i18n_utils/utils.py b/release/scripts/modules/bl_i18n_utils/utils.py
index 14587100aea..2224c39e48c 100644
--- a/release/scripts/modules/bl_i18n_utils/utils.py
+++ b/release/scripts/modules/bl_i18n_utils/utils.py
@@ -35,8 +35,6 @@ from bl_i18n_utils import (
     utils_rtl,
 )
 
-import bpy
-
 
 ##### Misc Utils #####
 _valid_po_path_re = re.compile(r"^\S+:[0-9]+$")
@@ -191,6 +189,12 @@ def enable_addons(addons=None, support=None, disable=False, check_only=False):
     """
     import addon_utils
 
+    try:
+        import bpy
+    except ModuleNotFoundError:
+        print("Could not import bpy, enable_addons must be run from whithin Blender.")
+        return
+
     if addons is None:
         addons = {}
     if support is None:
@@ -744,6 +748,13 @@ class I18nMessages:
                 rna_ctxt: the labels' i18n context.
                 rna_struct_name, rna_prop_name, rna_enum_name: should be self-explanatory!
         """
+        try:
+            import bpy
+        except ModuleNotFoundError:
+            print("Could not import bpy, find_best_messages_matches must be run from whithin Blender.")
+            return
+
+
         # Build helper mappings.
         # Note it's user responsibility to know when to invalidate (and hence force rebuild) this cache!
         if self._reverse_cache is None:
@@ -1294,7 +1305,7 @@ class I18n:
                 msgs.print_stats(prefix=msgs_prefix)
                 print(prefix)
 
-        nbr_contexts = len(self.contexts - {bpy.app.translations.contexts.default})
+        nbr_contexts = len(self.contexts - {self.settings.DEFAULT_CONTEXT})
         if nbr_contexts != 1:
             if nbr_contexts == 0:
                 nbr_contexts = "No"
@@ -1312,7 +1323,7 @@ class I18n:
             "    The org msgids are currently made of {} signs.\n".format(self.nbr_signs),
             "    All processed translations are currently made of {} signs.\n".format(self.nbr_trans_signs),
             "    {} specific context{} present:\n".format(self.nbr_contexts, _ctx_txt)) +
-            tuple("            " + c + "\n" for c in self.contexts - {bpy.app.translations.contexts.default}) +
+            tuple("            " + c + "\n" for c in self.contexts - {self.settings.DEFAULT_CONTEXT}) +
             ("\n",)
         )
         print(prefix.join(lines))



More information about the Bf-blender-cvs mailing list