Python scripts I use to manage my ssh connections, drive mounts, and other bash related things.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

122 lines
3.4 KiB

6 years ago
6 years ago
6 years ago
6 years ago
  1. """
  2. Simple python script to run on bash start up.
  3. This will print a welcome message and then a random quote from a text file
  4. 9-27-17
  5. Jeffery Russell
  6. """
  7. import random
  8. import sys
  9. import os.path
  10. import glob
  11. from utils import module
  12. from utils import print_message_bubble
  13. import configuration
  14. """Path to a text file containing quotes"""
  15. INPUT_FILE = configuration.get_config()["quotes"]
  16. """Pulls a list of the ascii art file names"""
  17. BASE_FILE = os.path.dirname(os.path.realpath(__file__))
  18. ASCII_ART = glob.glob(BASE_FILE + "/asciiArt/*.txt")
  19. def print_message(message, ascii_file = None):
  20. """
  21. Prints a dialog box with a message in it with an ascii
  22. animal below it
  23. :param message: a quote to print
  24. :param ascii_file: the file location of the ascii art speaking the message
  25. :return: NA
  26. """
  27. print_message_bubble.print_message(message)
  28. if ascii_file != None:
  29. filepath = '/'.join(INPUT_FILE.split('/')[:-1])
  30. filepath += "/asciiArt/" + ascii_file
  31. if (os.path.exists(filepath)):
  32. f = open(filepath, 'r')
  33. print(f.read())
  34. f.close()
  35. else:
  36. print(module.input_file_with_new_line(
  37. ASCII_ART[random.randint(0,(len(ASCII_ART) -1))]))
  38. else:
  39. print(module.input_file_with_new_line(
  40. ASCII_ART[random.randint(0,(len(ASCII_ART) -1))]))
  41. def print_usage():
  42. """
  43. Prints the usage message to the terminal
  44. :return: None
  45. """
  46. print("Usage:")
  47. print("\t-a quote \t: Adds a quote to the quotes list")
  48. print("\t-h \t\t: Prints usage message")
  49. print("\t-q quote \t: Print this quote")
  50. print("\t--<ASCII image to use> : name of a file (excluding extension) in "
  51. "the \"asciiArt\" directory to use for the ASCII image")
  52. def add_quote():
  53. """
  54. Adds the quote in the command line arguments
  55. to the quotes text file
  56. :return: None
  57. """
  58. quote = ""
  59. for i in range(2, len(sys.argv)):
  60. quote = quote + sys.argv[i]
  61. quote = quote + " "
  62. print("added " + quote + "to " + INPUT_FILE)
  63. module.append_file(INPUT_FILE, quote)
  64. def main():
  65. """
  66. This function calls the welcome function, then it calls the cowsay
  67. function with a random quote.
  68. :return: None
  69. """
  70. if len(sys.argv) > 1:
  71. if sys.argv[1].lower() == "-h" or sys.argv[1].lower() == "-help":
  72. print_usage()
  73. elif sys.argv[1].lower() == "-a":
  74. if len(sys.argv) > 2:
  75. add_quote()
  76. else:
  77. print("You forgot to enter a quote.")
  78. elif sys.argv[1].lower() == '-q':
  79. if len(sys.argv) > 2:
  80. quote = ' '.join(sys.argv[2:])
  81. print_message(quote)
  82. else:
  83. print('You forgot to enter a quote.')
  84. elif sys.argv[1][:2] == "--":
  85. quotes = module.input_file(INPUT_FILE)
  86. print_message(quotes[random.randint(0,(len(quotes) -1))], ascii_file=sys.argv[1][2:]+'.txt')
  87. else:
  88. print_usage()
  89. else:
  90. quotes = module.input_file(INPUT_FILE)
  91. if len(quotes) == 0:
  92. print("Quotes file : " + INPUT_FILE + " is empty.")
  93. else:
  94. print_message(quotes[random.randint(0,(len(quotes) -1))])
  95. """
  96. Makes sure that other programs don't execute the main
  97. """
  98. if __name__ == '__main__':
  99. try:
  100. main()
  101. except KeyboardInterrupt:
  102. exit()