|
1
|
|
|
import os |
|
|
|
|
|
|
2
|
|
|
import winreg |
|
3
|
|
|
|
|
4
|
|
|
from functools import lru_cache |
|
5
|
|
|
|
|
6
|
|
|
import sublime |
|
7
|
|
|
|
|
8
|
|
|
from .. import logger |
|
|
|
|
|
|
9
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
def _get_rainmeter_path_from_default_path(): |
|
|
|
|
|
|
12
|
|
|
""" |
|
|
|
|
|
|
13
|
|
|
Default location is "C:\Program Files\Rainmeter" in windows |
|
14
|
|
|
we can get "C:\Program Files" through the environmental variables |
|
15
|
|
|
%PROGRAMFILES% |
|
16
|
|
|
""" |
|
17
|
|
|
programfiles = os.getenv("PROGRAMFILES") |
|
18
|
|
|
rainmeterpath = os.path.join(programfiles, "Rainmeter") |
|
19
|
|
|
|
|
20
|
|
|
return rainmeterpath |
|
21
|
|
|
|
|
22
|
|
|
|
|
23
|
|
|
def _get_rainmeter_registry_key(): |
|
|
|
|
|
|
24
|
|
|
try: |
|
25
|
|
|
return winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, "SOFTWARE\\WOW6432Node\\Rainmeter") |
|
26
|
|
|
except FileNotFoundError: |
|
27
|
|
|
import sys |
|
28
|
|
|
|
|
29
|
|
|
is_64bits = sys.maxsize > 2**32 |
|
30
|
|
|
if is_64bits: |
|
31
|
|
|
other_view_flag = winreg.KEY_WOW64_32KEY |
|
32
|
|
|
else: |
|
33
|
|
|
other_view_flag = winreg.KEY_WOW64_64KEY |
|
34
|
|
|
|
|
35
|
|
|
try: |
|
36
|
|
|
return winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, "SOFTWARE\\WOW6432Node\\Rainmeter", access=winreg.KEY_READ | other_view_flag) |
|
37
|
|
|
except FileNotFoundError: |
|
38
|
|
|
''' |
|
39
|
|
|
We really could not find the key in both views. |
|
40
|
|
|
''' |
|
|
|
|
|
|
41
|
|
|
return None |
|
42
|
|
|
|
|
43
|
|
|
|
|
44
|
|
|
def _get_rainmeter_path_from_registry(): |
|
|
|
|
|
|
45
|
|
|
""" |
|
46
|
|
|
Registry |
|
47
|
|
|
""" |
|
48
|
|
|
regkey = __get_rainmeter_registry_key() |
|
|
|
|
|
|
49
|
|
|
if regkey: |
|
50
|
|
|
keyval = winreg.QueryValueEx(regkey, "Personal") |
|
51
|
|
|
|
|
52
|
|
|
for i in range(1024): |
|
53
|
|
|
try: |
|
54
|
|
|
asubkey_name = winreg.EnumKey(keyval, i) |
|
55
|
|
|
asubkey = winreg.OpenKey(keyval, asubkey_name) |
|
56
|
|
|
rainmeterpath = winreg.QueryValueEx(asubkey, "DisplayName") |
|
57
|
|
|
logger.info(__file__, "get_cached_program_path()", "found rainmeter path through registry: " + rainmeterpath) |
|
58
|
|
|
if rainmeterpath: |
|
59
|
|
|
return rainmeterpath |
|
60
|
|
|
except EnvironmentError: |
|
61
|
|
|
break |
|
62
|
|
|
|
|
63
|
|
|
return None |
|
64
|
|
|
|
|
65
|
|
|
|
|
66
|
|
|
@lru_cache(maxsize=None) |
|
67
|
|
|
def get_cached_program_path(): |
|
|
|
|
|
|
68
|
|
|
# Load setting |
|
69
|
|
|
settings = sublime.load_settings("Rainmeter.sublime-settings") |
|
70
|
|
|
rainmeterpath = settings.get("rainmeter_path", None) |
|
71
|
|
|
|
|
72
|
|
|
# If setting is not set, try default location |
|
73
|
|
|
if not rainmeterpath: |
|
74
|
|
|
logger.info( |
|
75
|
|
|
__file__, |
|
76
|
|
|
"get_cached_program_path()", |
|
77
|
|
|
"rainmeter_path not found in settings. Trying default location." |
|
78
|
|
|
) |
|
79
|
|
|
rainmeterpath = _get_rainmeter_path_from_default_path() |
|
80
|
|
|
|
|
81
|
|
|
# if it is not even specified by default, try using the registry to retrieve the installation path |
|
82
|
|
|
if not os.path.isdir(rainmeterpath): |
|
83
|
|
|
rainmeterpath = _get_rainmeter_path_from_registry() |
|
84
|
|
|
|
|
85
|
|
|
# Check if path exists and contains Rainmeter.exe |
|
86
|
|
|
if not os.path.isdir(rainmeterpath): |
|
87
|
|
|
message = "Path to Rainmeter.exe could neither be found in the standard directory nor via registry. Check your \"rainmeter_path\" setting." |
|
88
|
|
|
logger.info(__file__, "get_cached_program_path()", message) |
|
89
|
|
|
sublime.error_message(message) |
|
90
|
|
|
return |
|
91
|
|
|
|
|
92
|
|
|
# normalize path |
|
93
|
|
|
rainmeter_exe = os.path.join(rainmeterpath, "Rainmeter.exe") |
|
94
|
|
|
if not os.path.exists(rainmeter_exe): |
|
95
|
|
|
message = "Rainmeter path was found, but no Rainmeter.exe found. Check if you have correctly installed Rainmeter." |
|
96
|
|
|
logger.error(__file__, "get_cached_program_path()", message) |
|
97
|
|
|
sublime.error_message(message) |
|
98
|
|
|
return |
|
99
|
|
|
|
|
100
|
|
|
logger.info(__file__, "get_cached_program_path()", "Rainmeter found in " + rainmeterpath) |
|
101
|
|
|
return rainmeterpath + "\\" |
|
102
|
|
|
|
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.