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