Automatically register all extensions in the extension directory (without explicit init import)
This commit is contained in:
parent
7888d97220
commit
585633020b
4 changed files with 33 additions and 13 deletions
|
|
@ -0,0 +1,3 @@
|
||||||
|
from .autofocus import autofocus_extension_v2
|
||||||
|
from .scan import scan_extension_v2
|
||||||
|
from .zip_builder import zip_extension_v2
|
||||||
|
|
@ -109,7 +109,7 @@ def create_file(config_path):
|
||||||
|
|
||||||
|
|
||||||
def init_default_extensions(extension_path):
|
def init_default_extensions(extension_path):
|
||||||
global _DEFAULT_extension_INIT
|
global _DEFAULT_EXTENSION_INIT
|
||||||
os.makedirs(os.path.dirname(extension_path), exist_ok=True)
|
os.makedirs(os.path.dirname(extension_path), exist_ok=True)
|
||||||
|
|
||||||
if not os.path.exists(extension_path): # If user extensions file doesn't exist
|
if not os.path.exists(extension_path): # If user extensions file doesn't exist
|
||||||
|
|
@ -118,13 +118,7 @@ def init_default_extensions(extension_path):
|
||||||
|
|
||||||
logging.info("Populating {}...".format(extension_path))
|
logging.info("Populating {}...".format(extension_path))
|
||||||
with open(extension_path, "w") as outfile:
|
with open(extension_path, "w") as outfile:
|
||||||
outfile.write(_DEFAULT_extension_INIT)
|
outfile.write(_DEFAULT_EXTENSION_INIT)
|
||||||
|
|
||||||
|
|
||||||
_DEFAULT_extension_INIT = """
|
_DEFAULT_EXTENSION_INIT = "from openflexure_microscope.api.default_extensions import *"
|
||||||
from openflexure_microscope.api.default_extensions.autofocus import autofocus_extension_v2
|
|
||||||
from openflexure_microscope.api.default_extensions.scan import scan_extension_v2
|
|
||||||
from openflexure_microscope.api.default_extensions.zip_builder import zip_extension_v2
|
|
||||||
|
|
||||||
__extensions__ = [autofocus_extension_v2, scan_extension_v2, zip_extension_v2]
|
|
||||||
"""
|
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,8 @@ import copy
|
||||||
|
|
||||||
from importlib import util
|
from importlib import util
|
||||||
import sys
|
import sys
|
||||||
|
import os
|
||||||
|
import glob
|
||||||
|
|
||||||
from openflexure_microscope.common.labthings_core.utilities import get_docstring
|
from openflexure_microscope.common.labthings_core.utilities import get_docstring
|
||||||
from openflexure_microscope.utilities import camel_to_snake, snake_to_spine
|
from openflexure_microscope.utilities import camel_to_snake, snake_to_spine
|
||||||
|
|
@ -123,7 +125,16 @@ class BaseExtension:
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def find_extensions(extension_path, module_name="extensions"):
|
def find_instances_in_module(module, class_to_find):
|
||||||
|
objs = []
|
||||||
|
for attribute in dir(module):
|
||||||
|
if not attribute.startswith("__"):
|
||||||
|
if isinstance(getattr(module, attribute), class_to_find):
|
||||||
|
objs.append(getattr(module, attribute))
|
||||||
|
return objs
|
||||||
|
|
||||||
|
|
||||||
|
def find_extensions_in_file(extension_path, module_name="extensions"):
|
||||||
logging.debug(f"Loading extensions from {extension_path}")
|
logging.debug(f"Loading extensions from {extension_path}")
|
||||||
|
|
||||||
spec = util.spec_from_file_location(module_name, extension_path)
|
spec = util.spec_from_file_location(module_name, extension_path)
|
||||||
|
|
@ -133,6 +144,18 @@ def find_extensions(extension_path, module_name="extensions"):
|
||||||
spec.loader.exec_module(mod)
|
spec.loader.exec_module(mod)
|
||||||
|
|
||||||
if hasattr(mod, "__extensions__"):
|
if hasattr(mod, "__extensions__"):
|
||||||
return mod.__extensions__
|
return [getattr(mod, ext_name) for ext_name in mod.__extensions__]
|
||||||
else:
|
else:
|
||||||
return None
|
return find_instances_in_module(mod, BaseExtension)
|
||||||
|
|
||||||
|
|
||||||
|
def find_extensions(extension_dir, module_name="extensions"):
|
||||||
|
logging.debug(f"Loading extensions from {extension_dir}")
|
||||||
|
|
||||||
|
extensions = []
|
||||||
|
extension_paths = glob.glob(os.path.join(extension_dir, "*.py"))
|
||||||
|
|
||||||
|
for extension_path in extension_paths:
|
||||||
|
extensions.extend(find_extensions_in_file(extension_path, module_name=module_name))
|
||||||
|
|
||||||
|
return extensions
|
||||||
|
|
@ -182,7 +182,7 @@ DEFAULT_CONFIG_FILE_PATH = os.path.join(HERE, "microscope_settings.default.json"
|
||||||
|
|
||||||
USER_CONFIG_DIR = os.path.join(os.path.expanduser("~"), ".openflexure")
|
USER_CONFIG_DIR = os.path.join(os.path.expanduser("~"), ".openflexure")
|
||||||
USER_CONFIG_FILE_PATH = os.path.join(USER_CONFIG_DIR, "microscope_settings.json")
|
USER_CONFIG_FILE_PATH = os.path.join(USER_CONFIG_DIR, "microscope_settings.json")
|
||||||
USER_EXTENSIONS_PATH = os.path.join(USER_CONFIG_DIR, "microscope_extensions", "__init__.py")
|
USER_EXTENSIONS_PATH = os.path.join(USER_CONFIG_DIR, "microscope_extensions")
|
||||||
|
|
||||||
# Load the default config
|
# Load the default config
|
||||||
with open(DEFAULT_CONFIG_FILE_PATH, "r") as default_rc:
|
with open(DEFAULT_CONFIG_FILE_PATH, "r") as default_rc:
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue