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.
This commit is contained in:
Bastien Montagne 2020-12-04 15:08:11 +01:00
parent 7bd8b8cdac
commit 06ae2e3a60
2 changed files with 24 additions and 7 deletions

View File

@ -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"

View File

@ -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))