Created
November 13, 2014 18:49
-
-
Save ceholden/fdecc912438b4d33fabc to your computer and use it in GitHub Desktop.
Example using raw_input for shell interface menus
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
""" Command prompt | |
Hierarchy: | |
- Insert | |
+ What PR? | |
+ Who? | |
+ What project? | |
+ Location | |
+ Status | |
* Ordered | |
* Downloaded | |
* FMask / Stacked | |
* Manually screened | |
* Algorithm ran | |
* Outputs | |
- Query | |
+ What attribute? | |
* print_output(query) | |
- Output | |
+ What format? | |
* write_output(format) | |
- Help | |
+ print_help | |
- Exit | |
""" | |
from __future__ import print_function | |
from collections import OrderedDict | |
import readline | |
# Python 3 | |
import sys | |
if sys.version[0] == '2': | |
input = raw_input | |
main_cmds = ['insert', 'query', 'output', 'help', 'exit'] | |
### INSERT MENU VALIDATORS | |
def insert_valid_wrs2(value): | |
try: | |
v = int(value) | |
return v | |
except: | |
print('WRS2 must be integer') | |
return False | |
def insert_valid_owner(value): | |
owners = ['ceholden', 'bullocke', 'parevalo'] | |
if value.lower() in owners: | |
return value.lower() | |
else: | |
print('Unknown user') | |
return False | |
def insert_valid_project(value): | |
projects = ['CMS', 'IDS', 'CMS Mexico'] | |
if value in projects: | |
return value | |
else: | |
print('Unknown project') | |
return False | |
insert_questions = OrderedDict([ | |
['WRS2 Path/Row', insert_valid_wrs2], | |
['Owner', insert_valid_owner], | |
['Project', insert_valid_project], | |
]) | |
### MENUS | |
def insert_menu(): | |
print('INSERT MENU:') | |
answers = [] | |
for question, validator in insert_questions.iteritems(): | |
answer = False | |
while not answer: | |
answer = validator(input(question + ': ')) | |
answers.append(answer) | |
print('Answers:') | |
print(answers) | |
def main_menu(): | |
print('MAIN MENU') | |
print('Actions: ' + ', '.join(main_cmds)) | |
menu_action = input('> ').lower() | |
if menu_action == 'insert': | |
insert_menu() | |
elif menu_action == 'query': | |
print('GOING TO QUERY SOMETHING') | |
elif menu_action == 'output': | |
print('GOING TO OUTPUT SOMETHING') | |
elif menu_action == 'help': | |
print('SOME HELP') | |
elif menu_action == 'exit': | |
return True | |
else: | |
print('Error - unknown option') | |
if __name__ == '__main__': | |
exited = False | |
while not exited: | |
exited = main_menu() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment