forked from KenV99/script.service.kodi.callbacks
-
Notifications
You must be signed in to change notification settings - Fork 5
/
script.py
186 lines (159 loc) · 6.66 KB
/
script.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Copyright (C) 2016 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/>.
#
scriptdebug = False # TODO: check
testTasks = False # TODO: check
import os
import sys
import resources.lib.pubsub as PubSub_Threaded
import xbmc
import xbmcgui
from default import branch
from resources.lib.kodilogging import KodiLogger
from resources.lib.settings import Settings
from resources.lib.subscriberfactory import SubscriberFactory
from resources.lib.utils.debugger import startdebugger
from resources.lib.utils.kodipathtools import translatepath
from resources.lib.utils.poutil import KodiPo
def notify(msg):
dialog = xbmcgui.Dialog()
dialog.notification('Kodi Callbacks', msg, xbmcgui.NOTIFICATION_INFO, 5000)
kodipo = KodiPo()
_ = kodipo.getLocalizedString
log = KodiLogger.log
def test(key):
global log
log = KodiLogger.log
import resources.lib.tests.direct_test as direct_test
from resources.lib.events import Events
import traceback
log(msg=_('Running Test for Event: %s') % key)
events = Events().AllEvents
settings = Settings()
settings.getSettings()
log(msg=_('Settings for test read'))
evtsettings = settings.events[key]
topic = settings.topicFromSettingsEvent(key)
task_key = settings.events[key]['task']
tasksettings = settings.tasks[task_key]
testlogger = direct_test.TestLogger()
log(msg=_('Creating subscriber for test'))
subscriberfactory = SubscriberFactory(settings, testlogger)
subscriber = subscriberfactory.createSubscriber(key)
if subscriber is not None:
log(msg=_('Test subscriber created successfully'))
try:
kwargs = events[evtsettings['type']]['expArgs']
except KeyError:
kwargs = {}
testRH = direct_test.TestHandler(direct_test.testMsg(subscriber.taskmanagers[0], tasksettings, kwargs))
subscriber.taskmanagers[0].returnHandler = testRH.testReturnHandler
# Run test
log(msg=_('Running test'))
nMessage = PubSub_Threaded.Message(topic=topic, **kwargs)
try:
subscriber.notify(nMessage)
except Exception:
msg = _('Unspecified error during testing')
e = sys.exc_info()[0]
if hasattr(e, 'message'):
msg = str(e.message)
msg = msg + '\n' + traceback.format_exc()
log(msg=msg)
msgList = msg.split('\n')
import resources.lib.dialogtb as dialogtb
dialogtb.show_textbox('Error', msgList)
else:
log(msg=_('Test subscriber creation failed due to errors'))
msgList = testlogger.retrieveLogAsList()
import resources.lib.dialogtb as dialogtb
dialogtb.show_textbox('Error', msgList)
xbmc.sleep(2000)
if __name__ == '__main__':
dryrun = False
addonid = 'script.service.kodi.callbacks'
if len(sys.argv) > 1:
if scriptdebug is True:
startdebugger()
dryrun = True
if sys.argv[1] == 'regen':
from resources.lib.kodisettings.generate_xml import generate_settingsxml
generate_settingsxml()
dialog = xbmcgui.Dialog()
msg = _('Settings Regenerated')
dialog.ok(_('Kodi Callbacks'), msg)
elif sys.argv[1] == 'test':
KodiLogger.setLogLevel(KodiLogger.LOGNOTICE)
from resources.lib.tests.testTasks import testTasks
tt = testTasks()
tt.runTests()
dialog = xbmcgui.Dialog()
msg = _('Native Task Testing Complete - see log for results')
dialog.notification(_('Kodi Callbacks'), msg, xbmcgui.NOTIFICATION_INFO, 5000)
elif sys.argv[1] == 'updatefromzip':
from resources.lib.utils.updateaddon import UpdateAddon
KodiLogger.setLogLevel(KodiLogger.LOGNOTICE)
dialog = xbmcgui.Dialog()
zipfn = dialog.browse(1, _('Locate zip file'), 'files', '.zip', False, False, translatepath('~'))
if zipfn != translatepath('~'):
if os.path.isfile(zipfn):
ua = UpdateAddon(addonid)
ua.installFromZip(zipfn, updateonly=True, dryrun=dryrun)
else:
dialog.ok(_('Kodi Callbacks'), _('Incorrect path'))
elif sys.argv[1] == 'restorebackup':
KodiLogger.setLogLevel(KodiLogger.LOGNOTICE)
dialog = xbmcgui.Dialog()
zipfn = dialog.browse(1, _('Locate backup zip file'), 'files', '.zip', False, False,
translatepath('special://addondata/backup/'))
if zipfn != translatepath('special://addondata/backup/'):
from resources.lib.utils.updateaddon import UpdateAddon
ua = UpdateAddon(addonid)
ua.installFromZip(zipfn, updateonly=False, dryrun=dryrun)
elif sys.argv[1] == 'lselector':
from resources.lib.utils.selector import selectordialog
try:
result = selectordialog(sys.argv[2:])
except (SyntaxError, TypeError) as e:
xbmc.log(msg='Error: %s' % str(e), level=xbmc.LOGERROR)
elif sys.argv[1] == 'logsettings':
KodiLogger.setLogLevel(KodiLogger.LOGNOTICE)
settings = Settings()
settings.getSettings()
settings.logSettings()
dialog = xbmcgui.Dialog()
msg = _('Settings written to log')
dialog.ok(_('Kodi Callbacks'), msg)
elif branch != 'master' and sys.argv[1] == 'checkupdate':
try:
from resources.lib.utils.githubtools import processargs
except ImportError:
pass
else:
processargs(sys.argv)
else:
# Direct Event/Task Testing
KodiLogger.setLogLevel(KodiLogger.LOGNOTICE)
eventId = sys.argv[1]
test(eventId)
elif testTasks:
KodiLogger.setLogLevel(KodiLogger.LOGNOTICE)
startdebugger()
from resources.lib.tests.testTasks import testTasks
tt = testTasks()
tt.runTests()