The Gaudi Framework  v36r13 (995e4364)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
cpluginsvc.py
Go to the documentation of this file.
1 
11 from __future__ import print_function
12 
13 # cpluginsvc is a ctypes-based wrapper for the C-exposed API of GaudiPluginService
14 __doc__ = """
15 cpluginsvc is a ctypes-based wrapper for the C-API of the GaudiPluginService.
16 
17 e.g.:
18 
19 >>> from GaudiPluginService import cpluginsvc
20 >>> for _,f in cpluginsvc.factories().items():
21 ... try:
22 ... f.load()
23 ... except Exception:
24 ... print ("** could not load [%s] for factory [%s]" % (f.library, f.name))
25 ... continue
26 ... print(f)
27 ... for k,v in f.properties.iteritems():
28 ... print ("\t%s: %s" % (k,v))
29 """
30 
31 import ctypes
32 import ctypes.util
33 
34 __all__ = (
35  "Registry",
36  "registry",
37  "factories",
38  "Factory",
39  "Property",
40 )
41 
42 _libname = None
43 
44 
46  if _libname:
47  return _libname
48  import platform
49 
50  name = platform.system()
51 
52  fname = {
53  "Darwin": "libGaudiPluginService.dylib",
54  "Windows": "libGaudiPluginService.dll",
55  "Linux": "libGaudiPluginService.so",
56  }[name]
57  return fname
58 
59 
60 _libname = _get_filename()
61 _lib = ctypes.CDLL(_libname, ctypes.RTLD_GLOBAL)
62 
63 
64 class Registry(ctypes.Structure):
65  """Registry holds the list of factories known by the gaudi PluginService."""
66 
67  _fields_ = [("_registry", ctypes.c_void_p)]
68 
69  @property
70  def factories(self):
71  facts = {}
72  n = _lib.cgaudi_pluginsvc_get_factory_size(self)
73  for i in range(n):
74  f = _lib.cgaudi_pluginsvc_get_factory_at(self, i)
75  facts[f.name] = f
76  return facts
77 
78  pass
79 
80 
81 _instance = None
82 
83 
84 def registry():
85  """registry returns the singleton-like instance of the plugin service."""
86 
87  global _instance
88  if _instance:
89  return _instance
90  _instance = _lib.cgaudi_pluginsvc_instance()
91  return _instance
92 
93 
94 def factories():
95  """
96  factories returns the list of components factory informations known to the plugin service
97  """
98  return registry().factories
99 
100 
101 class Factory(ctypes.Structure):
102  """
103  Factory holds informations about a component's factory:
104  - its name
105  - the library hosting that component
106  - the type of component (algorithm, service, tool, ...)
107  - the return type of this factory
108  - the C++ class name of that component
109  - the properties which may decorate that component.
110  """
111 
112  _fields_ = [
113  ("_registry", Registry),
114  ("_id", ctypes.c_char_p),
115  ]
116 
117  @property
118  def name(self):
119  return self._id.decode("ascii")
120 
121  @property
122  def library(self):
123  return _lib.cgaudi_factory_get_library(self).decode("ascii")
124 
125  @property
126  def type(self):
127  return _lib.cgaudi_factory_get_type(self).decode("ascii")
128 
129  @property
130  def classname(self):
131  return _lib.cgaudi_factory_get_classname(self).decode("ascii")
132 
133  @property
134  def properties(self):
135  props = {}
136  nprops = _lib.cgaudi_factory_get_property_size(self)
137  for i in range(nprops):
138  prop = _lib.cgaudi_factory_get_property_at(self, i)
139  props[prop.key] = prop.value
140  return props
141 
142  def load(self):
143  """load the C++ library hosting this factory"""
144  return ctypes.CDLL(self.library, ctypes.RTLD_GLOBAL)
145 
146  def __repr__(self):
147  return "<Factory id=%s library=%s type=%s class=%s props=%d>" % (
148  self.name,
149  self.library,
150  self.type,
151  self.classname,
152  len(self.properties),
153  )
154 
155  pass
156 
157 
158 class Property(ctypes.Structure):
159  """
160  Property is a pair (key, value) optionally decorating a factory.
161  It is used to attach additional informations about a factory.
162  """
163 
164  _fields_ = [
165  ("_registry", Registry),
166  ("_id", ctypes.c_char_p),
167  ("_key", ctypes.c_char_p),
168  ]
169 
170  @property
171  def key(self):
172  return _lib.cgaudi_property_get_key(self).decode("ascii")
173 
174  @property
175  def value(self):
176  return _lib.cgaudi_property_get_value(self).decode("ascii")
177 
178  pass
179 
180 
181 _functions_list = [
182  (
183  "cgaudi_pluginsvc_instance",
184  [],
185  Registry,
186  ),
187  (
188  "cgaudi_pluginsvc_get_factory_size",
189  [Registry],
190  ctypes.c_int,
191  ),
192  (
193  "cgaudi_pluginsvc_get_factory_at",
194  [Registry, ctypes.c_int],
195  Factory,
196  ),
197  (
198  "cgaudi_factory_get_library",
199  [Factory],
200  ctypes.c_char_p,
201  ),
202  (
203  "cgaudi_factory_get_type",
204  [Factory],
205  ctypes.c_char_p,
206  ),
207  (
208  "cgaudi_factory_get_classname",
209  [Factory],
210  ctypes.c_char_p,
211  ),
212  (
213  "cgaudi_factory_get_property_size",
214  [Factory],
215  ctypes.c_int,
216  ),
217  (
218  "cgaudi_factory_get_property_at",
219  [Factory, ctypes.c_int],
220  Property,
221  ),
222  (
223  "cgaudi_property_get_key",
224  [Property],
225  ctypes.c_char_p,
226  ),
227  (
228  "cgaudi_property_get_value",
229  [Property],
230  ctypes.c_char_p,
231  ),
232 ]
233 
234 for f in _functions_list:
235  n = f[0]
236  func = getattr(_lib, n)
237  func.argtypes = f[1]
238  func.restype = f[2]
239  if len(f) == 4:
240  func.errcheck = f[3]
241  pass
242 
243 if __name__ == "__main__":
244  print("instance: %s" % registry())
245  print("factories: %d" % len(factories()))
246  for _, f in factories().items():
247  try:
248  f.load()
249  except Exception:
250  print("** could not load [%s] for factory [%s]" % (f.library, f.name))
251  continue
252  print(f)
253  for k, v in f.properties.items():
254  print("\t%s: %s" % (k, v))
255 
256 # EOF
GaudiPluginService.cpluginsvc.Factory.classname
def classname(self)
Definition: cpluginsvc.py:130
GaudiPluginService.cpluginsvc.Factory.name
def name(self)
Definition: cpluginsvc.py:118
GaudiPluginService.cpluginsvc.Factory.__repr__
def __repr__(self)
Definition: cpluginsvc.py:146
GaudiPluginService.cpluginsvc.Factory.library
def library(self)
Definition: cpluginsvc.py:122
GaudiPluginService.cpluginsvc.Factory.properties
def properties(self)
Definition: cpluginsvc.py:134
GaudiPluginService.cpluginsvc.registry
def registry()
Definition: cpluginsvc.py:84
GaudiPluginService.cpluginsvc.Factory
Definition: cpluginsvc.py:101
GaudiPluginService.cpluginsvc.Property.key
def key(self)
Definition: cpluginsvc.py:171
GaudiPluginService.cpluginsvc.Registry.factories
def factories(self)
Definition: cpluginsvc.py:70
GaudiPluginService.cpluginsvc.Property
Definition: cpluginsvc.py:158
GaudiPluginService.cpluginsvc.Factory.load
def load(self)
Definition: cpluginsvc.py:142
GaudiPluginService.cpluginsvc._get_filename
def _get_filename()
Definition: cpluginsvc.py:45
GaudiPluginService.cpluginsvc.Registry
Definition: cpluginsvc.py:64
GaudiPluginService.cpluginsvc.factories
def factories()
Definition: cpluginsvc.py:94
GaudiPluginService.cpluginsvc.Property.value
def value(self)
Definition: cpluginsvc.py:175
GaudiPluginService.cpluginsvc.Factory.type
def type(self)
Definition: cpluginsvc.py:126
Gaudi::Functional::details::zip::range
decltype(auto) range(Args &&... args)
Zips multiple containers together to form a single range.
Definition: FunctionalDetails.h:102
GaudiPython.Pythonizations.items
items
Definition: Pythonizations.py:546