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
Branch master (178204)
by thatsIch
01:08
created

install_into_skins_folder()   A

Complexity

Conditions 1

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
c 0
b 0
f 0
dl 0
loc 21
rs 9.3142
1
"""This module handles the business logic to install skins from a local directory."""
2
3
import os
4
import re
5
import shutil
6
7
from ..path.skin_path_provider import get_cached_skin_path
8
9
10
def folder_already_exists(skin_folder):
11
    """
12
    Checks if the folder already exists.
13
14
    This is to check if a skin is already installed determined by the skin folder.
15
    This enables us to switch in case the user does not want to overwrite the current skin.
16
    """
17
    skins_folder = get_cached_skin_path()
18
    inis = find_inis_in_folder(skin_folder)
19
    skin_name = os.path.basename(common_path(inis))
20
    target_skin_folder = os.path.join(skins_folder, skin_name)
21
22
    return os.path.exists(target_skin_folder)
23
24
25
def install_into_skins_folder(skin_folder):
26
    """
27
    Install skin folder into Rainmeter skins folder.
28
29
    Main entry point to the from_folder module.
30
    """
31
    skins_folder = get_cached_skin_path()
32
33
    inis = find_inis_in_folder(skin_folder)
34
    skin_name = os.path.basename(common_path(inis))
35
    resources_folders = find_resources_folders_in_folder(skin_folder)
36
37
    paths = []
38
    paths.extend(inis)
39
    paths.extend(resources_folders)
40
41
    target_skin_folder = os.path.join(skins_folder, skin_name)
42
43
    transposed_paths = transpose_paths(paths, target_skin_folder)
44
45
    return transposed_paths
46
47
48
def transpose_paths(paths, target):
49
    """
50
    Transpose a subtree to a new location.
51
52
    With that we can determine a root node which acts as a source folder
53
    and from which we can copy everything recursively to the target folder.
54
    """
55
    commoner = common_path(paths)
56
57
    return shutil.copytree(commoner, target)
58
59
60
def common_path(paths):
61
    """
62
    Find skin root folder.
63
64
    The root folder is defined as the parent folder of the @Resources folder.
65
    Since this one is optional the next solution would be to use the least common parent from all inis
66
    """
67
    return os.path.dirname(os.path.commonprefix([p + os.path.sep for p in paths]))
68
69
70
def find_resources_folders_in_folder(folder):
0 ignored issues
show
Coding Style Naming introduced by
The name find_resources_folders_in_folder does not conform to the function naming conventions ([a-z_][a-z0-9_]{2,30}$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
71
    """
72
    Find @Resources folders in the given folder.
73
74
    Is case insensitive.
75
    Will search for every @Resources folder found in the folder (e.g. in case of multi skin config).
76
    """
77
    resources = []
78
    for root, directories, dummy_files in os.walk(folder):
79
        for directory in directories:
80
            if directory.lower() == "@resources":
81
                resources.append(os.path.join(os.path.abspath(root), directory))
82
83
    return resources
84
85
86
def find_resources_folder_in_folder(folder):
87
    """
88
    Find a single @Resources folder in the given folder.
89
    """
90
    for root, directories, dummy_files in os.walk(folder):
91
        for directory in directories:
92
            if directory.lower() == "@resources":
93
                return os.path.join(os.path.abspath(root), directory)
94
95
96
NAME_PATTERN = re.compile(r"^\s*Name=(.+)$", re.IGNORECASE)
97
98
99
def find_skin_name_in_inis(inis):
100
    """
101
    Retrieve skin name in a configuration.
102
103
    A configuration can contain multiple skins.
104
    Each of them can contain a metadata with its real name,
105
    since due to the copying or zipping it could be skewed
106
    with informations like master or versioning.
107
    """
108
    for ini in inis:
109
        with open(ini, 'r') as ini_file_handler:
110
            for line in ini_file_handler:
111
                match = NAME_PATTERN.match(line)
112
                if match:
113
                    return match.group(1)
114
115
116
def find_inis_in_folder(folder):
117
    """
118
    Retrieve path of every file ending with .ini in folder.
119
120
    Returns the absolute path of each found file.
121
    """
122
    inis = []
123
124
    for root, dummy_dirs, files in os.walk(folder):
125
        for fil in files:
126
            if fil.endswith('.ini'):
127
                inis.append(os.path.join(os.path.abspath(root), fil))
128
129
    return inis
130