Commit 82ab5c5a authored by Laurent Claustre's avatar Laurent Claustre

Merge branch 'entry-points' into 'master'

Entry points

See merge request !34
parents 4c817dd4 b2e6001a
Pipeline #13626 passed with stages
in 26 seconds
......@@ -31,6 +31,12 @@ import functools
import PyTango
try:
import pkg_resources
except ImportError:
pkg_resources = None
ModDepend = ['Core', 'Espia']
Debug = 0
LimaDir = None
......@@ -51,10 +57,7 @@ def get_server_name(argv=None):
"""
if argv is None:
argv = sys.argv
full_exec_name = argv[0]
exec_name = os.path.split(full_exec_name)[-1]
exec_name = os.path.splitext(exec_name)[0]
return "/".join((exec_name, argv[1]))
return 'LimaCCDs/' + argv[1]
def get_device_class_map(server=None, cache=True):
"""
......@@ -442,3 +445,40 @@ def create_tango_objects(ct_control, name_template):
tango_ct_map[tango_ct_control_name] = tango_ct_control, tango_object
return server, tango_ct_map
def _import(name):
__import__(name)
return sys.modules[name]
def get_entry_point(group, name):
# try to find an extension using setuptools entry points
if pkg_resources is None:
return None
entry_points = tuple(pkg_resources.iter_entry_points(group, name))
if not entry_points:
return None
elif len(entry_points) > 1:
raise ValueError('found more than one entry point matching {}'.format(name))
return entry_points[0]
def get_camera_module(name):
"""Returns the python module for the given camera type"""
entry_point = get_entry_point('Lima_tango_camera', name)
if entry_point is None:
# fallback to search in local camera directory
mod_name = 'Lima.Server.camera.{}'.format(name)
return _import(mod_name)
return entry_point.load()
def get_plugin_module(name):
"""Returns the python module for the given plugin type"""
entry_point = get_entry_point('Lima_tango_plugin', name)
if entry_point is None:
# fallback to search in local plugins directory
mod_name = 'Lima.Server.plugins.{}'.format(name)
return _import(mod_name)
return entry_point.load()
......@@ -64,6 +64,7 @@ LimaCameraType = None
from .EnvHelper import get_sub_devices
from .EnvHelper import get_lima_camera_type, get_lima_device_name
from .EnvHelper import create_tango_objects
from .EnvHelper import get_camera_module, get_plugin_module
from .AttrHelper import get_attr_4u
from Lima.Server.AttrHelper import getDictKey, getDictValue
from Lima import Core
......@@ -382,7 +383,7 @@ class LimaCCDs(PyTango.Device_4Impl) :
@Core.DEB_MEMBER_FUNCT
def delete_device(self) :
try:
m = __import__('Lima.Server.camera.%s' % (self.LimaCameraType),None,None,'Lima.Server.camera.%s' % (self.LimaCameraType))
m = get_camera_module(self.LimaCameraType)
except ImportError:
pass
else:
......@@ -424,8 +425,7 @@ class LimaCCDs(PyTango.Device_4Impl) :
pass
else:
try:
m = __import__('Lima.Server.plugins.%s' % (accThresholdCallbackModule),None,None,
'Lima.Server.plugins.%s' % (accThresholdCallbackModule))
m = get_plugin_module(accThresholdCallbackModule)
except ImportError:
deb.Error("Couldn't import plugins.%s" % accThresholdCallbackModule)
else:
......@@ -2516,17 +2516,21 @@ def declare_camera_n_commun_to_tango_world(util) :
try:
if LimaCameraType and (module_name != LimaCameraType):
continue
m = __import__('Lima.Server.camera.%s' % (module_name),None,None,'Lima.Server.camera.%s' % (module_name))
m = get_camera_module(module_name)
except ImportError:
continue
else:
try:
func = getattr(m,'get_tango_specific_class_n_device')
specificClass,specificDevice = func()
class_info = func()
except AttributeError:
pass
else:
util.add_TgClass(specificClass,specificDevice,specificDevice.__name__)
if isinstance(class_info, (list, tuple)):
specificClass, specificDevice = class_info
else:
specificClass, specificDevice = class_info.TangoClassClass, class_info
util.add_class(specificClass, specificDevice)
try:
func = getattr(m, 'get_taco_specific_cmd_list_n_proxy_cont')
cmd_list, proxy_cont = func()
......@@ -2537,7 +2541,7 @@ def declare_camera_n_commun_to_tango_world(util) :
warningFlag = False
for module_name in plugins.__all__:
try:
m = __import__('Lima.Server.plugins.%s' % (module_name),None,None,'Lima.Server.plugins.%s' % (module_name))
m = get_plugin_module(module_name)
except ImportError:
print ("Warning optional plugin %s can't be load, dependency not satisfied." % module_name)
warningFlag = True
......@@ -2558,8 +2562,12 @@ def declare_camera_n_commun_to_tango_world(util) :
except AttributeError:
continue
else:
specificClass,specificDevice = func()
util.add_TgClass(specificClass,specificDevice,specificDevice.__name__)
class_info = func()
if isinstance(class_info, (list, tuple)):
specificClass, specificDevice = class_info
else:
specificClass, specificDevice = class_info.TangoClassClass, class_info
util.add_class(specificClass, specificDevice)
if warningFlag and verboseLevel < 4:
print ("For more plugins dependency information start server with -v4")
......@@ -2572,7 +2580,7 @@ def export_default_plugins() :
beamlineName,_,cameraName = masterDeviceName.split('/')
for module_name in plugins.__all__:
try:
m = __import__('Lima.Server.plugins.%s' % (module_name),None,None,'Lima.Server.plugins.%s' % (module_name))
m = get_plugin_module(module_name)
except ImportError:
continue
else:
......@@ -2612,7 +2620,7 @@ def export_ct_control(ct_map):
def _set_control_ref(ctrl_ref) :
for module_name in plugins.__all__:
try:
m = __import__('Lima.Server.plugins.%s' % (module_name),None,None,'Lima.Server.plugins.%s' % (module_name))
m = get_plugin_module(module_name)
except ImportError:
continue
else:
......@@ -2683,9 +2691,8 @@ def _get_control():
except KeyError: # wizard mode
return None
mod_name = 'Lima.Server.camera.' + camera_type
try:
m = __import__(mod_name, None, None, mod_name)
m = get_camera_module(camera_type)
except ImportError:
import traceback
traceback.print_exc()
......@@ -2693,12 +2700,16 @@ def _get_control():
properties = {}
className2deviceName = get_sub_devices()
try:
_, specificDevice = m.get_tango_specific_class_n_device()
class_info = m.get_tango_specific_class_n_device()
except AttributeError:
import traceback
traceback.print_exc()
pass
else:
if isinstance(class_info, (list, tuple)):
_, specificDevice = class_info
else:
specificDevice = class_info
typeFlagsNameList = []
for l in range(verboseLevel + 1):
typeFlagsNameList += VerboseLevel2TypeFlags.get(l, [])
......@@ -2726,10 +2737,13 @@ def _get_control():
#
#==================================================================
verboseLevel = 0
def main() :
def main(args=None) :
args = list(args or sys.argv)
args[0] = 'LimaCCDs'
global verboseLevel
verboseLevel = 0
for option in sys.argv:
for option in args:
if option.startswith('-v'):
try:
verboseLevel = int(option[2:])
......@@ -2738,7 +2752,7 @@ def main() :
pytango_ver = PyTango.__version_info__[:3]
try:
py = PyTango.Util(sys.argv)
py = PyTango.Util(args)
py.add_TgClass(LimaCCDsClass,LimaCCDs,'LimaCCDs')
try:
declare_camera_n_commun_to_tango_world(py)
......
......@@ -19,9 +19,10 @@
# You should have received a copy of the GNU General Public License
# along with this program; if not, see <http://www.gnu.org/licenses/>.
############################################################################
__all__ = []
def _init_module() :
import os
cameras = []
for root,dirs,files in os.walk(__path__[0]) :
for file_name in files :
if file_name.startswith('__') : continue
......@@ -30,6 +31,15 @@ def _init_module() :
subdir = root[len(__path__[0]) + 1:]
if subdir:
base = '%s.%s' % (subdir,base)
__all__.append(base)
_init_module()
cameras.append(base)
try:
import pkg_resources
except ImportError:
pass
else:
for ep in pkg_resources.iter_entry_points('Lima_tango_camera'):
cameras.append(ep.name)
return cameras
__all__ = _init_module()
......@@ -19,9 +19,10 @@
# You should have received a copy of the GNU General Public License
# along with this program; if not, see <http://www.gnu.org/licenses/>.
############################################################################
__all__ = []
def _init_module() :
import os
plugins = []
for root,dirs,files in os.walk(__path__[0],followlinks=True) :
for file_name in files :
if file_name.startswith('__') : continue
......@@ -30,6 +31,16 @@ def _init_module() :
subdir = root[len(__path__[0]) + 1:]
if subdir:
base = '%s.%s' % (subdir,base)
__all__.append(base)
_init_module()
plugins.append(base)
try:
import pkg_resources
except ImportError:
pass
else:
for ep in pkg_resources.iter_entry_points('Lima_tango_plugin'):
plugins.append(ep.name)
return plugins
__all__ = _init_module()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment