Passed
Push — master ( 0fa79d...5a4f44 )
by Swen
01:58
created

LocalizedValue._interpret_value()   C

Complexity

Conditions 7

Size

Total Lines 33

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 7

Importance

Changes 0
Metric Value
cc 7
c 0
b 0
f 0
dl 0
loc 33
rs 5.5
ccs 12
cts 12
cp 1
crap 7
1 1
import collections
2
3 1
from django.conf import settings
0 ignored issues
show
Configuration introduced by
The import django.conf could not be resolved.

This can be caused by one of the following:

1. Missing Dependencies

This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.

# .scrutinizer.yml
before_commands:
    - sudo pip install abc # Python2
    - sudo pip3 install abc # Python3
Tip: We are currently not using virtualenv to run pylint, when installing your modules make sure to use the command for the correct version.

2. Missing __init__.py files

This error could also result from missing __init__.py files in your module folders. Make sure that you place one file in each sub-folder.

Loading history...
4 1
from django.utils import translation
0 ignored issues
show
Configuration introduced by
The import django.utils could not be resolved.

This can be caused by one of the following:

1. Missing Dependencies

This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.

# .scrutinizer.yml
before_commands:
    - sudo pip install abc # Python2
    - sudo pip3 install abc # Python3
Tip: We are currently not using virtualenv to run pylint, when installing your modules make sure to use the command for the correct version.

2. Missing __init__.py files

This error could also result from missing __init__.py files in your module folders. Make sure that you place one file in each sub-folder.

Loading history...
5
6
7 1
class LocalizedValue(dict):
8
    """Represents the value of a :see:LocalizedField."""
9
10 1
    def __init__(self, keys: dict=None):
0 ignored issues
show
Bug introduced by
The __init__ method of the super-class dict is not called.

It is generally advisable to initialize the super-class by calling its __init__ method:

class SomeParent:
    def __init__(self):
        self.x = 1

class SomeChild(SomeParent):
    def __init__(self):
        # Initialize the super class
        SomeParent.__init__(self)
Loading history...
11
        """Initializes a new instance of :see:LocalizedValue.
12
13
        Arguments:
14
            keys:
15
                The keys to initialize this value with. Every
16
                key contains the value of this field in a
17
                different language.
18
        """
19
20 1
        self._interpret_value(keys);
0 ignored issues
show
Coding Style introduced by
Unnecessary semicolon
Loading history...
21
22 1
    def get(self, language: str=None) -> str:
23
        """Gets the underlying value in the specified or
24
        primary language.
25
26
        Arguments:
27
            language:
28
                The language to get the value in.
29
30
        Returns:
31
            The value in the current language, or
32
            the primary language in case no language
33
            was specified.
34
        """
35
36 1
        language = language or settings.LANGUAGE_CODE
37 1
        return super().get(language, None)
38
39 1
    def set(self, language: str, value: str):
40
        """Sets the value in the specified language.
41
42
        Arguments:
43
            language:
44
                The language to set the value in.
45
46
            value:
47
                The value to set.
48
        """
49
50 1
        self[language] = value
51 1
        self.__dict__.update(self)
52 1
        return self
53
54 1
    def deconstruct(self) -> dict:
55
        """Deconstructs this value into a primitive type.
56
57
        Returns:
58
            A dictionary with all the localized values
59
            contained in this instance.
60
        """
61
62 1
        path = 'localized_fields.fields.LocalizedValue'
63 1
        return path, [self.__dict__], {}
64
65 1
    def _interpret_value(self, value):
66
        """Interprets a value passed in the constructor as
67
        a :see:LocalizedValue.
68
69
        If string:
70
            Assumes it's the default language.
71
72
        If dict:
73
            Each key is a language and the value a string
74
            in that language.
75
76
        If list:
77
            Recurse into to apply rules above.
78
79
        Arguments:
80
            value:
81
                The value to interpret.
82
        """
83
84 1
        for lang_code, _ in settings.LANGUAGES:
85 1
            self.set(lang_code, None)
86
87 1
        if isinstance(value, str):
88 1
            self.set(settings.LANGUAGE_CODE, value)
89
90 1
        elif isinstance(value, dict):
91 1
            for lang_code, _ in settings.LANGUAGES:
92 1
                lang_value = value.get(lang_code) or None
93 1
                self.set(lang_code, lang_value)
94
95 1
        elif isinstance(value, collections.Iterable):
96 1
            for val in value:
97 1
                self._interpret_value(val)
98
99 1
    def __str__(self) -> str:
100
        """Gets the value in the current language, or falls
101
        back to the primary language if there's no value
102
        in the current language."""
103
104 1
        value = self.get(translation.get_language())
105
106 1
        if not value:
107 1
            value = self.get(settings.LANGUAGE_CODE)
108
109 1
        return value or ''
110
111 1
    def __eq__(self, other):
112
        """Compares :paramref:self to :paramref:other for
113
        equality.
114
115
        Returns:
116
            True when :paramref:self is equal to :paramref:other.
117
            And False when they are not.
118
        """
119
120 1
        if not isinstance(other, type(self)):
121
            if isinstance(other, str):
122
                return self.__str__() == other
123
            return False
124
125 1
        for lang_code, _ in settings.LANGUAGES:
126 1
            if self.get(lang_code) != other.get(lang_code):
127 1
                return False
128
129 1
        return True
130
131 1
    def __ne__(self, other):
132
        """Compares :paramref:self to :paramerf:other for
133
        in-equality.
134
135
        Returns:
136
            True when :paramref:self is not equal to :paramref:other.
137
            And False when they are.
138
        """
139
140 1
        return not self.__eq__(other)
141
142 1
    def __setattr__(self, language: str, value: str):
143
        """Sets the value for a language with the specified name.
144
145
        Arguments:
146
            language:
147
                The language to set the value in.
148
149
            value:
150
                The value to set.
151
        """
152
153 1
        self.set(language, value)
154
155
    def __repr__(self):  # pragma: no cover
156
        """Gets a textual representation of this object."""
157
158
        return 'LocalizedValue<%s> 0x%s' % (dict(self), id(self))
159