From 44d309e5b70a5ce0093118744d86e57c1e9e0932 Mon Sep 17 00:00:00 2001 From: tomkat83 Date: Tue, 30 Aug 2016 16:13:13 +0200 Subject: [PATCH] Cleanup initialsetup.py --- resources/lib/initialsetup.py | 260 ++++++++++++++++------------------ 1 file changed, 125 insertions(+), 135 deletions(-) diff --git a/resources/lib/initialsetup.py b/resources/lib/initialsetup.py index eed5460d..3396ddc6 100644 --- a/resources/lib/initialsetup.py +++ b/resources/lib/initialsetup.py @@ -2,11 +2,11 @@ ############################################################################### +import logging import xbmc import xbmcgui -import xbmcaddon -import utils +from utils import settings, window, language as lang import clientinfo import downloadutils import userclient @@ -16,12 +16,17 @@ from PlexFunctions import GetMachineIdentifier ############################################################################### +log = logging.getLogger("PLEX."+__name__) + +addonName = 'PlexKodiConnect' + +############################################################################### + -@utils.logging class InitialSetup(): def __init__(self): - self.logMsg('Entering initialsetup class', 1) + log.debug('Entering initialsetup class') self.clientInfo = clientinfo.ClientInfo() self.addonId = self.clientInfo.getAddonId() self.doUtils = downloadutils.DownloadUtils().downloadUrl @@ -29,10 +34,8 @@ class InitialSetup(): self.plx = PlexAPI.PlexAPI() self.dialog = xbmcgui.Dialog() - self.string = xbmcaddon.Addon().getLocalizedString - self.server = self.userClient.getServer() - self.serverid = utils.settings('plex_machineIdentifier') + self.serverid = settings('plex_machineIdentifier') # Get Plex credentials from settings file, if they exist plexdict = self.plx.GetPlexLoginFromSettings() self.myplexlogin = plexdict['myplexlogin'] == 'true' @@ -40,7 +43,7 @@ class InitialSetup(): self.plexToken = plexdict['plexToken'] self.plexid = plexdict['plexid'] if self.plexToken: - self.logMsg('Found a plex.tv token in the settings', 1) + log.debug('Found a plex.tv token in the settings') def PlexTVSignIn(self): """ @@ -66,24 +69,22 @@ class InitialSetup(): chk = self.plx.CheckConnection('plex.tv', token=self.plexToken) if chk in (401, 403): # HTTP Error: unauthorized. Token is no longer valid - self.logMsg('plex.tv connection returned HTTP %s' % str(chk), 1) + log.info('plex.tv connection returned HTTP %s' % str(chk)) # Delete token in the settings - utils.settings('plexToken', value='') - utils.settings('plexLogin', value='') + settings('plexToken', value='') + settings('plexLogin', value='') # Could not login, please try again - self.dialog.ok(self.addonName, - self.string(39009)) + self.dialog.ok(addonName, lang(39009)) answer = self.PlexTVSignIn() elif chk is False or chk >= 400: # Problems connecting to plex.tv. Network or internet issue? - self.logMsg('Problems connecting to plex.tv; connection returned ' - 'HTTP %s' % str(chk), 1) - self.dialog.ok(self.addonName, - self.string(39010)) + log.info('Problems connecting to plex.tv; connection returned ' + 'HTTP %s' % str(chk)) + self.dialog.ok(addonName, lang(39010)) answer = False else: - self.logMsg('plex.tv connection with token successful', 1) - utils.settings('plex_status', value='Logged in to plex.tv') + log.info('plex.tv connection with token successful') + settings('plex_status', value='Logged in to plex.tv') # Refresh the info from Plex.tv xml = self.doUtils('https://plex.tv/users/account', authenticate=False, @@ -91,15 +92,14 @@ class InitialSetup(): try: self.plexLogin = xml.attrib['title'] except (AttributeError, KeyError): - self.logMsg('Failed to update Plex info from plex.tv', -1) + log.error('Failed to update Plex info from plex.tv') else: - utils.settings('plexLogin', value=self.plexLogin) + settings('plexLogin', value=self.plexLogin) home = 'true' if xml.attrib.get('home') == '1' else 'false' - utils.settings('plexhome', value=home) - utils.settings('plexAvatar', value=xml.attrib.get('thumb')) - utils.settings( - 'plexHomeSize', value=xml.attrib.get('homeSize', '1')) - self.logMsg('Updated Plex info from plex.tv', 1) + settings('plexhome', value=home) + settings('plexAvatar', value=xml.attrib.get('thumb')) + settings('plexHomeSize', value=xml.attrib.get('homeSize', '1')) + log.info('Updated Plex info from plex.tv') return answer def CheckPMS(self): @@ -113,27 +113,26 @@ class InitialSetup(): not set before """ answer = True - chk = self.plx.CheckConnection(self.server, - verifySSL=False) + chk = self.plx.CheckConnection(self.server, verifySSL=False) if chk is False: - self.logMsg('Could not reach PMS %s' % self.server, -1) + log.warn('Could not reach PMS %s' % self.server) answer = False if answer is True and not self.serverid: - self.logMsg('No PMS machineIdentifier found for %s. Trying to ' - 'get the PMS unique ID' % self.server, 1) + log.info('No PMS machineIdentifier found for %s. Trying to ' + 'get the PMS unique ID' % self.server) self.serverid = GetMachineIdentifier(self.server) if self.serverid is None: - self.logMsg('Could not retrieve machineIdentifier', -1) + log.warn('Could not retrieve machineIdentifier') answer = False else: - utils.settings('plex_machineIdentifier', value=self.serverid) + settings('plex_machineIdentifier', value=self.serverid) elif answer is True: tempServerid = GetMachineIdentifier(self.server) if tempServerid != self.serverid: - self.logMsg('The current PMS %s was expected to have a ' - 'unique machineIdentifier of %s. But we got ' - '%s. Pick a new server to be sure' - % (self.server, self.serverid, tempServerid), 1) + log.warn('The current PMS %s was expected to have a ' + 'unique machineIdentifier of %s. But we got ' + '%s. Pick a new server to be sure' + % (self.server, self.serverid, tempServerid)) answer = False return answer @@ -144,7 +143,7 @@ class InitialSetup(): self.plx.discoverPMS(xbmc.getIPAddress(), plexToken=self.plexToken) serverlist = self.plx.returnServerList(self.plx.g_PMS) - self.logMsg('PMS serverlist: %s' % serverlist, 2) + log.debug('PMS serverlist: %s' % serverlist) return serverlist def _checkServerCon(self, server): @@ -217,14 +216,14 @@ class InitialSetup(): if item.get('machineIdentifier') == self.serverid: server = item if server is None: - name = utils.settings('plex_servername') - self.logMsg('The PMS you have used before with a unique ' - 'machineIdentifier of %s and name %s is ' - 'offline' % (self.serverid, name), -1) + name = settings('plex_servername') + log.warn('The PMS you have used before with a unique ' + 'machineIdentifier of %s and name %s is ' + 'offline' % (self.serverid, name)) # "PMS xyz offline" - self.dialog.notification(self.addonName, + self.dialog.notification(addonName, '%s %s' - % (name, self.string(39213)), + % (name, lang(39213)), xbmcgui.NOTIFICATION_ERROR, 7000, False) @@ -236,8 +235,8 @@ class InitialSetup(): httpsUpdated = True continue if chk == 401: - self.logMsg('Not yet authorized for Plex server %s' - % server['name'], -1) + log.warn('Not yet authorized for Plex server %s' + % server['name']) if self.CheckPlexTVSignIn() is True: if checkedPlexTV is False: # Try again @@ -245,20 +244,20 @@ class InitialSetup(): httpsUpdated = False continue else: - self.logMsg('Not authorized even though we are signed ' - ' in to plex.tv correctly', -1) - self.dialog.ok(self.addonName, '%s %s' - % self.string(39214) + server['name']) + log.warn('Not authorized even though we are signed ' + ' in to plex.tv correctly') + self.dialog.ok(addonName, '%s %s' + % lang(39214) + server['name']) return else: return # Problems connecting elif chk >= 400 or chk is False: - self.logMsg('Problems connecting to server %s. chk is %s' - % (server['name'], chk), -1) + log.warn('Problems connecting to server %s. chk is %s' + % (server['name'], chk)) return - self.logMsg('We found a server to automatically connect to: %s' - % server['name'], 1) + log.info('We found a server to automatically connect to: %s' + % server['name']) return server def _UserPickPMS(self): @@ -273,8 +272,8 @@ class InitialSetup(): serverlist = self._getServerList() # Exit if no servers found if len(serverlist) == 0: - self.logMsg('No plex media servers found!', -1) - self.dialog.ok(self.addonName, self.string(39011)) + log.warn('No plex media servers found!') + self.dialog.ok(addonName, lang(39011)) return # Get a nicer list dialoglist = [] @@ -282,10 +281,10 @@ class InitialSetup(): if server['local'] == '1': # server is in the same network as client. # Add"local" - msg = self.string(39022) + msg = lang(39022) else: # Add 'remote' - msg = self.string(39054) + msg = lang(39054) if server.get('ownername'): # Display username if its not our PMS dialoglist.append('%s (%s, %s)' @@ -296,7 +295,7 @@ class InitialSetup(): dialoglist.append('%s (%s)' % (server['name'], msg)) # Let user pick server from a list - resp = self.dialog.select(self.string(39012), dialoglist) + resp = self.dialog.select(lang(39012), dialoglist) server = serverlist[resp] chk = self._checkServerCon(server) @@ -307,20 +306,20 @@ class InitialSetup(): continue httpsUpdated = False if chk == 401: - self.logMsg('Not yet authorized for Plex server %s' - % server['name'], -1) + log.warn('Not yet authorized for Plex server %s' + % server['name']) # Please sign in to plex.tv - self.dialog.ok(self.addonName, - self.string(39013) + server['name'], - self.string(39014)) + self.dialog.ok(addonName, + lang(39013) + server['name'], + lang(39014)) if self.PlexTVSignIn() is False: # Exit while loop if user cancels return # Problems connecting elif chk >= 400 or chk is False: # Problems connecting to server. Pick another server? - answ = self.dialog.yesno(self.addonName, - self.string(39015)) + answ = self.dialog.yesno(addonName, + lang(39015)) # Exit while loop if user chooses No if not answ: return @@ -345,37 +344,37 @@ class InitialSetup(): 'ownername' Plex username of PMS owner } """ - utils.settings('plex_machineIdentifier', server['machineIdentifier']) - utils.settings('plex_servername', server['name']) - utils.settings('plex_serverowned', - 'true' if server['owned'] == '1' - else 'false') + settings('plex_machineIdentifier', server['machineIdentifier']) + settings('plex_servername', server['name']) + settings('plex_serverowned', + 'true' if server['owned'] == '1' + else 'false') # Careful to distinguish local from remote PMS if server['local'] == '1': scheme = server['scheme'] - utils.settings('ipaddress', server['ip']) - utils.settings('port', server['port']) - self.logMsg("Setting SSL verify to false, because server is " - "local", 1) - utils.settings('sslverify', 'false') + settings('ipaddress', server['ip']) + settings('port', server['port']) + log.debug("Setting SSL verify to false, because server is " + "local") + settings('sslverify', 'false') else: baseURL = server['baseURL'].split(':') scheme = baseURL[0] - utils.settings('ipaddress', baseURL[1].replace('//', '')) - utils.settings('port', baseURL[2]) - self.logMsg("Setting SSL verify to true, because server is not " - "local", 1) - utils.settings('sslverify', 'true') + settings('ipaddress', baseURL[1].replace('//', '')) + settings('port', baseURL[2]) + log.debug("Setting SSL verify to true, because server is not " + "local") + settings('sslverify', 'true') if scheme == 'https': - utils.settings('https', 'true') + settings('https', 'true') else: - utils.settings('https', 'false') + settings('https', 'false') # And finally do some logging - self.logMsg("Writing to Kodi user settings file", 0) - self.logMsg("PMS machineIdentifier: %s, ip: %s, port: %s, https: %s " - % (server['machineIdentifier'], server['ip'], - server['port'], server['scheme']), 0) + log.debug("Writing to Kodi user settings file") + log.debug("PMS machineIdentifier: %s, ip: %s, port: %s, https: %s " + % (server['machineIdentifier'], server['ip'], + server['port'], server['scheme']), 0) def setup(self): """ @@ -384,13 +383,12 @@ class InitialSetup(): Check server, user, direct paths, music, direct stream if not direct path. """ - self.logMsg("Initial setup called.", 0) + log.info("Initial setup called.") dialog = self.dialog - string = self.string # Optionally sign into plex.tv. Will not be called on very first run # as plexToken will be '' - utils.settings('plex_status', value='Not logged in to plex.tv') + settings('plex_status', value='Not logged in to plex.tv') if self.plexToken and self.myplexlogin: self.CheckPlexTVSignIn() @@ -398,12 +396,11 @@ class InitialSetup(): # return only if the right machine identifier is found getNewIP = False if self.server: - self.logMsg("PMS is already set: %s. Checking now..." - % self.server, 0) + log.info("PMS is already set: %s. Checking now..." % self.server) getNewIP = not self.CheckPMS() if getNewIP is False: - self.logMsg("Using PMS %s with machineIdentifier %s" - % (self.server, self.serverid), 0) + log.info("Using PMS %s with machineIdentifier %s" + % (self.server, self.serverid)) return # If not already retrieved myplex info, optionally let user sign in @@ -417,81 +414,74 @@ class InitialSetup(): self.WritePMStoSettings(server) # User already answered the installation questions - if utils.settings('InstallQuestionsAnswered') == 'true': + if settings('InstallQuestionsAnswered') == 'true': return # Additional settings where the user needs to choose # Direct paths (\\NAS\mymovie.mkv) or addon (http)? - if dialog.yesno(self.addonName, - string(39027), - string(39028), + if dialog.yesno(addonName, + lang(39027), + lang(39028), nolabel="Addon (Default)", yeslabel="Native (Direct Paths)"): - self.logMsg("User opted to use direct paths.", 1) - utils.settings('useDirectPaths', value="1") + log.debug("User opted to use direct paths.") + settings('useDirectPaths', value="1") # Are you on a system where you would like to replace paths # \\NAS\mymovie.mkv with smb://NAS/mymovie.mkv? (e.g. Windows) - if dialog.yesno(heading=self.addonName, - line1=string(39033)): - self.logMsg("User chose to replace paths with smb", 1) + if dialog.yesno(heading=addonName, line1=lang(39033)): + log.debug("User chose to replace paths with smb") else: - utils.settings('replaceSMB', value="false") + settings('replaceSMB', value="false") # complete replace all original Plex library paths with custom SMB - if dialog.yesno(heading=self.addonName, - line1=string(39043)): - self.logMsg("User chose custom smb paths", 1) - utils.settings('remapSMB', value="true") + if dialog.yesno(heading=addonName, line1=lang(39043)): + log.debug("User chose custom smb paths") + settings('remapSMB', value="true") # Please enter your custom smb paths in the settings under # "Sync Options" and then restart Kodi - dialog.ok(heading=self.addonName, - line1=string(39044)) + dialog.ok(heading=addonName, line1=lang(39044)) goToSettings = True # Go to network credentials? - if dialog.yesno(heading=self.addonName, - line1=string(39029), - line2=string(39030)): - self.logMsg("Presenting network credentials dialog.", 1) - utils.passwordsXML() + if dialog.yesno(heading=addonName, + line1=lang(39029), + line2=lang(39030)): + log.debug("Presenting network credentials dialog.") + from utils import passwordsXML + passwordsXML() # Disable Plex music? - if dialog.yesno(heading=self.addonName, - line1=string(39016)): - self.logMsg("User opted to disable Plex music library.", 1) - utils.settings('enableMusic', value="false") + if dialog.yesno(heading=addonName, line1=lang(39016)): + log.debug("User opted to disable Plex music library.") + settings('enableMusic', value="false") else: - utils.advancedSettingsXML() + from utils import advancedSettingsXML + advancedSettingsXML() # Download additional art from FanArtTV - if dialog.yesno(heading=self.addonName, - line1=string(39061)): - self.logMsg("User opted to use FanArtTV", 1) - utils.settings('FanartTV', value="true") + if dialog.yesno(heading=addonName, line1=lang(39061)): + log.debug("User opted to use FanArtTV") + settings('FanartTV', value="true") # Is your Kodi installed on a low-powered device like a Raspberry Pi? # If yes, then we will reduce the strain on Kodi to prevent it from # crashing. - if dialog.yesno(heading=self.addonName, - line1=string(39072)): - self.logMsg('User thinks that PKC runs on a raspi or similar', 1) - utils.settings('imageCacheLimit', value='1') + if dialog.yesno(heading=addonName, line1=lang(39072)): + log.debug('User thinks that PKC runs on a raspi or similar') + settings('imageCacheLimit', value='1') # Make sure that we only ask these questions upon first installation - utils.settings('InstallQuestionsAnswered', value='true') + settings('InstallQuestionsAnswered', value='true') if goToSettings is False: # Open Settings page now? You will need to restart! - goToSettings = dialog.yesno(heading=self.addonName, - line1=string(39017)) + goToSettings = dialog.yesno(heading=addonName, line1=lang(39017)) if goToSettings: - utils.window('plex_serverStatus', value="Stop") + window('plex_serverStatus', value="Stop") xbmc.executebuiltin( 'Addon.OpenSettings(plugin.video.plexkodiconnect)') else: # "Kodi will now restart to apply the changes" - dialog.ok( - heading=self.addonName, - line1=string(33033)) + dialog.ok(heading=addonName, line1=lang(33033)) xbmc.executebuiltin('RestartApp') # We should always restart to ensure e.g. Kodi settings for Music # are in use!