1
|
|
|
"""This module is about path resolving for the program path. |
2
|
|
|
|
3
|
|
|
This is required to execute Rainmeter.exe for example to refresh the current skin. |
4
|
|
|
""" |
5
|
|
|
|
6
|
|
|
import os |
7
|
|
|
import winreg |
8
|
|
|
|
9
|
|
|
from functools import lru_cache |
10
|
|
|
|
11
|
|
|
import sublime |
12
|
|
|
|
13
|
|
|
from .. import logger |
|
|
|
|
14
|
|
|
|
15
|
|
|
|
16
|
|
|
def _get_rm_path_from_default_path(): |
17
|
|
|
r""" |
18
|
|
|
Default location is "C:\Program Files\Rainmeter" in windows. |
19
|
|
|
|
20
|
|
|
We can get "C:\Program Files" through the environmental variables |
21
|
|
|
%PROGRAMFILES% |
22
|
|
|
""" |
23
|
|
|
programfiles = os.getenv("PROGRAMFILES") |
24
|
|
|
rainmeterpath = os.path.join(programfiles, "Rainmeter") |
25
|
|
|
|
26
|
|
|
return rainmeterpath |
27
|
|
|
|
28
|
|
|
|
29
|
|
|
def _get_rainmeter_registry_key(): |
30
|
|
|
r""" |
31
|
|
|
Throw FileNotFoundException if Software\WOW6432Node\Rainmeter does not exist. |
32
|
|
|
|
33
|
|
|
not much we can do to handle that |
34
|
|
|
""" |
35
|
|
|
return winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, "SOFTWARE\\WOW6432Node\\Rainmeter") |
36
|
|
|
|
37
|
|
|
|
38
|
|
|
def _get_rm_path_from_registry(): |
39
|
|
|
"""Registry.""" |
40
|
|
|
rainmeter_key = _get_rainmeter_registry_key() |
41
|
|
|
rainmeter_path = winreg.QueryValue(rainmeter_key, None) |
42
|
|
|
|
43
|
|
|
return rainmeter_path |
44
|
|
|
|
45
|
|
|
|
46
|
|
|
def __executable_exists(rm_path): |
|
|
|
|
47
|
|
|
# normalize path |
48
|
|
|
rainmeter_exe = os.path.join(rm_path, "Rainmeter.exe") |
49
|
|
|
if not os.path.exists(rainmeter_exe): |
50
|
|
|
message = """Rainmeter path was found, but no Rainmeter.exe found. |
51
|
|
|
Check if you have installed Rainmeter correctly.""" |
52
|
|
|
logger.error(__file__, "get_cached_program_path()", message) |
53
|
|
|
sublime.error_message(message) |
54
|
|
|
return False |
55
|
|
|
|
56
|
|
|
return True |
57
|
|
|
|
58
|
|
|
@lru_cache(maxsize=None) |
59
|
|
|
def get_cached_program_path(): |
60
|
|
|
"""Try to retrieve the program path of RM through magic. |
61
|
|
|
|
62
|
|
|
Possible options are: |
63
|
|
|
|
64
|
|
|
* It is given through settings |
65
|
|
|
* It is in the default installation path |
66
|
|
|
* It is registered in the registry |
67
|
|
|
* Ask the user for the path |
68
|
|
|
""" |
69
|
|
|
# Load setting |
70
|
|
|
settings = sublime.load_settings("Rainmeter.sublime-settings") |
71
|
|
|
rm_path = settings.get("rainmeter_path", None) |
72
|
|
|
|
73
|
|
|
# If setting is not set, try default location |
74
|
|
|
if not rm_path: |
75
|
|
|
logger.info( |
76
|
|
|
__file__, |
77
|
|
|
"get_cached_program_path()", |
78
|
|
|
"rainmeter_path not found in settings. Trying default location." |
79
|
|
|
) |
80
|
|
|
rm_path = _get_rm_path_from_default_path() |
81
|
|
|
|
82
|
|
|
# if it is not even specified by default, |
83
|
|
|
# try using the registry to retrieve the installation path |
84
|
|
|
if not os.path.isdir(rm_path): |
85
|
|
|
rm_path = _get_rm_path_from_registry() |
86
|
|
|
|
87
|
|
|
# Check if path exists and contains Rainmeter.exe |
88
|
|
|
if not os.path.isdir(rm_path): |
89
|
|
|
message = """Path to Rainmeter.exe could neither be found: |
90
|
|
|
|
91
|
|
|
* in the settings, |
92
|
|
|
* in the standard directory, |
93
|
|
|
* nor via registry. |
94
|
|
|
|
95
|
|
|
Check your \"rainmeter_path\" setting.""" |
96
|
|
|
logger.info(__file__, "get_cached_program_path()", message) |
97
|
|
|
sublime.error_message(message) |
98
|
|
|
return |
99
|
|
|
|
100
|
|
|
if not __executable_exists(rm_path): |
101
|
|
|
return |
102
|
|
|
|
103
|
|
|
logger.info(__file__, "get_cached_program_path()", "Rainmeter found in " + rm_path) |
104
|
|
|
return rm_path + "\\" |
105
|
|
|
|