Passed
Push — master ( aeb165...d9ae97 )
by Dean
03:04
created

Main   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 181
Duplicated Lines 0 %
Metric Value
dl 0
loc 181
rs 10
wmc 23

10 Methods

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