Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
#!/usr/bin/env python3
#-*- coding: utf-8 -*-
#pylint: disable-msg=
"""
File       : mkedlpr
Author     : Valentin Kuznetsov <vkuznet@gmail.com>
Description: mkedlpr code
"""

# system modules
import sys

if  sys.version_info < (2, 6):
    raise Exception("This script requires python 2.6 or greater")

# package modules
from FWCore.Skeletons.cms import generate

def pkg_help():
    "mkedlpr help function"
    msg  = '\nmkedlpr script generates CMS EDLooper code'
    msg += '\nUsage  : mkedlpr MyLooper -author "FirstName LastName"'
    msg += '\nUsage  : mkedlpr MyLooper RecName DataType1 DataType2 ...'
    msg += '\n         -author "FirstName LastName"'
    msg += '\n'
    msg += '\n         MyLooper = name of the looper'
    msg += '\n         RecName = name of the record to which the looper adds data'
    msg += '\n         DataType1 = data type created by the looper'
    msg += '\nOutput : '
    msg += """
        MyLooper/
        |  plugins/
        |  |-- BuildFile.xml
        |  |-- MyLooper.cc
        |  python/
        |  test/
        |  doc/
        """
    msg += '\nExample:'
    msg += '\n        # create new EDLooper code'
    msg += '\n        mkedlpr MyLooper'
    msg += '\n        # create new EDLooper code with given author'
    msg += '\n        mkedlpr MyLooper -author "First Last"'
    msg += '\n'
    return msg

def config(tmpl, pkg_help):
    "Parse input arguments to mk-script"
    kwds  = {'author': '', 'tmpl': tmpl,
             'args': {}, 'debug': False}
    etags = []
    args  = []
    if  len(sys.argv) >= 2: # user give us arguments
        if  sys.argv[1] in ['-h', '--help', '-help']:
            print(pkg_help)
            sys.exit(0)
        kwds['pname'] = sys.argv[1]
        for idx in range(2, len(sys.argv)):
            opt = sys.argv[idx]
            if  opt == '-author':
                kwds['author'] = sys.argv[idx+1]
            elif  opt.find('example') != -1:
                etags.append('@%s' % opt)
                continue
            elif  opt in ['-h', '--help', '-help']:
                print(pkg_help)
                sys.exit(0)
            elif  opt == '-debug':
                kwds['debug'] = True
                continue
            elif sys.argv[idx-1] != '-author':
                args.append(opt)
    elif len(sys.argv) == 1:
        # need to walk
        msg   = 'Please enter %s name: ' % tmpl.lower()
        kwds['pname'] = input(msg)
        msg   = 'Please enter record name: '
        args.append(input(msg))
        msg   = 'Please enter data types (separate by space): '
        args += [t for t in input(msg).split()]
    else:
        print(pkg_help)
        sys.exit(0)
    if  args:
        # first element is record name, others are data types
        record = args[0]
        datatypes = args[1:] if len(args) > 1 else ['MyData']
        kwds['args'] = {'__record__': record, '__datatypes__': datatypes}
    else:
        kwds['args'] = {'__record__': 'MyRecord', '__datatypes__': ['MyType']}
    kwds['tmpl_etags'] = etags
    return kwds

if __name__ == '__main__':
    generate(config('EDLooper', pkg_help()))