diff options
author | Bastien Montagne <bastien@blender.org> | 2020-12-04 17:08:11 +0300 |
---|---|---|
committer | Bastien Montagne <bastien@blender.org> | 2020-12-04 17:14:16 +0300 |
commit | 06ae2e3a609fdc2081108e4163b7c62540618310 (patch) | |
tree | a36d2e62c7f20bd516b1ec5583ba0a1d9a7f47fb /release/scripts/modules/bl_i18n_utils | |
parent | 7bd8b8cdacd2cd7036851a77b41c4c7767f09e5e (diff) |
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.
Diffstat (limited to 'release/scripts/modules/bl_i18n_utils')
-rw-r--r-- | release/scripts/modules/bl_i18n_utils/settings.py | 12 | ||||
-rw-r--r-- | release/scripts/modules/bl_i18n_utils/utils.py | 19 |
2 files changed, 24 insertions, 7 deletions
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)) |