Source code for menusys.menusys

#!/usr/bin/env python3
"""
##########################################################
# Script Name: menusys.py                                #
# Script Type: Python                                    #
# Updated By: Benjamin P. Trachtenberg                   #
# Date Written 1/11/2015                                 #
#                                                        #
# Description:                                           #
# Menu system tools                                      #
#                                                        #
##########################################################
"""
import logging
import os as __os
import sys as __sys
import platform as __platform
import textwrap as __textwrap


LOGGER = logging.getLogger(__name__)


yes_no_dict = {
    1: {"MENU": "Yes"},
    2: {"MENU": "No"}}

prim_sec_dict = {
    1: {"MENU": "Primary"},
    2: {"MENU": "Secondary"}}


[docs]def word_wrap_string_and_print(string_to_wrap): """ Function to word wrap a string depending on the console and print it :type string_to_wrap: String :param string_to_wrap: The string you want to wrap :rtype: None :return: None but it does call the print function """ try: term_width, term_height = __os.get_terminal_size() except OSError: LOGGER.critical('Function word_wrap_string_and_print OSError') term_width = 80 print(__textwrap.fill(string_to_wrap, term_width - 10))
[docs]def word_wrap_string(string_to_wrap): """ Function to word wrap a string depending on the console :type string_to_wrap: String :param string_to_wrap: The string you want to wrap :rtype: String :return: A word wrapped string """ try: term_width, term_height = __os.get_terminal_size() except OSError: LOGGER.critical('Function word_wrap_string OSError') term_width = 80 return __textwrap.fill(string_to_wrap, term_width - 10)
[docs]def chunk_up_string(string_to_chunk, size_of_chunk=100): """ Function to chunk up a string, and make a list of chunks :type string_to_chunk: String :param string_to_chunk: The string you want to chunk up :type size_of_chunk: Integer :param size_of_chunk: The size of the chunks in characters :rtype: List :return: A list containing the chunks. """ temp_list = list() for chunk in range(0, len(string_to_chunk), size_of_chunk): temp_list.append(string_to_chunk[chunk:chunk + size_of_chunk]) return temp_list
[docs]def make_menu_dict_from_dict(orig_dict, dict_key_for_display): """ Function to create a menu dictionary with sub dictionary :type orig_dict: Dict :param orig_dict: Dictionary you want to make a menu from :type dict_key_for_display: String :param dict_key_for_display: Dictionary item to become the menu :rtype: Dict :return: A dictionary with menu and dictionary in line """ temp_dict = dict() menu_new_key = 1 for orig_dict_key in orig_dict: temp_dict[menu_new_key] = {'MENU': orig_dict[orig_dict_key][dict_key_for_display], 'SUBDIC': orig_dict[orig_dict_key]} menu_new_key += 1 return temp_dict
[docs]def make_menu_dict_from_list(orig_list): """ Function to create a menu dictionary from a list :type orig_list: List :param orig_list: List you want to make a menu from :rtype: Dict :return: A dictionary with menu """ temp_dict = dict() menu_new_key = 1 for orig_list_line in orig_list: temp_dict[menu_new_key] = {'MENU': orig_list_line} menu_new_key += 1 return temp_dict
[docs]def clear_screen(): """ Function to do a clear screen in Linux or Windows :rtype: None :return: None it clears the screen """ if __platform.system() == "Windows": __os.system("cls") elif __platform.system() == "Linux": __os.system("clear") else: print("Your OS doesn't seem to be supported!")
if __name__ == "__main__": help(__name__)