|
| 1 | +""" |
| 2 | +Load this Python start up file (into Windows) by setting the appropriate |
| 3 | +environmental variable. |
| 4 | +""" |
| 5 | + |
| 6 | +import os |
| 7 | +import sys |
| 8 | +import winreg |
| 9 | +from enum import Enum |
| 10 | +from pathlib import Path |
| 11 | + |
| 12 | +import colorama |
| 13 | + |
| 14 | +colorama.init() |
| 15 | + |
| 16 | +STARTUP_FILE = Path.cwd() / 'home' / '.startup.py' |
| 17 | +REGISTRY_PATH = 'Environment' |
| 18 | +REGISTRY_KEY = 'PYTHONSTARTUP' |
| 19 | + |
| 20 | + |
| 21 | +# modified from https://stackoverflow.com/a/35286642 |
| 22 | +def set_reg(registry_path, name, value): |
| 23 | + try: |
| 24 | + winreg.CreateKey(winreg.HKEY_CURRENT_USER, registry_path) |
| 25 | + registry_key = winreg.OpenKey(winreg.HKEY_CURRENT_USER, registry_path, |
| 26 | + 0, winreg.KEY_WRITE) |
| 27 | + winreg.SetValueEx(registry_key, name, 0, winreg.REG_SZ, value) |
| 28 | + winreg.CloseKey(registry_key) |
| 29 | + return True |
| 30 | + except WindowsError: |
| 31 | + return False |
| 32 | + |
| 33 | +# https://stackoverflow.com/a/35286642 |
| 34 | +def get_reg(registry_path, name): |
| 35 | + try: |
| 36 | + registry_key = winreg.OpenKey(winreg.HKEY_CURRENT_USER, registry_path, |
| 37 | + 0, winreg.KEY_READ) |
| 38 | + value, regtype = winreg.QueryValueEx(registry_key, name) |
| 39 | + winreg.CloseKey(registry_key) |
| 40 | + return value |
| 41 | + except WindowsError: |
| 42 | + return None |
| 43 | + |
| 44 | +# from minchin.text |
| 45 | +class Answers(Enum): |
| 46 | + ''' |
| 47 | + Possibles answers to queries. |
| 48 | + YES and ALL are "Truth-y", while NO, QUIT, and NONE are "False-y". |
| 49 | + ''' |
| 50 | + |
| 51 | + NO = 0 |
| 52 | + YES = 1 |
| 53 | + QUIT = 2 |
| 54 | + ALL = 3 |
| 55 | + NONE = 4 |
| 56 | + |
| 57 | + def __bool__(self): |
| 58 | + if self.value in [0, 2, 4]: |
| 59 | + return False |
| 60 | + elif self.value in [1, 3]: |
| 61 | + return True |
| 62 | + |
| 63 | +# from minchin.text |
| 64 | +def query_yes_no(question, default="yes"): |
| 65 | + '''Ask a yes/no question via raw_input() and return their answer. |
| 66 | + "question" is a string that is presented to the user. |
| 67 | + "default" is the presumed answer if the user just hits <Enter>. |
| 68 | + It must be "yes" (the default), "no" or None (meaning |
| 69 | + an answer is required of the user). |
| 70 | + The return value is one of Answers.YES or Answers.NO. |
| 71 | + Copied (and modified) from |
| 72 | + http://stackoverflow.com/questions/3041986/python-command-line-yes-no-input |
| 73 | + ''' |
| 74 | + valid = {"yes": Answers.YES, "y": Answers.YES, "ye": Answers.YES, |
| 75 | + "no": Answers.NO, "n": Answers.NO} |
| 76 | + if default is None: |
| 77 | + prompt = " [y/n] " |
| 78 | + elif default == "yes": |
| 79 | + prompt = " [Y/n] " |
| 80 | + elif default == "no": |
| 81 | + prompt = " [y/N] " |
| 82 | + else: |
| 83 | + raise ValueError("invalid default answer: '%s'" % default) |
| 84 | + |
| 85 | + while True: |
| 86 | + sys.stdout.write(question + prompt) |
| 87 | + choice = input().lower() |
| 88 | + if default is not None and choice == '': |
| 89 | + return valid[default] |
| 90 | + elif choice in valid: |
| 91 | + return valid[choice] |
| 92 | + else: |
| 93 | + sys.stdout.write("Please respond with 'yes' or 'no' " |
| 94 | + "(or 'y' or 'n').\n") |
| 95 | + |
| 96 | + |
| 97 | +if __name__ == "__main__": |
| 98 | + key_existing = get_reg(REGISTRY_PATH, REGISTRY_KEY) |
| 99 | + if key_existing is not None: |
| 100 | + print('[{}WARN{}] Registry Key already exists, and is set to "{}"' |
| 101 | + .format(colorama.Fore.YELLOW, colorama.Style.RESET_ALL, |
| 102 | + key_existing)) |
| 103 | + to_proceed = query_yes_no('Proceed anyway?', default="no") |
| 104 | + if to_proceed == Answers.NO: |
| 105 | + sys.exit(1) |
| 106 | + |
| 107 | + if not STARTUP_FILE.exists(): |
| 108 | + print("[{}WARN{}] Startup file does not seem to exist..." |
| 109 | + .format(colorama.Fore.YELLOW, colorama.Style.RESET_ALL)) |
| 110 | + sys.exit(2) |
| 111 | + |
| 112 | + results = set_reg(REGISTRY_PATH, REGISTRY_KEY, str(STARTUP_FILE)) |
| 113 | + if results: |
| 114 | + print("[{}GOOD{}] Startup file loaded!" |
| 115 | + .format(colorama.Fore.GREEN, colorama.Style.RESET_ALL)) |
| 116 | + print("You may need to re-open the console window for this to take " |
| 117 | + "effect.") |
| 118 | + else: |
| 119 | + print("[{}ERROR{}] Error writing to registry" |
| 120 | + .format(colorama.Fore.RED, colorama.Style.RESET_ALL)) |
0 commit comments