From 1c971ad2603cdfd35e454aa92629bb8c53fdb907 Mon Sep 17 00:00:00 2001 From: jrtechs Date: Sun, 20 May 2018 16:40:06 -0400 Subject: [PATCH] Updated config file to update users .bash/.zshrc file with aliases --- src/configuration.py | 103 +++++++++++++++++++++++++++++++++++++++++-- src/module.py | 2 + src/ssh_manager.py | 2 +- 3 files changed, 103 insertions(+), 4 deletions(-) diff --git a/src/configuration.py b/src/configuration.py index 209111d..ad73ec6 100644 --- a/src/configuration.py +++ b/src/configuration.py @@ -43,8 +43,7 @@ def single_conf_input(param): print("by default it will be " + os.path.dirname(__file__) + "/" + param + ".txt") i = input("Enter selection:") - - + if i.strip() == "": return param + ": " + os.path.dirname(__file__) + "/" + param + ".txt" else: @@ -133,7 +132,105 @@ def get_config(): create_config() return get_config() - +def generate_bash_aliases(): + """ + Generates the bash aliases to add to the bash congig + :return: list of strings with bash aliases + """ + aliases = [] + path = os.path.dirname(os.path.abspath(__file__)) + "/" + aliases.append("alias roosay=\"python3 " + path + "roosay.py\"") + aliases.append("alias ss=\"python3 " + path + "ssh_manager.py\"") + aliases.append("alias ssh_manager=\"python3 " + path + "ssh_manager.py\"") + aliases.append("alias mm=\"python3 " + path + "mount_ssh_drive.py\"") + aliases.append("alias ssh-mount=\"python3 " + path + "mount_ssh_drive.py\"") + aliases.append("alias quote=\"python3 " + path + "quote.py\"") + + return aliases + + +def generate_extra_sauce(): + """ + Creates a list of bash configurations things that I use + :return: string list with all my extra bash sauce + """ + sauce = [] + + sauce.append("alias ls=\"ls -abp --color=auto\"") + sauce.append("function cd {") + sauce.append("\tbuiltin cd \"$@\" && ls") + sauce.append("}") + sauce.append("quote") + + return sauce + + +def view_shell_sauce(): + """ + Displays the output of generate_bash_aliases() and generate_extra_sauce() + """ + print("\n#Bash Aliases:") + + for line in generate_bash_aliases(): + print(line) + print("\n") + print("#Extra Sauce") + for line in generate_extra_sauce(): + print(line) + + +def write_to_bash_config(sauce): + """ + prompts user for name of shell config file and write contents of + sauce to it + """ + path = input("Enter name of shell (.bashrc or .zshrc):") + path = "~/" + path + + for line in sauce: + module.append_file(path, line) + + print("Added the following to " + path) + + for line in sauce: + print("\t" + line) + + +def main(): + """ + Prompts user to either update the config file, or make aliases + in the bash configuration + """ + + options = [] + options.append("1) Update Configuration File") + options.append("2) Make Aliases") + options.append("3) Add Extra Sauce") + options.append("4) View proposed shell conf") + options.append("5) Exit") + i = '0' + + while i != '5': + module.print_menu("Configuration Manager", options) + i = input("Enter Option:") + if i == '1': + create_config() + elif i == '2': + write_to_bash_config(generate_bash_aliases()) + elif i == '3': + write_to_bash_config(generate_extra_sauce()) + elif i == '4': + view_shell_sauce() + + +""" +Makes sure that other programs don't execute the main +""" +if __name__ == '__main__': + try: + main() + except KeyboardInterrupt: + pass diff --git a/src/module.py b/src/module.py index be5b1ee..bb107c6 100644 --- a/src/module.py +++ b/src/module.py @@ -4,6 +4,7 @@ Jeffery Russell """ import subprocess +import os.path def input_file(file_name): @@ -25,6 +26,7 @@ def append_file(file_name, append): :param append: message to append on file :return: None """ + file_name = os.path.expanduser(file_name) f = open(file_name, "a+") f.write(append + "\n") f.close() diff --git a/src/ssh_manager.py b/src/ssh_manager.py index da4ccc6..522b0fb 100644 --- a/src/ssh_manager.py +++ b/src/ssh_manager.py @@ -208,4 +208,4 @@ if __name__ == '__main__': try: main() except KeyboardInterrupt: - exit_program(); + exit_program()