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.
Passed
Pull Request — master (#22)
by
unknown
01:09
created

QLinearGradientConformer.to_scss()   B

Complexity

Conditions 3

Size

Total Lines 25
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 10
nop 2
dl 0
loc 25
rs 8.8571
c 0
b 0
f 0
1
# -*- coding: utf-8 -*-
2
# -----------------------------------------------------------------------------
3
# Copyright (c) 2015 Yann Lanthony
4
# Copyright (c) 2017-2018 Spyder Project Contributors
5
#
6
# Licensed under the terms of the MIT License
7
# (See LICENSE.txt for details)
8
# -----------------------------------------------------------------------------
9
"""Conform qss to compliant scss and css to valid qss."""
10
11
# Standard library imports
12
from __future__ import absolute_import, print_function
13
import re
14
15
16
class Conformer(object):
0 ignored issues
show
Unused Code introduced by
The variable __class__ seems to be unused.
Loading history...
17
    """Base class for all text transformations."""
18
19
    def to_scss(self, qss):
0 ignored issues
show
Unused Code introduced by
The argument qss seems to be unused.
Loading history...
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...
20
        """Transform some qss to valid scss."""
21
22
        return NotImplemented
23
24
    def to_qss(self, css):
0 ignored issues
show
Unused Code introduced by
The argument css seems to be unused.
Loading history...
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...
25
        """Transform some css to valid qss."""
26
27
        return NotImplemented
28
29
30
class NotConformer(Conformer):
0 ignored issues
show
Unused Code introduced by
The variable __class__ seems to be unused.
Loading history...
31
    """Conform QSS "!" in selectors."""
32
33
    def to_scss(self, qss):
34
        """Replaces "!" in selectors with "_qnot_"."""
35
36
        return qss.replace(':!', ':_qnot_')
37
38
    def to_qss(self, css):
39
        """Replaces "_qnot_" in selectors with "!"."""
40
41
        return css.replace(':_qnot_', ':!')
42
43
44
class QLinearGradientConformer(Conformer):
0 ignored issues
show
Unused Code introduced by
The variable __class__ seems to be unused.
Loading history...
45
    """Conform QSS qlineargradient function."""
46
47
    qss_pattern = re.compile(
48
        'qlineargradient\('
0 ignored issues
show
Bug introduced by
A suspicious escape sequence \( was found. Did you maybe forget to add an r prefix?

Escape sequences in Python are generally interpreted according to rules similar to standard C. Only if strings are prefixed with r or R are they interpreted as regular expressions.

The escape sequence that was used indicates that you might have intended to write a regular expression.

Learn more about the available escape sequences. in the Python documentation.

Loading history...
49
        '((?:(?:\s+)?(?:x1|y1|x2|y2):(?:\s+)?[0-9A-Za-z$_-]+,?)+)'  # coords
0 ignored issues
show
Bug introduced by
A suspicious escape sequence \s was found. Did you maybe forget to add an r prefix?

Escape sequences in Python are generally interpreted according to rules similar to standard C. Only if strings are prefixed with r or R are they interpreted as regular expressions.

The escape sequence that was used indicates that you might have intended to write a regular expression.

Learn more about the available escape sequences. in the Python documentation.

Loading history...
50
        '((?:(?:\s+)?stop:.*,?)+(?:\s+)?)?'  # stops
0 ignored issues
show
Bug introduced by
A suspicious escape sequence \s was found. Did you maybe forget to add an r prefix?

Escape sequences in Python are generally interpreted according to rules similar to standard C. Only if strings are prefixed with r or R are they interpreted as regular expressions.

The escape sequence that was used indicates that you might have intended to write a regular expression.

Learn more about the available escape sequences. in the Python documentation.

Loading history...
51
        '\)',
0 ignored issues
show
Bug introduced by
A suspicious escape sequence \) was found. Did you maybe forget to add an r prefix?

Escape sequences in Python are generally interpreted according to rules similar to standard C. Only if strings are prefixed with r or R are they interpreted as regular expressions.

The escape sequence that was used indicates that you might have intended to write a regular expression.

Learn more about the available escape sequences. in the Python documentation.

Loading history...
52
        re.MULTILINE
53
    )
54
55
    def _conform_group_to_scss(self, group):
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...
56
        """
57
        Takes a qss str containing xy coords or stops and returns a str
58
        containing just the values.
59
60
        'x1: 0, y1: 0, x2: 0, y2: 0' => '0, 0, 0, 0'
61
        'stop: 0 red, stop: 1 blue' => '0 red, 1 blue'
62
        """
63
        new_group = []
64
        for part in group.strip().split(','):
65
            if part:
66
                _, value = part.split(':')
67
                new_group.append(value.strip())
68
        return ', '.join(new_group)
69
70
    def to_scss(self, qss):
71
        """
72
        Conform qss qlineargradient to scss qlineargradient form.
73
74
        Normalizes all whitespace including the removal of newline chars.
75
76
        qlineargradient(x1: 0, y1: 0, x2: 0, y2: 0, stop: 0 red, stop: 1 blue)
77
        =>
78
        qlineargradient(0, 0, 0, 0, (0 red, 1 blue))
79
        """
80
81
        conformed = qss
82
83
        for coords, stops in self.qss_pattern.findall(qss):
84
85
            new_coords = self._conform_group_to_scss(coords)
86
            conformed = conformed.replace(coords, new_coords, 1)
87
88
            if not stops:
89
                continue
90
91
            new_stops = ', ({})'.format(self._conform_group_to_scss(stops))
92
            conformed = conformed.replace(stops, new_stops, 1)
93
94
        return conformed
95
96
    def to_qss(self, css):
97
        """Handled by qlineargradient function passed to sass.compile"""
98
99
        return css
100
101
102
conformers = [c() for c in Conformer.__subclasses__() if c is not Conformer]
0 ignored issues
show
Coding Style Naming introduced by
The name conformers does not conform to the constant naming conventions ((([A-Z_][A-Z0-9_]*)|(__.*__))$).

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...
103
104
105
def scss_conform(input_str):
106
    """
107
    Conform qss to valid scss.
108
109
    Runs the to_scss method of all Conformer subclasses on the input_str.
110
    Conformers are run in order of definition.
111
112
    :param input_str: QSS string
113
    :returns: Valid SCSS string
114
    """
115
116
    conformed = input_str
117
    for conformer in conformers:
118
        conformed = conformer.to_scss(conformed)
119
120
    return conformed
121
122
123
def qt_conform(input_str):
124
    """
125
    Conform css to valid qss.
126
127
    Runs the to_qss method of all Conformer subclasses on the input_str.
128
    Conformers are run in reverse order.
129
130
    :param input_str: CSS string
131
    :returns: Valid QSS string
132
    """
133
134
    conformed = input_str
135
    for conformer in conformers[::-1]:
136
        conformed = conformer.to_qss(conformed)
137
    return conformed
138