GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 3f5dce...fb7819 )
by thatsIch
01:08
created

_executable_exists()   A

Complexity

Conditions 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
1
"""
2
This module is about path resolving for the program path.
3
4
This is required to execute Rainmeter.exe for example to refresh the current skin.
5
"""
6
7
import os
8
from functools import lru_cache
9
import winreg
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
    """Check if Rainmeter executable exists."""
48
    # normalize path
49
    rainmeter_exe = os.path.join(rm_path, "Rainmeter.exe")
50
    if not os.path.exists(rainmeter_exe):
51
        message = """Rainmeter path was found, but no Rainmeter.exe found.
52
                     Check if you have installed Rainmeter correctly."""
53
        logger.error(message)
54
        sublime.error_message(message)
55
        return False
56
57
    return True
58
59
60
@lru_cache(maxsize=None)
61
def get_cached_program_path():
62
    """Try to retrieve the program path of RM through magic.
63
64
    Possible options are:
65
66
    * It is given through settings
67
    * It is in the default installation path
68
    * It is registered in the registry
69
    * Ask the user for the path
70
    """
71
    # Load setting
72
    settings = sublime.load_settings("Rainmeter.sublime-settings")
73
    rm_path = settings.get("rainmeter_path", None)
74
75
    # If setting is not set, try default location
76
    if not rm_path:
77
        logger.info("rainmeter_path not found in settings. Trying default location.")
78
        rm_path = get_rm_path_from_default_path()
79
80
    # if it is not even specified by default,
81
    # try using the registry to retrieve the installation path
82
    if not os.path.isdir(rm_path):
83
        rm_path = get_rm_path_from_registry()
84
85
    # Check if path exists and contains Rainmeter.exe
86
    if not os.path.isdir(rm_path):
87
        message = """Path to Rainmeter.exe could neither be found:
88
89
                     * in the settings,
90
                     * in the standard directory,
91
                     * nor via registry.
92
93
                     Check your \"rainmeter_path\" setting."""
94
        logger.info(message)
95
        return
96
97
    if not _executable_exists(rm_path):
98
        return
99
100
    logger.info("Rainmeter found in " + rm_path)
101
    return rm_path + "\\"
102