Refactor service.py

This commit is contained in:
tomkat83 2017-01-24 18:48:13 +01:00
parent 1b74f25dfa
commit b1f6bd1edf
6 changed files with 60 additions and 50 deletions

View file

@ -18,6 +18,7 @@
<string id="30014">Connection</string> <string id="30014">Connection</string>
<string id="30015">Network</string> <string id="30015">Network</string>
<string id="30016">Device Name</string> <string id="30016">Device Name</string>
<string id="30017">Unauthorized for PMS</string>
<string id="30022">Advanced</string> <string id="30022">Advanced</string>
<string id="30024">Username</string><!-- Verified --> <string id="30024">Username</string><!-- Verified -->

View file

@ -14,6 +14,8 @@
<string id="30011">Caching-Mechanismus</string> <string id="30011">Caching-Mechanismus</string>
<string id="30012">OK</string> <string id="30012">OK</string>
<string id="30013">Nie anzeigen</string> <string id="30013">Nie anzeigen</string>
<string id="30017">Nicht autorisiert für PMS</string>
<string id="30500">Host SSL Zertifikat überprüfen (sicherer)</string> <string id="30500">Host SSL Zertifikat überprüfen (sicherer)</string>
<string id="30507">Synchronisations-Fortschritt anzeigen</string> <string id="30507">Synchronisations-Fortschritt anzeigen</string>

View file

@ -6,9 +6,7 @@ import logging
import requests import requests
import xml.etree.ElementTree as etree import xml.etree.ElementTree as etree
import xbmcgui from utils import settings, window, language as lang, dialog
from utils import settings, window, language as lang
import clientinfo as client import clientinfo as client
############################################################################### ###############################################################################
@ -280,10 +278,10 @@ class DownloadUtils():
log.debug('Setting PMS server status to ' log.debug('Setting PMS server status to '
'unauthorized') 'unauthorized')
window('plex_serverStatus', value="401") window('plex_serverStatus', value="401")
xbmcgui.Dialog().notification( dialog('notification',
lang(29999), lang(29999),
"Unauthorized for PMS", lang(30017),
xbmcgui.NOTIFICATION_ERROR) icon='{error}')
else: else:
# there might be other 401 where e.g. PMS under strain # there might be other 401 where e.g. PMS under strain
log.info('PMS might only be under strain') log.info('PMS might only be under strain')

View file

@ -91,14 +91,27 @@ def language(stringid):
return ADDON.getLocalizedString(stringid) return ADDON.getLocalizedString(stringid)
def dialog(type_, *args, **kwargs): def dialog(typus, *args, **kwargs):
"""
Displays xbmcgui Dialog. Pass a string as typus:
'yesno', 'ok', 'notification', 'input', 'select', 'numeric'
Icons:
icon='{plex}' Display Plex standard icon
icon='{info}' xbmcgui.NOTIFICATION_INFO
icon='{warning}' xbmcgui.NOTIFICATION_WARNING
icon='{error}' xbmcgui.NOTIFICATION_ERROR
"""
d = xbmcgui.Dialog() d = xbmcgui.Dialog()
if "icon" in kwargs: if "icon" in kwargs:
kwargs['icon'] = kwargs['icon'].replace( repl = {
"{plex}", '{plex}': 'special://home/addons/plugin.video.plexkodiconnect/icon.png',
"special://home/addons/plugin.video.plexkodiconnect/icon.png") '{info}': xbmcgui.NOTIFICATION_INFO,
'{warning}': xbmcgui.NOTIFICATION_WARNING,
'{error}': xbmcgui.NOTIFICATION_ERROR
}
for key, value in repl.iteritems():
kwargs['icon'] = kwargs['icon'].replace(key, value)
if "heading" in kwargs: if "heading" in kwargs:
kwargs['heading'] = kwargs['heading'].replace("{plex}", kwargs['heading'] = kwargs['heading'].replace("{plex}",
language(29999)) language(29999))
@ -111,7 +124,7 @@ def dialog(type_, *args, **kwargs):
'select': d.select, 'select': d.select,
'numeric': d.numeric 'numeric': d.numeric
} }
return types[type_](*args, **kwargs) return types[typus](*args, **kwargs)
def tryEncode(uniString, encoding='utf-8'): def tryEncode(uniString, encoding='utf-8'):

View file

@ -9,6 +9,8 @@ ADDON_VERSION = Addon().getAddonInfo('version')
KODILANGUAGE = xbmc.getLanguage(xbmc.ISO_639_1) KODILANGUAGE = xbmc.getLanguage(xbmc.ISO_639_1)
KODIVERSION = int(xbmc.getInfoLabel("System.BuildVersion")[:2]) KODIVERSION = int(xbmc.getInfoLabel("System.BuildVersion")[:2])
KODILONGVERSION = xbmc.getInfoLabel('System.BuildVersion')
KODI_PROFILE = xbmc.translatePath("special://profile")
if xbmc.getCondVisibility('system.platform.osx'): if xbmc.getCondVisibility('system.platform.osx'):
PLATFORM = "MacOSX" PLATFORM = "MacOSX"

View file

@ -3,35 +3,34 @@
############################################################################### ###############################################################################
import logging import logging
import os from os import path as os_path
import sys from sys import path as sys_path
import xbmc from xbmc import translatePath, Monitor, sleep
import xbmcaddon from xbmcaddon import Addon
import xbmcgui
############################################################################### ###############################################################################
_addon = xbmcaddon.Addon(id='plugin.video.plexkodiconnect') _addon = Addon(id='plugin.video.plexkodiconnect')
try: try:
_addon_path = _addon.getAddonInfo('path').decode('utf-8') _addon_path = _addon.getAddonInfo('path').decode('utf-8')
except TypeError: except TypeError:
_addon_path = _addon.getAddonInfo('path').decode() _addon_path = _addon.getAddonInfo('path').decode()
try: try:
_base_resource = xbmc.translatePath(os.path.join( _base_resource = translatePath(os_path.join(
_addon_path, _addon_path,
'resources', 'resources',
'lib')).decode('utf-8') 'lib')).decode('utf-8')
except TypeError: except TypeError:
_base_resource = xbmc.translatePath(os.path.join( _base_resource = translatePath(os_path.join(
_addon_path, _addon_path,
'resources', 'resources',
'lib')).decode() 'lib')).decode()
sys.path.append(_base_resource) sys_path.append(_base_resource)
############################################################################### ###############################################################################
from utils import settings, window, language as lang from utils import settings, window, language as lang, dialog
from userclient import UserClient from userclient import UserClient
import initialsetup import initialsetup
from kodimonitor import KodiMonitor from kodimonitor import KodiMonitor
@ -81,11 +80,11 @@ class Service():
def __init__(self): def __init__(self):
logLevel = self.getLogLevel() logLevel = self.getLogLevel()
self.monitor = xbmc.Monitor() self.monitor = Monitor()
window('plex_logLevel', value=str(logLevel)) window('plex_logLevel', value=str(logLevel))
window('plex_kodiProfile', window('plex_kodiProfile',
value=xbmc.translatePath("special://profile")) value=translatePath("special://profile"))
window('plex_context', window('plex_context',
value='true' if settings('enableContext') == "true" else "") value='true' if settings('enableContext') == "true" else "")
window('fetch_pms_item_number', window('fetch_pms_item_number',
@ -94,7 +93,7 @@ class Service():
# Initial logging # Initial logging
log.warn("======== START %s ========" % v.ADDON_NAME) log.warn("======== START %s ========" % v.ADDON_NAME)
log.warn("Platform: %s" % v.PLATFORM) log.warn("Platform: %s" % v.PLATFORM)
log.warn("KODI Version: %s" % xbmc.getInfoLabel('System.BuildVersion')) log.warn("KODI Version: %s" % v.KODILONGVERSION)
log.warn("%s Version: %s" % (v.ADDON_NAME, v.ADDON_VERSION)) log.warn("%s Version: %s" % (v.ADDON_NAME, v.ADDON_VERSION))
log.warn("Using plugin paths: %s" log.warn("Using plugin paths: %s"
% (settings('useDirectPaths') != "true")) % (settings('useDirectPaths') != "true"))
@ -137,7 +136,7 @@ class Service():
# Important: Threads depending on abortRequest will not trigger # Important: Threads depending on abortRequest will not trigger
# if profile switch happens more than once. # if profile switch happens more than once.
monitor = self.monitor monitor = self.monitor
kodiProfile = xbmc.translatePath("special://profile") kodiProfile = v.KODI_PROFILE
# Detect playback start early on # Detect playback start early on
self.monitor_kodi_play = Monitor_Kodi_Play(self) self.monitor_kodi_play = Monitor_Kodi_Play(self)
@ -184,12 +183,10 @@ class Service():
if welcome_msg is True: if welcome_msg is True:
# Reset authentication warnings # Reset authentication warnings
welcome_msg = False welcome_msg = False
xbmcgui.Dialog().notification( dialog.notification(
heading=lang(29999), lang(29999),
message="%s %s" % (lang(33000), "%s %s" % (lang(33000),
self.user.currUser), self.user.currUser),
icon="special://home/addons/plugin."
"video.plexkodiconnect/icon.png",
time=2000, time=2000,
sound=False) sound=False)
# Start monitoring kodi events # Start monitoring kodi events
@ -237,7 +234,7 @@ class Service():
if monitor.waitForAbort(5): if monitor.waitForAbort(5):
# Abort was requested while waiting. We should exit # Abort was requested while waiting. We should exit
break break
xbmc.sleep(50) sleep(50)
else: else:
# Wait until Plex server is online # Wait until Plex server is online
# or Kodi is shut down. # or Kodi is shut down.
@ -256,13 +253,11 @@ class Service():
window('suspend_LibraryThread', value='true') window('suspend_LibraryThread', value='true')
log.error("Plex Media Server went offline") log.error("Plex Media Server went offline")
if settings('show_pms_offline') == 'true': if settings('show_pms_offline') == 'true':
xbmcgui.Dialog().notification( dialog('notification',
heading=lang(33001), lang(33001),
message="%s %s" "%s %s" % (lang(29999), lang(33002)),
% (lang(29999), lang(33002)), icon='{plex}',
icon="special://home/addons/plugin.video." sound=False)
"plexkodiconnect/icon.png",
sound=False)
counter += 1 counter += 1
# Periodically check if the IP changed, e.g. per minute # Periodically check if the IP changed, e.g. per minute
if counter > 20: if counter > 20:
@ -284,13 +279,12 @@ class Service():
# Alert the user that server is online. # Alert the user that server is online.
if (welcome_msg is False and if (welcome_msg is False and
settings('show_pms_offline') == 'true'): settings('show_pms_offline') == 'true'):
xbmcgui.Dialog().notification( dialog('notification',
heading=lang(29999), lang(29999),
message=lang(33003), lang(33003),
icon="special://home/addons/plugin.video." icon='{plex}',
"plexkodiconnect/icon.png", time=5000,
time=5000, sound=False)
sound=False)
log.info("Server %s is online and ready." % server) log.info("Server %s is online and ready." % server)
window('plex_online', value="true") window('plex_online', value="true")
if window('plex_authenticated') == 'true': if window('plex_authenticated') == 'true':
@ -344,7 +338,7 @@ class Service():
delay = int(settings('startupDelay')) delay = int(settings('startupDelay'))
log.warn("Delaying Plex startup by: %s sec..." % delay) log.warn("Delaying Plex startup by: %s sec..." % delay)
if delay and xbmc.Monitor().waitForAbort(delay): if delay and Monitor().waitForAbort(delay):
# Start the service # Start the service
log.warn("Abort requested while waiting. PKC not started.") log.warn("Abort requested while waiting. PKC not started.")
else: else: