1
|
|
|
import os.path |
2
|
|
|
|
3
|
|
|
import sublime |
4
|
|
|
|
5
|
|
|
# own dependencies |
6
|
|
|
from . import logger |
7
|
|
|
|
8
|
|
|
from .path.program_path_provider import get_cached_program_path |
9
|
|
|
from .path.setting_path_provider import get_cached_setting_path |
10
|
|
|
from .path.program_drive_provider import get_cached_program_drive |
11
|
|
|
from .path.plugin_path_provider import get_cached_plugin_path |
12
|
|
|
from .path.addon_path_provider import get_cached_addon_path |
13
|
|
|
from .path.skin_path_provider import get_cached_skin_path |
14
|
|
|
from .path.prompt_dialog import browse_file, browse_folder |
15
|
|
|
|
16
|
|
|
|
17
|
|
|
def __require_path(path): |
18
|
|
|
if not os.path.exists(path): |
19
|
|
|
os.makedirs(path) |
20
|
|
|
|
21
|
|
|
|
22
|
|
|
def __copy_on_absence_or_newer(user_path, binary_path): |
23
|
|
|
""" |
24
|
|
|
. |
25
|
|
|
|
26
|
|
|
user_path: path of the file in the user directory |
27
|
|
|
binary_path: Packages/Rainmeter/xxx path inside the package |
28
|
|
|
""" |
29
|
|
|
# could be already copied on a previous run |
30
|
|
|
need_update = not os.path.exists(user_path) |
31
|
|
|
binary = sublime.load_binary_resource(binary_path) |
32
|
|
|
|
33
|
|
|
# could be a newer version there |
34
|
|
|
# only check if no newer is required since expensive |
35
|
|
|
# generally happens in consecutive calls without updates |
36
|
|
|
if not need_update: |
37
|
|
|
need_update = os.path.getsize(user_path) != len(binary) |
38
|
|
|
if need_update: |
39
|
|
|
logger.info("Newer version of color picker found. Copying data over to '" + user_path + "'") |
40
|
|
|
with open(user_path, "wb") as file_handler: |
41
|
|
|
file_handler.write(binary) |
42
|
|
|
else: |
43
|
|
|
logger.info("You are using the most current version of '" + binary_path + "'. Continue loading...") |
44
|
|
|
|
45
|
|
|
|
46
|
|
|
def __handle_program_path_init(): |
47
|
|
|
program_path = get_cached_program_path() |
48
|
|
|
if not program_path: |
49
|
|
|
# Open dialog and set program path. |
50
|
|
|
# Due to cache this might be annoying |
51
|
|
|
# since I cant call cached stuff |
52
|
|
|
# before I can assure that the path is correct |
53
|
|
|
# or just reset the cache. |
54
|
|
|
|
55
|
|
|
def on_rainmeter_exe_browsed(message): |
56
|
|
|
""" |
57
|
|
|
If file is browsed we need to verify that this is the Rainmeter.exe. |
58
|
|
|
If it is we need to reset the cache via. |
59
|
|
|
""" |
60
|
|
|
if os.path.exists(message): |
61
|
|
|
# If file is browsed |
62
|
|
|
# - verify Rainmeter.exe. |
63
|
|
|
# - reset cache |
64
|
|
|
# - save new path in settings |
65
|
|
|
|
66
|
|
|
logger.info("Verified existence of Rainmeter.exe in '" + message + "'.") |
67
|
|
|
settings = sublime.load_settings("Rainmeter.sublime-settings") |
68
|
|
|
rm_dir = os.path.dirname(message) |
69
|
|
|
normed_rm_dir = os.path.normpath(rm_dir) |
70
|
|
|
|
71
|
|
|
settings.set("rainmeter_path", normed_rm_dir) |
72
|
|
|
sublime.save_settings("Rainmeter.sublime-settings") |
73
|
|
|
logger.info("Rewrote settings to include rainmeter path.") |
74
|
|
|
sublime.message_dialog("Successfully set the Rainmeter application path to '" + normed_rm_dir + "'.") |
75
|
|
|
|
76
|
|
|
# we have to reset every cache because all are dependent on the program path |
77
|
|
|
# at least transitively |
78
|
|
|
get_cached_program_path.cache_clear() |
79
|
|
|
get_cached_program_drive.cache_clear() |
80
|
|
|
get_cached_setting_path.cache_clear() |
81
|
|
|
get_cached_skin_path.cache_clear() |
82
|
|
|
get_cached_addon_path.cache_clear() |
83
|
|
|
get_cached_plugin_path.cache_clear() |
84
|
|
|
logger.info("Cleared cache.") |
85
|
|
|
|
86
|
|
|
elif message == "-1": |
87
|
|
|
logger.info("User canceled Rainmeter.exe input.") |
88
|
|
|
|
89
|
|
|
else: |
90
|
|
|
# retry by recalling it again. |
91
|
|
|
logger.info("No valid Rainmeter.exe found. Retrying again.") |
92
|
|
|
browse_file(on_rainmeter_exe_browsed) |
93
|
|
|
|
94
|
|
|
browse_file(on_rainmeter_exe_browsed) |
95
|
|
|
|
96
|
|
|
|
97
|
|
|
def __handle_skin_path_init(): |
98
|
|
|
skin_path = get_cached_skin_path() |
99
|
|
|
if not skin_path: |
100
|
|
|
# Open folder dialog and set skin path. |
101
|
|
|
|
102
|
|
|
def on_skins_folder_browsed(skin_dir): |
103
|
|
|
""".""" |
104
|
|
|
logger.info("skin_dir: " + skin_dir) |
105
|
|
|
if os.path.exists(skin_dir): |
106
|
|
|
# If folder is browsed: |
107
|
|
|
# - verify skin folder |
108
|
|
|
# - reset cache |
109
|
|
|
# - save new path in settings |
110
|
|
|
|
111
|
|
|
logger.info("Verified existence of Rainmeter Skin folder in '" + skin_dir + "'.") |
112
|
|
|
settings = sublime.load_settings("Rainmeter.sublime-settings") |
113
|
|
|
normed_skin_dir = os.path.normpath(skin_dir) |
114
|
|
|
|
115
|
|
|
settings.set("rainmeter_skins_path", normed_skin_dir) |
116
|
|
|
sublime.save_settings("Rainmeter.sublime-settings") |
117
|
|
|
logger.info("Rewrote settings to include skin path.") |
118
|
|
|
sublime.message_dialog("Successfully set the Rainmeter Skins path to '" + skin_dir + "'.") |
119
|
|
|
|
120
|
|
|
# we have to reset every cache because all are dependent on the program path |
121
|
|
|
# at least transitively |
122
|
|
|
get_cached_skin_path.cache_clear() |
123
|
|
|
logger.info("Cleared cache.") |
124
|
|
|
|
125
|
|
|
elif skin_dir == "-1": |
126
|
|
|
logger.info("User canceled Rainmeter Skin folder input.") |
127
|
|
|
|
128
|
|
|
else: |
129
|
|
|
# retry by recalling it again. |
130
|
|
|
logger.info("No valid Rainmeter Skin folder found. Retrying again.") |
131
|
|
|
browse_folder(on_skins_folder_browsed) |
132
|
|
|
|
133
|
|
|
browse_folder(on_skins_folder_browsed) |
134
|
|
|
|
135
|
|
|
|
136
|
|
|
def plugin_loaded(): |
137
|
|
|
""" |
138
|
|
|
Called automatically from ST3 if plugin is loaded. |
139
|
|
|
|
140
|
|
|
Is required now due to async call and ignoring sublime.* from main routine |
141
|
|
|
""" |
142
|
|
|
|
143
|
|
|
packages = sublime.packages_path() |
144
|
|
|
prompt_dir = os.path.join(packages, "User", "Rainmeter", "path") |
145
|
|
|
|
146
|
|
|
__require_path(prompt_dir) |
147
|
|
|
|
148
|
|
|
open_file_dialog_bat = os.path.join(prompt_dir, "open_file_dialog.ps1") |
149
|
|
|
open_folder_dialog_bat = os.path.join(prompt_dir, "open_folder_dialog.ps1") |
150
|
|
|
|
151
|
|
|
__copy_on_absence_or_newer(open_file_dialog_bat, "Packages/Rainmeter/path/open_file_dialog.ps1") |
152
|
|
|
__copy_on_absence_or_newer(open_folder_dialog_bat, "Packages/Rainmeter/path/open_folder_dialog.ps1") |
153
|
|
|
|
154
|
|
|
__handle_program_path_init() |
155
|
|
|
__handle_skin_path_init() |
156
|
|
|
|
157
|
|
|
padding = 16 |
158
|
|
|
logger.info("#PROGRAMPATH#:".ljust(padding) + get_cached_program_path()) # Rainmeter.exe |
159
|
|
|
logger.info("#PROGRAMDRIVE#:".ljust(padding) + get_cached_program_drive()) |
160
|
|
|
logger.info("#SETTINGSPATH#:".ljust(padding) + get_cached_setting_path()) # Rainmeter.ini |
161
|
|
|
logger.info("#SKINSPATH#:".ljust(padding) + get_cached_skin_path()) # Rainmeter/Skins path |
162
|
|
|
logger.info("#PLUGINSPATH#:".ljust(padding) + get_cached_plugin_path()) |
163
|
|
|
logger.info("#ADDONSPATH#:".ljust(padding) + get_cached_addon_path()) |
164
|
|
|
|