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 inspect
|
||||||
import re
|
import re
|
||||||
import tempfile
|
import tempfile
|
||||||
|
from contextlib import contextmanager
|
||||||
|
|
||||||
from . import desc
|
from . import desc
|
||||||
from .graph import * # TODO: remove this
|
from .graph import * # TODO: remove this
|
||||||
|
@ -10,44 +11,58 @@ cacheFolder = os.path.join(tempfile.gettempdir(), 'processGraphCache')
|
||||||
nodesDesc = {}
|
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
|
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 = []
|
nodeTypes = []
|
||||||
errors = []
|
errors = []
|
||||||
for pluginFile in pluginfiles:
|
|
||||||
if pluginFile.startswith('__'):
|
|
||||||
continue
|
|
||||||
|
|
||||||
try:
|
# 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
|
||||||
|
|
||||||
pluginName = os.path.splitext(pluginFile)[0]
|
pluginName = os.path.splitext(pluginFile)[0]
|
||||||
module = '.' + pluginName
|
pluginModule = '.' + pluginName
|
||||||
m = importlib.import_module(module, package=package)
|
try:
|
||||||
p = [a for a in m.__dict__.values() if inspect.isclass(a) and issubclass(a, desc.Node)]
|
m = importlib.import_module(pluginModule, package=package.__name__)
|
||||||
if not p:
|
p = [a for a in m.__dict__.values() if inspect.isclass(a) and issubclass(a, desc.Node)]
|
||||||
raise RuntimeError('No class defined in plugin: %s' % module)
|
if not p:
|
||||||
nodeTypes.extend(p)
|
raise RuntimeError('No class defined in plugin: %s' % pluginModule)
|
||||||
except Exception as e:
|
nodeTypes.extend(p)
|
||||||
errors.append(' * Errors while loading "{}".\n File: {}\n {}'.format(pluginName, pluginFile, str(e)))
|
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])
|
||||||
nodesDesc = dict([(m.__name__, m) for m in nodeTypes])
|
print('Plugins loaded: ', ', '.join(nodesDesc.keys()))
|
||||||
print('Plugins loaded: ', ', '.join(nodesDesc.keys()))
|
if errors:
|
||||||
if errors:
|
print('== Error while loading the following plugins: ==')
|
||||||
print('== Error while loading the following plugins: ==')
|
print('\n'.join(errors))
|
||||||
print('\n'.join(errors))
|
print('================================================')
|
||||||
print('================================================')
|
|
||||||
|
|
||||||
return nodeTypes
|
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