forked from MediaBrowser/plugin.video.emby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.py
143 lines (98 loc) · 4.29 KB
/
service.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# -*- coding: utf-8 -*-
#################################################################################################
import imp
import logging
import os
import threading
import sys
import xbmc
import xbmcvfs
import xbmcaddon
#################################################################################################
__addon__ = xbmcaddon.Addon(id='plugin.video.emby')
__addon_path__ = __addon__.getAddonInfo('path').decode('utf-8')
__base__ = xbmc.translatePath(os.path.join(__addon_path__, 'resources', 'lib')).decode('utf-8')
__libraries__ = xbmc.translatePath(os.path.join(__addon_path__, 'libraries')).decode('utf-8')
__pcache__ = xbmc.translatePath(os.path.join(__addon__.getAddonInfo('profile'), 'emby')).decode('utf-8')
__cache__ = xbmc.translatePath('special://temp/emby').decode('utf-8')
sys.path.insert(0, __libraries__)
if not xbmcvfs.exists(__pcache__ + '/'):
from resources.lib.helper.utils import copytree
copytree(os.path.join(__base__, 'objects'), os.path.join(__pcache__, 'objects'))
sys.path.insert(0, __cache__)
sys.path.insert(0, __pcache__)
sys.path.append(__base__)
sys.argv.append('service')
#################################################################################################
from helper import settings
import entrypoint
#################################################################################################
LOG = logging.getLogger("EMBY.service")
DELAY = int(settings('startupDelay') if settings('SyncInstallRunDone.bool') else 4 or 0)
#################################################################################################
class ServiceManager(threading.Thread):
''' Service thread.
To allow to restart and reload modules internally.
Restart service
Delete lib and objects entries to reload them as if it were the first time.
Delete .pyo files to force Kodi to recreate them.
Finally, re-initialize modules that are used in __main__ to reload all our modules.
'''
exception = None
def __init__(self):
threading.Thread.__init__(self)
def run(self):
global entrypoint
global settings
service = None
try:
service = entrypoint.Service()
if DELAY and xbmc.Monitor().waitForAbort(DELAY):
raise Exception("Aborted during startup delay")
service.service()
except Exception as error:
self.exception = error
LOG.error(error)
if service is not None:
if not 'ExitService' in error:
service.shutdown()
if 'RestartService' in error:
for mod in dict(sys.modules):
module = sys.modules[mod]
try:
module_path = imp.find_module(mod.split('.')[0])[1]
if ('plugin.video.emby' in module_path and not 'libraries' in module_path or
mod.startswith('objects')):
LOG.debug("[ reload/%s ]", mod)
del sys.modules[mod]
except ImportError: #xbmc built-in functions or entries with None
pass
import entrypoint
import helper
import objects
try:
helper.utils.delete_pyo(__addon_path__)
helper.utils.delete_pyo(__pcache__)
except Exception:
pass
imp.reload(entrypoint)
imp.reload(helper)
imp.reload(objects)
from helper import settings
if __name__ == '__main__':
LOG.warn("-->[ service ]")
LOG.warn("Delay startup by %s seconds.", DELAY)
while True:
if not settings('enableAddon.bool'):
LOG.warn("Emby for Kodi is not enabled.")
break
try:
session = ServiceManager()
session.start()
session.join() # Block until the thread exits.
if 'RestartService' in session.exception:
continue
except Exception as error:
LOG.exception(error)
break
LOG.warn("--<[ service ]")