-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcutcookie.py
176 lines (157 loc) · 6.62 KB
/
cutcookie.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
"""
This script will probe your system for rainmeter info to fill in the
default variables used in the cookiecutter process.
"""
import os
import time
import winreg
import configparser
import json
from cookiecutter.main import cookiecutter
from cookiecutter import prompt, vcs
defaults = {
"author_full_name": os.getenv("USERNAME"),
"github_username": "my.github.account",
"req_rainmeter_version": "4.3",
"req_windows_version": [
"Windows XP",
"Windows Vista",
"Windows 7",
"Windows 8.1",
"Windows 10",
],
"_windows_version_alias": {
"Windows XP": "5.1",
"Windows Vista": "6.0",
"Windows 7": "6.1",
"Windows 8.1": "6.3",
"Windows 10": "10.0",
},
"_year": time.strftime("%Y", time.localtime()),
"_skins_path": "",
"_layouts_path": "",
"installed": {"Skin": ["Create a new skin"], "Layout": []},
"import_type": ["Skin", "Layout"],
"import": [],
"_load_skin": "NONE",
"project_name": "",
"repository_name": "",
"short_description": "A collection of my skins for Rainmeter",
"license": ["MIT", "CC BY-SA", "GNU GPLv3"],
"_copy_without_render": ["*.github/*"],
"_init_commit": "no",
}
def read_config(file_path):
parser = configparser.ConfigParser(default_section="Rainmeter")
try: # trys to open file with default utf-8 encoding
parser.read(file_path)
except configparser.MissingSectionHeaderError:
# This exception on this file likely means encoding is utf-16
parser.read(file_path, encoding="utf-16")
finally:
return parser
def aggregate():
"""Gathers default data before prompting user for input."""
rm_reg_key = None
try:
rm_reg_key = winreg.OpenKey(
winreg.HKEY_LOCAL_MACHINE,
"SOFTWARE\\WOW6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\Rainmeter",
)
except FileNotFoundError:
print("Rainmeter is not installed!")
if rm_reg_key is not None:
temp = winreg.QueryValueEx(
rm_reg_key, "DisplayVersion"
)[0]
temp = temp.replace(" beta r", ".")
temp = temp.replace(" r", ".")
temp = temp.rsplit(".")
while len(temp) < 4:
temp.insert(len(temp) - 1, "0")
defaults["req_rainmeter_version"] = ".".join(temp)
defaults["_layouts_path"] = os.getenv("APPDATA") + os.sep + "Rainmeter\\Layouts"
# get path to Rainmeter skins
parsed = read_config(os.getenv("APPDATA") + "\\Rainmeter\\Rainmeter.ini")
if "Rainmeter" in parsed and "SkinPath" in parsed["Rainmeter"]:
if parsed["Rainmeter"]["SkinPath"].endswith(os.sep):
# remove trailing path seperator
defaults["_skins_path"] = parsed["Rainmeter"]["SkinPath"][:-1]
else:
defaults["_skins_path"] = parsed["Rainmeter"]["SkinPath"]
# grab names of installed skins
if len(defaults["_skins_path"]):
for d in os.listdir(defaults["_skins_path"]):
if not d.startswith("@"):
# ignore @Backup & @Vault folders
defaults["installed"]["Skin"].append(d)
# now collect layouts
for d in os.listdir(defaults["_layouts_path"]):
defaults["installed"]["Layout"].append(d)
else: # Rainmeter not installed, so cannot import layout or skin
# only remove Layout from import_type options because
# Skin options to import will only contain "Create a new skin"
defaults["import_type"].remove("Layout")
def prompt_user():
# Prompt user for input
for key, value in defaults.items():
if not key.startswith("_"):
if isinstance(value, list):
defaults[key] = prompt.read_user_choice(key, value)
elif isinstance(value, str):
defaults[key] = prompt.read_user_variable(key, value)
# adjust defaults based on special conditions
if key.endswith("import_type"):
defaults["import"] = defaults["installed"][defaults[key]]
elif key.endswith("import"):
defaults["project_name"] = defaults[key]
defaults["repository_name"] = (
defaults[key].replace(" ", "_") + "_Rainmeter_Skin"
)
# promt user to pick skin to load if importing an installed skin
if (
defaults["import_type"] == "Skin"
and defaults["import"] != "Create a new skin"
):
# now ask the user which skin to load on-install
skin_configs = []
for dirpath, _, filenames in os.walk(
defaults["_skins_path"] + os.sep + defaults[key]
):
for f in filenames:
if f.endswith(".ini"):
skin_configs.append(
dirpath.replace(
defaults["_skins_path"] + os.sep, "",
)
+ os.sep
+ f
)
defaults["_load_skin"] = prompt.read_user_choice(
"skin to load", skin_configs
)
elif key.endswith("project_name"):
defaults["repository_name"] = (
defaults[key].replace(" ", "_") + "_Rainmeter_Skin"
)
elif key.endswith("req_windows_version"):
defaults["req_windows_version"] = defaults["_windows_version_alias"][defaults[key]]
elif key.endswith("req_rainmeter_version"):
# restrict windows version based on rainmeter version
if float(defaults[key][:3]) >= 4.0:
defaults["req_windows_version"].remove("Windows XP")
defaults["req_windows_version"].remove("Windows Vista")
if vcs.is_vcs_installed("git"):
print("git is installed")
init_commit = prompt.read_user_yes_no("Create initial commit? (y/n)", "y")
if isinstance(init_commit, str):
init_commit = True if init_commit == "y" else False
defaults["_init_commit"] = init_commit
# NOTE for debugging -> dump to JSON
# with open("defaults.json", "w") as file:
# json.dump(defaults, file, indent=4)
if __name__ == "__main__":
aggregate()
prompt_user()
# now pass user input and begin cookie-cutting
cookiecutter(".", output_dir="..", extra_context=defaults, no_input=True)