Ejemplo n.º 1
0
def setupProgram():  ### Basic Setup of Program
    '''
    Basic setup of Program:
    - Reads sys.argv and augments if appropriate
    - Makes Info, Out and Log objects
    - Returns [info,out,log,cmd_list]
    '''
    try:
        ### Initial Command Setup & Info ###
        info = makeInfo()
        cmd_list = rje.getCmdList(
            sys.argv[1:], info=info)  ### Load defaults from program.ini
        ### Out object ###
        out = rje.Out(cmd_list=cmd_list)
        out.verbose(2, 2, cmd_list, 1)
        out.printIntro(info)
        ### Additional commands ###
        cmd_list = cmdHelp(info, out, cmd_list)
        ### Log ###
        log = rje.setLog(info=info, out=out, cmd_list=cmd_list)
        return [info, out, log, cmd_list]
    except SystemExit:
        sys.exit()
    except KeyboardInterrupt:
        sys.exit()
    except:
        rje.printf('Problem during initial setup.')
        raise
Ejemplo n.º 2
0
def cmdHelp(
        info=None,
        out=None,
        cmd_list=[]):  ### Prints *.__doc__ and asks for more sys.argv commands
    '''Prints *.__doc__ and asks for more sys.argv commands.'''
    try:  ### ~ [1] ~ Setup ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ###
        if not info: info = makeInfo()
        if not out: out = rje.Out()
        ### ~ [2] ~ Look for help commands and print options if found ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ###
        help = cmd_list.count('help') + cmd_list.count(
            '-help') + cmd_list.count('-h')
        if help > 0:
            rje.printf('\n\nHelp for {0} {1}: {2}\n'.format(
                info.program, info.version,
                time.asctime(time.localtime(info.start_time))))
            out.verbose(-1, 4, text=__doc__)
            if rje.yesNo('Show general commandline options?'):
                out.verbose(-1, 4, text=rje.__doc__)
            if rje.yesNo('Quit?'): sys.exit()  # Option to quit after help
            cmd_list += rje.inputCmds(
                out, cmd_list)  # Add extra commands interactively.
        elif out.stat['Interactive'] > 1:
            cmd_list += rje.inputCmds(out, cmd_list)  # Ask for more commands
        ### ~ [3] ~ Return commands ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ###
        return cmd_list
    except SystemExit:
        sys.exit()
    except KeyboardInterrupt:
        sys.exit()
    except:
        rje.printf('Major Problem with cmdHelp()')
Ejemplo n.º 3
0
def runMain():
    ### ~ [1] ~ Basic Setup of Program  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ###
    try:
        (info, out, mainlog, cmd_list) = setupProgram()
    except SystemExit:
        return
    except:
        rje.printf('Unexpected error during program setup:',
                   sys.exc_info()[0])
        return

    ### ~ [2] ~ Rest of Functionality... ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ###
    try:  #HTML(mainlog,cmd_list).run()
        rje.printf(
            '\n\n{0}\n\n *** No standalone functionality! *** \n\n'.format(
                rje_zen.Zen().wisdom()))

    ### ~ [3] ~ End ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ###
    except SystemExit:
        return  # Fork exit etc.
    except KeyboardInterrupt:
        mainlog.errorLog('User terminated.')
    except:
        mainlog.errorLog('Fatal error in main %s run.' % info.program)
    mainlog.printLog(
        '#LOG',
        '%s V:%s End: %s\n' % (info.program, info.version,
                               time.asctime(time.localtime(time.time()))))
Ejemplo n.º 4
0
def runMain():
    ### ~ [1] ~ Basic Setup of Program  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ###
    try:
        (info, out, mainlog, cmd_list) = setupProgram()
    except SystemExit:
        return
    except:
        rje.printf('Unexpected error during program setup:',
                   sys.exc_info()[0])
        return

    ### ~ [2] ~ Rest of Functionality... ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ###
    try:
        KAT(mainlog, cmd_list).run()

        ### ~ [3] ~ End ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ###
    except SystemExit:
        return  # Fork exit etc.
    except KeyboardInterrupt:
        mainlog.errorLog('User terminated.')
    except:
        mainlog.errorLog('Fatal error in main %s run.' % info.program)
    mainlog.endLog(info)
Ejemplo n.º 5
0
def setupProgram():  ### Basic Setup of Program when called from commandline.
    '''
    Basic Setup of Program when called from commandline:
    - Reads sys.argv and augments if appropriate
    - Makes Info, Out and Log objects
    - Returns [info,out,log,cmd_list]
    '''
    try:  ### ~ [1] ~ Initial Command Setup & Info ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ###
        info = makeInfo()  # Sets up Info object with program details
        if len(sys.argv) == 2 and sys.argv[1] in [
                'version', '-version', '--version'
        ]:
            rje.printf(info.version)
            sys.exit(0)
        if len(sys.argv) == 2 and sys.argv[1] in [
                'details', '-details', '--details'
        ]:
            rje.printf('%s v%s' % (info.program, info.version))
            sys.exit(0)
        if len(sys.argv) == 2 and sys.argv[1] in [
                'description', '-description', '--description'
        ]:
            rje.printf('%s: %s' % (info.program, info.description))
            sys.exit(0)
        cmd_list = rje.getCmdList(
            sys.argv[1:],
            info=info)  # Reads arguments and load defaults from program.ini
        out = rje.Out(cmd_list=cmd_list
                      )  # Sets up Out object for controlling output to screen
        out.verbose(2, 2, cmd_list,
                    1)  # Prints full commandlist if verbosity >= 2
        out.printIntro(
            info)  # Prints intro text using details from Info object
        cmd_list = cmdHelp(
            info, out,
            cmd_list)  # Shows commands (help) and/or adds commands from user
        log = rje.setLog(
            info, out,
            cmd_list)  # Sets up Log object for controlling log file output
        return (info, out, log, cmd_list)  # Returns objects for use in program
    except SystemExit:
        sys.exit()
    except KeyboardInterrupt:
        sys.exit()
    except:
        rje.printf('Problem during initial setup.')
        raise
Ejemplo n.º 6
0
def cmdHelp(
        info=None,
        out=None,
        cmd_list=[]):  ### Prints *.__doc__ and asks for more sys.argv commands
    '''Prints *.__doc__ and asks for more sys.argv commands.'''
    try:
        if not info: info = makeInfo()
        if len(sys.argv) == 2 and sys.argv[1] in [
                'version', '-version', '--version'
        ]:
            rje.printf(info.version)
            sys.exit(0)
        if len(sys.argv) == 2 and sys.argv[1] in [
                'details', '-details', '--details'
        ]:
            rje.printf('{0} v{1}'.format(info.program, info.version))
            sys.exit(0)
        if not out: out = rje.Out()
        help = cmd_list.count('help') + cmd_list.count(
            '-help') + cmd_list.count('-h')
        if help > 0:
            rje.printf('\n\nHelp for {0} {1}: {2}\n'.format(
                info.program, info.version,
                time.asctime(time.localtime(info.start_time))))
            out.verbose(-1, 4, text=__doc__)
            if rje.yesNo('Show general commandline options?'):
                out.verbose(-1, 4, text=rje.__doc__)
            if rje.yesNo('Quit?'): sys.exit()
            cmd_list += rje.inputCmds(out, cmd_list)
        elif out.stat['Interactive'] > 1:
            cmd_list += rje.inputCmds(out, cmd_list)  # Ask for more commands
        return cmd_list
    except SystemExit:
        sys.exit()
    except KeyboardInterrupt:
        sys.exit()
    except:
        rje.printf('Major Problem with cmdHelp()')
Ejemplo n.º 7
0
    ### ~ [2] ~ Rest of Functionality... ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ###
    try:  #HTML(mainlog,cmd_list).run()
        rje.printf(
            '\n\n{0}\n\n *** No standalone functionality! *** \n\n'.format(
                rje_zen.Zen().wisdom()))

    ### ~ [3] ~ End ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ###
    except SystemExit:
        return  # Fork exit etc.
    except KeyboardInterrupt:
        mainlog.errorLog('User terminated.')
    except:
        mainlog.errorLog('Fatal error in main %s run.' % info.program)
    mainlog.printLog(
        '#LOG',
        '%s V:%s End: %s\n' % (info.program, info.version,
                               time.asctime(time.localtime(time.time()))))


#########################################################################################################################
if __name__ == "__main__":  ### Call runMain
    try:
        runMain()
    except:
        rje.printf('Cataclysmic run error: {0}'.format(sys.exc_info()[0]))
    sys.exit()
#########################################################################################################################
### END OF SECTION IV                                                                                                   #
#########################################################################################################################