forked from malvinas2/script.videoextras
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathestuaryupdate.py
150 lines (124 loc) · 6.81 KB
/
estuaryupdate.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
# -*- coding: utf-8 -*-
import os
import traceback
import xbmc
import xbmcaddon
import xbmcvfs
import xbmcgui
import datetime
# Import the common settings
from resources.lib.settings import log
from resources.lib.settings import os_path_join
ADDON = xbmcaddon.Addon(id='script.videoextras')
CWD = ADDON.getAddonInfo('path')
RES_DIR = xbmcvfs.translatePath(os.path.join(CWD, 'resources'))
# Ideally we would use an XML parser to do this like ElementTree
# However they all end up re-ordering the attributes, so doing a diff
# between changed files is very hard, so for this reason we do it
# all manually without the aid of an XML parser
class EstuaryUpdate():
def __init__(self):
# Find out where the estuary skin files are located
estuaryAddon = xbmcaddon.Addon(id='skin.estuary')
self.estuarypath = xbmcvfs.translatePath(estuaryAddon.getAddonInfo('path'))
self.estuarypath = os_path_join(self.estuarypath, 'xml')
log("Estuary Location: %s" % self.estuarypath)
# Create the timestamp centrally, as we want all files changed for a single
# run to have the same backup timestamp so it can be easily undone if the
# user wishes to switch it back
self.bak_timestamp = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
self.errorToLog = False
# Method to update all the required Estuary files
def updateSkin(self):
# Update the files one at a time
self._updateDialogVideoInfo()
# Now either print the complete message or the "check log" message
if self.errorToLog:
xbmcgui.Dialog().ok(ADDON.getLocalizedString(32001), ADDON.getLocalizedString(32157), ADDON.getLocalizedString(32152))
else:
xbmcgui.Dialog().ok(ADDON.getLocalizedString(32001), ADDON.getLocalizedString(32162), ADDON.getLocalizedString(32159))
# Save the new contents, taking a backup of the old file
def _saveNewFile(self, dialogXml, dialogXmlStr):
log("SaveNewFile: New file content: %s" % dialogXmlStr)
# Now save the file to disk, start by backing up the old file
xbmcvfs.copy(dialogXml, "%s.videoextras-%s.bak" % (dialogXml, self.bak_timestamp))
# Now save the new file
dialogXmlFile = xbmcvfs.File(dialogXml, 'w')
dialogXmlFile.write(dialogXmlStr)
dialogXmlFile.close()
##########################################################################
# UPDATES FOR DialogVideoInfo.xml
##########################################################################
# Makes all the required changes to DialogVideoInfo.xml
def _updateDialogVideoInfo(self):
# Get the location of the information dialog XML file
dialogXml = os_path_join(self.estuarypath, 'DialogVideoInfo.xml')
log("DialogVideoInfo: Estuary dialog XML file: %s" % dialogXml)
# Make sure the file exists (It should always exist)
if not xbmcvfs.exists(dialogXml):
log("DialogVideoInfo: Unable to find the file DialogVideoInfo.xml, skipping file", xbmc.LOGERROR)
self.errorToLog = True
return
# Load the DialogVideoInfo.xml into a string
dialogXmlFile = xbmcvfs.File(dialogXml, 'r')
dialogXmlStr = dialogXmlFile.read()
dialogXmlFile.close()
# Now check to see if the skin file has already had the video extras bits added
if 'script.videoextras' in dialogXmlStr:
# Already have video extras referenced, so we do not want to do anything else
# to this file
log("DialogVideoInfo: Video extras already referenced in %s, skipping file" % dialogXml, xbmc.LOGINFO)
self.errorToLog = True
return
# Start by adding the onLoad section
previousOnLoad = '<controls>'
if previousOnLoad not in dialogXmlStr:
# The file has had a standard component deleted, so quit
log("DialogVideoInfo: Could not find controls command, skipping file", xbmc.LOGERROR)
self.errorToLog = True
return
# Now add the Video Extras onLoad command after the allowoverlay one
DIALOG_VIDEO_INFO_ONLOAD = '<onload condition="System.HasAddon(script.videoextras)">RunScript(script.videoextras,check,"$INFO[ListItem.FilenameAndPath]")</onload>\n\t'
insertTxt = DIALOG_VIDEO_INFO_ONLOAD + previousOnLoad
dialogXmlStr = dialogXmlStr.replace(previousOnLoad, insertTxt)
# Now we need to add the button after the Final button
previousButton = '<param name="label" value="$VAR[VideoInfoPlayButtonLabelVar]" />'
if previousButton not in dialogXmlStr:
# The file has had a standard component deleted, so quit
log("DialogVideoInfo: Could not find play button, skipping file", xbmc.LOGERROR)
self.errorToLog = True
return
# Check to make sure we use a unique ID value for the button
idOK = False
idval = 200
while not idOK:
idStr = '<param name="id" value="%d"' % idval
if idStr not in dialogXmlStr:
idOK = True
else:
idval = idval + 1
# Now add the Video Extras button after the Play one
DIALOG_VIDEO_INFO_BUTTON = '''\n\t\t\t\t\t</include>\n\t\t\t\t\t<include content="InfoDialogButton">
\t\t\t\t\t\t<param name="id" value="%d" />
\t\t\t\t\t\t<param name="icon" value="special://masterprofile/../addons/script.videoextras/resources/skins/icons/overlay3.png" />
\t\t\t\t\t\t<param name="label" value="$ADDON[script.videoextras 32001]" />
\t\t\t\t\t\t<param name="onclick_1" value="Action(close)" />
\t\t\t\t\t\t<param name="onclick_2" value="RunScript(script.videoextras,display,$INFO[ListItem.FilenameAndPath])" />
\t\t\t\t\t\t<param name="visible" value="System.HasAddon(script.videoextras) + [String.IsEqual(ListItem.DBType,movie) | String.IsEqual(ListItem.DBType,tvshow) | String.IsEqual(ListItem.DBType,season) | String.IsEqual(ListItem.DBType,episode)] + String.IsEmpty(Window(movieinformation).Property(HideVideoExtrasButton))" />'''
insertTxt = previousButton + (DIALOG_VIDEO_INFO_BUTTON % idval)
dialogXmlStr = dialogXmlStr.replace(previousButton, insertTxt)
self._saveNewFile(dialogXml, dialogXmlStr)
#########################
# Main
#########################
if __name__ == '__main__':
log("VideoExtras: Updating Estuary Skin (version %s)" % ADDON.getAddonInfo('version'))
doUpdate = xbmcgui.Dialog().yesno(ADDON.getLocalizedString(32001), ADDON.getLocalizedString(32163))
if doUpdate:
try:
estuaryUp = EstuaryUpdate()
estuaryUp.updateSkin()
del estuaryUp
except:
log("VideoExtras: %s" % traceback.format_exc(), xbmc.LOGERROR)
xbmcgui.Dialog().ok(ADDON.getLocalizedString(32001), ADDON.getLocalizedString(32164), ADDON.getLocalizedString(32152))