Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Menu.py a script of scripts.

I am constantly forgetting the exact paths and command structure for the various scripts I h...
The script truncates the name and description to keep things clean.

If I choose 6 to generate a 12 character password:
When I hit enter the screen clears and I am taken back to the menu.
Entering “n” at the main menu takes me to the next men...
Pressing enter again clears the screen and takes me back to the menu I was on.

Entering “p” takes me back one page.
And entering “q” at any menu takes me out of the program.

The shell script looks like this:
james@coenj-suse1:~> cat bin/...
# This program reads a file that contains a list of programs #
# and then creates a menu based upon that list. The user ca...
count = count + 1
print
if pages == 1:
   print 'Choose the number corresponding to your choice'
elif pages > 1 and pages ...
retVal = 0
             jnk = raw_input('Press enter to continue.')
             sys.stdout.write(os.popen('clear').read()...
Upcoming SlideShare
Loading in …5
×

Python Menu

2,278 views

Published on

Python Menu a script of scripts

  • Be the first to comment

  • Be the first to like this

Python Menu

  1. 1. Menu.py a script of scripts. I am constantly forgetting the exact paths and command structure for the various scripts I have written, and I have some standard ways I run them. So, I created a script of scripts that I use to access them. The program menu.py reads a comma delimited file and creates a menu that can then be navigated with relative ease. For now I have kept it fairly simple. I log in and type menu This is a shell script that calls my menu.py script and is stored in my path so I can execute it from anywhere.
  2. 2. The script truncates the name and description to keep things clean. If I choose 6 to generate a 12 character password:
  3. 3. When I hit enter the screen clears and I am taken back to the menu. Entering “n” at the main menu takes me to the next menu. But entering “n” on this menu gives me an error.
  4. 4. Pressing enter again clears the screen and takes me back to the menu I was on. Entering “p” takes me back one page.
  5. 5. And entering “q” at any menu takes me out of the program. The shell script looks like this: james@coenj-suse1:~> cat bin/menu #!/usr/bin/sh cd Python/etc python menu.py Note the script first cd's to the location I have stored menu.py. This is because the menu.dat file that stores my script information is stored in the same directory as the menu.py script itself. james@coenj-suse1:~/Python/etc> ls menu* menu.dat menu.py Here is a look at the menu.dat file: Note the file is comma delimited and each text field is enclosed in double quotation marks. The first field is the name you want displayed on the menu. The second is the command and path to execute the script (in this case python <full path and arguments for script>). It should work just as well with shell scripts by using sh in place of python. The last field is a short description (remember the menu.py script will truncate the name and description). The following is the source code for the menu.py script. #!/usr/bin/python #---------------------------------------------------------------# # menu.py # # Created 2009.10.27 # # By James D. Coen Sr #
  6. 6. # This program reads a file that contains a list of programs # # and then creates a menu based upon that list. The user can # # then select a program to execute. # # The layout of the file is as follows: # # <"program name">,<"full execution path">,<"description"> # #---------------------------------------------------------------# import sys, os, csv # Menu data file relative to script is menu.dat myMenu = {} # create our menu # index is count: program name, path, description menuFile = os.path.join(os.path.abspath(os.curdir) + os.sep + 'menu.dat') def truncString(inStr, max=20): # max size defaults to 20 characters i=0 outStr = '' for let in inStr: if i < max: outStr = outStr + let else: break i=i+1 return outStr def genMenu(): # limit to 10 programs per page pglim = 10 retVal = 0 page = 1 pages = 1 max = 0 fp = open(menuFile, 'rb') csvFile = csv.reader(fp, delimiter=',', quotechar='"') for line in csvFile: max = max + 1 myMenu[max] = [line[0], line[1], line[2]] pages = pages + (max / pglim) while page in range(1, pages+1): count = 0 print '------------------------------------------------------------' print ' My Python Scripts Menu ' print ' Page {0:2d} of {1:2d} '.format(page,pages) for id, vals in myMenu.items(): if (id / pglim + 1) == page: prog = truncString(vals[0], 20) dscr = truncString(vals[2], 30) print ' {0:2d}) {1:20s} {2:25s}'.format(id, prog, dscr)
  7. 7. count = count + 1 print if pages == 1: print 'Choose the number corresponding to your choice' elif pages > 1 and pages == page: print 'Choose the number corresponding to your choice or ' print 'p/P for previous to change pages' elif pages > 1 and pages != page and page > 1: print 'Choose the number corresponding to your choice or ' print 'p/P for previous n/N for next to change pages' else: print 'Choose the number corresponding to your choice or ' print 'n/N for next to change pages' print ' or enter q/Q to quit' print '------------------------------------------------------------' choice = raw_input(' Enter your choice: ') if choice.lower() == 'n' and pages > 1 and page != pages: # next page print '' page = page + 1 sys.stdout.write(os.popen('clear').read()) # clear screen retVal = 0 elif choice.lower() == 'p' and pages > 1 and page != 1: # previous page print '' page = page - 1 sys.stdout.write(os.popen('clear').read()) # clear screen retVal = 0 elif choice.lower() == 'q': # chose to quit print 'Exiting...' retVal = -1 break else: try: choice = int(choice) if choice not in range(1, count+1): print 'Invalid selection...' retVal = 0 jnk = raw_input('Press enter to continue.') sys.stdout.write(os.popen('clear').read()) # clear screen else: sys.stdout.write(os.popen('clear').read()) # clear screen print 'Executing %s ...'%(myMenu[choice][1]) print err = os.system(myMenu[choice][1]) if err != 0: print 'Return Value: ' + str(err) print
  8. 8. retVal = 0 jnk = raw_input('Press enter to continue.') sys.stdout.write(os.popen('clear').read()) # clear screen except: print 'Invalid selection...' retVal = 0 jnk = raw_input('Press enter to continue.') sys.stdout.write(os.popen('clear').read()) # clear screen return retVal menuOpt = 0 while menuOpt == 0: sys.stdout.write(os.popen('clear').read()) # clear screen menuOpt = genMenu() sys.stdout.write(os.popen('clear').read()) # clear screen Hopefully you will find this little menu program useful.

×