|
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
|
|
|
""" |
|
|
|
|
|
|
25
|
|
|
throws FileNotFoundException if Software\WOW6432Node\Rainmeter does not exist |
|
26
|
|
|
not much we can do to handle that |
|
27
|
|
|
""" |
|
28
|
|
|
return winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, "SOFTWARE\\WOW6432Node\\Rainmeter") |
|
29
|
|
|
|
|
30
|
|
|
|
|
31
|
|
|
def _get_rainmeter_path_from_registry(): |
|
|
|
|
|
|
32
|
|
|
""" |
|
33
|
|
|
Registry |
|
34
|
|
|
""" |
|
35
|
|
|
rainmeter_key = _get_rainmeter_registry_key() |
|
36
|
|
|
rainmeter_path = winreg.QueryValue(rainmeter_key, None) |
|
37
|
|
|
|
|
38
|
|
|
return rainmeter_path |
|
39
|
|
|
|
|
40
|
|
|
|
|
41
|
|
|
@lru_cache(maxsize=None) |
|
42
|
|
|
def get_cached_program_path(): |
|
|
|
|
|
|
43
|
|
|
# Load setting |
|
44
|
|
|
settings = sublime.load_settings("Rainmeter.sublime-settings") |
|
45
|
|
|
rm_path = settings.get("rainmeter_path", None) |
|
46
|
|
|
|
|
47
|
|
|
# If setting is not set, try default location |
|
48
|
|
|
if not rm_path: |
|
49
|
|
|
logger.info( |
|
50
|
|
|
__file__, |
|
51
|
|
|
"get_cached_program_path()", |
|
52
|
|
|
"rainmeter_path not found in settings. Trying default location." |
|
53
|
|
|
) |
|
54
|
|
|
rm_path = _get_rainmeter_path_from_default_path() |
|
55
|
|
|
|
|
56
|
|
|
# if it is not even specified by default, try using the registry to retrieve the installation path |
|
57
|
|
|
if not os.path.isdir(rm_path): |
|
58
|
|
|
rm_path = _get_rainmeter_path_from_registry() |
|
59
|
|
|
|
|
60
|
|
|
# Check if path exists and contains Rainmeter.exe |
|
61
|
|
|
if not os.path.isdir(rm_path): |
|
62
|
|
|
message = "Path to Rainmeter.exe could neither be found in the standard directory nor via registry. Check your \"rainmeter_path\" setting." |
|
63
|
|
|
logger.info(__file__, "get_cached_program_path()", message) |
|
64
|
|
|
sublime.error_message(message) |
|
65
|
|
|
return |
|
66
|
|
|
|
|
67
|
|
|
# normalize path |
|
68
|
|
|
rainmeter_exe = os.path.join(rm_path, "Rainmeter.exe") |
|
69
|
|
|
if not os.path.exists(rainmeter_exe): |
|
70
|
|
|
message = "Rainmeter path was found, but no Rainmeter.exe found. Check if you have correctly installed Rainmeter." |
|
71
|
|
|
logger.error(__file__, "get_cached_program_path()", message) |
|
72
|
|
|
sublime.error_message(message) |
|
73
|
|
|
return |
|
74
|
|
|
|
|
75
|
|
|
logger.info(__file__, "get_cached_program_path()", "Rainmeter found in " + rm_path) |
|
76
|
|
|
return rm_path + "\\" |
|
77
|
|
|
|
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.