Test Failed
Push — develop ( a80574...5ed66c )
by Dean
02:36
created

Create   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 16
Duplicated Lines 0 %

Test Coverage

Coverage 50%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
c 1
b 0
f 0
dl 0
loc 16
ccs 5
cts 10
cp 0.5
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B __call__() 0 15 5
1 1
from plugin.core.exceptions import PluginDisabledError
2 1
from plugin.core.message import InterfaceMessages
3
from plugin.models import db
4 1
5 1
import apsw
0 ignored issues
show
Configuration introduced by
The import apsw 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...
6 1
import inspect
7 1
import logging
8
import peewee
9 1
10
log = logging.getLogger(__name__)
11
12 1
13 1
class Method(object):
14 1
    def __init__(self, manager, *args, **kwargs):
0 ignored issues
show
Unused Code introduced by
The argument kwargs seems to be unused.
Loading history...
Unused Code introduced by
The argument args seems to be unused.
Loading history...
15
        self.manager = manager
16 1
17
    @property
18 1
    def model(self):
19
        return self.manager.model
20
21 1
22 1
class Get(Method):
23
    def __call__(self, *query, **kwargs):
24
        if InterfaceMessages.critical:
0 ignored issues
show
Bug introduced by
The Class InterfaceMessages does not seem to have a member named critical.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
25
            raise PluginDisabledError()
26
27
        obj = self.model.get(*query, **kwargs)
28
29
        if obj:
30 1
            obj._created = False
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like _created was declared protected and should not be accessed from this context.

Prefixing a member variable _ is usually regarded as the equivalent of declaring it with protected visibility that exists in other languages. Consequentially, such a member should only be accessed from the same class or a child class:

class MyParent:
    def __init__(self):
        self._x = 1;
        self.y = 2;

class MyChild(MyParent):
    def some_method(self):
        return self._x    # Ok, since accessed from a child class

class AnotherClass:
    def some_method(self, instance_of_my_child):
        return instance_of_my_child._x   # Would be flagged as AnotherClass is not
                                         # a child class of MyParent
Loading history...
31
32
        return obj
33 1
34
    def all(self):
35
        if InterfaceMessages.critical:
0 ignored issues
show
Bug introduced by
The Class InterfaceMessages does not seem to have a member named critical.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
36 1
            raise PluginDisabledError()
37 1
38 1
        return self.model.select()
39
40
    def by_id(self, id):
0 ignored issues
show
Bug Best Practice introduced by
This seems to re-define the built-in id.

It is generally discouraged to redefine built-ins as this makes code very hard to read.

Loading history...
41
        return self(self.model.id == id)
42
43
    def or_create(self, *query, **kwargs):
44 1
        try:
45
            return self.manager.create(**kwargs)
46
        except (apsw.ConstraintError, peewee.IntegrityError) as ex:
47
            log.debug('or_create() - ex: %r', ex)
48 1
49 1
        return self(*query)
50 1
51
    def where(self, *query):
52
        if InterfaceMessages.critical:
0 ignored issues
show
Bug introduced by
The Class InterfaceMessages does not seem to have a member named critical.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
53 1
            raise PluginDisabledError()
54 1
55
        return self.model.select().where(*query)
56 1
57
58 1
class Create(Method):
59
    def __call__(self, **kwargs):
60 1
        if not self.model:
61
            raise Exception('Manager %r has no "model" attribute defined' % self.manager)
62
63 1
        if InterfaceMessages.critical:
0 ignored issues
show
Bug introduced by
The Class InterfaceMessages does not seem to have a member named critical.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
64 1
            raise PluginDisabledError()
65
66 1
        with db.transaction():
67
            obj = self.model.create(**kwargs)
68
69
        if obj:
70
            # Set flag
71
            obj._created = True
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like _created was declared protected and should not be accessed from this context.

Prefixing a member variable _ is usually regarded as the equivalent of declaring it with protected visibility that exists in other languages. Consequentially, such a member should only be accessed from the same class or a child class:

class MyParent:
    def __init__(self):
        self._x = 1;
        self.y = 2;

class MyChild(MyParent):
    def some_method(self):
        return self._x    # Ok, since accessed from a child class

class AnotherClass:
    def some_method(self, instance_of_my_child):
        return instance_of_my_child._x   # Would be flagged as AnotherClass is not
                                         # a child class of MyParent
Loading history...
72
73
        return obj
74
75
76
class Update(Method):
77
    keys = []
78
79
    def __call__(self, obj, data, save=True):
80
        changed = False
81
82
        for key, value in data.items():
83
            if not hasattr(obj, key):
84
                raise KeyError('%r has no key %r' % (obj, key))
85
86
            if getattr(obj, key) == value:
87 1
                continue
88
89
            changed = True
90
            setattr(obj, key, value)
91
92
        if not changed:
93
            return False
94
95
        if save:
96
            if InterfaceMessages.critical:
0 ignored issues
show
Bug introduced by
The Class InterfaceMessages does not seem to have a member named critical.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
97
                raise PluginDisabledError()
98
99
            obj.save()
100
101
        return True
102
103
    def from_dict(self, obj, changes, save=True):
104
        if not changes:
105
            return False
106
107
        # Resolve `account`
108
        if inspect.isfunction(obj):
109
            obj = obj()
110 1
111 1
        # Update `TraktAccount`
112 1
        data = {}
113
114 1
        for key in self.keys:
115 1
            if key not in changes:
116
                continue
117
118 1
            data[key] = changes[key]
119 1
120
        if data and not self(obj, data, save=save):
121 1
            log.debug('Unable to update %r (nothing changed?)', obj)
122
123
        return True
124
125 1
126
class ManagerMeta(type):
127
    def __init__(cls, name, bases, attributes):
128 1
        super(ManagerMeta, cls).__init__(name, bases, attributes)
129 1
130
        if '__metaclass__' in attributes:
131 1
            return
132 1
133 1
        # Construct manager methods
134
        for key in ['get', 'create', 'update']:
135 1
            value = getattr(cls, key)
136
137
            if not value or not inspect.isclass(value):
138
                continue
139
140
            # Construct method
141
            setattr(cls, key, value(cls))
142
143
144
class Manager(object):
145
    __metaclass__ = ManagerMeta
146
147
    create = Create
148
    get = Get
149
    update = Update
150
151
    model = None
152