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.

RainmeterNewSkinCommand.create_skin()   D
last analyzed

Complexity

Conditions 10

Size

Total Lines 65

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
dl 0
loc 65
rs 4.5
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Complexity

Complex classes like RainmeterNewSkinCommand.create_skin() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
"""
2
This module provides commands to create new Rainmeter skins.
3
4
This can be either by adding a new skin file to an existing/current skin
5
or by create a whole new skin in general.
6
"""
7
8
9
import os
10
import re
11
12
import sublime
13
import sublime_plugin
14
15
from .path.skin_path_provider import get_cached_skin_path
16
17
18
class RainmeterNewSkinFileCommand(sublime_plugin.WindowCommand):  # pylint: disable=R0903; sublime text API, methods are overriden
19
    """Open a new view and insert a skin skeleton."""
20
21
    def run(self):
22
        """Called when the command is run."""
23
        view = self.window.new_file()
24
        view.run_command(
25
            "insert_snippet",
26
            {"name": "Packages/Rainmeter/Snippets/skin.sublime-snippet"})
27
28
        if os.path.exists("Packages/User/Rainmeter.sublime-syntax"):
29
            view.set_syntax_file("Packages/User/Rainmeter.sublime-syntax")
30
        else:
31
            view.set_syntax_file("Packages/Rainmeter/Rainmeter.sublime-syntax")
32
33
34
class RainmeterNewSkinCommand(sublime_plugin.WindowCommand):
35
    """
36
    Create a new skin, complete with folders, open it and refresh Rainmeter.
37
38
    Prompts the user for the name of a skin and creates a new skin of that
39
    name in the skins folder, if it doesn't already exist. Then opens the skin
40
    file, inserts a basic skin skeleton and refreshes Rainmeter.
41
    """
42
43
    def run(self):
44
        """Called when the command is run."""
45
        self.window.show_input_panel("Enter Skin Name:",
46
                                     "",
47
                                     self.create_skin,
48
                                     None,
49
                                     None)
50
51
    def create_skin(self, name):
52
        """Callback method executed after the user entered the skin name."""
53
        skinspath = get_cached_skin_path()
54
        if not skinspath or not os.path.exists(skinspath):
55
            sublime.error_message(
56
                "Error while trying to create new skin: " +
57
                "Skins path could not be found. Please check the value" +
58
                " of your \"skins_path\" setting.")
59
            return
60
61
        name = os.path.normpath(name.strip("\\").strip("/")) + "\\"
62
63
        # Path where the new ini file will be created
64
        newskinpath = os.path.join(skinspath, name)
65
66
        # Path where the @Resources folder should be created
67
        basepath = os.path.join(skinspath,
68
                                re.match("(.*?)\\\\", name).group(1))
69
70
        try:
71
            os.makedirs(newskinpath)
72
        except os.error:
73
            sublime.error_message(
74
                "Error while trying to create new skin: " +
75
                "Directory " + newskinpath + " could not be created. " +
76
                "Does it already exist?")
77
            return
78
79
        # Check which folders should be created
80
        settings = sublime.load_settings("Rainmeter.sublime-settings")
81
        make_resources = settings.get(
82
            "rainmeter_new_skin_create_resources_folder",
83
            True)
84
        make_images = settings.get(
85
            "rainmeter_new_skin_create_images_folder",
86
            True)
87
        make_fonts = settings.get(
88
            "rainmeter_new_skin_create_fonts_folder",
89
            True)
90
        make_scripts = settings.get(
91
            "rainmeter_new_skin_create_scripts_folder",
92
            True)
93
94
        try:
95
            if make_resources:
96
                os.makedirs(os.path.join(basepath, "@Resources"))
97
                if make_images:
98
                    os.makedirs(os.path.join(basepath, "@Resources\\Images"))
99
                if make_fonts:
100
                    os.makedirs(os.path.join(basepath, "@Resources\\Fonts"))
101
                if make_scripts:
102
                    os.makedirs(os.path.join(basepath, "@Resources\\Scripts"))
103
        except os.error:
104
            sublime.status_message("Did not create @Resources folder or" +
105
                                   " subfolders because they already exist")
106
107
        window = self.window
108
        filename = os.path.basename(os.path.normpath(name))
109
        open(os.path.join(newskinpath, filename + ".ini"), 'a')
110
        newview = window.open_file(os.path.join(newskinpath,
111
                                                filename + ".ini"))
112
113
        # We have to wait until the file is fully loaded (even if it's empty
114
        # because it was just created)
115
        sublime.set_timeout((lambda: self.open_skin_file(newview)), 100)
116
117
    def open_skin_file(self, view):
118
        """Callback method executed after the file is fully loaded."""
119
        if view.is_loading():
120
            sublime.set_timeout(lambda: self.open_skin_file(view),
121
                                100)
122
            return
123
124
        view.run_command(
125
            "insert_snippet",
126
            {"name": "Packages/Rainmeter/Snippets/skin.sublime-snippet"})
127
128
        if os.path.exists("Packages/User/Rainmeter.sublime-syntax"):
129
            view.set_syntax_file("Packages/User/Rainmeter.sublime-syntax")
130
        else:
131
            view.set_syntax_file("Packages/Rainmeter/Rainmeter.sublime-syntax")
132
133
        sublime.run_command("rainmeter_refresh")
134