mirror of
https://github.com/alicevision/Meshroom.git
synced 2025-04-28 17:57:16 +02:00
[graph] fix nodes plugin import from folder path
This commit is contained in:
parent
a758a47475
commit
0485ce4738
1 changed files with 45 additions and 30 deletions
|
@ -2,6 +2,7 @@ import importlib
|
|||
import inspect
|
||||
import re
|
||||
import tempfile
|
||||
from contextlib import contextmanager
|
||||
|
||||
from . import desc
|
||||
from .graph import * # TODO: remove this
|
||||
|
@ -10,35 +11,48 @@ cacheFolder = os.path.join(tempfile.gettempdir(), 'processGraphCache')
|
|||
nodesDesc = {}
|
||||
|
||||
|
||||
def loadNodesDesc(folder, package='nodes'):
|
||||
'''
|
||||
'''
|
||||
@contextmanager
|
||||
def add_to_path(p):
|
||||
import sys
|
||||
old_path = sys.path
|
||||
sys.path = sys.path[:]
|
||||
sys.path.insert(0, p)
|
||||
try:
|
||||
yield
|
||||
finally:
|
||||
sys.path = old_path
|
||||
|
||||
|
||||
def loadNodesDesc(folder, packageName='nodes'):
|
||||
"""
|
||||
"""
|
||||
global nodesDesc
|
||||
|
||||
pysearchre = re.compile('.py$', re.IGNORECASE)
|
||||
pluginfiles = filter(pysearchre.search,
|
||||
os.listdir(os.path.join(folder,
|
||||
package)))
|
||||
# import parent module
|
||||
importlib.import_module(package)
|
||||
nodeTypes = []
|
||||
errors = []
|
||||
for pluginFile in pluginfiles:
|
||||
|
||||
# temporarily add folder to python path
|
||||
with add_to_path(folder):
|
||||
# import node package
|
||||
package = importlib.import_module(packageName)
|
||||
|
||||
pysearchre = re.compile('.py$', re.IGNORECASE)
|
||||
pluginFiles = filter(pysearchre.search, os.listdir(os.path.dirname(package.__file__)))
|
||||
for pluginFile in pluginFiles:
|
||||
if pluginFile.startswith('__'):
|
||||
continue
|
||||
|
||||
try:
|
||||
pluginName = os.path.splitext(pluginFile)[0]
|
||||
module = '.' + pluginName
|
||||
m = importlib.import_module(module, package=package)
|
||||
pluginModule = '.' + pluginName
|
||||
try:
|
||||
m = importlib.import_module(pluginModule, package=package.__name__)
|
||||
p = [a for a in m.__dict__.values() if inspect.isclass(a) and issubclass(a, desc.Node)]
|
||||
if not p:
|
||||
raise RuntimeError('No class defined in plugin: %s' % module)
|
||||
raise RuntimeError('No class defined in plugin: %s' % pluginModule)
|
||||
nodeTypes.extend(p)
|
||||
except Exception as e:
|
||||
errors.append(' * Errors while loading "{}".\n File: {}\n {}'.format(pluginName, pluginFile, str(e)))
|
||||
|
||||
|
||||
nodesDesc = dict([(m.__name__, m) for m in nodeTypes])
|
||||
print('Plugins loaded: ', ', '.join(nodesDesc.keys()))
|
||||
if errors:
|
||||
|
@ -48,6 +62,7 @@ def loadNodesDesc(folder, package='nodes'):
|
|||
|
||||
return nodeTypes
|
||||
|
||||
# Load plugins
|
||||
loadNodesDesc(folder=os.path.dirname(os.path.dirname(__file__)))
|
||||
|
||||
# Load plugins
|
||||
# TODO: seems "risky" at module level, should be in a registerNodes function in meshroom package
|
||||
loadNodesDesc(folder=os.path.dirname(os.path.dirname(__file__)))
|
||||
|
|
Loading…
Add table
Reference in a new issue