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 ( e6133a...21aacd )
by thatsIch
01:08
created

__sections_contain_section_id()   A

Complexity

Conditions 3

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
c 2
b 0
f 0
dl 0
loc 7
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 .compiler import compile_keys
23
from .yaml_content_reader import YamlContentReader
24
25
26
def str_equal_case_ignore(str1, str2):
27
    """Compare two strings ignoring the case."""
28
    return str1.casefold() == str2.casefold()
29
30
31
def sections_contain_section_id(sections, section_id):
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...
32
        # value not used here
33
        for section in sections:
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 4 spaces were expected, but 8 were found.
Loading history...
34
            if str_equal_case_ignore(section, section_id):
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 8 spaces were expected, but 12 were found.
Loading history...
35
                return True
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 12 spaces were expected, but 16 were found.
Loading history...
36
37
        return False
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 4 spaces were expected, but 8 were found.
Loading history...
38
39
40
class SkinSectionAutoCompleter(YamlContentReader):  # pylint: disable=R0903; only provide one method
41
    """Ths class is the logical state holder for the auto completion suggestions.
42
43
    Upon the request the respective yaml file is parsed and converted into a logical
44
    representation of the completions. Depending on the prior information the completions
45
    can be filtered containing less entries.
46
    """
47
48
    def __get_completions(self):
49
        """IO access to the yaml file.
50
51
        Uses a yaml loader to parse it into a python object.
52
        """
53
        try:
54
            section_content = self._get_yaml_content("completion/", "section.yaml")
55
            section = yaml.load(section_content)
56
57
            return section
58
59
        except yaml.YAMLError as error:
60
            logger.error(error)
61
            return []
62
63
    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...
64
        # use lazy initialization because else the API is not available yet
65
        if not self.all_completions:
66
            self.all_completions = self.__get_completions()
67
            self.all_key_completions = compile_keys(self.all_completions)
68
69
    def __filter_completions_by_sec(self, sections):
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
        # filter by already existing keys
71
        completions = []
72
73
        settings = sublime.load_settings("Rainmeter.sublime-settings")
74
        allow_duplicates = settings.get("allow_completion_section_duplicates", False)
75
76
        for completion in self.all_key_completions:
77
            # trigger is not used here
78
            section_id, display, content, unique = completion
79
80
            # we only need to search for duplicates
81
            # if we are having a unique section like [Rainmeter]
82
            # and not allow duplicates
83
            if unique and not allow_duplicates:
84
                contained = sections_contain_section_id(sections, section_id)
85
86
                if not contained:
87
                    completions.append((display, content))
88
            else:
89
                completions.append((display, content))
90
91
        return completions
92
93
    # only show our completion list because nothing else makes sense in this context
94
    flags = sublime.INHIBIT_EXPLICIT_COMPLETIONS | sublime.INHIBIT_WORD_COMPLETIONS
95
96
    all_completions = None
97
    all_key_completions = None
98
99
    def get_key_context_completion(self, prefix, line_content, sections):
100
        """Provide all possible sections without duplicates of unique ones."""
101
        # if section.casefold() != "Metadata".casefold():
102
        #     return None
103
104
        self.__lazy_initialize_completions()
105
        completions = self.__filter_completions_by_sec(sections)
106
        # no results, means all keys are used up
107
        if not completions:
108
            return None
109
110
        # only show sorted by distance if something was already typed
111
        # because distance to empty string makes no sense
112
        if line_content != "":
113
            # sort by levenshtein distance
114
            sorted_completions = sorted(
115
                completions,
116
                key=lambda completion: levenshtein(completion[1], prefix)
117
            )
118
            return sorted_completions, self.flags
119
        else:
120
            return completions, self.flags
121