588def gen_pyfiles(pkg="", klass="", klass_type='pyalg', fname='foo'):
589 """Simple helper function to generate (python) files based off some
590 user informations.
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
595 """
596 try:
597 py_template = getattr(Templates, '%s_template'%klass_type)
598 except AttributeError as err:
599 print ("::: UNKNOWN klass_type [%s] !" % klass_type)
600 raise err
601
602 invalid_py_chars = ( ':', '.', '>', '<', ' ' )
603
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)
608 print (err)
609 raise RuntimeError(err)
610
611 fname=''.join([fname,'.py'])
612 d = dict( pkg=pkg,
613 klass=klass,
614 fname=fname,
615 copyright=Templates.copyright_template
616 )
617 o = open(fname, 'w')
618 o.writelines(py_template%d)
619 o.flush()
620 o.close()
621 return 0
622
623@acmdlib.command(name='gen-klass')
624@acmdlib.argument(
625 "--klass",
626 required=True,
627 help = "The (fully qualified) name of the python or C++ class to create (ex: ElectronContainer, Analysis::Electron, MyAlgTool, PyTestAlg)")
628@acmdlib.argument(
629 "--pkg",
630 required=True,
631 help = "The name of the package holding the C++ class to create (ex: MyAnalysis, JetEvent)")
632@acmdlib.argument(
633 "--type",
634 dest = "klass_type",
635 required=True,
636 choices = GenTypes.values,
637 help = "The type of class to create")
638@acmdlib.argument(
639 "--ipkg",
640 default = None,
641 help = "The name of the package holding the interface of the C++ class (mandatory for 'svc' and 'tool' types)")
642@acmdlib.argument(
643 "--iklass",
644 default = None,
645 help = "The name of the interface the C++ class is implementing (mandatory for 'svc' and 'tool' types)")
646@acmdlib.argument(
647 "-o",
648 "--output-file",
649 required=True,
650 dest = "fname",
651 help = "The name of the file(s) which will hold header and implementation of the class (ex: 'Foo' --> ('Foo.h','Foo.cxx'))")