Gaudi Framework, version v23r5

Home   Generated: Wed Nov 28 2012
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
IoComponentMgr.cpp
Go to the documentation of this file.
1 
2 // IoComponentMgr.cxx
3 // Implementation file for class IoComponentMgr
4 // Author: S.Binet<binet@cern.ch>
6 
7 // python includes
8 #include <Python.h>
9 
10 // GaudiMP includes
11 #include "IoComponentMgr.h"
12 
13 
14 // STL includes
15 
16 // FrameWork includes
17 #include "GaudiKernel/SvcFactory.h"
18 #include "GaudiKernel/Property.h"
19 
21 
22 
23 
24 // Public methods:
26 
27 // Constructors
29 IoComponentMgr::IoComponentMgr( const std::string& name,
30  ISvcLocator* svc )
31 : base_class(name,svc), m_log(msgSvc(), name ),
32  m_dict ( 0 )
33 {
34  //
35  // Property declaration
36  //
37  //declareProperty( "Property", m_nProperty );
38 
39 // declareProperty ("Registry",
40 // m_dict_location = "GaudiMP.IoRegistry.registry",
41 // "Location of the python dictionary holding the "
42 // "associations: \n"
43 // " {component-name:{ 'old-fname' : ['io','new-fname'] }}\n"
44 // "\nSyntax: <python-module>.<python-module>.<fct-name> \n"
45 // " where fct-name is a function returning the wanted "
46 // " dictionary.");
47 }
48 
49 // Destructor
52 {
53  Py_XDECREF (m_dict);
54 }
55 
56 // Service's Hooks
59 {
60  m_log << MSG::INFO << "Initializing " << name() << "..." << endmsg;
61 
62  if ( Service::initialize().isFailure() ) {
63  m_log << MSG::ERROR << "Unable to initialize Service base class" << endmsg;
64  return StatusCode::FAILURE;
65  }
67 
68  if ( ! Py_IsInitialized() ) {
69  if (m_log.level() <= MSG::DEBUG) {
70  m_log << MSG::DEBUG << "Initializing Python" << endmsg;
71  }
72  PyEval_InitThreads();
73  Py_Initialize();
74 
75  if ( ! Py_IsInitialized() ) {
76  m_log << MSG::ERROR << "Unable to initialize Python" << endmsg;
77  return StatusCode::FAILURE;
78  }
79  }
80 
81 
82  // retrieve the python dictionary holding the I/O registry
83  const std::string py_module_name = "GaudiMP.IoRegistry";
84  m_log << MSG::DEBUG << "importing module [" << py_module_name << "]..."
85  << endmsg;
86  PyObject *module = PyImport_ImportModule ((char*)py_module_name.c_str());
87  if ( !module || !PyModule_Check (module) ) {
88  m_log << MSG::ERROR << "Could not import [" << py_module_name << "] !"
89  << endmsg;
90  Py_XDECREF (module);
91  return StatusCode::FAILURE;
92  }
93 
94  const std::string py_class_name = "IoRegistry";
95  PyObject *pyclass = PyDict_GetItemString (PyModule_GetDict(module),
96  (char*)py_class_name.c_str());
97  // borrowed ref.
98  Py_XINCREF (pyclass);
99  if ( !pyclass ) {
100  m_log << MSG::ERROR << "Could not import ["
101  << py_class_name << "] from module ["
102  << py_module_name << "] !"
103  << endmsg ;
104  Py_XDECREF (pyclass);
105  Py_DECREF (module);
106  return StatusCode::FAILURE;
107  }
108 
109  m_dict = PyObject_GetAttrString (pyclass, (char*)"instances");
110  if ( !m_dict || !PyDict_Check (m_dict) ) {
111  m_log << MSG::ERROR
112  << "could not retrieve attribute [instances] from class ["
113  << py_module_name << "." << py_class_name << "] !" << endmsg;
114  Py_DECREF (pyclass);
115  Py_DECREF (module);
116  return StatusCode::FAILURE;
117  }
118 
119  m_log << MSG::INFO << "python I/O registry retrieved [ok]" << endmsg;
120  if ( m_log.level() <= MSG::DEBUG ) {
121  std::string repr = "";
122  // PyObject_Repr returns a new ref.
123  PyObject* py_repr = PyObject_Repr (m_dict);
124  if ( py_repr && PyString_Check(py_repr) ) {
125  repr = PyString_AsString(py_repr);
126  }
127  Py_XDECREF( py_repr );
128  m_log << MSG::DEBUG << "content: " << repr << endmsg;
129  }
130 
131  return StatusCode::SUCCESS;
132 }
133 
135 {
136  m_log << MSG::INFO << "Finalizing " << name() << "..." << endmsg;
137 
138 
139  if (m_log.level() <= MSG::DEBUG) {
140  m_log << MSG::DEBUG << "Listing all monitored entries: " << std::endl;
141 
142  std::string repr = "";
143  // PyObject_Repr returns a new ref.
144  PyObject* py_repr = PyObject_Repr (m_dict);
145  if ( py_repr && PyString_Check(py_repr) ) {
146  repr = PyString_AsString(py_repr);
147  }
148  Py_XDECREF( py_repr );
149  m_log << MSG::DEBUG << "content: " << repr << endmsg;
150  }
151 
152  return StatusCode::SUCCESS;
153 }
154 
156 // Const methods:
158 
161 bool
163 {
164  if ( 0 == iocomponent ) {
165  return false;
166  }
167  const std::string& ioname = iocomponent->name();
169  return io != m_ioregistry.end();
170 }
171 
175 bool
177  const std::string& fname) const
178 {
179  if ( 0 == iocomponent ) {
180  return false;
181  }
182  const std::string& ioname = iocomponent->name();
183 
184  // m_dict is a python dictionary like so:
185  // { 'iocomp-name' : { 'oldfname' : [ 'iomode', 'newfname' ] } }
186 
187  // -> check there is an 'iocomp-name' entry
188  // -> retrieve that entry
189  PyObject *o = PyDict_GetItemString (m_dict, (char*)ioname.c_str());
190  Py_XINCREF (o);
191 
192  // -> check it is a dictionary
193  if ( NULL==o || !PyDict_Check (o) ) {
194  Py_XDECREF (o);
195 
196  return false;
197  }
198 
199  // -> check 'oldfname' exists
200  PyObject *item = PyDict_GetItemString (o, (char*)fname.c_str());
201 
202  const bool contains = (item != 0);
203  if ( contains == false ) {
204  std::string repr = "";
205  // PyObject_Repr returns a new ref.
206  PyObject* py_repr = PyObject_Repr (o);
207  if ( py_repr && PyString_Check(py_repr) ) {
208  repr = PyString_AsString(py_repr);
209  }
210  Py_XDECREF( py_repr );
211  m_log << MSG::ERROR << "content: " << repr << endmsg;
212  }
213  Py_DECREF (o);
214  Py_XDECREF(item);
215  return contains;
216 }
217 
219 // Non-const methods:
221 
230 {
231  if ( 0 == iocomponent ) {
232  m_log << MSG::ERROR
233  << "io_register (component) received a NULL pointer !" << endmsg;
234  return StatusCode::FAILURE;
235  }
236  const std::string& ioname = iocomponent->name();
238  if ( itr == m_ioregistry.end() ) {
239  iocomponent->addRef(); // ownership...
240  m_ioregistry[ioname] = iocomponent;
241  m_iostack.push_back (iocomponent);
242  } else {
243  m_log << MSG::INFO << "IoComponent[" << iocomponent->name()
244  <<"] already registered @" << (void*)itr->second << endmsg;
245  }
246  return StatusCode::SUCCESS;
247 }
248 
258  const std::string& fname)
259 {
260  if ( 0 == iocomponent ) {
261  return StatusCode::FAILURE;
262  }
263  const std::string& ioname = iocomponent->name();
264 
265  if ( !io_hasitem (iocomponent) ) {
266  if ( !io_register (iocomponent).isSuccess() ) {
267  m_log << MSG::ERROR
268  << "could not register component [" << iocomponent->name() << "] "
269  << "with the I/O component manager !"
270  << endmsg;
271  return StatusCode::FAILURE;
272  }
273  }
274 
275  // m_dict is a python dictionary like so:
276  // { 'iocomp-name' : { 'oldfname' : [ 'iomode', 'newfname' ] } }
277 
278  // -> check there is an 'iocomp-name' entry
279  // -> retrieve that entry
280  PyObject *o = PyDict_GetItemString (m_dict, (char*)ioname.c_str());
281 
282  // -> check it is a dictionary
283  if ( NULL==o ) {
284  o = PyDict_New();
285  if (NULL == o) {
286  m_log << MSG::ERROR << "could not create an I/O entry for ["
287  << ioname << "] "
288  << "in the I/O registry !" << endmsg;
289  return StatusCode::FAILURE;
290  }
291  if ( 0 != PyDict_SetItemString (m_dict, (char*)ioname.c_str(), o) ) {
292  Py_DECREF (o);
293  m_log << MSG::ERROR << "could not create an I/O entry for ["
294  << ioname << "] " << "in the I/O registry !" << endmsg;
295  return StatusCode::FAILURE;
296  }
297  } else if ( !PyDict_Check (o) ) {
298  m_log << MSG::ERROR
299  << "internal consistency error (expected a dictionary !)"
300  << endmsg;
301  return StatusCode::FAILURE;
302  } else {
303  // borrowed ref.
304  Py_INCREF (o);
305  }
306 
307  // -> check if 'fname' has already been registered
308  std::string mode;
309  switch (iomode) {
310  case IIoComponentMgr::IoMode::Input: mode ="<input>"; break;
311  case IIoComponentMgr::IoMode::Output:mode ="<output>";break;
312  default:
313  m_log << MSG::ERROR << "unknown value for iomode: [" << iomode << "] !"
314  << endmsg;
315  Py_DECREF (o);
316  return StatusCode::FAILURE;
317  }
318 
319  PyObject *val = PyDict_GetItemString (o, (char*)fname.c_str());
320  if ( 0 != val ) {
321  Py_DECREF (o);
322  return StatusCode::SUCCESS;
323  }
324 
325  val = PyList_New(2);
326  if ( 0 == val ) {
327  m_log << MSG::ERROR << "could not allocate a python-list !" << endmsg;
328  Py_DECREF (o);
329  return StatusCode::FAILURE;
330  }
331 
332  int err = PyList_SetItem (val,
333  0, PyString_FromString ((char*)mode.c_str()));
334  if (err) {
335  Py_DECREF (val);
336  Py_DECREF (o);
337  m_log << MSG::ERROR << "could not set py-iomode !" << endmsg;
338  return StatusCode::FAILURE;
339  }
340  Py_INCREF (Py_None);
341  // PyList_SetItem steals the ref...
342  err = PyList_SetItem (val, 1, Py_None);
343  if (err) {
344  Py_DECREF (val);
345  Py_DECREF (o);
346  m_log << MSG::ERROR << "could not properly fill python-list !" << endmsg;
347  return StatusCode::FAILURE;
348  }
349  err = PyDict_SetItemString (o, (char*)fname.c_str(), val);
350  if (err) {
351  m_log << MSG::ERROR << "could not properly fill registry w/ python-list !"
352  << endmsg;
353  Py_DECREF (val);
354  Py_DECREF (o);
355  return StatusCode::FAILURE;
356  }
357  Py_DECREF (o);
358  return StatusCode::SUCCESS;
359 }
360 
366  std::string& fname)
367 {
368  if ( 0 == iocomponent ) {
369  return StatusCode::FAILURE;
370  }
371 
372  const std::string& ioname = iocomponent->name();
373 
374  // m_dict is a python dictionary like so:
375  // { 'iocomp-name' : { 'oldfname' : [ 'iomode', 'newfname' ] } }
376 
377  // -> check there is an 'iocomp-name' entry
378  // -> retrieve that entry
379  PyObject *o = PyDict_GetItemString (m_dict, (char*)ioname.c_str());
380  Py_XINCREF (o);
381 
382  // -> check it is a dictionary
383  if ( NULL==o || !PyDict_Check (o) ) {
384  Py_XDECREF (o);
385  return StatusCode::FAILURE;
386  }
387 
388  // -> check 'oldfname' exists
389  PyObject *pylist = PyDict_GetItemString (o, (char*)fname.c_str());
390  Py_XINCREF (pylist);
391 
392  if ( NULL==pylist || !PyList_Check (pylist) ) {
393  Py_XDECREF(pylist);
394  Py_DECREF (o);
395  return StatusCode::FAILURE;
396  }
397 
398  const std::size_t sz = PyList_Size (pylist);
399  if ( 2 != sz ) {
400  m_log << MSG::ERROR << "[" << ioname << "][" << fname << "] list has size ["
401  << sz << " ! (expected sz==2)"
402  << endmsg;
403  Py_DECREF (o);
404  Py_DECREF (pylist);
405  return StatusCode::FAILURE;
406  }
407 
408  fname = PyString_AsString ( PyList_GetItem (pylist, 1) );
409  Py_DECREF (o);
410  Py_DECREF (pylist);
411  return StatusCode::SUCCESS;
412 }
413 
420 {
421  m_log << MSG::DEBUG << "reinitializing I/O subsystem..." << endmsg;
422  bool allgood = true;
423  for ( IoStack_t::iterator io = m_iostack.begin(), ioEnd = m_iostack.end();
424  io != ioEnd;
425  ++io ) {
426  m_log << MSG::DEBUG << " [" << (*io)->name() << "]->io_reinit()..."
427  << endmsg;
428  if ( !(*io)->io_reinit().isSuccess() ) {
429  allgood = false;
430  m_log << MSG::ERROR << "problem in [" << (*io)->name()
431  << "]->io_reinit() !" << endmsg;
432  }
433  // we are done with this guy... release it
434  (*io)->release();
435  }
436 
437  // we are done.
438  // FIXME: shall we allow for multiple io_reinitialize ?
439  m_iostack.clear();
441 
442  return allgood
445 }
446 
453 {
454  return StatusCode::SUCCESS;
455 }
456 
458 // Protected methods:
460 
462 // Const methods:
464 
466 // Non-const methods:
468 
469 

Generated at Wed Nov 28 2012 12:17:16 for Gaudi Framework, version v23r5 by Doxygen version 1.8.2 written by Dimitri van Heesch, © 1997-2004