[Bf-blender-cvs] [8ab88f7367] app-templates: Experimental use of package for storing

Campbell Barton noreply at git.blender.org
Thu Mar 23 09:52:14 CET 2017


Commit: 8ab88f73672153d4f170072ca3f8ba5268856741
Author: Campbell Barton
Date:   Thu Mar 23 17:47:58 2017 +1100
Branches: app-templates
https://developer.blender.org/rB8ab88f73672153d4f170072ca3f8ba5268856741

Experimental use of package for storing

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

M	release/scripts/modules/bl_app_template_utils.py
M	release/scripts/modules/bpy/utils/__init__.py
A	release/scripts/startup/bl_app_templates_system/__init__.py
R099	release/datafiles/app_templates/101/template/__init__.py	release/scripts/startup/bl_app_templates_system/blender_101/__init__.py
R100	release/datafiles/app_templates/101/addons/template_101_library/__init__.py	release/scripts/startup/bl_app_templates_system/blender_101/addons/template_101_library/__init__.py
R100	release/datafiles/app_templates/101/addons/template_101_library/data/object/icosahedron.blend	release/scripts/startup/bl_app_templates_system/blender_101/addons/template_101_library/data/object/icosahedron.blend
R100	release/datafiles/app_templates/101/addons/template_101_library/data/object/teapot.blend	release/scripts/startup/bl_app_templates_system/blender_101/addons/template_101_library/data/object/teapot.blend
R100	release/datafiles/app_templates/101/splash.png	release/scripts/startup/bl_app_templates_system/blender_101/splash.png
R100	release/datafiles/app_templates/101/splash_2x.png	release/scripts/startup/bl_app_templates_system/blender_101/splash_2x.png
R100	release/datafiles/app_templates/101/startup.blend	release/scripts/startup/bl_app_templates_system/blender_101/startup.blend
R100	release/datafiles/app_templates/101/template/ui.py	release/scripts/startup/bl_app_templates_system/blender_101/ui.py
R100	release/datafiles/app_templates/101/userpref.blend	release/scripts/startup/bl_app_templates_system/blender_101/userpref.blend
R100	release/datafiles/app_templates/3d_print/template/__init__.py	release/scripts/startup/bl_app_templates_system/lighting/__init__.py
R100	release/datafiles/app_templates/lighting/startup.blend	release/scripts/startup/bl_app_templates_system/lighting/startup.blend
R100	release/datafiles/app_templates/lighting/template/__init__.py	release/scripts/startup/bl_app_templates_system/print_3d/__init__.py
R100	release/datafiles/app_templates/3d_print/startup.blend	release/scripts/startup/bl_app_templates_system/print_3d/startup.blend
M	release/scripts/startup/bl_ui/space_userpref.py
M	source/blender/blenkernel/BKE_appdir.h
M	source/blender/blenkernel/intern/appdir.c
M	source/blender/windowmanager/intern/wm_files.c

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

diff --git a/release/scripts/modules/bl_app_template_utils.py b/release/scripts/modules/bl_app_template_utils.py
index af0dbe507f..834280cb75 100644
--- a/release/scripts/modules/bl_app_template_utils.py
+++ b/release/scripts/modules/bl_app_template_utils.py
@@ -57,35 +57,6 @@ _app_template = {
 _modules = {}
 
 
-# -----------------------------------------------------------------------------
-# Helper Classes
-
-# Avoids leaving sys.paths & modules in an unknown state.
-class _IsolateImportHelper:
-
-    __slots__ = ("path", "module", "module_name")
-
-    def __init__(self, path, module_name):
-        self.path = path
-        self.module_name = module_name
-
-    def __enter__(self):
-        import sys
-        self.module = sys.modules.pop(self.module_name, None)
-        sys.path.insert(0, self.path)
-
-    def __exit__(self, type, value, traceback):
-        import sys
-        if self.module is not None:
-            sys.modules[self.module_name] = self.module
-        else:
-            sys.modules.pop(self.module_name, None)
-        try:
-            sys.path.remove(self.path)
-        except Exception:
-            pass
-
-
 def _enable(template_id, *, handle_error=None, ignore_not_found=False):
     import os
     import sys
@@ -177,17 +148,20 @@ def _disable(template_id, *, handle_error=None):
 def import_from_path(path, ignore_not_found=False):
     """
     Imports 'startup' from a path.
+
+    /usr/share/blender/2.79/scripts/startup/bl_app_templates_system/101
     """
-    module_name = "template"
-    # loading packages without modifying sys.path is some dark-art.
-    # for now just use regular import but don't use sys.modules for cache.
-    with _IsolateImportHelper(path, module_name):
-        try:
-            return __import__(module_name)
-        except ModuleNotFoundError as ex:
-            if ignore_not_found and ex.name == module_name:
-                return None
-            raise ex
+    import os
+    from importlib import import_module
+    base_module, template_id = path.rsplit(os.sep, 2)[-2:]
+    module_name = base_module + "." + template_id
+
+    try:
+        return import_module(module_name)
+    except ModuleNotFoundError as ex:
+        if ignore_not_found and ex.name == module_name:
+            return None
+        raise ex
 
 
 def import_from_id(template_id, ignore_not_found=False):
@@ -241,10 +215,7 @@ def reset(*, reload_scripts=False):
         for key, mod in _modules.items():
             # Will always be 'template' but just use convention of __name__ to be sure.
             module_name = mod.__name__
-            with _IsolateImportHelper(os.path.dirname(mod.__file__), module_name):
-                sys.modules[module_name] = mod
-                _modules_new[key] = importlib.reload(mod)
-                del sys.modules[module_name]
+            _modules_new[key] = importlib.reload(mod)
         _modules.clear()
         _modules.update(_modules_new)
         del _modules_new
diff --git a/release/scripts/modules/bpy/utils/__init__.py b/release/scripts/modules/bpy/utils/__init__.py
index e512dbe0f5..5e21ef1905 100644
--- a/release/scripts/modules/bpy/utils/__init__.py
+++ b/release/scripts/modules/bpy/utils/__init__.py
@@ -373,28 +373,15 @@ def app_template_paths(subdir=None):
     :return: script paths.
     :rtype: list
     """
-    # All possible paths, no duplicates, keep order.
-    base_paths = (
-        path for path in (_os.path.join(resource_path(res), "datafiles", "app_templates")
-        for res in ('LOCAL', 'USER', 'SYSTEM')))
-
-    templates = []
-    for path in base_paths:
-        if path:
-            path = _os.path.normpath(path)
-            if _os.path.isdir(path):
-                templates.append(path)
-
-    if subdir is None:
-        return templates
-
-    templates_subdir = []
-    for path in templates:
-        path_subdir = _os.path.join(path, subdir)
-        if _os.path.isdir(path_subdir):
-            templates_subdir.append(path_subdir)
-
-    return templates_subdir
+    import os
+    scripts = []
+    for sub in ("bl_app_templates_user", "bl_app_templates_system"):
+        if subdir is not None:
+            sub = os.path.join("startup", sub, subdir)
+        else:
+            sub = os.path.join("startup", sub)
+        scripts.extend(script_paths(subdir=sub, user_pref=False, check_all=True))
+    return scripts
 
 
 def preset_paths(subdir):
diff --git a/release/datafiles/app_templates/lighting/template/__init__.py b/release/scripts/startup/bl_app_templates_system/__init__.py
similarity index 91%
copy from release/datafiles/app_templates/lighting/template/__init__.py
copy to release/scripts/startup/bl_app_templates_system/__init__.py
index 44e622c10e..37c607c6fb 100644
--- a/release/datafiles/app_templates/lighting/template/__init__.py
+++ b/release/scripts/startup/bl_app_templates_system/__init__.py
@@ -18,9 +18,10 @@
 
 # <pep8-80 compliant>
 
+
 def register():
-    print("Template Register", __file__)
+    pass
 
 
 def unregister():
-    print("Template Unregister", __file__)
+    pass
diff --git a/release/datafiles/app_templates/101/template/__init__.py b/release/scripts/startup/bl_app_templates_system/blender_101/__init__.py
similarity index 99%
rename from release/datafiles/app_templates/101/template/__init__.py
rename to release/scripts/startup/bl_app_templates_system/blender_101/__init__.py
index 2c21bf5836..190ad4bd52 100644
--- a/release/datafiles/app_templates/101/template/__init__.py
+++ b/release/scripts/startup/bl_app_templates_system/blender_101/__init__.py
@@ -175,7 +175,7 @@ class AppStateStore(AppOverrideState):
     def addon_paths():
         import os
         return (
-            os.path.normpath(os.path.join(os.path.dirname(__file__), "..", "addons")),
+            os.path.normpath(os.path.join(os.path.dirname(__file__), "addons")),
         )
 
     @staticmethod
diff --git a/release/datafiles/app_templates/101/addons/template_101_library/__init__.py b/release/scripts/startup/bl_app_templates_system/blender_101/addons/template_101_library/__init__.py
similarity index 100%
rename from release/datafiles/app_templates/101/addons/template_101_library/__init__.py
rename to release/scripts/startup/bl_app_templates_system/blender_101/addons/template_101_library/__init__.py
diff --git a/release/datafiles/app_templates/101/addons/template_101_library/data/object/icosahedron.blend b/release/scripts/startup/bl_app_templates_system/blender_101/addons/template_101_library/data/object/icosahedron.blend
similarity index 100%
rename from release/datafiles/app_templates/101/addons/template_101_library/data/object/icosahedron.blend
rename to release/scripts/startup/bl_app_templates_system/blender_101/addons/template_101_library/data/object/icosahedron.blend
diff --git a/release/datafiles/app_templates/101/addons/template_101_library/data/object/teapot.blend b/release/scripts/startup/bl_app_templates_system/blender_101/addons/template_101_library/data/object/teapot.blend
similarity index 100%
rename from release/datafiles/app_templates/101/addons/template_101_library/data/object/teapot.blend
rename to release/scripts/startup/bl_app_templates_system/blender_101/addons/template_101_library/data/object/teapot.blend
diff --git a/release/datafiles/app_templates/101/splash.png b/release/scripts/startup/bl_app_templates_system/blender_101/splash.png
similarity index 100%
rename from release/datafiles/app_templates/101/splash.png
rename to release/scripts/startup/bl_app_templates_system/blender_101/splash.png
diff --git a/release/datafiles/app_templates/101/splash_2x.png b/release/scripts/startup/bl_app_templates_system/blender_101/splash_2x.png
similarity index 100%
rename from release/datafiles/app_templates/101/splash_2x.png
rename to release/scripts/startup/bl_app_templates_system/blender_101/splash_2x.png
diff --git a/release/datafiles/app_templates/101/startup.blend b/release/scripts/startup/bl_app_templates_system/blender_101/startup.blend
similarity index 100%
rename from release/datafiles/app_templates/101/startup.blend
rename to release/scripts/startup/bl_app_templates_system/blender_101/startup.blend
diff --git a/release/datafiles/app_templates/101/template/ui.py b/release/scripts/startup/bl_app_templates_system/blender_101/ui.py
similarity index 100%
rename from release/datafiles/app_templates/101/template/ui.py
rename to release/scripts/startup/bl_app_templates_system/blender_101/ui.py
diff --git a/release/datafiles/app_templates/101/userpref.blend b/release/scripts/startup/bl_app_templates_system/blender_101/userpref.blend
similarity index 100%
rename from release/datafiles/app_templates/101/userpref.blend
rename to release/scripts/startup/bl_app_templates_system/blender_101/userpref.blend
diff --git a/release/datafiles/app_templates/3d_print/template/__init__.py b/release/scripts/startup/bl_app_templates_system/lighting/__init__.py
similarity index 100%
rename from release/datafiles/app_templates/3d_print/template/__init__.py
rename to release/scripts/startup/bl_app_templates_system/lighting/__init__.py
diff --git a/release/datafiles/app_templates/lighting/startup.blend b/release/scripts/startup/bl_app_templates_system/lighting/startup.blend
similarity index 100%
rename from release/datafiles/app_templates/lighting/startup.blend
rename to release/scripts/startup/bl_app_templates_system/lighting/startup.blend
diff --git a/release/datafiles/app_templates/lighting/template/__init__.py b/release/scripts/startup/bl_app_templates_system/print_3d/__init__.py
similarity index 100%
rename from release/datafiles/app_templates/lighting/template/__init__.py
rename to release/scripts/startup/bl_app_templates_system/print_3d/__init__.py
diff --git a/release/datafiles/app_templates/3d_print/startup.blend b/release/scripts/startup/bl_app_templates_system/print_3d/startup.blend
similarity index 100%
rename from release/datafiles/app_templates/3d_print/startup.blend
rename to release/scripts/startup/bl_app_templates_system/print_3d/startup.blend
diff --git a/release/scripts/startup/bl_ui/space_userpref.py b/release/scripts/startup/bl_ui/space_userpref.py
index 5f18403ef4..5c06b79001 100644
--- a/release/scripts/startup/bl_ui/space_userpref.py
+++ b

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list