Skip to content
Snippets Groups Projects
Commit d21bdc6d authored by Tor-Einar Skog's avatar Tor-Einar Skog
Browse files

First working/testable version of model_factory

parent e11c53e9
No related branches found
No related tags found
1 merge request!2Model factory
# Keeping this deployment specific
models.txt
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
......
# All the models that should be available through this instance of VIPSCore-Python must be
# installed through running pip install -r models.txt
# The file models.txt is ignored by git, so it will be instance specific.
# To start, copy the models_example.txt file to models.txt and add models from
# the preferred sources
# pyPIP example
# model_name
# Gitlab/github example
# git+https://gitlab.nibio.no/VIPS/models/python/referencemodel.git@0.1.5
# Local disk example
# /home/treinar/prosjekter/vips/Sourcecode/models/Python/ReferenceModel
\ No newline at end of file
import importlib
import pkgutil
from vipscore_common.vips_model import VIPSModel
_models = None
_packages = None
def discover_models():
"""
Searches the current Python path for packages that contains "vips" in the package or module name
"""
models = {}
global _packages
_packages = []
for package in pkgutil.walk_packages():
# Package or module has to contain "vips",
# otherwise it will not be indexed
if "vips" in package.name:
_packages.append(package)
print(package.name)
try:
importlib.import_module(package.name)
except:
pass
for vipsmodel_subclass in VIPSModel.__subclasses__():
# We must have an instance to reach the property
vipsmodel_instance = vipsmodel_subclass()
if models.get(vipsmodel_instance.model_id) is None:
models[vipsmodel_instance.model_id] = vipsmodel_subclass
else:
possible_duplicate = models.get(vipsmodel_instance.model_id)
raise ValueError("ERROR when trying to index VIPS model %s: VIPS model %s has the same ID (%s). Please check your model installations" % (vipsmodel_subclass, possible_duplicate, vipsmodel_instance.model_id))
return models
def get_model_instance(model_id):
global _models
if _models is None:
print("Indexing models")
_models = discover_models()
return _models.get(model_id)()
def get_vips_model_packages():
global _packages
if _packages is None:
discover_models()
return _packages
# USAGE EXAMPLE. REMOVE when U start using the module from elsewhere
model = get_model_instance("REFERENCEM")
print(model.get_model_name())
print(get_vips_model_packages())
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment