11 __author__ = 
"Sebastien Binet" 
   13 helper script to generate header and cxx files of various athena 
   14 components (svc/tool/alg/isvc/itool/object) 
   18 from datetime 
import datetime
 
   20 import PyUtils.acmdlib 
as acmdlib
 
   26               'alg', 
'reentrant_alg',
 
   28               'pyalg', 
'pysvc', 
'pytool', 
'pyaud' 
   30     needing_iface = (
'svc', 
'tool')
 
   34     copyright_template = 
"Copyright (C) 2002-%d CERN for the benefit of the ATLAS collaboration" % datetime.now().year
 
   35     isvc_hdr_template = 
"""\ 
   42 #include "GaudiKernel/IService.h" 
   48 class %(klass)s : virtual public IService { 
   50   DeclareInterfaceID(%(klass)s, 1, 0); 
   52   virtual ~%(klass)s() override {} 
   58     isvc_cxx_template = 
"" 
   60     itool_hdr_template = 
"""\ 
   67 #include "GaudiKernel/IAlgTool.h" 
   73 class %(klass)s : virtual public IAlgTool { 
   75   DeclareInterfaceID(%(klass)s, 1, 0); 
   77   virtual ~%(klass)s() override {} 
   83     itool_cxx_template = 
"" 
   85     object_hdr_template = 
"""\ 
  101   %(klass)s(const %(klass)s& rhs); 
  102   %(klass)s& operator=(const %(klass)s& rhs); 
  103   virtual ~%(klass)s(); 
  106 //std::ostream& operator<<(std::ostream& out, const %(klass)s& o); 
  111     object_cxx_template = 
"""\ 
  116 #include "%(pkg)s/%(klass)s.h" 
  121     svc_hdr_template = 
"""\ 
  129 #include "%(ipkg)s/%(iklass)s.h" 
  131 // Framework includes 
  132 #include "AthenaBaseComps/AthService.h" 
  141 class %(klass)s : public extends<AthService, %(iklass)s> { 
  143   %(klass)s(const std::string& name, ISvcLocator* pSvcLocator); 
  144   virtual ~%(klass)s() override; 
  146   virtual StatusCode initialize() override; 
  147   virtual StatusCode finalize() override; 
  150   //Gaudi::Property<int> m_myInt{this, "MyInt", 0, "An Integer"}; 
  156     svc_cxx_template = 
"""\ 
  160 #include "%(klass)s.h" 
  162 %(klass)s::%(klass)s(const std::string& name, ISvcLocator* pSvcLocator) : 
  163   base_class(name, pSvcLocator) 
  167 %(klass)s::~%(klass)s() 
  171 StatusCode %(klass)s::initialize() 
  173   //ATH_MSG_DEBUG("Use macros for logging!"); 
  174   return StatusCode::SUCCESS; 
  177 StatusCode %(klass)s::finalize() 
  179   return StatusCode::SUCCESS; 
  184     alg_hdr_template = 
"""\ 
  191 #include "AthenaBaseComps/AthAlgorithm.h" 
  199 class %(klass)s : public AthAlgorithm { 
  201   %(klass)s(const std::string& name, ISvcLocator* pSvcLocator); 
  202   virtual ~%(klass)s() override; 
  204   virtual StatusCode initialize() override; 
  205   virtual StatusCode execute() override; 
  206   virtual StatusCode finalize() override; 
  209   //Gaudi::Property<int> m_myInt{this, "MyInt", 0, "An Integer"}; 
  215     alg_cxx_template = 
"""\ 
  220 #include "%(klass)s.h" 
  222 %(klass)s::%(klass)s(const std::string& name, ISvcLocator* pSvcLocator) : 
  223   AthAlgorithm(name, pSvcLocator) 
  227 %(klass)s::~%(klass)s() 
  231 StatusCode %(klass)s::initialize() 
  233   //ATH_MSG_DEBUG("Use macros for logging!"); 
  234   return StatusCode::SUCCESS; 
  237 StatusCode %(klass)s::finalize() 
  239   return StatusCode::SUCCESS; 
  242 StatusCode %(klass)s::execute() 
  244   return StatusCode::SUCCESS; 
  249     reentrant_alg_hdr_template = 
"""\ 
  256 // Framework includes 
  257 #include "AthenaBaseComps/AthReentrantAlgorithm.h" 
  266 class %(klass)s : public AthReentrantAlgorithm { 
  268   %(klass)s(const std::string& name, ISvcLocator* pSvcLocator); 
  269   virtual ~%(klass)s() override; 
  271   virtual StatusCode initialize() override; 
  272   virtual StatusCode execute(const EventContext& context) const override; 
  273   virtual StatusCode finalize() override; 
  276   //Gaudi::Property<int> m_myInt{this, "MyInt", 0, "An Integer"}; 
  282     reentrant_alg_cxx_template = 
"""\ 
  287 #include "%(klass)s.h" 
  289 %(klass)s::%(klass)s(const std::string& name, ISvcLocator* pSvcLocator) : 
  290   AthReentrantAlgorithm(name, pSvcLocator) 
  294 %(klass)s::~%(klass)s() 
  298 StatusCode %(klass)s::initialize() 
  300   //ATH_MSG_DEBUG("Use macros for logging!"); 
  301   return StatusCode::SUCCESS; 
  304 StatusCode %(klass)s::finalize() 
  306   return StatusCode::SUCCESS; 
  309 StatusCode %(klass)s::execute(const EventContext& context) const 
  311   return StatusCode::SUCCESS; 
  316     tool_hdr_template = 
"""\ 
  324 #include "%(ipkg)s/%(iklass)s.h" 
  326 // Framework includes 
  327 #include "AthenaBaseComps/AthAlgTool.h" 
  336 class %(klass)s : public extends<AthAlgTool, %(iklass)s> { 
  338   %(klass)s(const std::string& type, const std::string& name, const IInterface* parent); 
  339   virtual ~%(klass)s() override; 
  341   virtual StatusCode initialize() override; 
  342   virtual StatusCode finalize() override; 
  345   //Gaudi::Property<int> m_myInt{this, "MyInt", 0, "An Integer"}; 
  351     tool_cxx_template = 
"""\ 
  356 #include "%(klass)s.h" 
  358 %(klass)s::%(klass)s(const std::string& type, const std::string& name, const IInterface* parent) : 
  359   base_class(type, name, parent) 
  363 %(klass)s::~%(klass)s() 
  367 StatusCode %(klass)s::initialize() 
  369   //ATH_MSG_DEBUG("Use macros for logging!"); 
  370   return StatusCode::SUCCESS; 
  373 StatusCode %(klass)s::finalize() 
  375   return StatusCode::SUCCESS; 
  380     pyalg_template = 
"""\ 
  385 # @file:    %(pkg)s/python/%(fname)s 
  386 # @purpose: <put some purpose here> 
  387 # @author:  Sebastien Binet <binet@cern.ch> 
  389 __doc__     = 'some documentation here' 
  390 __author__  = 'Sebastien Binet <binet@cern.ch>' 
  392 import AthenaCommon.SystemOfUnits as Units 
  393 import AthenaPython.PyAthena as PyAthena 
  394 from AthenaPython.PyAthena import StatusCode 
  396 class %(klass)s (PyAthena.Alg): 
  397     'put some documentation here' 
  398     def __init__(self, name='%(klass)s', **kw): 
  401         super(%(klass)s, self).__init__(**kw) 
  403         ## properties and data members 
  404         #self.foo = kw.get('foo', 10) # default value 
  407     def initialize(self): 
  408         self.msg.info('==> initialize...') 
  409         return StatusCode.Success 
  412         return StatusCode.Success 
  415         self.msg.info('==> finalize...') 
  416         return StatusCode.Success 
  421     pysvc_template = 
"""\ 
  426 # @file:    %(pkg)s/python/%(fname)s 
  427 # @purpose: <put some purpose here> 
  428 # @author:  Sebastien Binet <binet@cern.ch> 
  430 __doc__     = 'some documentation here' 
  431 __author__  = 'Sebastien Binet <binet@cern.ch>' 
  433 import AthenaCommon.SystemOfUnits as Units 
  434 import AthenaPython.PyAthena as PyAthena 
  435 from AthenaPython.PyAthena import StatusCode 
  437 class %(klass)s (PyAthena.Svc): 
  438     'put some documentation here' 
  439     def __init__(self, name='%(klass)s', **kw): 
  442         super(%(klass)s, self).__init__(**kw) 
  444         ## properties and data members 
  445         #self.foo = kw.get('foo', 10) # default value 
  448     def initialize(self): 
  449         self.msg.info('==> initialize...') 
  450         return StatusCode.Success 
  453         self.msg.info('==> finalize...') 
  454         return StatusCode.Success 
  459     pytool_template = 
"""\ 
  464 # @file:    %(pkg)s/python/%(fname)s 
  465 # @purpose: <put some purpose here> 
  466 # @author:  Sebastien Binet <binet@cern.ch> 
  468 __doc__     = 'some documentation here' 
  469 __author__  = 'Sebastien Binet <binet@cern.ch>' 
  471 import AthenaCommon.SystemOfUnits as Units 
  472 import AthenaPython.PyAthena as PyAthena 
  473 from AthenaPython.PyAthena import StatusCode 
  475 class %(klass)s (PyAthena.AlgTool): 
  476     'put some documentation here' 
  477     def __init__(self, name='%(klass)s', **kw): 
  480         super(%(klass)s, self).__init__(**kw) 
  482         ## properties and data members 
  483         #self.foo = kw.get('foo', 10) # default value 
  486     def initialize(self): 
  487         self.msg.info('==> initialize...') 
  488         return StatusCode.Success 
  491         self.msg.info('==> finalize...') 
  492         return StatusCode.Success 
  497     pyaud_template = 
"""\ 
  502 # @file:    %(pkg)s/python/%(fname)s 
  503 # @purpose: <put some purpose here> 
  504 # @author:  Sebastien Binet <binet@cern.ch> 
  506 __doc__     = 'some documentation here' 
  507 __author__  = 'Sebastien Binet <binet@cern.ch>' 
  509 import AthenaCommon.SystemOfUnits as Units 
  510 import AthenaPython.PyAthena as PyAthena 
  511 from AthenaPython.PyAthena import StatusCode 
  513 class %(klass)s (PyAthena.Aud): 
  514     'put some documentation here' 
  515     def __init__(self, name='%(klass)s', **kw): 
  518         super(%(klass)s, self).__init__(**kw) 
  520         ## properties and data members 
  521         #self.foo = kw.get('foo', 10) # default value 
  524     def initialize(self): 
  525         self.msg.info('==> initialize...') 
  526         return StatusCode.Success 
  529         self.msg.info('==> finalize...') 
  530         return StatusCode.Success 
  535 def gen_files(pkg="", klass="", klass_type='object', fname='foo',
 
  537     """Simple helper function to generate files based off some informations 
  538      @param pkg the name of the package holding the class we want to generate 
  539      @param klass the (fully qualified) name of the C++ class to generate 
  540      @param klass_type the type of class to generate (svc/tool/alg/object) 
  541      @param fname the filename to generate 
  542      @param ipkg the name of the package holding the interface of the class 
  543      @param iklass the name of the interface of the class we generate 
  546         hdr = getattr(Templates, 
'%s_hdr_template'%klass_type)
 
  547         cxx = getattr(Templates, 
'%s_cxx_template'%klass_type)
 
  548     except AttributeError 
as err:
 
  549         print (
"::: UNKNOWN klass_type [%s] !" % klass_type)
 
  552     namespace_klass = klass.replace(
'::',
'_')
 
  553     namespace_begin,namespace_end = 
"",
"" 
  554     if klass.count(
"::")>0:
 
  555         nm    = klass.split(
"::")[0]
 
  556         klass = klass.split(
"::")[1]
 
  557         namespace_begin = os.linesep + 
"namespace %s {" % nm + os.linesep
 
  558         namespace_end   = os.linesep + 
"} // namespace %s" % nm + os.linesep
 
  561     guard = 
"%s_%s_H" % (pkg.upper(), namespace_klass.upper())
 
  568               namespace_begin=namespace_begin,
 
  569               namespace_end=namespace_end,
 
  570               copyright=Templates.copyright_template
 
  572     fname = os.path.splitext(fname)[0]
 
  574     o_hdr = 
open(fname+
'.h', 
'w')
 
  575     o_hdr.writelines(hdr%d)
 
  580         o_cxx = 
open(fname+
'.cxx', 
'w')
 
  581         o_cxx.writelines(cxx%d)
 
  588 def gen_pyfiles(pkg="", klass="", klass_type='pyalg', fname='foo'):
 
  589     """Simple helper function to generate (python) files based off some 
  591      @param pkg the name of the package holding the class we want to generate 
  592      @param klass the name of the python class to generate 
  593      @param klass_type the type of class to generate (pysvc/pytool/pyalg/pyaud) 
  594      @param fname the filename to generate 
  597         py_template = getattr(Templates, 
'%s_template'%klass_type)
 
  598     except AttributeError 
as err:
 
  599         print (
"::: UNKNOWN klass_type [%s] !" % klass_type)
 
  602     invalid_py_chars = ( 
':', 
'.', 
'>', 
'<', 
' ' )
 
  604     if any([c 
for c 
in invalid_py_chars 
if c 
in klass]):
 
  605         err = 
"::: INVALID class name ! (%s) !\n"%klass
 
  606         err += 
"::: python class names can *NOT* contain any character of %s"%\
 
  607                repr(invalid_py_chars)
 
  609         raise RuntimeError(err)
 
  611     fname=
''.
join([fname,
'.py'])
 
  615               copyright=Templates.copyright_template
 
  618     o.writelines(py_template%d)
 
  623 @acmdlib.command(name=
'gen-klass')
 
  627     help = 
"The (fully qualified) name of the python or C++ class to create (ex: ElectronContainer, Analysis::Electron, MyAlgTool, PyTestAlg)")
 
  631     help = 
"The name of the package holding the C++ class to create (ex: MyAnalysis, JetEvent)")
 
  636     choices = GenTypes.values,
 
  637     help = 
"The type of class to create")
 
  641     help = 
"The name of the package holding the interface of the C++ class (mandatory for 'svc' and 'tool' types)")
 
  645     help = 
"The name of the interface the C++ class is implementing (mandatory for 'svc' and 'tool' types)")
 
  651     help = 
"The name of the file(s) which will hold header and implementation of the class (ex: 'Foo' --> ('Foo.h','Foo.cxx'))")
 
  653     """helper script to generate header and cxx files 
  654     of various athena components (svc/tool/alg/isvc/itool/object) 
  659     if args.klass_type 
in GenTypes.needing_iface 
and \
 
  660        ( args.ipkg 
is None or args.iklass 
is None ) :
 
  661         print (
":: You have to give 'ipkg' and 'iklass' options to properly ",)
 
  662         print (
"generate an implementation for '%s'"%args.klass_type)
 
  666     if args.ipkg 
is None:
 
  669     if args.iklass 
is None:
 
  672     if args.klass_type.startswith(
'py'):
 
  675             klass_type=args.klass_type,
 
  682             klass_type=args.klass_type,