forked from KenV99/script.service.kodi.callbacks
-
Notifications
You must be signed in to change notification settings - Fork 5
/
default.py
167 lines (141 loc) · 5.28 KB
/
default.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Copyright (C) 2015 KenV99
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
debug = False # TODO: check
testdebug = False # TODO: check
testTasks = False # TODO: check
branch = 'master'
build = '1018'
from resources.lib.utils.debugger import startdebugger
if debug:
startdebugger()
import os
import sys
import threading
import xbmc
import xbmcaddon
import resources.lib.pubsub as PubSub_Threaded
from resources.lib.kodilogging import KodiLogger
from resources.lib.publisherfactory import PublisherFactory
from resources.lib.subscriberfactory import SubscriberFactory
from resources.lib.settings import Settings
from resources.lib.utils.poutil import KodiPo
import xbmcgui
kodipo = KodiPo()
_ = kodipo.getLocalizedString
log = KodiLogger.log
try:
_addonversion_ = xbmcaddon.Addon().getAddonInfo('version')
except RuntimeError:
try:
_addonversion_ = xbmcaddon.Addon('script.service.kodi.callbacks').getAddonInfo('version')
except RuntimeError:
_addonversion_ = 'ERROR getting version'
class Cache(object):
publishers = None
dispatcher = None
class MainMonitor(xbmc.Monitor):
def __init__(self):
super(MainMonitor, self).__init__()
def onSettingsChanged(self):
dialog = xbmcgui.Dialog()
msg = _('If improperly implemented, running user tasks can damage your system.\n\nThe user assumes all risks and liability for running tasks.')
dialog.ok(_('Kodi Callbacks'), msg)
log(msg=_('Settings change detected - attempting to restart'))
abortall()
start()
def abortall():
for p in Cache.publishers:
try:
p.abort()
except threading.ThreadError as e:
log(msg=_('Error aborting: %s - Error: %s') % (str(p), str(e)))
Cache.dispatcher.abort()
for p in Cache.publishers:
p.join(0.5)
Cache.dispatcher.join(0.5)
if len(threading.enumerate()) > 1:
main_thread = threading.current_thread()
log(msg=_('Enumerating threads to kill others than main (%i)') % main_thread.ident)
for t in threading.enumerate():
if t is not main_thread and t.is_alive():
log(msg=_('Attempting to kill thread: %i: %s') % (t.ident, t.name))
try:
t.abort(0.5)
except (threading.ThreadError, AttributeError):
log(msg=_('Error killing thread'))
else:
if not t.is_alive():
log(msg=_('Thread killed succesfully'))
else:
log(msg=_('Error killing thread'))
def start():
global log
settings = Settings()
settings.getSettings()
kl = KodiLogger()
log = kl.log
log(msg=_('Settings read'))
Cache.dispatcher = PubSub_Threaded.Dispatcher(interval=settings.general['TaskFreq'], sleepfxn=xbmc.sleep)
log(msg=_('Dispatcher initialized'))
subscriberfactory = SubscriberFactory(settings, kl)
subscribers = subscriberfactory.createSubscribers()
for subscriber in subscribers:
Cache.dispatcher.addSubscriber(subscriber)
publisherfactory = PublisherFactory(settings, subscriberfactory.topics, Cache.dispatcher, kl, debug)
publisherfactory.createPublishers()
Cache.publishers = publisherfactory.ipublishers
Cache.dispatcher.start()
log(msg=_('Dispatcher started'))
for p in Cache.publishers:
try:
p.start()
except threading.ThreadError:
raise
log(msg=_('Publisher(s) started'))
def main():
msg = _('$$$ [kodi.callbacks] - Staring kodi.callbacks ver: %s (%s:build %s) python: %s') % (str(_addonversion_), branch, build, sys.version)
xbmc.log(msg=msg, level=xbmc.LOGINFO)
if branch != 'master':
xbmcaddon.Addon().setSetting('installed branch', branch)
start()
Cache.dispatcher.q_message(PubSub_Threaded.Message(PubSub_Threaded.Topic('onStartup')))
monitor = MainMonitor()
log(msg=_('Entering wait loop'))
monitor.waitForAbort()
# Shutdown tasks
Cache.dispatcher.q_message(PubSub_Threaded.Message(PubSub_Threaded.Topic('onShutdown'), pid=os.getpid()))
log(msg=_('Shutdown started'))
abortall()
log(msg='Shutdown complete')
if __name__ == '__main__':
if testTasks:
KodiLogger.setLogLevel(KodiLogger.LOGINFO)
startdebugger()
from resources.lib.tests.testTasks import testTasks
tt = testTasks()
tt.runTests()
else:
if branch != 'master':
try:
from resources.lib.utils.githubtools import processargs
except ImportError:
pass
else:
processargs(sys.argv)
main()