forked from MikeSiLVO/script.skinshortcuts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
default.py
409 lines (342 loc) · 18.8 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
# coding=utf-8
import os, sys
import xbmc, xbmcaddon, xbmcgui, xbmcplugin, urllib, xbmcvfs
import xml.etree.ElementTree as xmltree
import cPickle as pickle
import pstats
import random
import time
import calendar
import thread
from time import gmtime, strftime
from datetime import datetime
from traceback import print_exc
# Uncomment when profiling performance
# import cProfile
if sys.version_info < (2, 7):
import simplejson
else:
import json as simplejson
ADDON = xbmcaddon.Addon()
ADDONID = ADDON.getAddonInfo('id').decode( 'utf-8' )
ADDONVERSION = ADDON.getAddonInfo('version')
LANGUAGE = ADDON.getLocalizedString
CWD = ADDON.getAddonInfo('path').decode("utf-8")
ADDONNAME = ADDON.getAddonInfo('name').decode("utf-8")
RESOURCE = xbmc.translatePath( os.path.join( CWD, 'resources', 'lib' ) ).decode("utf-8")
DATAPATH = os.path.join( xbmc.translatePath( "special://profile/" ).decode( 'utf-8' ), "addon_data", ADDONID )
MASTERPATH = os.path.join( xbmc.translatePath( "special://masterprofile/" ).decode( 'utf-8' ), "addon_data", ADDONID )
sys.path.append(RESOURCE)
import xmlfunctions, datafunctions, library, nodefunctions
XML = xmlfunctions.XMLFunctions()
DATA = datafunctions.DataFunctions()
LIBRARY = library.LibraryFunctions()
NODE = nodefunctions.NodeFunctions()
ISEMPTY = "IsEmpty"
if int( xbmc.getInfoLabel( "System.BuildVersion" ).split(".")[0] ) >= 17:
ISEMPTY = "String.IsEmpty"
hashlist = []
def log(txt):
if ADDON.getSetting( "enable_logging" ) == "true":
if isinstance (txt,str):
txt = txt.decode('utf-8')
message = u'%s: %s' % (ADDONID, txt)
xbmc.log(msg=message.encode('utf-8'), level=xbmc.LOGDEBUG)
class Main:
# MAIN ENTRY POINT
def __init__(self):
self._parse_argv()
self.WINDOW = xbmcgui.Window(10000)
# Create data and master paths if not exists
if not xbmcvfs.exists(DATAPATH):
xbmcvfs.mkdir(DATAPATH)
if not xbmcvfs.exists(MASTERPATH):
xbmcvfs.mkdir(MASTERPATH)
# Perform action specified by user
if not self.TYPE:
line1 = "This addon is for skin developers, and requires skin support"
xbmcgui.Dialog().ok(ADDONNAME, line1)
if self.TYPE=="buildxml":
xbmc.sleep(100)
XML.buildMenu( self.MENUID, self.GROUP, self.LEVELS, self.MODE, self.OPTIONS, self.MINITEMS )
if self.TYPE=="launch":
xbmcplugin.setResolvedUrl( handle=int( sys.argv[1]), succeeded=False, listitem=xbmcgui.ListItem() )
self._launch_shortcut( self.PATH )
if self.TYPE=="launchpvr":
xbmc.executeJSONRPC('{ "jsonrpc": "2.0", "id": 0, "method": "Player.Open", "params": { "item": {"channelid": ' + self.CHANNEL + '} } }')
if self.TYPE=="manage":
self._manage_shortcuts( self.GROUP, self.DEFAULTGROUP, self.NOLABELS, self.GROUPNAME )
if self.TYPE=="hidesubmenu":
self._hidesubmenu( self.MENUID )
if self.TYPE=="resetlist":
self._resetlist( self.MENUID, self.NEXTACTION )
if self.TYPE=="shortcuts":
# We're just going to choose a shortcut, and save its details to the given
# skin labels
# Load library shortcuts in thread
thread.start_new_thread( LIBRARY.loadAllLibrary, () )
if self.GROUPING is not None:
selectedShortcut = LIBRARY.selectShortcut( "", grouping = self.GROUPING, custom = self.CUSTOM, showNone = self.NONE )
else:
selectedShortcut = LIBRARY.selectShortcut( "", custom = self.CUSTOM, showNone = self.NONE )
# Now set the skin strings
if selectedShortcut is not None and selectedShortcut.getProperty( "Path" ):
path = selectedShortcut.getProperty( "Path" )
if selectedShortcut.getProperty( "chosenPath" ):
path = selectedShortcut.getProperty( "chosenPath" )
if path.startswith( "pvr-channel://" ):
path = "RunScript(script.skinshortcuts,type=launchpvr&channel=" + path.replace( "pvr-channel://", "" ) + ")"
if self.LABEL is not None and selectedShortcut.getLabel() != "":
xbmc.executebuiltin( "Skin.SetString(" + self.LABEL + "," + selectedShortcut.getLabel() + ")" )
if self.ACTION is not None:
xbmc.executebuiltin( "Skin.SetString(" + self.ACTION + "," + path + " )" )
if self.SHORTCUTTYPE is not None:
xbmc.executebuiltin( "Skin.SetString(" + self.SHORTCUTTYPE + "," + selectedShortcut.getLabel2() + ")" )
if self.THUMBNAIL is not None and selectedShortcut.getProperty( "icon" ):
xbmc.executebuiltin( "Skin.SetString(" + self.THUMBNAIL + "," + selectedShortcut.getProperty( "icon" ) + ")" )
if self.THUMBNAIL is not None and selectedShortcut.getProperty( "thumbnail" ):
xbmc.executebuiltin( "Skin.SetString(" + self.THUMBNAIL + "," + selectedShortcut.getProperty( "thumbnail" ) + ")" )
if self.LIST is not None:
xbmc.executebuiltin( "Skin.SetString(" + self.LIST + "," + DATA.getListProperty( path ) + ")" )
elif selectedShortcut is not None and selectedShortcut.getLabel() == "::NONE::":
# Clear the skin strings
if self.LABEL is not None:
xbmc.executebuiltin( "Skin.Reset(" + self.LABEL + ")" )
if self.ACTION is not None:
xbmc.executebuiltin( "Skin.Reset(" + self.ACTION + " )" )
if self.SHORTCUTTYPE is not None:
xbmc.executebuiltin( "Skin.Reset(" + self.SHORTCUTTYPE + ")" )
if self.THUMBNAIL is not None:
xbmc.executebuiltin( "Skin.Reset(" + self.THUMBNAIL + ")" )
if self.THUMBNAIL is not None:
xbmc.executebuiltin( "Skin.Reset(" + self.THUMBNAIL + ")" )
if self.LIST is not None:
xbmc.executebuiltin( "Skin.Reset(" + self.LIST + ")" )
if self.TYPE=="widgets":
# We're just going to choose a widget, and save its details to the given
# skin labels
# Load library shortcuts in thread
thread.start_new_thread( LIBRARY.loadAllLibrary, () )
# Check if we should show the custom option (if the relevant widgetPath skin string is provided and isn't empty)
showCustom = False
if self.WIDGETPATH and xbmc.getCondVisibility( "!%s(Skin.String(%s))" %( ISEMPTY, self.WIDGETPATH ) ):
showCustom = True
if self.GROUPING:
if self.GROUPING.lower() == "default":
selectedShortcut = LIBRARY.selectShortcut( "", custom = showCustom, showNone = self.NONE )
else:
selectedShortcut = LIBRARY.selectShortcut( "", grouping = self.GROUPING, custom = showCustom, showNone = self.NONE )
else:
selectedShortcut = LIBRARY.selectShortcut( "", grouping = "widget", custom = showCustom, showNone = self.NONE )
# Now set the skin strings
if selectedShortcut is None:
# The user cancelled
return
elif selectedShortcut.getProperty( "Path" ) and selectedShortcut.getProperty( "custom" ) == "true":
# The user updated the path - so we just set that property
xbmc.executebuiltin( "Skin.SetString(%s,%s)" %( self.WIDGETPATH, urllib.unquote( selectedShortcut.getProperty( "Path" ) ) ) )
elif selectedShortcut.getProperty( "Path" ):
# The user selected the widget they wanted
if self.WIDGET:
if selectedShortcut.getProperty( "widget" ):
xbmc.executebuiltin( "Skin.SetString(%s,%s)" %( self.WIDGET, selectedShortcut.getProperty( "widget" ) ) )
else:
xbmc.executebuiltin( "Skin.Reset(%s)" %( self.WIDGET ) )
if self.WIDGETTYPE:
if selectedShortcut.getProperty( "widgetType" ):
xbmc.executebuiltin( "Skin.SetString(%s,%s)" %( self.WIDGETTYPE, selectedShortcut.getProperty( "widgetType" ) ) )
else:
xbmc.executebuiltin( "Skin.Reset(%s)" %( self.WIDGETTYPE ) )
if self.WIDGETNAME:
if selectedShortcut.getProperty( "widgetName" ):
xbmc.executebuiltin( "Skin.SetString(%s,%s)" %( self.WIDGETNAME, selectedShortcut.getProperty( "widgetName" ) ) )
else:
xbmc.executebuiltin( "Skin.Reset(%s)" %( self.WIDGETNAME ) )
if self.WIDGETTARGET:
if selectedShortcut.getProperty( "widgetTarget" ):
xbmc.executebuiltin( "Skin.SetString(%s,%s)" %( self.WIDGETTARGET, selectedShortcut.getProperty( "widgetTarget" ) ) )
else:
xbmc.executebuiltin( "Skin.Reset(%s)" %( self.WIDGETTARGET ) )
if self.WIDGETPATH:
if selectedShortcut.getProperty( "widgetPath" ):
xbmc.executebuiltin( "Skin.SetString(%s,%s)" %( self.WIDGETPATH, urllib.unquote( selectedShortcut.getProperty( "widgetPath" ) ) ) )
else:
xbmc.executebuiltin( "Skin.Reset(%s)" %( self.WIDGETPATH ) )
elif selectedShortcut.getLabel() == "::NONE::":
# Clear the skin strings
if self.WIDGET is not None:
xbmc.executebuiltin( "Skin.Reset(" + self.WIDGET + ")" )
if self.WIDGETTYPE is not None:
xbmc.executebuiltin( "Skin.Reset(" + self.WIDGETTYPE + " )" )
if self.WIDGETNAME is not None:
xbmc.executebuiltin( "Skin.Reset(" + self.WIDGETNAME + ")" )
if self.WIDGETTARGET is not None:
xbmc.executebuiltin( "Skin.Reset(" + self.WIDGETTARGET + ")" )
if self.WIDGETPATH is not None:
xbmc.executebuiltin( "Skin.Reset(" + self.WIDGETPATH + ")" )
if self.TYPE=="context":
# Context menu addon asking us to add a folder to the menu
if not xbmc.getCondVisibility( "Skin.HasSetting(SkinShortcuts-FullMenu)" ):
xbmcgui.Dialog().ok(ADDONNAME, ADDON.getLocalizedString(32116))
else:
NODE.addToMenu( self.CONTEXTFILENAME, self.CONTEXTLABEL, self.CONTEXTICON, self.CONTEXTCONTENT, self.CONTEXTWINDOW, DATA )
if self.TYPE=="setProperty":
# External request to set properties of a menu item
NODE.setProperties( self.PROPERTIES, self.VALUES, self.LABELID, self.GROUPNAME, DATA )
if self.TYPE=="resetall":
# Tell XBMC not to try playing any media
try:
xbmcplugin.setResolvedUrl( handle=int( sys.argv[1]), succeeded=False, listitem=xbmcgui.ListItem() )
except:
log( "Not launched from a list item" )
self._reset_all_shortcuts()
def _parse_argv( self ):
try:
params = dict( arg.split( "=" ) for arg in sys.argv[ 1 ].split( "&" ) )
self.TYPE = params.get( "type", "" )
except:
#print_exc()
try:
params = dict( arg.split( "=" ) for arg in sys.argv[ 2 ].split( "&" ) )
self.TYPE = params.get( "?type", "" )
except:
self.TYPE = ""
params = {}
self.GROUP = params.get( "group", "" )
self.GROUPNAME = params.get( "groupname", None )
self.GROUPING = params.get( "grouping", None )
self.PATH = params.get( "path", "" )
self.MENUID = params.get( "mainmenuID", "0" )
self.NEXTACTION = params.get( "action", "0" )
self.LEVELS = params.get( "levels", "0" )
self.MODE = params.get( "mode", None )
self.CHANNEL = params.get( "channel", None )
# Properties when using LIBRARY._displayShortcuts
self.LABEL = params.get( "skinLabel", None )
self.ACTION = params.get( "skinAction", None )
self.SHORTCUTTYPE = params.get( "skinType", None )
self.THUMBNAIL = params.get( "skinThumbnail", None )
self.LIST = params.get( "skinList", None )
self.CUSTOM = params.get( "custom", "False" )
self.NONE = params.get( "showNone", "False" )
self.WIDGET = params.get( "skinWidget", None )
self.WIDGETTYPE = params.get( "skinWidgetType", None )
self.WIDGETNAME = params.get( "skinWidgetName", None )
self.WIDGETTARGET = params.get( "skinWidgetTarget", None )
self.WIDGETPATH = params.get( "skinWidgetPath", None )
if self.CUSTOM == "True" or self.CUSTOM == "true":
self.CUSTOM = True
else:
self.CUSTOM = False
if self.NONE == "True" or self.NONE == "true":
self.NONE = True
else:
self.NONE = False
self.NOLABELS = params.get( "nolabels", "false" ).lower()
self.OPTIONS = params.get( "options", "" ).split( "|" )
self.MINITEMS = int( params.get( "minitems", "0" ) )
self.WARNING = params.get( "warning", None )
self.DEFAULTGROUP = params.get( "defaultGroup", None )
# Properties from context menu addon
self.CONTEXTFILENAME = urllib.unquote( params.get( "filename", "" ) )
self.CONTEXTLABEL = params.get( "label", "" )
self.CONTEXTICON = params.get( "icon", "" )
self.CONTEXTCONTENT = params.get( "content", "" )
self.CONTEXTWINDOW = params.get( "window", "" )
# Properties from external request to set properties
self.PROPERTIES = urllib.unquote( params.get( "property", "" ) )
self.VALUES = urllib.unquote( params.get( "value", "" ) )
self.LABELID = params.get( "labelID", "" )
# -----------------
# PRIMARY FUNCTIONS
# -----------------
def _launch_shortcut( self, path ):
action = urllib.unquote( self.PATH )
if action.find("::MULTIPLE::") == -1:
# Single action, run it
xbmc.executebuiltin( action )
else:
# Multiple actions, separated by |
actions = action.split( "|" )
for singleAction in actions:
if singleAction != "::MULTIPLE::":
xbmc.executebuiltin( singleAction )
def _manage_shortcuts( self, group, defaultGroup, nolabels, groupname ):
homeWindow = xbmcgui.Window( 10000 )
if homeWindow.getProperty( "skinshortcuts-loading" ) and int( calendar.timegm( gmtime() ) ) - int( homeWindow.getProperty( "skinshortcuts-loading" ) ) <= 5:
return
homeWindow.setProperty( "skinshortcuts-loading", str( calendar.timegm( gmtime() ) ) )
import gui
ui= gui.GUI( "script-skinshortcuts.xml", CWD, "default", group=group, defaultGroup=defaultGroup, nolabels=nolabels, groupname=groupname )
ui.doModal()
del ui
# Update home window property (used to automatically refresh type=settings)
homeWindow.setProperty( "skinshortcuts",strftime( "%Y%m%d%H%M%S",gmtime() ) )
# Clear window properties for this group, and for backgrounds, widgets, properties
homeWindow.clearProperty( "skinshortcuts-" + group )
homeWindow.clearProperty( "skinshortcutsWidgets" )
homeWindow.clearProperty( "skinshortcutsCustomProperties" )
homeWindow.clearProperty( "skinshortcutsBackgrounds" )
def _reset_all_shortcuts( self ):
log( "### Resetting all shortcuts" )
dialog = xbmcgui.Dialog()
shouldRun = None
if self.WARNING is not None and self.WARNING.lower() == "false":
shouldRun = True
# Ask the user if they're sure they want to do this
if shouldRun is None:
shouldRun = dialog.yesno( LANGUAGE( 32037 ), LANGUAGE( 32038 ) )
if shouldRun:
isShared = DATA.checkIfMenusShared()
for files in xbmcvfs.listdir( DATAPATH ):
# Try deleting all shortcuts
if files:
for file in files:
deleteFile = False
if file == "settings.xml":
continue
if isShared:
deleteFile = True
elif file.startswith( xbmc.getSkinDir() ) and ( file.endswith( ".properties" ) or file.endswith( ".DATA.xml" ) ):
deleteFile = True
#if file != "settings.xml" and ( not isShared or file.startswith( "%s-" %( xbmc.getSkinDir() ) ) ) or file == "%s.properties" %( xbmc.getSkinDir() ):
if deleteFile:
file_path = os.path.join( DATAPATH, file.decode( 'utf-8' ) ).encode( 'utf-8' )
if xbmcvfs.exists( file_path ):
try:
xbmcvfs.delete( file_path )
except:
print_exc()
log( "### ERROR could not delete file %s" % file )
else:
log( "Not deleting file %s" %[ file ] )
# Update home window property (used to automatically refresh type=settings)
xbmcgui.Window( 10000 ).setProperty( "skinshortcuts",strftime( "%Y%m%d%H%M%S",gmtime() ) )
# Functions for providing whoe menu in single list
def _hidesubmenu( self, menuid ):
count = 0
while xbmc.getCondVisibility( "!%s(Container(%s).ListItem(%i).Property(isSubmenu))" %( ISEMPTY, menuid, count ) ):
count -= 1
if count != 0:
xbmc.executebuiltin( "Control.Move(" + menuid + "," + str( count ) + " )" )
xbmc.executebuiltin( "ClearProperty(submenuVisibility, 10000)" )
def _resetlist( self, menuid, action ):
count = 0
while xbmc.getCondVisibility( "!%s(Container(%s).ListItemNoWrap(%i).Label)" %( ISEMPTY, menuid, count ) ):
count -= 1
count += 1
if count != 0:
xbmc.executebuiltin( "Control.Move(" + menuid + "," + str( count ) + " )" )
xbmc.executebuiltin( urllib.unquote( action ) )
if ( __name__ == "__main__" ):
log('script version %s started' % ADDONVERSION)
# Uncomment when profiling performance
#filename = os.path.join( DATAPATH, strftime( "%Y%m%d%H%M%S",gmtime() ) + "-" + str( random.randrange(0,100000) ) + ".log" )
#cProfile.run( 'Main()', filename )
#stream = open( filename + ".txt", 'w')
#p = pstats.Stats( filename, stream = stream )
#p.sort_stats( "cumulative" )
#p.print_stats()
# Comment out the following line when profiling performance
Main()
log('script stopped')