Test Setup Failed
Push — develop ( 12ef70...f1d94a )
by Dean
02:47
created

Main.init_raven()   A

Complexity

Conditions 2

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 2
dl 0
loc 13
rs 9.4285
1
from core.header import Header
0 ignored issues
show
Bug introduced by
The name header does not seem to exist in module core.
Loading history...
Configuration introduced by
The import core.header 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...
2
from core.helpers import get_class_name, md5
0 ignored issues
show
Bug introduced by
The name helpers does not seem to exist in module core.
Loading history...
Configuration introduced by
The import core.helpers 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...
3
from core.logger import Logger
0 ignored issues
show
Bug introduced by
The name logger does not seem to exist in module core.
Loading history...
Configuration introduced by
The import core.logger 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
from core.update_checker import UpdateChecker
0 ignored issues
show
Bug introduced by
The name update_checker does not seem to exist in module core.
Loading history...
Configuration introduced by
The import core.update_checker 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
from plugin.core.constants import ACTIVITY_MODE, PLUGIN_VERSION
0 ignored issues
show
Configuration introduced by
The import plugin.core.constants 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...
7
from plugin.core.cache import CacheManager
0 ignored issues
show
Configuration introduced by
The import plugin.core.cache 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...
8
from plugin.core.helpers.thread import module_start
0 ignored issues
show
Configuration introduced by
The import plugin.core.helpers.thread 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...
9
from plugin.core.logger import LOG_HANDLER, update_loggers
0 ignored issues
show
Configuration introduced by
The import plugin.core.logger 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...
10
from plugin.core.logger.handlers.error_reporter import RAVEN
0 ignored issues
show
Configuration introduced by
The import plugin.core.logger.handlers.error_reporter 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...
11
from plugin.managers import TraktAccountManager
0 ignored issues
show
Configuration introduced by
The import plugin.managers 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
from plugin.models import TraktAccount
0 ignored issues
show
Configuration introduced by
The import plugin.models 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...
13
from plugin.modules.core.manager import ModuleManager
0 ignored issues
show
Configuration introduced by
The import plugin.modules.core.manager 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...
14
from plugin.preferences import Preferences
0 ignored issues
show
Configuration introduced by
The import plugin.preferences 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...
15
from plugin.scrobbler.core.session_prefix import SessionPrefix
0 ignored issues
show
Configuration introduced by
The import plugin.scrobbler.core.session_prefix 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...
16
17
from plex import Plex
0 ignored issues
show
Configuration introduced by
The import plex 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...
18
from plex_activity import Activity
0 ignored issues
show
Configuration introduced by
The import plex_activity 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...
19
from plex_metadata import Metadata
0 ignored issues
show
Configuration introduced by
The import plex_metadata 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...
20
from requests.packages.urllib3.util import Retry
0 ignored issues
show
Bug introduced by
The name packages does not seem to exist in module requests.
Loading history...
Configuration introduced by
The import requests.packages.urllib3.util 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...
21
from threading import Thread
22
from trakt import Trakt
0 ignored issues
show
Configuration introduced by
The import trakt 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...
23
import os
24
import uuid
25
26
log = Logger()
27
28
29
class Main(object):
30
    modules = [
31
        # core
32
        UpdateChecker()
33
    ]
34
35
    def __init__(self):
36
        Header.show(self)
37
38
        update_loggers()
39
40
        self.init_trakt()
41
        self.init_plex()
42
        self.init()
43
44
        ModuleManager.initialize()
45
46
        # Initialize sentry error reporting
47
        self.init_raven()
48
49
        # Construct main thread
50
        self.thread = Thread(target=self.run, name='main')
51
52
    def init(self):
53
        names = []
54
55
        # Initialize modules
56
        for module in self.modules:
57
            names.append(get_class_name(module))
58
59
            if hasattr(module, 'initialize'):
60
                module.initialize()
61
62
        log.info('Initialized %s modules: %s', len(names), ', '.join(names))
63
64
    @classmethod
65
    def init_raven(cls):
66
        # Retrieve server details
67
        server = Plex.detail()
68
69
        if not server:
70
            return
71
72
        # Set client name to a hash of `machine_identifier`
73
        RAVEN.name = md5(server.machine_identifier)
74
75
        RAVEN.tags.update({
76
            'server.version': server.version
77
        })
78
79
    @staticmethod
80
    def init_plex():
81
        # Ensure client identifier has been generated
82
        if not Dict['plex.client.identifier']:
0 ignored issues
show
Comprehensibility Best Practice introduced by
Undefined variable 'Dict'
Loading history...
83
            # Generate identifier
84
            Dict['plex.client.identifier'] = uuid.uuid4()
0 ignored issues
show
Comprehensibility Best Practice introduced by
Undefined variable 'Dict'
Loading history...
85
86
        # plex.py
87
        Plex.configuration.defaults.authentication(
88
            os.environ.get('PLEXTOKEN')
89
        )
90
91
        Plex.configuration.defaults.client(
92
            identifier=Dict['plex.client.identifier'],
0 ignored issues
show
Comprehensibility Best Practice introduced by
Undefined variable 'Dict'
Loading history...
93
94
            product='trakt (for Plex)',
95
            version=PLUGIN_VERSION
96
        )
97
98
        # plex.activity.py
99
        path = os.path.join(LOG_HANDLER.baseFilename, '..', '..', 'Plex Media Server.log')
100
        path = os.path.abspath(path)
101
102
        Activity['logging'].add_hint(path)
103
104
        # plex.metadata.py
105
        Metadata.configure(
106
            cache=CacheManager.get(
107
                'plex.metadata',
108
                serializer='pickle:///?protocol=2'
109
            ),
110
            client=Plex.client
111
        )
112
113
    @classmethod
114
    def init_trakt(cls):
115
        # Client
116
        Trakt.configuration.defaults.client(
117
            id='c9ccd3684988a7862a8542ae0000535e0fbd2d1c0ca35583af7ea4e784650a61',
118
            secret='bf00575b1ad252b514f14b2c6171fe650d474091daad5eb6fa890ef24d581f65'
119
        )
120
121
        # Application
122
        Trakt.configuration.defaults.app(
123
            name='trakt (for Plex)',
124
            version=PLUGIN_VERSION
125
        )
126
127
        # Setup request retrying
128
        Trakt.http.adapter_kwargs = {'max_retries': Retry(total=3, read=0)}
129
        Trakt.http.rebuild()
130
131
        Trakt.on('oauth.token_refreshed', cls.on_token_refreshed)
132
133
    @classmethod
134
    def on_token_refreshed(cls, authorization):
135
        log.debug('Authentication - PIN authorization refreshed')
136
137
        # Retrieve trakt account matching this `authorization`
138
        with Trakt.configuration.http(retry=True).oauth(token=authorization.get('access_token')):
139
            settings = Trakt['users/settings'].get()
140
141
        if not settings:
142
            log.warn('Authentication - Unable to retrieve account details for authorization')
143
            return
144
145
        # Retrieve trakt account username from `settings`
146
        username = settings.get('user', {}).get('username')
147
148
        if not username:
149
            log.warn('Authentication - Unable to retrieve username for authorization')
150
            return None
151
152
        # Find matching trakt account
153
        trakt_account = (TraktAccount
154
            .select()
155
            .where(
156
                TraktAccount.username == username
157
            )
158
        ).first()
159
160
        if not trakt_account:
161
            log.warn('Authentication - Unable to find TraktAccount with the username %r', username)
162
163
        # Update oauth credential
164
        TraktAccountManager.update.from_dict(trakt_account, {
165
            'authorization': {
166
                'oauth': authorization
167
            }
168
        })
169
170
        log.info('Authentication - Updated OAuth credential for %r', trakt_account)
171
172
    def start(self):
173
        self.thread.start()
174
175
    def run(self):
176
        # Check for authentication token
177
        log.info('X-Plex-Token: %s', 'available' if os.environ.get('PLEXTOKEN') else 'unavailable')
178
179
        # Process server startup state
180
        self.process_server_state()
181
182
        # Start new-style modules
183
        module_start()
184
185
        # Start modules
186
        names = []
187
188
        for module in self.modules:
189
            if not hasattr(module, 'start'):
190
                continue
191
192
            names.append(get_class_name(module))
193
194
            module.start()
195
196
        log.info('Started %s modules: %s', len(names), ', '.join(names))
197
198
        ModuleManager.start()
199
200
        # Start plex.activity.py
201
        Activity.start(ACTIVITY_MODE.get(Preferences.get('activity.mode')))
202
203
    @classmethod
204
    def process_server_state(cls):
205
        # Check startup state
206
        server = Plex.detail()
207
208
        if server is None:
209
            log.info('Unable to check startup state, detail request failed')
210
            return
211
212
        # Check server startup state
213
        if server.start_state is None:
214
            return
215
216
        if server.start_state == 'startingPlugins':
217
            return cls.on_starting_plugins()
218
219
        log.error('Unhandled server start state %r', server.start_state)
220
221
    @staticmethod
222
    def on_starting_plugins():
223
        log.debug('on_starting_plugins')
224
225
        SessionPrefix.increment()
226
227
    @staticmethod
228
    def on_configuration_changed():
229
        update_loggers()
230