TheresAFewConors/Sooty

Redoing the menus to clean up the code

naveci opened this issue · 5 comments

What is the feature that you are requesting?
I'm thinking of redoing the menus to clean up the code. By putting the contents in dictionaries, we could generate the menus more consistently. It would allow us to take a lot of the printing code out. I haven't counted lines, but it might be quite a lot.
Another advantage: adding a new function only requires an edit in one part of the document - the dictionary.

What's your take?

Additional information
An example is listed below. I do need to really think about this carefully as it won't be entirely as easy as the example code below.

Is your feature request related to a problem? Please describe.
N/A

Additional context
Current code example for the phishing menu:

def phishingMenu():
    print("\n --------------------------------- ")
    print("          P H I S H I N G          ")
    print(" --------------------------------- ")
    print(" What would you like to do? ")
    print(" OPTION 1: Analyze an Email ")
    print(" OPTION 2: Analyze an Email Address for Known Activity")
    print(" OPTION 3: Generate an Email Template based on Analysis")
    print(" OPTION 4: Analyze an URL with Phishtank")
    print(" OPTION 9: HaveIBeenPwned")
    print(" OPTION 0: Exit to Main Menu")
    phishingSwitch(input())

def phishingSwitch(choice):
    if choice == '1':
        analyzePhish()
    if choice == '2':
        analyzeEmailInput()
    if choice == '3':
        emailTemplateGen()
    if choice == '4':
        phishtankModule()
    if choice == '9':
        haveIBeenPwned()
    else:
        mainMenu()

We could shorten this to the following dict:

phishingMenuDict = {
    # Number is the keyboard shortcut, name is the functionname, desc is what the users see as menu option
    0: {"name":"exit", "desc":"back to main menu"},
    1: {"name":"analyzePhish", "desc":"Analyze an Email"},
    2: {"name":"analyzeEmailInput", "desc":"Analyze an Email Address for Known Activity"},
    3: {"name":"emailTemplateGen", "desc":"Generate an Email Template based on Analysis"},
    4: {"name":"phishtankModule", "desc":"Analyze an URL with Phishtank"}
    9: {"name":"haveIBeenPwned", "desc":"HaveIBeenPwned"}
}

Then we need to write 1 function to generate a menu so that we could throw multiple of these dicts to that function. some semi code:

def menuGenerator(menuName):
    print(20 * "-")
    if menu = "main":
        print(mainMenuDict["title"])
    else:
        print(someOtherMenuDictName)
    print((20 * "-") + "\n")

    # That's the printing covered, below is the menu selection
    for menuItem in someOtherMenuDict:
        # Prints something like    Option 1: Analyze an Email
        print("  Option " + menuItem + ": " + someOtherMenuDictName["desc"])
    
    userChoice = input()
    # Check user input
    if userChoice in someOtherMenuDict:
        someOtherMenuDict(userChoice)
    else:
        print("Invalid option, please choose again")

Seems like a useful update. The menu does need to be redone since the project has grown a lot since version 1.0 into something more maintainable for the future.

Is it possible to create a separate branch for dev?
Then I can PR against that, which allows a bit more testing before merging into master. I've found a nice way of building the menu's, but it's definitely an overhaul.

@naveci I created a dev branch that should fulfill what you need.

First version is up:
https://github.com/naveci/Sooty/tree/menu-redesign

Still need to fix a few minor things (press any key to continue) etc.
Also looking at a slightly nicer representation like your current one.
I think the code is already a bit cleaner and lost about 100 lines (give or take).

Will merge with dev when i feel it's ready =)

@naveci no rush, I'm in the midst of a code refactor and moving some of the modules around on Dev to clean up and improve usability atm.