Advertisement
ezarchproject

Gsmit.py

Mar 23rd, 2015
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.52 KB | None | 0 0
  1. #!/usr/bin/env python
  2. #-------------------------------------------------------------------------------
  3. #This program is free software: you can redistribute it and/or modify
  4. #it under the terms of the GNU General Public License as published by
  5. #the Free Software Foundation, either version 3 of the License, or
  6. #(at your option) any later version.
  7. #
  8. #This program is distributed in the hope that it will be useful,
  9. #but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11. #GNU General Public License for more details.
  12. #
  13. #You should have received a copy of the GNU General Public License
  14. #along with this program.  If not, see <http://www.gnu.org/licenses/>.
  15. #-------------------------------------------------------------------------------
  16. #
  17. #  Python Source Code was orignally written by
  18. #
  19. #  -*- coding: utf-8 -*-
  20. # Topmenu and the submenus are based of the example found at this location http://blog.skeltonnetworks.com/2010/03/python-curses-custom-menu/
  21. # The rest of the work was done by Matthew Bennett and he requests you keep these two mentions when you reuse the code :-)
  22. # Basic code refactoring by Andrew Scheller
  23. #
  24. ########################################################################################################################
  25. #  I decided to make GNU/SMIT menu using this code which has been modified to make it similar to SMIT but not entirely a clone of it.
  26. #  This script will link with shell scripts as if you seen on SCO UNIX's sco admin menu
  27. #
  28. #
  29. #   The shell scripts that connects to this menu are done by Vincent <ezarchproject@gmail.com>
  30. #
  31. #
  32. ##########################################################################################################3
  33.  
  34.  
  35. from time import sleep
  36. import curses, os #curses is the interface for capturing key presses on the menu, os launches the files
  37. screen = curses.initscr() #initializes a new window for capturing key presses
  38. curses.noecho() # Disables automatic echoing of key presses (prevents program from input each key twice)
  39. curses.cbreak() # Disables line buffering (runs each key as it is pressed rather than waiting for the return key to pressed)
  40. curses.start_color() # Lets you use colors when highlighting selected menu option
  41. screen.keypad(1) # Capture input from keypad
  42.  
  43. # Change this to use different colors when highlighting
  44. curses.init_pair(1,curses.COLOR_BLACK, curses.COLOR_WHITE) # Sets up color pair #1, it does black text with white background
  45. h = curses.color_pair(1) #h is the coloring for a highlighted menu option
  46. n = curses.A_NORMAL #n is the coloring for a non highlighted menu option
  47.  
  48. MENU = "menu"
  49. COMMAND = "command"
  50. EXITMENU = "exitmenu"
  51.  
  52.  
  53. menu_data = {
  54.   'title': "Systems Administration", 'type': MENU, 'subtitle': "Please move the cursor to the desired item and press Enter",
  55.   'options':[
  56.     { 'title': "C - Process Viewer", 'type': COMMAND, 'command': 'top' },
  57.     { 'title': "M - Filesystem Management", 'type':COMMAND,'command':'filesystem/fsmenu.py'},
  58.         { 'title': "M - User Management", 'type': COMMAND, 'command': 'users/usermenu.py' },
  59.      { 'title': "M - Init Manager", 'type': MENU, 'subtitle': "Select INIT type for your system",
  60.         'options': [
  61.           {'title': "M - Systemd (Common on GNU/Linux Distros)", 'type': COMMAND,'command':'systemd/sysdmenu.py' },
  62.           {'title': "M - OpenRC (GNU/Linux that does not use systemd)", 'type': COMMAND, 'command': 'openrc/openrcmenu.py' },
  63.           {'title': "M - System V Init (Slackware and Old GNU/Linux)", 'type': COMMAND, 'command': 'sysvinit/sysvinitmenu.py' }
  64.         ]
  65.         },  
  66.         { 'title': "C - Shell", 'type':COMMAND,'command':'/bin/bash'},
  67.          { 'title': "I - About GNU/SMIT", 'type':COMMAND,'command':'cat doc/Gsmit.txt | more'},
  68.   ]  
  69. }
  70.  
  71.  
  72. # This function displays the appropriate menu and returns the option selected
  73. def runmenu(menu, parent):
  74.  
  75.   # work out what text to display as the last menu option
  76.   if parent is None:
  77.     lastoption = "Exit"
  78.   else:
  79.     lastoption = "[(..)]" % parent
  80.    
  81.  
  82.   optioncount = len(menu['options']) # how many options in this menu
  83.  
  84.   pos=0 #pos is the zero-based index of the hightlighted menu option. Every time runmenu is called, position returns to 0, when runmenu ends the position is returned and tells the program what opt$
  85.   oldpos=None # used to prevent the screen being redrawn every time
  86.   x = None #control for while loop, let's you scroll through options until return key is pressed then returns pos to program
  87.  
  88.   # Loop until return key is pressed
  89.   while x !=ord('\n'):
  90.     if pos != oldpos:
  91.       oldpos = pos
  92.       screen.border(0)
  93.       screen.addstr(1,30, menu['title'], curses.A_BOLD) # Title for this menu
  94.       screen.addstr(3,2, menu['subtitle'], curses.A_BOLD) #Subtitle for this menu
  95.  
  96.       # Display all the menu items, showing the 'pos' item highlighted
  97.       for index in range(optioncount):
  98.         textstyle = n
  99.         if pos==index:
  100.           textstyle = h
  101.         #screen.addstr(5+index,4, "%d - %s" % (index+1, menu['options'][index]['title']), textstyle)
  102.         screen.addstr(5+index,4, "%s" % (menu['options'][index]['title']), textstyle)
  103.       # Now display Exit/Return at bottom of menu
  104.       textstyle = n
  105.       if pos==optioncount:
  106.         textstyle = h
  107.       #screen.addstr(5+optioncount,4, "%d - %s" % (optioncount+1, lastoption), textstyle)
  108.       screen.addstr(6+optioncount,4, "%s" % (lastoption), textstyle)
  109.      
  110.       #Added by Vincent
  111.       screen.addstr(20,2, "M = Menu/Submenu     C = Command  I = Information ", curses.A_NORMAL)  
  112.       screen.addstr(22,2, "Enter=Do", curses.A_BOLD)
  113.       screen.refresh()
  114.       # finished updating screen
  115.  
  116.     x = screen.getch() # Gets user input
  117.    
  118.        
  119.         # What is user input?
  120.     if x >= ord('1') and x <= ord(str(optioncount+1)):
  121.       pos = x - ord('0') - 1 # convert keypress back to a number, then subtract 1 to get index
  122.     elif x == 258: # down arrow
  123.       if pos < optioncount:
  124.         pos += 1
  125.       else: pos = 0
  126.     elif x == 259: # up arrow
  127.       if pos > 0:
  128.         pos += -1
  129.       else: pos = optioncount
  130.  
  131.   # return index of the selected item
  132.   return pos
  133.  
  134.  
  135.  
  136.  
  137. # This function calls showmenu and then acts on the selected item
  138. def processmenu(menu, parent=None):
  139.   optioncount = len(menu['options'])
  140.   exitmenu = False
  141.   while not exitmenu: #Loop until the user exits the menu
  142.     getin = runmenu(menu, parent)
  143.     if getin == optioncount:
  144.         exitmenu = True
  145.     elif menu['options'][getin]['type'] == COMMAND:
  146.       curses.def_prog_mode()    # save curent curses environment
  147.       os.system('reset')
  148.       screen.clear() #clears previous screen
  149.       os.system(menu['options'][getin]['command']) # run the command
  150.       screen.clear() #clears previous screen on key press and updates display based on pos
  151.       curses.reset_prog_mode()   # reset to 'current' curses environment
  152.       curses.curs_set(1)         # reset doesn't do this right
  153.       curses.curs_set(0)
  154.     elif menu['options'][getin]['type'] == MENU:
  155.           screen.clear() #clears previous screen on key press and updates display based on pos
  156.           processmenu(menu['options'][getin], menu) # display the submenu
  157.           screen.clear() #clears previous screen on key press and updates display based on pos
  158.     elif menu['options'][getin]['type'] == EXITMENU:
  159.           exitmenu = True
  160.  
  161. # Main program
  162. processmenu(menu_data)
  163. curses.endwin() #VITAL! This closes out the menu system and returns you to the bash prompt.
  164. os.system('clear')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement