Skip to content

Commit 4ff7efb

Browse files
committed
First pass
1 parent 98a4a17 commit 4ff7efb

3 files changed

Lines changed: 169 additions & 0 deletions

File tree

home/.startup.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
"""
2+
Personal Python startup file for Wm Minchin
3+
4+
History:
5+
2019-04-17 Original Writing
6+
- colored prompt
7+
- colored tracebacks
8+
"""
9+
10+
import sys
11+
from pprint import pprint
12+
13+
PY2 = sys.version_info[0] == 2
14+
PY3 = sys.version_info[0] == 3
15+
16+
# if PY2:
17+
# from __future__ import (absolute_import, division, print_function,
18+
# with_statement)
19+
20+
try:
21+
import colorama
22+
except ImportError:
23+
pass
24+
finally:
25+
colorama.init()
26+
27+
# set up colored prompt
28+
#
29+
# consider numbering these lines; do this by create a function to generate
30+
# ps1 and ps2
31+
sys.ps1 = "{}>>> {}".format(colorama.Fore.GREEN, colorama.Style.RESET_ALL)
32+
sys.ps2 = "{}... {}".format(colorama.Fore.YELLOW, colorama.Style.RESET_ALL)
33+
34+
# colored tracebacks
35+
try:
36+
import tbvaccine
37+
except ImportError:
38+
pass
39+
finally:
40+
tbvaccine.add_hook(isolate=False)
41+
42+
43+
print()
44+
# http://www.wiseoldsayings.com/python-quotes/
45+
print("Python is executable pseudocode. Perl is executable line noise.")
46+
print("Smile, and have fun! :)")
47+
print()

load-windows.py

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
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))

requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
colorama
2+
tbvaccine

0 commit comments

Comments
 (0)