Support for procedure imports from scripts base module
This commit is contained in:
parent
25ea0ea77c
commit
124269776d
1 changed files with 26 additions and 21 deletions
|
@ -8,6 +8,8 @@ import pkgutil
|
||||||
import re
|
import re
|
||||||
import socket
|
import socket
|
||||||
import sys
|
import sys
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
import yaml
|
import yaml
|
||||||
|
|
||||||
from platypush.utils import get_hash, is_functional_procedure, is_functional_hook
|
from platypush.utils import get_hash, is_functional_procedure, is_functional_hook
|
||||||
|
@ -184,33 +186,36 @@ class Config(object):
|
||||||
|
|
||||||
return config
|
return config
|
||||||
|
|
||||||
def _load_scripts(self):
|
def _load_module(self, modname: str, prefix: Optional[str] = None):
|
||||||
scripts_dir = self._config['scripts_dir']
|
|
||||||
sys_path = sys.path.copy()
|
|
||||||
sys.path = [scripts_dir] + sys.path
|
|
||||||
|
|
||||||
for x, modname, y in pkgutil.walk_packages(path=[scripts_dir], onerror=lambda x: None):
|
|
||||||
try:
|
try:
|
||||||
module = importlib.import_module(modname)
|
module = importlib.import_module(modname)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print('Unhandled exception while importing module {} from {}: {}'.format(
|
print('Unhandled exception while importing module {}: {}'.format(modname, str(e)))
|
||||||
modname, scripts_dir, str(e)
|
return
|
||||||
))
|
|
||||||
|
|
||||||
continue
|
|
||||||
|
|
||||||
|
prefix = modname + '.' if prefix is None else prefix
|
||||||
self.procedures.update(**{
|
self.procedures.update(**{
|
||||||
modname + '.' + name: obj
|
prefix + name: obj
|
||||||
for name, obj in inspect.getmembers(module)
|
for name, obj in inspect.getmembers(module)
|
||||||
if is_functional_procedure(obj)
|
if is_functional_procedure(obj)
|
||||||
})
|
})
|
||||||
|
|
||||||
self.event_hooks.update(**{
|
self.event_hooks.update(**{
|
||||||
modname + '.' + name: obj
|
prefix + name: obj
|
||||||
for name, obj in inspect.getmembers(module)
|
for name, obj in inspect.getmembers(module)
|
||||||
if is_functional_hook(obj)
|
if is_functional_hook(obj)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
def _load_scripts(self):
|
||||||
|
scripts_dir = self._config['scripts_dir']
|
||||||
|
sys_path = sys.path.copy()
|
||||||
|
sys.path = [scripts_dir] + sys.path
|
||||||
|
scripts_modname = os.path.basename(scripts_dir)
|
||||||
|
self._load_module(scripts_modname, prefix='')
|
||||||
|
|
||||||
|
for _, modname, _ in pkgutil.walk_packages(path=[scripts_dir], onerror=lambda x: None):
|
||||||
|
self._load_module(modname)
|
||||||
|
|
||||||
sys.path = sys_path
|
sys.path = sys_path
|
||||||
|
|
||||||
def _init_components(self):
|
def _init_components(self):
|
||||||
|
|
Loading…
Reference in a new issue