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 info == None: info = makeInfo() if out == None: out = rje.Out() helpx = cmd_list.count('help') + cmd_list.count('-help') + cmd_list.count('-h') if helpx > 0: print '\n\nHelp for %s %s: %s\n' % (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: # Ask for more commands cmd_list += rje.inputCmds(out,cmd_list) return cmd_list except SystemExit: sys.exit() except KeyboardInterrupt: sys.exit() except: print 'Major Problem with cmdHelp()'
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 info == None: info = makeInfo() if out == None: out = rje.Out() help = cmd_list.count("help") + cmd_list.count("-help") + cmd_list.count("-h") if help > 0: print "\n\nHelp for %s %s: %s\n" % ( 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: # Ask for more commands cmd_list += rje.inputCmds(out, cmd_list) return cmd_list except SystemExit: sys.exit() except KeyboardInterrupt: sys.exit() except: print "Major Problem with cmdHelp()"
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: print "\n\nHelp for %s %s: %s\n" % ( 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("Show disorder commandline options?"): out.verbose(-1, 4, text=rje_disorder.__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: print "Major Problem with cmdHelp()"
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 info == None: info = makeInfo() if out == None: out = rje.Out() helpx = cmd_list.count('help') + cmd_list.count('-help') + cmd_list.count('-h') if helpx > 0: print '\n\nHelp for %s %s: %s\n' % (info.program, info.version, time.asctime(time.localtime(info.start_time))) out.verbose(-1,4,text=__doc__) if rje.yesNo('Show sequence commandline options?'): out.verbose(-1,4,text=rje_seq.__doc__) if rje.yesNo('Show tree commandline options?'): out.verbose(-1,4,text=rje_tree.__doc__) if rje.yesNo('Show ancestral sequence prediction commandline options?'): out.verbose(-1,4,text=rje_ancseq.__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: # Ask for more commands cmd_list += rje.inputCmds(out,cmd_list) return cmd_list except SystemExit: sys.exit() except KeyboardInterrupt: sys.exit() except: print 'Major Problem with cmdHelp()'
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 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ### helpx = cmd_list.count('help') + cmd_list.count( '-help') + cmd_list.count('-h') if helpx > 0: print '\n\nHelp for %s %s: %s\n' % ( 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: print 'Major Problem with cmdHelp()'
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 info == None: info = makeInfo() if out == None: out = rje.Out() help = cmd_list.count('help') + cmd_list.count( '-help') + cmd_list.count('-h') if help > 0: print "\n\nHelp for " + info.program + " V:" + info.version + ": " + time.asctime( time.localtime(info.start_time)) + "\n" out.verbose(-1, -1, text=__doc__) sys.exit() elif out.stat['Interactive'] > 1: # Ask for more commands cmd_list += rje.inputCmds(out, cmd_list) return cmd_list except SystemExit: sys.exit() except KeyboardInterrupt: sys.exit() except: print 'Major Problem with cmdHelp()'
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 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ### cmd_help = cmd_list.count('help') + cmd_list.count('-help') + cmd_list.count('-h') if cmd_help > 0: print '\n\nHelp for %s %s: %s\n' % (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: print 'Major Problem with cmdHelp()'
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 info == None: info = makeInfo() if out == None: out = rje.Out() help = cmd_list.count('help') + cmd_list.count('-help') + cmd_list.count('-h') if help > 0: print "\n\nHelp for " + info.program + " V:" + info.version + ": " + time.asctime(time.localtime(info.start_time)) + "\n" out.verbose(-1,-1,text=__doc__) cmd_list += rje.inputCmds(out,cmd_list) elif out.stat['Interactive'] > 1: # Ask for more commands cmd_list += rje.inputCmds(out,cmd_list) return cmd_list except KeyboardInterrupt: raise except: print 'Major Problem with cmdHelp()'
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 info == None: info = makeInfo() if out == None: out = rje.Out([]) help = cmd_list.count('help') + cmd_list.count('-help') + cmd_list.count('-h') if help > 0: print "\n\nHelp for " + info.program + " V:" + info.version + ": " + time.asctime(time.localtime(info.start_time)) + "\n" out.verbose(out.verbosity,out.interactive,__doc__,1) out.verbose(out.verbosity,out.interactive,rje.__doc__,1) # ! # Add additional user modules and objects here! cmd_list += rje.inputCmds(out,cmd_list) elif out.interactive > 0: # Ask for more commands cmd_list += rje.inputCmds(out,cmd_list) return cmd_list except KeyboardInterrupt: raise except: print 'Major Problem with cmdHelp()'
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()')
def menu(callobj, headtext='', menulist=[], choicetext='Please select:', changecase=True, default='', jointxt=' = ', confirm=False): ### Main Menu method ''' Main Menu method. >> callobj:Object for which attributes are to be read and altered. Also controls interactivity and log. >> headtext:str [''] = Introductory text for menu system. >> menulist:list [] = List of menu item tuples (edit code,description,optiontype,optionkey) - e.g. ('0','Sequence file','info','Name') would edit callobj.info['Name']) - If optiontype == 'return' then menu will return the value given in optionkey - If optiontype == '' then description will be printed as a breaker - If optiontype == 'infile' then callobj.info['Name'] would be changed using rje.getFileName(mustexist=True) - If optiontype == 'outfile' then callobj.info['Name'] would be changed using rje.getFileName(confirm=True) - If optiontype == 'showtext' then optionkey should contain text to be printed with verbose - If optiontype == 'addcmd' then commands can be added. >> choicetext:str ['Please select:'] = Text to display for choice option >> changecase:boolean [True] = change all choices and codes to upper text >> default:str [''] = What to return if nothing selected. >> jointxt:str [' = '] = What to join code and description with when listing options. >> confirm:bool [False] = Whether to confirm selection. << returns optionkey if appropriate, else True ''' try: ### ~ [0] Setup ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ### ## ~ [0a] Fill out partial (return) tuples ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ## newlist = [] for mtuple in menulist: if len(mtuple) == 2: newlist.append(mtuple + ('return', mtuple[0])) elif len(mtuple) == 3: newlist.append(mtuple + (mtuple[0], )) else: newlist.append(mtuple) menulist = newlist ## ~ [0b] Choice Dictionary ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ## choicedict = {} for (code, desc, vtype, key) in menulist: if not vtype: continue if changecase: choicedict[code.upper()] = (vtype, key) else: choicedict[code] = (vtype, key) ## ~ [0c] Setup Header Text ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ## maxlen = 0 for line in string.split(headtext, '\n'): if len(line) > maxlen: maxlen = len(line) headlist = ['#' * (maxlen + 10)] for line in string.split(headtext, '\n')[0:]: while len(line) < maxlen: line += ' ' headlist.append('# #> %s <# #' % line) headlist.append(headlist[0]) ### ~ [1] Main Menu Loop ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ### while menulist: ## ~ [1a] Setup ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ## mtxt = '\n%s' % string.join(headlist, '\n') while mtxt[-2:] != '\n\n': mtxt += '\n' for (code, desc, vtype, key) in menulist: if vtype and (code or desc): if code and desc: mtxt += '%s%s%s' % (code, jointxt, desc) elif code: mtxt += code elif desc: mtxt += desc if vtype in [ 'info', 'list', 'opt', 'stat', 'int', 'str', 'bool', 'num' ]: mtxt += ': %s' % callobj.getAttribute( vtype, key, default='#!#ERROR#!#') elif vtype in ['infile', 'outfile']: mtxt += ': %s' % callobj.getAttribute( 'info', key, default='#!#ERROR#!#') else: mtxt += desc mtxt += '\n' ## ~ [1b] Give Choices ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ## print(mtxt) while mtxt: try: ## ~ Input user choice ~~~ ## choice = rje.choice(choicetext, default=default, confirm=confirm) if changecase: choice = choice.upper() ## ~ Process user choice ~ ## if choicedict.has_key(choice): (vtype, key) = choicedict[choice] if vtype in ['str', 'info']: callobj.setInfo({ key: callobj._editChoice(key, callobj.getStr(key)) }) if vtype in ['num', 'stat']: callobj.setStat({ key: callobj._editChoice(key, callobj.getNum(key), numeric=True) }) if vtype == 'int': callobj.setStat({ key: int( callobj._editChoice(key, callobj.getInt(key), numeric=True)) }) if vtype in ['bool', 'opt']: callobj.setOpt({key: not callobj.getBool(key)}) if vtype == 'list': callobj.list[key] = string.split( callobj._editChoice(key, callobj.list[key])) if vtype == 'infile': callobj.setInfo({ key: rje.getFileName('%s File Name?' % key, callobj.getStr(key)) }) if vtype == 'outfile': callobj.setInfo({ key: rje.getFileName('%s File Name?' % key, callobj.getStr(key), mustexist=False, confirm=True) }) if vtype == 'showtext': callobj.verbose(-1, -1, key) break if vtype == 'addcmd': prevcmd = callobj.cmd_list callobj.cmd_list = rje.inputCmds(out, choice) callobj.printLog( '#CMD', 'User Added commands: %s' % callobj.cmd_list) callobj._cmdList() callobj.cmd_list = prevcmd + callobj.cmd_list break if vtype in [ 'info', 'list', 'opt', 'stat', 'infile', 'outfile', 'str', 'bool', 'int', 'num' ]: callobj.printLog('#%s' % vtype.upper(), 'User edited %s parameter' % key) break elif vtype == 'return': return key print('Choice "%s" not recognised!\n' % choice) except KeyboardInterrupt: if rje.yesNo('Terminate program?'): raise SystemExit if rje.yesNo('Exit menu and proceed?'): return default except: raise ### End ### return True except KeyboardInterrupt: raise except SystemExit: raise except: if callobj: callobj.errorLog('Major disaster in rje_menu.menu()', quitchoice=True) else: raise
def menu(callobj,headtext='',menulist=[],choicetext='Please select:',changecase=True,default=''): ### Main Menu method ''' Main Menu method. >> callobj:Object for which attributes are to be read and altered. Also controls interactivity and log. >> headtext:str [''] = Introductory text for menu system. >> menulist:list [] = List of menu item tuples (edit code,description,optiontype,optionkey) - e.g. ('0','Sequence file','info','Name') would edit callobj.info['Name']) - If optiontype == 'return' then menu will return the value given in optionkey - If optiontype == '' then description will be printed as a breaker - If optiontype == 'infile' then callobj.info['Name'] would be changed using rje.getFileName(mustexist=True) - If optiontype == 'outfile' then callobj.info['Name'] would be changed using rje.getFileName(confirm=True) - If optiontype == 'showtext' then optionkey should contain text to be printed with verbose - If optiontype == 'addcmd' then commands can be added. >> choicetext:str ['Please select:'] = Text to display for choice option >> changecase:boolean [True] = change all choices and codes to upper text << returns optionkey if appropriate, else True ''' try:### ~ [0] Setup ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ### ## ~ [0a] Choice Dictionary ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ## choicedict = {} for (code,desc,type,key) in menulist: if not type: continue if changecase: choicedict[code.upper()] = (type,key) else: choicedict[code] = (type,key) ## ~ [0b] Setup Header Text ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ## maxlen = 0 for line in string.split(headtext,'\n'): if len(line) > maxlen: maxlen = len(line) headlist = ['#' * (maxlen + 10)] for line in string.split(headtext,'\n')[0:]: while len(line) < maxlen: line += ' ' headlist.append('# #> %s <# #' % line) headlist.append(headlist[0]) ### ~ [1] Main Menu Loop ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ### while menulist: ## ~ [1a] Setup ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ## mtxt = '\n%s' % string.join(headlist,'\n') while mtxt[-2:] != '\n\n': mtxt += '\n' for (code,desc,type,key) in menulist: if type and (code or desc): mtxt += '<%s> %s' % (code,desc) if type in ['info','list','opt','stat','int','str','bool','num']: mtxt += ': %s' % callobj.getAttribute(type,key,default='#!#ERROR#!#') elif type in ['infile','outfile']: mtxt += ': %s' % callobj.getAttribute('info',key,default='#!#ERROR#!#') else: mtxt += desc mtxt += '\n' ## ~ [1b] Give Choices ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ## print mtxt while mtxt: try:## ~ Input user choice ~~~ ## choice = rje.choice(choicetext,default=default) if changecase: choice = choice.upper() ## ~ Process user choice ~ ## if choicedict.has_key(choice): (type,key) = choicedict[choice] if type in ['str','info']: callobj.setInfo({key:callobj._editChoice(key,callobj.getStr(key))}) if type in ['num','stat']: callobj.setStat({key:callobj._editChoice(key,callobj.getNum(key),numeric=True)}) if type == 'int': callobj.setStat({key:int(callobj._editChoice(key,callobj.getInt(key),numeric=True))}) if type in ['bool','opt']: callobj.setOpt({key: not callobj.getBool(key)}) if type == 'list': callobj.list[key] = string.split(callobj._editChoice(key,callobj.list[key])) if type == 'infile': callobj.setInfo({key: rje.getFileName('%s File Name?' % key,callobj.getStr(key))}) if type == 'outfile': callobj.setInfo({key: rje.getFileName('%s File Name?' % key,callobj.getStr(key),mustexist=False,confirm=True)}) if type == 'showtext': callobj.verbose(-1,-1,key); break if type == 'addcmd': prevcmd = callobj.cmd_list callobj.cmd_list = rje.inputCmds(out,prevcmd) callobj.printLog('#CMD','User Added commands: %s' % callobj.cmd_list) callobj._cmdList() callobj.cmd_list = prevcmd + callobj.cmd_list break if type in ['info','list','opt','stat','infile','outfile','str','bool','int','num']: callobj.printLog('#%s' % type.upper(),'User edited %s parameter' % key); break elif type == 'return': return key print 'Choice "%s" not recognised!\n' % choice except KeyboardInterrupt: if rje.yesNo('Terminate program?'): raise if rje.yesNo('Exit menu and proceed?'): if default: return default else: return True except: raise ### End ### return True except KeyboardInterrupt: raise except: if callobj: callobj.errorLog('Major disaster in rje_menu.menu()',quitchoice=True) else: raise