-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathIconify.py
201 lines (126 loc) · 5.57 KB
/
Iconify.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import os, re
from win32com.client import Dispatch #used to create shortcut
from PIL import Image
import pathlib
import xml.etree.ElementTree
from UserOptions import UserOptions
"""'src' is the file we want to make a shortcut of, 'dest' is the file of the shortcut we are creating wich should end with '.lnk'"""
def createShortcut(src, dest, icon=""):
# ref: http://www.blog.pythonlibrary.org/2010/01/23/using-python-to-create-shortcuts/
shell = Dispatch("WScript.Shell")
shortcut = shell.createShortcut(dest)
shortcut.Targetpath = src
shortcut.workingDirectory = os.path.dirname(src)
if(icon != ""):
shortcut.IconLocation = icon
shortcut.save()
def pathExist(path):
COLOR = "\033[94m"
END = "\033[0m"
if(os.path.exists(path)):
if(os.path.isfile(path)):
print(f"{COLOR}File{END} '{path}' {COLOR}exists.{END}")
else:
print(f"{COLOR}Folder{END} '{path}' {COLOR}exists.{END}")
return True
else:
print(f"{COLOR}Path{END} '{path}' {COLOR}doesn't exist.{END}")
return False
class Iconify:
userOptions = None
def __init__(self, userOptions):
# check if userOptions is valid
if(not isinstance(userOptions, UserOptions)):
raise ValueError("invalid UserOptions")
self.userOptions = userOptions
# prepare folders
self.checkAndCreateGameFolder()
# prepare the VBS file used to execute the game using the steam url
self.createVBS()
# create the shortcut in the tileIconify's shortcut folder, this is where we can pin them to the start menu
self.createVBSShortcut()
#https://docs.microsoft.com/en-us/uwp/schemas/appxpackage/uapmanifestschema/element-uap-visualelements
#create the visual elements manifest file and prepare the folder to receive the icons
self.createVisualElementsManifest()
#create icons for the visual elements from the image specified in the configs
self.createIcons()
self.createIconMetadata()
print("\033[92mSuccess!\033[0m")
def applyUserOptionsToData(self, fileData):
for key in self.userOptions.__dict__:
if(re.match("^__.*__$", key)): # ignore any magic attribute
continue
tag = "{" + key + "}"
value = self.userOptions.__dict__[key]
fileData = fileData.replace(tag, value)
return fileData
def checkAndCreateGameFolder(self):
gameFolderPath = self.userOptions.gameFolderPath
if(not pathExist(gameFolderPath)):
print("Creating...")
os.makedirs(gameFolderPath)
def createVBS(self):
vbsFile = self.userOptions.vbsFile
if(not pathExist(vbsFile)):
print("Creating...")
# load template
with open("templates/template.vbs", "r") as f:
vbsTemplate = f.read()
#modify template
vbsTemplate = self.applyUserOptionsToData(vbsTemplate)
# write vbs with modified template
with open(vbsFile, "w") as f:
f.write(vbsTemplate)
def createVBSShortcut(self):
shortcut = self.userOptions.shortcutPath
shortcutFolder = "\\".join(shortcut.split("\\")[:-1])
# create the shortcut folder
if(not pathExist(shortcutFolder)):
print("Creating...")
os.makedirs(shortcutFolder)
if(not pathExist(shortcut)):
print("Creating...")
createShortcut(self.userOptions.vbsFile, shortcut, "steamIcon.ico")
def createVisualElementsManifest(self):
vemFile = self.userOptions.visualElementsManifestFile
vemFolder = self.userOptions.visualElementsFolder
if(not pathExist(vemFile)):
print("Creating...")
with open("templates/template.VisualElementsManifest.xml", "r") as f:
template = f.read()
template = self.applyUserOptionsToData(template)
with open(vemFile, "w") as f:
f.write(template)
if(not pathExist(vemFolder)):
print("Creating...")
os.makedirs(vemFolder)
def createIcons(self):
originalIconPath = self.userOptions.originalIconPath
mediumIconPath = self.userOptions.mediumIconPath
smallIconPath = self.userOptions.smallIconPath
icon = Image.open(originalIconPath)
if(not pathExist(mediumIconPath)):
print("Creating...")
icon.thumbnail((300,300), Image.ANTIALIAS)
icon.save(mediumIconPath)
if(not pathExist(smallIconPath)):
print("Creating...")
icon.thumbnail((150,150), Image.ANTIALIAS)
icon.save(smallIconPath)
del icon
# tileIconifier need those metadata file
def createIconMetadata(self):
iconPath = self.userOptions.originalIconPath
mediumIconMeta = self.userOptions.mediumIconMetaPath
smallIconMeta = self.userOptions.smallIconMetaPath
# open the xml template
template = "templates\\template_Metadata.xml"
elementTree = xml.etree.ElementTree.parse(template)
# write values to it
elementTree.getroot().find("OriginalPath").text = iconPath
elementTree.getroot().find("Height").text = "100"
elementTree.getroot().find("Width").text = "100"
elementTree.write(mediumIconMeta)
elementTree.getroot().find("Height").text = "50"
elementTree.getroot().find("Width").text = "50"
elementTree.write(smallIconMeta)