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 ( 3de7ad...d0b14b )
by thatsIch
55s
created

SkinSectionAutoCompleter.__get_completions()   A

Complexity

Conditions 2

Size

Total Lines 14

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 14
rs 9.4285
1
"""This module handles the skin section auto completion.
2
3
This provides the means to automatically add the base rainmeter sections
4
upon auto complete request with:
5
6
* [Rainmeter]
7
* [Metadata]
8
* [Variables]
9
* [Measure]
10
* [Meter]
11
* [MeterStyle]
12
13
This only activates if the file is empty or at least 2 new lines above the current caret.
14
"""
15
16
import yaml
17
18
import sublime
19
20
from .. import logger
0 ignored issues
show
Bug introduced by
The name logger does not seem to exist in module completion.
Loading history...
21
from .levenshtein import levenshtein
22
from .yaml_content_reader import YamlContentReader
23
24
25
class SkinSectionAutoCompleter(YamlContentReader):
26
    """Ths class is the logical state holder for the auto completion suggestions.
27
28
    Upon the request the respective yaml file is parsed and converted into a logical
29
    representation of the completions. Depending on the prior information the completions
30
    can be filtered containing less entries.
31
    """
32
33
    def __get_completions(self):
34
        """IO access to the yaml file.
35
36
        Uses a yaml loader to parse it into a python object.
37
        """
38
        try:
39
            section_content = self._get_yaml_content("completion/", "section.yaml")
40
            section = yaml.load(section_content)
41
42
            return section
43
44
        except yaml.YAMLError as error:
45
            logger.error(__file__, "__get_completions(self)", error)
46
            return []
47
48
    def __get_compiled_key_completions(self, options):
0 ignored issues
show
Coding Style introduced by
This method could be written as a function/class method.

If a method does not access any attributes of the class, it could also be implemented as a function or static method. This can help improve readability. For example

class Foo:
    def some_method(self, x, y):
        return x + y;

could be written as

class Foo:
    @classmethod
    def some_method(cls, x, y):
        return x + y;
Loading history...
49
        """Completion can contain lots of duplicate information.
50
51
        For example the trigger is most of the time also the result.
52
        Only in case of a value attribute is that returned.
53
        It also takes hints into consideration for compilation.
54
        """
55
        keys = []
56
        for option in options:
57
            title = option['title'] + "\t" + option['hint']
58
59
            if 'value' in option:
60
                result = option['value']
61
            else:
62
                result = option['title']
63
64
            pair = (title, result)
65
            keys.append(pair)
66
67
        return keys
68
69
    def __lazy_initialize_completions(self):
0 ignored issues
show
Coding Style introduced by
This method 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...
70
        # use lazy initialization because else the API is not available yet
71
        if not self.all_completions:
72
            self.all_completions = self.__get_completions()
73
            self.all_key_completions = self.__get_compiled_key_completions(self.all_completions)
74
75
    def __filter_completions_by_already_defined_sections(self, sections):
0 ignored issues
show
Coding Style Naming introduced by
The name __filter_completions_by_already_defined_sections does not conform to the method 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...
Coding Style introduced by
This method 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...
76
        # filter by already existing keys
77
        completions = []
78
79
        for completion in self.all_key_completions:
80
            # trigger is not used here
81
            _, content = completion
82
83
            contained = 0
84
            # value not used here
85
            for section in sections:
86
                if section.casefold() == content.casefold():
87
                    contained = 1
88
                    break
89
90
            if contained == 0:
91
                completions.append(completion)
92
93
        return completions
94
95
    # only show our completion list because nothing else makes sense in this context
96
    flags = sublime.INHIBIT_EXPLICIT_COMPLETIONS | sublime.INHIBIT_WORD_COMPLETIONS
97
98
    all_completions = None
99
    all_key_completions = None
100
101
    def get_key_context_completion(self, prefix, line_content, sections):
102
        """Provide all possible sections without duplicates of unique ones."""
103
        # if section.casefold() != "Metadata".casefold():
104
        #     return None
105
106
        self.__lazy_initialize_completions()
107
        completions = self.__filter_completions_by_already_defined_sections(sections)
108
109
        # no results, means all keys are used up
110
        if not completions:
111
            return None
112
113
        # only show sorted by distance if something was already typed
114
        # because distance to empty string makes no sense
115
        if line_content != "":
116
            # sort by levenshtein distance
117
            sorted_completions = sorted(
118
                completions,
119
                key=lambda completion: levenshtein(completion[1], prefix)
120
            )
121
            return sorted_completions, self.flags
122
        else:
123
            return completions, self.flags
124