forked from BigNoid/script.skinshortcuts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdefault.py
More file actions
349 lines (295 loc) · 16.6 KB
/
default.py
File metadata and controls
349 lines (295 loc) · 16.6 KB
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
# coding=utf-8
import os, sys
import xbmc, xbmcaddon, xbmcgui, xbmcplugin, urllib, xbmcvfs
import xml.etree.ElementTree as xmltree
import cPickle as pickle
import cProfile
import pstats
import random
import time
import calendar
from time import gmtime, strftime
from datetime import datetime
from traceback import print_exc
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__ )
__profilepath__ = xbmc.translatePath( "special://profile/" ).decode('utf-8')
__skinpath__ = xbmc.translatePath( "special://skin/shortcuts/" ).decode('utf-8')
__defaultpath__ = xbmc.translatePath( os.path.join( __cwd__, 'resources', 'shortcuts').encode("utf-8") ).decode("utf-8")
__xbmcversion__ = xbmc.getInfoLabel( "System.BuildVersion" ).split(".")[0]
sys.path.append(__resource__)
import xmlfunctions, datafunctions, library, nodefunctions
XML = xmlfunctions.XMLFunctions()
DATA = datafunctions.DataFunctions()
LIBRARY = library.LibraryFunctions()
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":
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
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 = urllib.unquote( selectedShortcut.getProperty( "Path" ) )
if selectedShortcut.getProperty( "chosenPath" ):
path = urllib.unquote( 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
if self.GROUPING:
if self.GROUPING.lower() == "default":
selectedShortcut = LIBRARY.selectShortcut( "", custom = False, showNone = self.NONE )
else:
selectedShortcut = LIBRARY.selectShortcut( "", grouping = self.GROUPING, custom = False, showNone = self.NONE )
else:
selectedShortcut = LIBRARY.selectShortcut( "", grouping = "widget", custom = False, showNone = self.NONE )
# Now set the skin strings
if selectedShortcut is not None and selectedShortcut.getProperty( "Path" ):
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 is not None and 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=="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 )
# -----------------
# 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" )
log( repr( self.WARNING) )
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:
for files in xbmcvfs.listdir( __datapath__ ):
# Try deleting all shortcuts
if files:
for file in files:
if file != "settings.xml":
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[0] )
# 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( "!IsEmpty(Container(" + menuid + ").ListItem(" + str( count ) + ").Property(isSubmenu))" ):
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( "!IsEmpty(Container(" + menuid + ").ListItemNoWrap(" + str( count ) + ").Label)" ):
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__)
# Profiling
#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()
# No profiling
Main()
log('script stopped')