| The Gaudi Framework v32r1 (f65d50dc)

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