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 ( e8485a...be6ed5 )
by thatsIch
01:08
created

_guess_documents_path()   A

Complexity

Conditions 2

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
c 1
b 0
f 0
dl 0
loc 19
rs 9.4285
1
"""
2
This module is about path resolving for the skin path.
3
4
This is particular useful if you for example try create new skins
5
or open an existing one.
6
"""
7
8
import getpass
9
import io
10
import os
11
import platform
12
import re
13
from functools import lru_cache
14
import winreg
15
16
import sublime
17
18
from .. import logger
19
20
from .program_path_provider import get_cached_program_path
21
from .setting_path_provider import get_cached_setting_path
22
23
24
def get_path_from_st_settings():
25
    """Load the value from the "rainmeter_skins_path" setting."""
26
    loaded_settings = sublime.load_settings("Rainmeter.sublime-settings")
27
    skinspath = loaded_settings.get("rainmeter_skins_path", None)
28
29
    # if it's found, return it
30
    # We trust the user to enter something meaningful here
31
    # and don't check anything.
32
    if skinspath:
33
        logger.info("Skins path '" + skinspath + "' found in sublime-settings file.")
34
        return os.path.normpath(skinspath) + "\\"
35
36
37
def get_path_from_rm_settings(rm_path, settings_path):
38
    """We check the Rainmeter.ini for the installation path."""
39
    # If it's not set, try to detect it automagically
40
    if not rm_path or not settings_path:
41
        return
42
43
    # First, try to read the SkinPath setting from Rainmeter.ini
44
    fhnd = io.open(os.path.join(settings_path, "Rainmeter.ini"))
45
    lines = fhnd.read()
46
    fhnd.close()
47
48
    # Find the skinspath setting in the file
49
    match = re.search(r"""(?imsx)
50
51
                     # Find the first [Rainmeter] section
52
                     (^\s*\[\s*Rainmeter\s*\]\s*$)
53
                     (.*?
54
55
                         # Find the "SkinPath" and "="
56
                         (^\s*SkinPath\s*=\s*
57
58
                             # Read until the next line ending and store
59
                             # in named group
60
                             (?P<skinpath>[^$]+?)\s*?$
61
                         )
62
                     ).*?
63
64
                     # All of this needs to happen before the next section
65
                     (?:^\s*\[\s*[^\[\]\s]+\s*\]\s*$)
66
                     """, lines)
67
68
    # if skinspath setting was found, return it
69
    if match:
70
        logger.info("Skins path found in Rainmeter.ini.")
71
        return match.group("skinpath").strip().replace("/", "\\")
72
73
74
def get_path_from_portable_rm(rm_path, settings_path):
75
    """
76
    If program path and setting path are equal, we have a portable installation.
77
78
    In this case, the Skins folder is inside the rainmeter path.
79
    """
80
    if os.path.samefile(rm_path, settings_path):
81
        logger.info("Skin path found in #PROGRAMPATH#" +
82
                    " because portable installation")
83
        return os.path.join(rm_path, "Skins") + "\\"
84
85
86
def get_path_from_registry():
87
    """
88
    If it's not a portable installation, we try looking into the "My Documents" folder.
89
90
    Since it could be relocated by the user, we have to query its value from the registry."""
91
    try:
92
        regkey = winreg.OpenKey(winreg.HKEY_CURRENT_USER,
93
                                r"Software\Microsoft\Windows" +
94
                                r"\CurrentVersion\Explorer" +
95
                                r"\User Shell Folders")
96
        keyval = winreg.QueryValueEx(regkey, "Personal")
97
98
        pathrep = keyval[0]
99
100
        # The path could (and most likely, will) contain environment
101
        # variables that have to be expanded first
102
        pathrep = os.path.expandvars(pathrep)
103
104
        logger.info("Guessed Skin path from My Documents" +
105
                    " location in registry")
106
        return os.path.join(pathrep, "Rainmeter\\Skins") + "\\"
107
108
    except OSError:
0 ignored issues
show
Unused Code introduced by
This except handler seems to be unused and could be removed.

Except handlers which only contain pass and do not have an else clause can usually simply be removed:

try:
    raises_exception()
except:  # Could be removed
    pass
Loading history...
109
        pass
110
111
112
def _guess_documents_path(username):
0 ignored issues
show
Coding Style introduced by
This function should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
113
    # check if windows version is XP
114
    winversion = platform.version()
115
    if int(winversion[0]) < 6:
116
        mydocuments = os.path.join("C:\\Documents and Settings",
117
                                   username,
118
                                   "My Documents") + "\\"
119
120
        logger.info("Found Windows XP or lower." +
121
                    " Skins path assumed to be " + mydocuments +
122
                    "Rainmeter\\Skins\\")
123
    else:
124
        mydocuments = os.path.join("C:\\Users",
125
                                   username,
126
                                   "Documents") + "\\"
127
128
        logger.info("Found Windows Vista or higher." +
129
                    " Skins path assumed to be " + mydocuments +
130
                    "Rainmeter\\Skins\\")
131
132
133
def guess_path_from_user_documents():
134
    """If the value could not be retrieved from the registry, we try some educated guesses about default locations."""
135
136
    try:
137
        username = getpass.getuser()
138
    except Exception:  # pylint: disable=W0703; The docs to not specify the exception type
0 ignored issues
show
Best Practice introduced by
Catching very general exceptions such as Exception is usually not recommended.

Generally, you would want to handle very specific errors in the exception handler. This ensure that you do not hide other types of errors which should be fixed.

So, unless you specifically plan to handle any error, consider adding a more specific exception.

Loading history...
139
        logger.info("Skins path could not be located." +
140
                    " Please set the \"skins_path\" setting in your Rainmeter" +
141
                    " settings file.")
142
        return
143
    else:
144
        mydocuments = _guess_documents_path(username)
0 ignored issues
show
Bug introduced by
Assigning to function call which doesn't return
Loading history...
145
146
        logger.info("Skin path guessed from user name" +
147
                    " and Windows version")
148
        return os.path.join(mydocuments, "Rainmeter\\Skins") + "\\"
149
150
151
@lru_cache(maxsize=None)
152
def get_cached_skin_path():
153
    """
154
    Get the value of the #SKINSPATH# variable.
155
156
    This can be collected from various installation locations,
157
    since there are numerous ways to install Rainmeter.
158
159
    The easiest solution is, if the user tells Sublime Rainmeter,
160
    that he installed Rainmeter in a specific folder. We trust,
161
    that the user knows where he installed it.
162
163
    If we know, where the rainmeter settings are,
164
    we can parse that from the Raimeter.ini.
165
166
    If the user chose a portable installation,
167
    it is also directly in the Rainmeter installation path.
168
169
    If the user chose a default installation,
170
    and enabled Rainmeter to store the information in the registry,
171
    we can use that to determine the path.
172
173
    If all fails we start guessing from the user document folder upwards.
174
    """
175
176
    rm_path = get_cached_program_path()
177
    settings_path = get_cached_setting_path()
178
179
    return get_path_from_st_settings() \
180
        or get_path_from_rm_settings(rm_path, settings_path) \
181
        or get_path_from_portable_rm(rm_path, settings_path) \
182
        or get_path_from_registry() \
183
        or guess_path_from_user_documents()
184