Passed
Push — develop ( 3d8444...ce4f6d )
by Dean
03:03
created

GetLSession   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 37
Duplicated Lines 70.27 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
c 1
b 0
f 0
dl 26
loc 37
ccs 0
cts 18
cp 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B or_create() 26 26 3
A __call__() 0 8 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
from plugin.core.helpers.variable import to_integer, merge
2
from plugin.managers.core.base import Manager
0 ignored issues
show
Bug introduced by
The name base does not seem to exist in module plugin.managers.core.
Loading history...
Configuration introduced by
Unable to import 'plugin.managers.core.base' (invalid syntax (<string>, line 39))

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...
3
from plugin.managers.client import ClientManager
4
from plugin.managers.core.exceptions import FilteredException
5
from plugin.managers.session.base import GetSession, UpdateSession
0 ignored issues
show
Bug introduced by
The name base does not seem to exist in module plugin.managers.session.
Loading history...
Configuration introduced by
Unable to import 'plugin.managers.session.base' (invalid syntax (<string>, line 63))

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
from plugin.managers.user import UserManager
7
from plugin.models import Session
8
from plugin.modules.core.manager import ModuleManager
0 ignored issues
show
Bug introduced by
The name manager does not seem to exist in module plugin.modules.core.
Loading history...
Configuration introduced by
Unable to import 'plugin.modules.core.manager' (invalid syntax (<string>, line 36))

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...
9
10
from datetime import datetime
11
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...
12
import logging
13
import peewee
14
15
log = logging.getLogger(__name__)
16
17
18
class GetLSession(GetSession):
0 ignored issues
show
Coding Style introduced by
This class has no __init__ method.
Loading history...
19
    def __call__(self, info):
20
        machine_identifier = info.get('machineIdentifier')
21
22
        if not machine_identifier:
23
            return None
24
25
        return super(GetLSession, self).__call__(
26
            Session.session_key == machine_identifier
27
        )
28
29 View Code Duplication
    def or_create(self, info, fetch=False):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
30
        machine_identifier = info.get('machineIdentifier')
31
32
        if not machine_identifier:
33
            log.info('No machine identifier available, unable to create session')
34
            return None
35
36
        try:
37
            # Create new session
38
            obj = self.manager.create(
0 ignored issues
show
Bug introduced by
The Instance of GetLSession does not seem to have a member named manager.

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...
39
                rating_key=to_integer(info.get('ratingKey')),
40
                session_key=machine_identifier,
41
42
                state='create'
43
            )
44
45
            # Update newly created object
46
            self.manager.update(obj, info, fetch)
0 ignored issues
show
Bug introduced by
The Instance of GetLSession does not seem to have a member named manager.

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...
47
48
            # Update active sessions
49
            ModuleManager['sessions'].on_created(obj)
50
51
            return obj
52
        except (apsw.ConstraintError, peewee.IntegrityError):
53
            # Return existing object
54
            return self(info)
55
56
57
class UpdateLSession(UpdateSession):
0 ignored issues
show
Coding Style introduced by
This class has no __init__ method.
Loading history...
58
    def __call__(self, obj, info, fetch=False):
59
        data = self.to_dict(obj, info, fetch)
60
61
        success = super(UpdateLSession, self).__call__(
62
            obj, data
63
        )
64
65
        ModuleManager['sessions'].on_updated(obj)
66
        return success
67
68
    def to_dict(self, obj, info, fetch=False):
69
        view_offset = to_integer(info.get('time'))
70
        rating_key = info.get('ratingKey')
71
72
        result = {
73
            'view_offset': view_offset,
74
75
            'updated_at': datetime.utcnow()
76
        }
77
78
        if not fetch:
79
            # Return simple update
80
            return merge(result, {
81
                'progress': self.get_progress(obj.duration, view_offset)
0 ignored issues
show
Bug introduced by
The Instance of UpdateLSession does not seem to have a member named get_progress.

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...
82
            })
83
84
        # Retrieve session
85
        # Retrieve metadata and guid
86
        p_metadata, guid = self.get_metadata(rating_key)
0 ignored issues
show
Bug introduced by
The Instance of UpdateLSession does not seem to have a member named get_metadata.

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...
87
88
        if not p_metadata:
89
            log.warn('Unable to retrieve metadata for rating_key %r', rating_key)
90
            return result
91
92
        if not guid:
93
            return merge(result, {
94
                'duration': p_metadata.duration,
95
                'progress': self.get_progress(p_metadata.duration, view_offset)
0 ignored issues
show
Bug introduced by
The Instance of UpdateLSession does not seem to have a member named get_progress.

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...
96
            })
97
98
        try:
99
            # Create/Retrieve `Client` for session
100
            result['client'] = ClientManager.get.or_create({
0 ignored issues
show
Bug introduced by
It seems like a value for argument player is missing in the unbound method call.
Loading history...
101
                'key': info.get('machineIdentifier'),
102
                'title': info.get('client')
103
            }, fetch=True)
104
105
            # Create/Retrieve `User` for session
106
            result['user'] = UserManager.get.or_create({
0 ignored issues
show
Bug introduced by
It seems like a value for argument user is missing in the unbound method call.
Loading history...
107
                'key': to_integer(info.get('user_id')),
108
                'title': info.get('user_name')
109
            }, fetch=True)
110
111
            # Pick account from `client` or `user` objects
112
            result['account'] = self.get_account(result)
0 ignored issues
show
Bug introduced by
The Instance of UpdateLSession does not seem to have a member named get_account.

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...
113
        except FilteredException:
114
            log.debug('Activity has been filtered')
115
116
            result['client'] = None
117
            result['user'] = None
118
119
            result['account'] = None
120
121
        return merge(result, {
122
            'duration': p_metadata.duration,
123
            'progress': self.get_progress(p_metadata.duration, view_offset)
0 ignored issues
show
Bug introduced by
The Instance of UpdateLSession does not seem to have a member named get_progress.

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...
124
        })
125
126
127
class LSessionManager(Manager):
0 ignored issues
show
Coding Style introduced by
This class has no __init__ method.
Loading history...
128
    get = GetLSession
129
    update = UpdateLSession
130
131
    model = Session
132