Gaudi Framework, version v25r2

Home   Generated: Wed Jun 4 2014
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
genconf.cpp
Go to the documentation of this file.
1 #ifdef _WIN32
2 // Disable a warning in Boost program_options headers:
3 // inconsistent linkage in program_options/variables_map.hpp
4 #pragma warning ( disable : 4273 )
5 
6 // Avoid conflicts between windows and the message service.
7 #define NOMSG
8 #define NOGDI
9 #endif
10 
11 #ifdef __ICC
12 // disable icc warning #279: controlling expression is constant
13 // ... a lot of noise produced by the boost/filesystem/operations.hpp
14 #pragma warning(disable:279)
15 // Avoid icc remark #193: zero used for undefined preprocessing identifier "_MSC_VER"
16 #if !defined(_WIN32) && !defined(_MSC_VER)
17 #define _MSC_VER 0
18 #endif
19 #endif
20 
21 #include "boost/program_options.hpp"
22 // the hack for remark #193 is needed only for program_options and breaks regex.
23 #if defined(__ICC) && !defined(_WIN32) && (_MSC_VER == 0)
24 #undef _MSC_VER
25 #endif
26 
27 // Include files----------------------------------------------------------------
28 #include "boost/filesystem/operations.hpp"
29 #include "boost/filesystem/exception.hpp"
30 #include "boost/filesystem/convenience.hpp"
31 #include "boost/algorithm/string/split.hpp"
32 #include "boost/algorithm/string/classification.hpp"
33 #include "boost/algorithm/string/trim.hpp"
34 #include "boost/algorithm/string/case_conv.hpp"
35 #include "boost/algorithm/string/replace.hpp"
36 #include "boost/format.hpp"
37 #include "boost/regex.hpp"
38 
39 #include "GaudiKernel/System.h"
41 #include "GaudiKernel/IProperty.h"
42 #include "GaudiKernel/IAppMgrUI.h"
43 #include "GaudiKernel/IAlgorithm.h"
44 #include "GaudiKernel/IAlgTool.h"
45 #include "GaudiKernel/IAuditor.h"
46 #include "GaudiKernel/IAppMgrUI.h"
47 #include "GaudiKernel/Service.h"
48 #include "GaudiKernel/Bootstrap.h"
49 #include "GaudiKernel/SmartIF.h"
50 #include "GaudiKernel/HashMap.h"
52 
53 #include "GaudiKernel/Auditor.h"
54 #include "GaudiKernel/Service.h"
55 #include "GaudiKernel/AlgTool.h"
56 #include "GaudiKernel/Algorithm.h"
57 
58 #include "GaudiKernel/Time.h"
59 
60 #include <Gaudi/PluginService.h>
61 
62 #include <algorithm>
63 #include <iostream>
64 #include <fstream>
65 #include <sstream>
66 #include <exception>
67 #include <set>
68 #include <vector>
69 
70 #include "DsoUtils.h"
71 
72 namespace po = boost::program_options;
73 namespace fs = boost::filesystem;
74 
75 using namespace std;
76 
77 class IConverter;
78 
79 // useful typedefs
80 typedef std::vector<std::string> Strings_t;
81 typedef std::vector<fs::path> LibPathNames_t;
82 
83 namespace {
84  const std::string py_tab = " ";
85 
88  const boost::regex pythonIdentifier("^[a-zA-Z_][a-zA-Z0-9_]*$");
89 }
90 
92 {
94  string m_pkgName;
95 
99 
101  stringstream m_pyBuf;
102 
106 
111  stringstream m_dbBuf;
112 
119 
120 public:
121  configGenerator( const string& pkgName,
122  const string& outputDirName ) :
123  m_pkgName ( pkgName ),
124  m_outputDirName ( outputDirName ),
125  m_pyBuf ( ),
126  m_importGaudiHandles( false ),
127  m_dbBuf ( ),
128  m_configurable ( )
129  {}
130 
135  int genConfig( const Strings_t& modules, const string& userModule );
136 
138  void setConfigurableModule( const std::string& moduleName )
139  {
140  m_configurable[ "Module" ] = moduleName;
141  }
142 
144  void setConfigurableDefaultName( const std::string& defaultName )
145  {
146  m_configurable[ "DefaultName" ] = defaultName;
147  }
148 
150  void setConfigurableAlgorithm( const std::string& cfgAlgorithm )
151  {
152  m_configurable[ "Algorithm" ] = cfgAlgorithm;
153  }
154 
156  void setConfigurableAlgTool( const std::string& cfgAlgTool )
157  {
158  m_configurable[ "AlgTool" ] = cfgAlgTool;
159  }
160 
162  void setConfigurableAuditor( const std::string& cfgAuditor )
163  {
164  m_configurable[ "Auditor" ] = cfgAuditor;
165  }
166 
168  void setConfigurableService( const std::string& cfgService )
169  {
170  m_configurable[ "Service" ] = cfgService;
171  m_configurable[ "ApplicationMgr" ] = cfgService;
172  }
173 
174 private:
175  int genComponent( const std::string& libName,
176  const std::string& componentName,
177  const std::string& componentType,
178  const vector<Property*>& properties );
179  void genImport( std::ostream& s, const boost::format& frmt,std::string indent);
180  void genHeader( std::ostream& pyOut, std::ostream& dbOut );
181  void genBody( std::ostream& pyOut,
182  std::ostream& dbOut ) {
183  pyOut << m_pyBuf.str() << flush;
184  dbOut << m_dbBuf.str() << flush;
185  }
186  void genTrailer( std::ostream& pyOut,
187  std::ostream& dbOut );
188 
190  void pythonizeValue( const Property* prop,
191  string& pvalue,
192  string& ptype );
193 
195  void pythonizeName( string& name );
196 };
197 
198 int createAppMgr();
199 
200 //--- Command main program-----------------------------------------------------
201 int main ( int argc, char** argv )
202 //-----------------------------------------------------------------------------
203 {
204  fs::path pwd = fs::initial_path();
205  fs::path out;
206  Strings_t libs;
207  std::string pkgName;
208  std::string userModule;
209 
210  // declare a group of options that will be allowed only on command line
211  po::options_description generic("Generic options");
212  generic.add_options()
213  ("help,h",
214  "produce this help message")
215  ("package-name,p",
216  po::value<string>(),
217  "name of the package for which we create the configurables file")
218  ("input-libraries,i",
219  po::value<string>(),
220  "libraries to extract the component configurables from")
221  ("input-cfg,c",
222  po::value<string>(),
223  "path to the cfg file holding the description of the Configurable base "
224  "classes, the python module holding the Configurable definitions, etc...")
225  ("output-dir,o",
226  po::value<string>()->default_value("../genConf"),
227  "output directory for genconf files.")
228  ("debug-level,d",
229  po::value<int>()->default_value(0),
230  "debug level")
231  ("load-library,l",
232  po::value< Strings_t >()->composing(),
233  "preloading library")
234  ("user-module,m",
235  po::value<string>(),
236  "user-defined module to be imported by the genConf-generated one")
237  ("no-init",
238  "do not generate the (empty) __init__.py")
239  ;
240 
241  // declare a group of options that will be allowed both on command line
242  // _and_ in configuration file
243  po::options_description config("Configuration");
244  config.add_options()
245  ("configurable-module",
246  po::value<string>()->default_value("AthenaCommon"),
247  "Name of the module holding the configurable classes")
248  ("configurable-default-name",
249  po::value<string>()->default_value("Configurable.DefaultName"),
250  "Default name for the configurable instance")
251  ("configurable-algorithm",
252  po::value<string>()->default_value("ConfigurableAlgorithm"),
253  "Name of the configurable base class for Algorithm components")
254  ("configurable-algtool",
255  po::value<string>()->default_value("ConfigurableAlgTool"),
256  "Name of the configurable base class for AlgTool components")
257  ("configurable-auditor",
258  po::value<string>()->default_value("ConfigurableAuditor"),
259  "Name of the configurable base class for Auditor components")
260  ("configurable-service",
261  po::value<string>()->default_value("ConfigurableService"),
262  "Name of the configurable base class for Service components")
263  ;
264 
265  po::options_description cmdline_options;
266  cmdline_options.add(generic).add(config);
267 
268  po::options_description config_file_options;
269  config_file_options.add(config);
270 
271  po::options_description visible("Allowed options");
272  visible.add(generic).add(config);
273 
274  po::variables_map vm;
275 
276  try {
277  po::store( po::command_line_parser(argc, argv).
278  options(cmdline_options).run(),
279  vm );
280 
281  po::notify(vm);
282 
283  // try to read configuration from the optionally given configuration file
284  if( vm.count("input-cfg") ) {
285  string cfgFileName = vm["input-cfg"].as<string>();
286  cfgFileName = fs::system_complete( fs::path( cfgFileName ) ).string();
287  std::ifstream ifs( cfgFileName.c_str() );
288  po::store( parse_config_file( ifs, config_file_options ), vm );
289  }
290 
291  po::notify(vm);
292  }
293  catch ( po::error& err ) {
294  cout << "ERROR: error detected while parsing command options: "<< err.what() << endl;
295  return EXIT_FAILURE;
296  }
297 
298  //--- Process command options -----------------------------------------------
299  if( vm.count("help")) {
300  cout << visible << endl;
301  return EXIT_FAILURE;
302  }
303 
304  if( vm.count("package-name") ) {
305  pkgName = vm["package-name"].as<string>();
306  }
307  else {
308  cout << "ERROR: 'package-name' required" << endl;
309  cout << visible << endl;
310  return EXIT_FAILURE;
311  }
312 
313  if( vm.count("user-module") ) {
314  userModule = vm["user-module"].as<string>();
315  cout << "INFO: will import user module " << userModule << endl;
316  }
317 
318  if( vm.count("input-libraries") ) {
319  // re-shape the input arguments:
320  // - removes spurious spaces,
321  // - split into tokens.
322  Strings_t inputLibs;
323  {
324  string tmp = vm["input-libraries"].as<string>();
325  boost::trim(tmp);
326  boost::split( inputLibs, tmp,
327  boost::is_any_of(" "),
328  boost::token_compress_on );
329  }
330 
331  //
332  libs.reserve( inputLibs.size() );
333  for ( Strings_t::const_iterator iLib = inputLibs.begin();
334  iLib != inputLibs.end();
335  ++iLib ) {
336  std::string lib = fs::path(*iLib).stem().string();
337  if ( 0 == lib.find("lib") ) {
338  lib = lib.substr(3); // For *NIX remove "lib"
339  }
340  // remove duplicates
341  if ( !lib.empty() &&
342  std::find( libs.begin(), libs.end(), lib ) == libs.end() ) {
343  libs.push_back( lib );
344  }
345  } //> end loop over input-libraries
346  if ( libs.empty() ) {
347  cout << "ERROR: input component library(ies) required !\n"
348  << "ERROR: 'input-libraries' argument was ["
349  << vm["input-libraries"].as<string>()
350  << "]"
351  << endl;
352  return EXIT_FAILURE;
353  }
354  }
355  else {
356  cout << "ERROR: input component library(ies) required" << endl;
357  cout << visible << endl;
358  return EXIT_FAILURE;
359  }
360 
361  if( vm.count("output-dir") ) {
362  out = fs::system_complete( fs::path( vm["output-dir"].as<string>() ) );
363  }
364 
365  if ( vm.count("debug-level") ) {
366  Gaudi::PluginService::SetDebug( vm["debug-level"].as<int>() );
367  }
368 
369  if ( vm.count("load-library") ) {
370  Strings_t lLib_list = vm["load-library"].as< Strings_t >();
371  for (Strings_t::const_iterator lLib=lLib_list.begin();
372  lLib != lLib_list.end();
373  ++lLib) {
374  // load done through Gaudi helper class
375  System::ImageHandle tmp; // we ignore the library handle
376  unsigned long err = System::loadDynamicLib(*lLib, &tmp);
377  if (err != 1) {
378  cout << "WARNING: failed to load: "<< *lLib << endl;
379  }
380  }
381  }
382 
383 
384  if ( !fs::exists( out ) ) {
385  try {
386  fs::create_directory(out);
387  }
388  catch ( fs::filesystem_error &err ) {
389  cout << "ERROR: error creating directory: "<< err.what() << endl;
390  return EXIT_FAILURE;
391  }
392  }
393 
394  cout << ":::::: libraries : [ ";
395  copy( libs.begin(), libs.end(), ostream_iterator<string>(cout, " ") );
396  cout << "] ::::::" << endl;
397 
398  configGenerator py( pkgName, out.string() );
399  py.setConfigurableModule (vm["configurable-module"].as<string>());
400  py.setConfigurableDefaultName(vm["configurable-default-name"].as<string>());
401  py.setConfigurableAlgorithm (vm["configurable-algorithm"].as<string>());
402  py.setConfigurableAlgTool (vm["configurable-algtool"].as<string>());
403  py.setConfigurableAuditor (vm["configurable-auditor"].as<string>());
404  py.setConfigurableService (vm["configurable-service"].as<string>());
405 
406  int sc = EXIT_FAILURE;
407  try {
408  sc = py.genConfig( libs, userModule );
409  }
410  catch ( exception& e ) {
411  cout << "ERROR: Could not generate Configurable(s) !\n"
412  << "ERROR: Got exception: " << e.what() << endl;
413  return EXIT_FAILURE;
414  }
415 
416  if ( EXIT_SUCCESS == sc && ! vm.count("no-init")) {
417  // create an empty __init__.py file in the output dir
418  fstream initPy( ( out / fs::path( "__init__.py" ) ).string().c_str(),
419  std::ios_base::out|std::ios_base::trunc );
420  initPy << "## Hook for " << pkgName << " genConf module\n" << flush;
421  }
422 
423  cout << ":::::: libraries : [ ";
424  copy( libs.begin(), libs.end(), ostream_iterator<string>(cout, " ") );
425  cout << "] :::::: [DONE]" << endl;
426 
427  return sc;
428 }
429 
430 //-----------------------------------------------------------------------------
431 int configGenerator::genConfig( const Strings_t& libs, const string& userModule )
432 //-----------------------------------------------------------------------------
433 {
434  //--- Disable checking StatusCode -------------------------------------------
436 
437  const Strings_t::const_iterator endLib = libs.end();
438 
439  const std::string gaudiSvc = "GaudiCoreSvc";
440  const bool isGaudiSvc = ( std::find( libs.begin(), endLib, gaudiSvc ) != endLib );
441 
442  //--- Instantiate ApplicationMgr --------------------------------------------
443  if ( !isGaudiSvc && createAppMgr() ) {
444  cout << "ERROR: ApplicationMgr can not be created. Check environment" << endl;
445  return EXIT_FAILURE;
446  }
447 
448  //--- Iterate over component factories --------------------------------------
449  using Gaudi::PluginService::Details::Registry;
450  Registry& registry = Registry::instance();
451 
452  std::set<std::string> bkgNames = registry.loadedFactories();
453 
454  ISvcLocator* svcLoc = Gaudi::svcLocator();
455  IInterface* dummySvc = new Service( "DummySvc", svcLoc );
456  dummySvc->addRef();
457 
458  bool allGood = true;
459 
460  // iterate over all the requested libraries
461  for ( Strings_t::const_iterator iLib=libs.begin(); iLib != endLib; ++iLib ) {
462 
463  std::cout << ":::: processing library: " << *iLib << "..." << std::endl;
464 
465  // reset state
466  m_importGaudiHandles = false;
467  m_pyBuf.str("");
468  m_dbBuf.str("");
469 
470  //--- Load component library ----------------------------------------------
471  System::ImageHandle handle;
472  unsigned long err = System::loadDynamicLib( *iLib, &handle );
473  if ( err != 1 ) {
474  cout << "ERROR: " << System::getLastErrorString() << endl;
475  allGood = false;
476  continue;
477  }
478 
479  std::set<std::string> factories = registry.loadedFactories();
480 
481  for ( std::set<std::string>::iterator it = factories.begin();
482  it != factories.end(); ++it ) {
483  const string ident = *it;
484  if ( bkgNames.find(ident) != bkgNames.end() ) {
486  cout << "\t==> skipping [" << ident << "]..." << endl;
487  }
488  continue;
489  }
490 
491  const Registry::FactoryInfo info = registry.getInfo(*it);
492  const string rtype = info.rtype;
493 
494  // do not generate configurables for the Reflex-compatible aliases
495  if (info.properties.find("ReflexName") != info.properties.end())
496  continue;
497 
498  // Atlas contributed code (patch #1247)
499  // Skip the generation of configurables if the component does not come
500  // from the same library we are processing (i.e. we found a symbol that
501  // is coming from a library loaded by the linker).
502  if ( !DsoUtils::inDso( info.ptr, DsoUtils::libNativeName(*iLib) ) ) {
503  cout << "WARNING: library [" << *iLib << "] exposes factory ["
504  << ident << "] which is declared in ["
505  << DsoUtils::dsoName(info.ptr) << "] !!" << endl;
506  continue;
507  }
508 
509  string type;
510  bool known = true;
511  if ( ident == "ApplicationMgr" ) type = "ApplicationMgr";
512  else if ( rtype == typeid(IInterface*).name() ) type = "IInterface";
513  else if ( rtype == typeid(IAlgorithm*).name() ) type = "Algorithm";
514  else if ( rtype == typeid(IService* ).name() ) type = "Service";
515  else if ( rtype == typeid(IAlgTool* ).name() ) type = "AlgTool";
516  else if ( rtype == typeid(IAuditor* ).name() ) type = "Auditor";
517  else if ( rtype == typeid(IConverter*).name() ) type = "Converter";
518  else if ( rtype == typeid(DataObject*).name() ) type = "DataObject";
519  else type = "Unknown", known = false;
520  string name = ident;
521  // handle possible problems with templated components
522  boost::trim(name);
523 
524  if ( type == "IInterface" ) {
527  continue;
528  }
529 
530  if ( type == "Converter" || type == "DataObject" ) {
532  continue;
533  }
534 
535  if ( !known ) {
536  cout << "WARNING: Unknown (return) type [" << rtype << "] !!\n"
537  << "WARNING: component [" << ident << "] is skipped !"
538  << endl;
539  allGood = false;
540  continue;
541  }
542 
543  cout << " - component: " << info.className << " (";
544  if (info.className != name)
545  cout << name << ": ";
546  cout << type << ")" << endl;
547 
548  string cname = "DefaultName";
549  SmartIF<IProperty> prop;
550  try {
551  if ( type == "Algorithm" ) {
552  prop = SmartIF<IAlgorithm>(Algorithm::Factory::create(ident, cname, svcLoc));
553  }
554  else if ( type == "Service") {
555  prop = SmartIF<IService>(Service::Factory::create(ident, cname, svcLoc));
556  }
557  else if ( type == "AlgTool") {
558  prop = SmartIF<IAlgTool>(AlgTool::Factory::create(ident, cname, type, dummySvc));
559  // FIXME: AlgTool base class increase artificially by 1 the refcount.
560  prop->release();
561  }
562  else if ( type == "Auditor") {
563  prop = SmartIF<IAuditor>(Auditor::Factory::create(ident, cname, svcLoc));
564  }
565  else if ( type == "ApplicationMgr") {
566  prop = SmartIF<ISvcLocator>(svcLoc);
567  }
568  else {
569  continue; // unknown
570  }
571  }
572  catch ( exception& e ) {
573  cout << "ERROR: Error instantiating " << name
574  << " from " << *iLib << endl;
575  cout << "ERROR: Got exception: " << e.what() << endl;
576  allGood = false;
577  continue;
578  }
579  catch ( ... ) {
580  cout << "ERROR: Error instantiating " << name
581  << " from " << *iLib << endl;
582  allGood = false;
583  continue;
584  }
585  if( prop ) {
586  if (genComponent( *iLib, name, type, prop->getProperties() )) {
587  allGood = false;
588  }
589  prop.reset();
590  } else {
591  cout << "ERROR: could not cast IInterface* object to an IProperty* !\n"
592  << "ERROR: return type from PluginSvc is [" << rtype << "]...\n"
593  << "ERROR: NO Configurable will be generated for ["
594  << name << "] !"
595  << endl;
596  allGood = false;
597  }
598  } //> end loop over factories
599 
603  const std::string pyName = ( fs::path(m_outputDirName) /
604  fs::path(*iLib+"Conf.py") ).string();
605  const std::string dbName = ( fs::path(m_outputDirName) /
606  fs::path(*iLib+"_confDb.py") ).string();
607 
608  std::fstream py( pyName.c_str(),
609  std::ios_base::out|std::ios_base::trunc );
610  std::fstream db( dbName.c_str(),
611  std::ios_base::out|std::ios_base::trunc );
612 
613  genHeader ( py, db );
614  if (!userModule.empty())
615  py << "from " << userModule << " import *" <<endl;
616  genBody ( py, db );
617  genTrailer( py, db );
618 
619  } //> end loop over libraries
620 
621  dummySvc->release();
622  dummySvc = 0;
623 
624  return allGood ? EXIT_SUCCESS : EXIT_FAILURE;
625 }
626 
627 void configGenerator::genImport( std::ostream& s,
628  const boost::format& frmt,
629  std::string indent = ""){
630 
631  std::string::size_type pos = 0, nxtpos = 0;
632  std::string mod;
633 
634  while ( std::string::npos != pos ){
635  // find end of module name
636  nxtpos = m_configurable["Module"].find_first_of(',',pos);
637 
638  // Prepare import string
639  mod = m_configurable["Module"].substr(pos,nxtpos-pos);
640  std::ostringstream import;
641  import << boost::format(frmt) % mod;
642 
643  // append a normal import or a try/except enclosed one depending
644  // on availability of a fall-back module (next in the list)
645  if ( std::string::npos == nxtpos ) {
646  // last possible module
647  s << indent << import.str() << "\n" << flush;
648  pos = std::string::npos;
649  } else {
650  // we have a fallback for this
651  s << indent << "try:\n"
652  << indent << py_tab << import.str() << "\n"
653  << indent << "except ImportError:\n"
654  << flush;
655  pos = nxtpos+1;
656  }
657  // increase indentation level for next iteration
658  indent += py_tab;
659  }
660 }
661 
662 //-----------------------------------------------------------------------------
663 void configGenerator::genHeader( std::ostream& py,
664  std::ostream& db )
665 //-----------------------------------------------------------------------------
666 {
667  // python file part
668  std::string now = Gaudi::Time::current().format(true);
669  py << "#" << now //<< "\n"
670  << "\"\"\"Automatically generated. DO NOT EDIT please\"\"\"\n";
671  if ( m_importGaudiHandles ) {
672  py << "from GaudiKernel.GaudiHandles import *\n";
673  }
674 
675  genImport(py,boost::format("from %1%.Configurable import *"));
676 
677  // db file part
678  db << "## -*- python -*- \n"
679  << "# db file automatically generated by genconf on: "
680  << now; // << "\n";
681  db << "## insulates outside world against anything bad that could happen\n"
682  << "## also prevents global scope pollution\n"
683  << "def _fillCfgDb():\n";
684  genImport(db,boost::format("from %1%.ConfigurableDb import CfgDb"),py_tab);
685 
686  db << "\n"
687  << py_tab << "# get a handle on the repository of Configurables\n"
688  << py_tab << "cfgDb = CfgDb()\n"
689  << "\n"
690  << py_tab << "# populate the repository with informations on Configurables \n"
691  << "\n"
692  << flush;
693 }
694 //-----------------------------------------------------------------------------
695 void configGenerator::genTrailer( std::ostream& /*py*/,
696  std::ostream& db )
697 //-----------------------------------------------------------------------------
698 {
699  // db file part
700  db << py_tab << "return #_fillCfgDb\n"
701  << "# fill cfgDb at module import...\n"
702  << "try:\n"
703  << py_tab << "_fillCfgDb()\n"
704  << py_tab << "#house cleaning...\n"
705  << py_tab << "del _fillCfgDb\n"
706  << "except Exception,err:\n"
707  << py_tab << "print \"Py:ConfigurableDb ERROR Problem with [%s] content!"
708  << "\" % __name__\n"
709  << py_tab << "print \"Py:ConfigurableDb ERROR\",err\n"
710  << py_tab << "print \"Py:ConfigurableDb ERROR ==> culprit is package ["
711  << m_pkgName << "] !\"\n"
712  << std::flush;
713 }
714 
715 //-----------------------------------------------------------------------------
716 int
717 configGenerator::genComponent( const std::string& libName,
718  const std::string& componentName,
719  const std::string& componentType,
720  const vector<Property*>& properties )
721 //-----------------------------------------------------------------------------
722 {
723  string cname = componentName;
724  pythonizeName(cname);
725 
726  typedef GaudiUtils::HashMap<std::string, std::string> PropertyDoc_t;
727  PropertyDoc_t propDoc;
728 
729  m_pyBuf << "\n";
730  m_pyBuf << "class " << cname
731  << "( " << m_configurable[componentType] << " ) :"
732  << "\n";
733  m_pyBuf << " __slots__ = { \n";
734  for ( vector<Property*>::const_iterator it = properties.begin() ;
735  it != properties.end(); ++it ) {
736 
737  const string pname = (*it)->name();
738  // Validate property name (it must be a valid Python identifier)
739  if (!boost::regex_match(pname, pythonIdentifier)) {
740  std::cout << "ERROR: invalid property name \"" << pname
741  << "\" in component " << cname
742  << " (invalid Python identifier)" << std::endl;
743  // try to make the buffer at least more or less valid python code.
744  m_pyBuf << " #ERROR-invalid identifier '" << pname << "'\n"
745  << " }\n";
746  return 1;
747  }
748 
749  string pvalue, ptype;
750  pythonizeValue( (*it), pvalue, ptype );
751  m_pyBuf << " '" << pname << "' : " << pvalue <<", # " << ptype << "\n";
752 
753  if ( (*it)->documentation() != "none" ) {
754  propDoc[pname] = (*it)->documentation();
755  }
756 
757  }
758  m_pyBuf << " }\n";
759  m_pyBuf << " _propertyDocDct = { \n";
760  for ( PropertyDoc_t::const_iterator iProp = propDoc.begin();
761  iProp != propDoc.end();
762  ++iProp ) {
763  m_pyBuf << std::setw(5)
764  << "'" << iProp->first << "' : "
765  << "\"\"\" " << iProp->second << " \"\"\",\n";
766  }
767  m_pyBuf << " }\n";
768 
769  m_pyBuf
770  << " def __init__(self, name = " << m_configurable["DefaultName"]
771  << ", **kwargs):\n"
772  << " super(" << cname << ", self).__init__(name)\n"
773  << " for n,v in kwargs.items():\n"
774  << " setattr(self, n, v)\n"
775  << " def getDlls( self ):\n"
776  << " return '" << libName << "'\n"
777  << " def getType( self ):\n"
778  << " return '" << componentName << "'\n"
779  << " pass # class " << cname << "\n"
780  << flush;
781 
782  // name of the auto-generated module
783  const string pyName = ( fs::path(m_outputDirName) /
784  fs::path(libName+"Conf.py") ).string();
785  const string modName = fs::basename( fs::path( pyName ).leaf() );
786 
787  // now the db part
788  m_dbBuf
789  << py_tab << "cfgDb.add( configurable = '" << cname << "',\n"
790  << py_tab << " package = '" << m_pkgName << "',\n"
791  << py_tab << " module = '" << m_pkgName << "." << modName << "',\n"
792  << py_tab << " lib = '" << libName << "' )\n"
793  << flush;
794 
795  return 0;
796 }
797 //-----------------------------------------------------------------------------
798 void configGenerator::pythonizeName( string& name )
799 //-----------------------------------------------------------------------------
800 {
801  static string in("<>&*,: ().");
802  static string out("__rp__s___");
803  boost::algorithm::replace_all(name, ", ", ",");
804  for ( string::iterator i = name.begin(); i != name.end(); ++i ) {
805  if ( in.find(*i) != string::npos ) *i = out[in.find(*i)];
806  }
807 }
808 
809 //-----------------------------------------------------------------------------
811  string& pvalue, string& ptype )
812 //-----------------------------------------------------------------------------
813 {
814  const std::string cvalue = p->toString();
815  const type_info& ti = *p->type_info();
816  if ( ti == typeid(bool) ) {
817  pvalue = ( cvalue == "0" || cvalue == "False" || cvalue == "false" )
818  ? "False"
819  : "True";
820  ptype = "bool";
821  }
822  else if ( ti == typeid(char) || ti == typeid(signed char)
823  || ti == typeid(unsigned char) ||
824  ti == typeid(short) || ti == typeid(unsigned short) ||
825  ti == typeid(int) || ti == typeid(unsigned int) ||
826  ti == typeid(long) || ti == typeid(unsigned long) ) {
827  pvalue = cvalue;
828  ptype = "int";
829  }
830  else if ( ti == typeid(long long) || ti == typeid(unsigned long long) ) {
831  pvalue = cvalue + "L";
832  ptype = "long";
833  }
834  else if ( ti == typeid(float) || ti == typeid(double) ) {
835  // forces python to handle this as a float: put a dot in there...
836  pvalue = boost::to_lower_copy(cvalue);
837  if ( pvalue == "nan" ) {
838  pvalue = "float('nan')";
839  std::cout << "WARNING: default value for ["
840  << p->name() << "] is NaN !!"
841  << std::endl;
842  } else if ( std::string::npos == pvalue.find(".") &&
843  std::string::npos == pvalue.find("e") ) {
844  pvalue = cvalue + ".0";
845  }
846  ptype = "float";
847  }
848  else if ( ti == typeid(string) ) {
849 
850  pvalue = "'"+cvalue+"'";
851  ptype = "str";
852  }
853  else if ( ti == typeid(GaudiHandleBase) ) {
854  m_importGaudiHandles = true;
855  const GaudiHandleProperty& hdl
856  = dynamic_cast<const GaudiHandleProperty&>(*p);
857  const GaudiHandleBase& base = hdl.value();
858 
859  pvalue = base.pythonRepr();
860  ptype = "GaudiHandle";
861  }
862  else if ( ti == typeid(GaudiHandleArrayBase) ) {
863  m_importGaudiHandles = true;
864  const GaudiHandleArrayProperty& hdl
865  = dynamic_cast<const GaudiHandleArrayProperty&>(*p);
866  const GaudiHandleArrayBase& base = hdl.value();
867 
868  pvalue = base.pythonRepr();
869  ptype = "GaudiHandleArray";
870  }
871  else {
872  std::ostringstream v_str;
873  v_str.setf(std::ios::fixed); // to correctly display floats
874  p->toStream(v_str);
875  pvalue = v_str.str();
876  ptype = "list";
877  }
878 }
879 
880 //-----------------------------------------------------------------------------
882 //-----------------------------------------------------------------------------
883 {
885  SmartIF<IProperty> propMgr ( iface );
886  SmartIF<IAppMgrUI> appUI ( iface );
887 
888  if ( propMgr.isValid() && appUI.isValid() ) {
889  propMgr->setProperty( "JobOptionsType", "NONE" ); // No job options
890  propMgr->setProperty( "AppName", ""); // No initial printout message
891  propMgr->setProperty( "OutputLevel", "7"); // No other printout messages
892  appUI->configure();
893  return EXIT_SUCCESS;
894  }
895  else {
896  return EXIT_FAILURE;
897  }
898 }

Generated at Wed Jun 4 2014 14:48:57 for Gaudi Framework, version v25r2 by Doxygen version 1.8.2 written by Dimitri van Heesch, © 1997-2004