11 from __future__
import print_function
13 __author__ =
"Sebastien Binet"
15 helper script to generate header and cxx files of various athena
16 components (svc/tool/alg/isvc/itool/object)
20 from datetime
import datetime
22 import PyUtils.acmdlib
as acmdlib
28 'alg',
'reentrant_alg',
30 'pyalg',
'pysvc',
'pytool',
'pyaud'
32 needing_iface = (
'svc',
'tool')
36 copyright_template =
"Copyright (C) 2002-%d CERN for the benefit of the ATLAS collaboration" % datetime.now().year
37 isvc_hdr_template =
"""\
44 #include "GaudiKernel/IService.h"
50 class %(klass)s : virtual public IService {
52 DeclareInterfaceID(%(klass)s, 1, 0);
54 virtual ~%(klass)s() override {}
60 isvc_cxx_template =
""
62 itool_hdr_template =
"""\
69 #include "GaudiKernel/IAlgTool.h"
75 class %(klass)s : virtual public IAlgTool {
77 DeclareInterfaceID(%(klass)s, 1, 0);
79 virtual ~%(klass)s() override {}
85 itool_cxx_template =
""
87 object_hdr_template =
"""\
103 %(klass)s(const %(klass)s& rhs);
104 %(klass)s& operator=(const %(klass)s& rhs);
105 virtual ~%(klass)s();
108 //std::ostream& operator<<(std::ostream& out, const %(klass)s& o);
113 object_cxx_template =
"""\
118 #include "%(pkg)s/%(klass)s.h"
123 svc_hdr_template =
"""\
131 #include "%(ipkg)s/%(iklass)s.h"
133 // Framework includes
134 #include "AthenaBaseComps/AthService.h"
143 class %(klass)s : public extends<AthService, %(iklass)s> {
145 %(klass)s(const std::string& name, ISvcLocator* pSvcLocator);
146 virtual ~%(klass)s() override;
148 virtual StatusCode initialize() override;
149 virtual StatusCode finalize() override;
152 //Gaudi::Property<int> m_myInt{this, "MyInt", 0, "An Integer"};
158 svc_cxx_template =
"""\
162 #include "%(klass)s.h"
164 %(klass)s::%(klass)s(const std::string& name, ISvcLocator* pSvcLocator) :
165 base_class(name, pSvcLocator)
169 %(klass)s::~%(klass)s()
173 StatusCode %(klass)s::initialize()
175 //ATH_MSG_DEBUG("Use macros for logging!");
176 return StatusCode::SUCCESS;
179 StatusCode %(klass)s::finalize()
181 return StatusCode::SUCCESS;
186 alg_hdr_template =
"""\
193 #include "AthenaBaseComps/AthAlgorithm.h"
201 class %(klass)s : public AthAlgorithm {
203 %(klass)s(const std::string& name, ISvcLocator* pSvcLocator);
204 virtual ~%(klass)s() override;
206 virtual StatusCode initialize() override;
207 virtual StatusCode execute() override;
208 virtual StatusCode finalize() override;
211 //Gaudi::Property<int> m_myInt{this, "MyInt", 0, "An Integer"};
217 alg_cxx_template =
"""\
222 #include "%(klass)s.h"
224 %(klass)s::%(klass)s(const std::string& name, ISvcLocator* pSvcLocator) :
225 AthAlgorithm(name, pSvcLocator)
229 %(klass)s::~%(klass)s()
233 StatusCode %(klass)s::initialize()
235 //ATH_MSG_DEBUG("Use macros for logging!");
236 return StatusCode::SUCCESS;
239 StatusCode %(klass)s::finalize()
241 return StatusCode::SUCCESS;
244 StatusCode %(klass)s::execute()
246 return StatusCode::SUCCESS;
251 reentrant_alg_hdr_template =
"""\
258 // Framework includes
259 #include "AthenaBaseComps/AthReentrantAlgorithm.h"
268 class %(klass)s : public AthReentrantAlgorithm {
270 %(klass)s(const std::string& name, ISvcLocator* pSvcLocator);
271 virtual ~%(klass)s() override;
273 virtual StatusCode initialize() override;
274 virtual StatusCode execute(const EventContext& context) const override;
275 virtual StatusCode finalize() override;
278 //Gaudi::Property<int> m_myInt{this, "MyInt", 0, "An Integer"};
284 reentrant_alg_cxx_template =
"""\
289 #include "%(klass)s.h"
291 %(klass)s::%(klass)s(const std::string& name, ISvcLocator* pSvcLocator) :
292 AthReentrantAlgorithm(name, pSvcLocator)
296 %(klass)s::~%(klass)s()
300 StatusCode %(klass)s::initialize()
302 //ATH_MSG_DEBUG("Use macros for logging!");
303 return StatusCode::SUCCESS;
306 StatusCode %(klass)s::finalize()
308 return StatusCode::SUCCESS;
311 StatusCode %(klass)s::execute(const EventContext& context) const
313 return StatusCode::SUCCESS;
318 tool_hdr_template =
"""\
326 #include "%(ipkg)s/%(iklass)s.h"
328 // Framework includes
329 #include "AthenaBaseComps/AthAlgTool.h"
338 class %(klass)s : public extends<AthAlgTool, %(iklass)s> {
340 %(klass)s(const std::string& type, const std::string& name, const IInterface* parent);
341 virtual ~%(klass)s() override;
343 virtual StatusCode initialize() override;
344 virtual StatusCode finalize() override;
347 //Gaudi::Property<int> m_myInt{this, "MyInt", 0, "An Integer"};
353 tool_cxx_template =
"""\
358 #include "%(klass)s.h"
360 %(klass)s::%(klass)s(const std::string& type, const std::string& name, const IInterface* parent) :
361 base_class(type, name, parent)
365 %(klass)s::~%(klass)s()
369 StatusCode %(klass)s::initialize()
371 //ATH_MSG_DEBUG("Use macros for logging!");
372 return StatusCode::SUCCESS;
375 StatusCode %(klass)s::finalize()
377 return StatusCode::SUCCESS;
382 pyalg_template =
"""\
387 # @file: %(pkg)s/python/%(fname)s
388 # @purpose: <put some purpose here>
389 # @author: Sebastien Binet <binet@cern.ch>
391 __doc__ = 'some documentation here'
392 __author__ = 'Sebastien Binet <binet@cern.ch>'
394 import AthenaCommon.SystemOfUnits as Units
395 import AthenaPython.PyAthena as PyAthena
396 from AthenaPython.PyAthena import StatusCode
398 class %(klass)s (PyAthena.Alg):
399 'put some documentation here'
400 def __init__(self, name='%(klass)s', **kw):
403 super(%(klass)s, self).__init__(**kw)
405 ## properties and data members
406 #self.foo = kw.get('foo', 10) # default value
409 def initialize(self):
410 self.msg.info('==> initialize...')
411 return StatusCode.Success
414 return StatusCode.Success
417 self.msg.info('==> finalize...')
418 return StatusCode.Success
423 pysvc_template =
"""\
428 # @file: %(pkg)s/python/%(fname)s
429 # @purpose: <put some purpose here>
430 # @author: Sebastien Binet <binet@cern.ch>
432 __doc__ = 'some documentation here'
433 __author__ = 'Sebastien Binet <binet@cern.ch>'
435 import AthenaCommon.SystemOfUnits as Units
436 import AthenaPython.PyAthena as PyAthena
437 from AthenaPython.PyAthena import StatusCode
439 class %(klass)s (PyAthena.Svc):
440 'put some documentation here'
441 def __init__(self, name='%(klass)s', **kw):
444 super(%(klass)s, self).__init__(**kw)
446 ## properties and data members
447 #self.foo = kw.get('foo', 10) # default value
450 def initialize(self):
451 self.msg.info('==> initialize...')
452 return StatusCode.Success
455 self.msg.info('==> finalize...')
456 return StatusCode.Success
461 pytool_template =
"""\
466 # @file: %(pkg)s/python/%(fname)s
467 # @purpose: <put some purpose here>
468 # @author: Sebastien Binet <binet@cern.ch>
470 __doc__ = 'some documentation here'
471 __author__ = 'Sebastien Binet <binet@cern.ch>'
473 import AthenaCommon.SystemOfUnits as Units
474 import AthenaPython.PyAthena as PyAthena
475 from AthenaPython.PyAthena import StatusCode
477 class %(klass)s (PyAthena.AlgTool):
478 'put some documentation here'
479 def __init__(self, name='%(klass)s', **kw):
482 super(%(klass)s, self).__init__(**kw)
484 ## properties and data members
485 #self.foo = kw.get('foo', 10) # default value
488 def initialize(self):
489 self.msg.info('==> initialize...')
490 return StatusCode.Success
493 self.msg.info('==> finalize...')
494 return StatusCode.Success
499 pyaud_template =
"""\
504 # @file: %(pkg)s/python/%(fname)s
505 # @purpose: <put some purpose here>
506 # @author: Sebastien Binet <binet@cern.ch>
508 __doc__ = 'some documentation here'
509 __author__ = 'Sebastien Binet <binet@cern.ch>'
511 import AthenaCommon.SystemOfUnits as Units
512 import AthenaPython.PyAthena as PyAthena
513 from AthenaPython.PyAthena import StatusCode
515 class %(klass)s (PyAthena.Aud):
516 'put some documentation here'
517 def __init__(self, name='%(klass)s', **kw):
520 super(%(klass)s, self).__init__(**kw)
522 ## properties and data members
523 #self.foo = kw.get('foo', 10) # default value
526 def initialize(self):
527 self.msg.info('==> initialize...')
528 return StatusCode.Success
531 self.msg.info('==> finalize...')
532 return StatusCode.Success
537 def gen_files(pkg="", klass="", klass_type='object', fname='foo',
539 """Simple helper function to generate files based off some informations
540 @param pkg the name of the package holding the class we want to generate
541 @param klass the (fully qualified) name of the C++ class to generate
542 @param klass_type the type of class to generate (svc/tool/alg/object)
543 @param fname the filename to generate
544 @param ipkg the name of the package holding the interface of the class
545 @param iklass the name of the interface of the class we generate
548 hdr = getattr(Templates,
'%s_hdr_template'%klass_type)
549 cxx = getattr(Templates,
'%s_cxx_template'%klass_type)
550 except AttributeError
as err:
551 print (
"::: UNKNOWN klass_type [%s] !" % klass_type)
554 namespace_klass = klass.replace(
'::',
'_')
555 namespace_begin,namespace_end =
"",
""
556 if klass.count(
"::")>0:
557 nm = klass.split(
"::")[0]
558 klass = klass.split(
"::")[1]
559 namespace_begin = os.linesep +
"namespace %s {" % nm + os.linesep
560 namespace_end = os.linesep +
"} // namespace %s" % nm + os.linesep
563 guard =
"%s_%s_H" % (pkg.upper(), namespace_klass.upper())
570 namespace_begin=namespace_begin,
571 namespace_end=namespace_end,
572 copyright=Templates.copyright_template
574 fname = os.path.splitext(fname)[0]
576 o_hdr =
open(fname+
'.h',
'w')
577 o_hdr.writelines(hdr%d)
582 o_cxx =
open(fname+
'.cxx',
'w')
583 o_cxx.writelines(cxx%d)
590 def gen_pyfiles(pkg="", klass="", klass_type='pyalg', fname='foo'):
591 """Simple helper function to generate (python) files based off some
593 @param pkg the name of the package holding the class we want to generate
594 @param klass the name of the python class to generate
595 @param klass_type the type of class to generate (pysvc/pytool/pyalg/pyaud)
596 @param fname the filename to generate
599 py_template = getattr(Templates,
'%s_template'%klass_type)
600 except AttributeError
as err:
601 print (
"::: UNKNOWN klass_type [%s] !" % klass_type)
604 invalid_py_chars = (
':',
'.',
'>',
'<',
' ' )
606 if any([c
for c
in invalid_py_chars
if c
in klass]):
607 err =
"::: INVALID class name ! (%s) !\n"%klass
608 err +=
"::: python class names can *NOT* contain any character of %s"%\
609 repr(invalid_py_chars)
611 raise RuntimeError(err)
613 fname=
''.
join([fname,
'.py'])
617 copyright=Templates.copyright_template
620 o.writelines(py_template%d)
625 @acmdlib.command(name=
'gen-klass')
629 help =
"The (fully qualified) name of the python or C++ class to create (ex: ElectronContainer, Analysis::Electron, MyAlgTool, PyTestAlg)")
633 help =
"The name of the package holding the C++ class to create (ex: MyAnalysis, JetEvent)")
638 choices = GenTypes.values,
639 help =
"The type of class to create")
643 help =
"The name of the package holding the interface of the C++ class (mandatory for 'svc' and 'tool' types)")
647 help =
"The name of the interface the C++ class is implementing (mandatory for 'svc' and 'tool' types)")
653 help =
"The name of the file(s) which will hold header and implementation of the class (ex: 'Foo' --> ('Foo.h','Foo.cxx'))")
655 """helper script to generate header and cxx files
656 of various athena components (svc/tool/alg/isvc/itool/object)
661 if args.klass_type
in GenTypes.needing_iface
and \
662 ( args.ipkg
is None or args.iklass
is None ) :
663 print (
":: You have to give 'ipkg' and 'iklass' options to properly ",)
664 print (
"generate an implementation for '%s'"%args.klass_type)
668 if args.ipkg
is None:
671 if args.iklass
is None:
674 if args.klass_type.startswith(
'py'):
677 klass_type=args.klass_type,
684 klass_type=args.klass_type,