From 9314c4a363ddac26fcdb84e9bb75b79215a89def Mon Sep 17 00:00:00 2001 From: angelblue05 Date: Thu, 16 Jun 2016 16:24:07 -0500 Subject: [PATCH] Centralized Logging --- resources/lib/artwork.py | 6 ++-- resources/lib/clientinfo.py | 30 ++++++++-------- resources/lib/playbackutils.py | 2 +- resources/lib/playlist.py | 46 +++++++++++------------- resources/lib/playutils.py | 64 ++++++++++++++-------------------- 5 files changed, 67 insertions(+), 81 deletions(-) diff --git a/resources/lib/artwork.py b/resources/lib/artwork.py index 2c8afea9..663cfb06 100644 --- a/resources/lib/artwork.py +++ b/resources/lib/artwork.py @@ -279,7 +279,7 @@ class Artwork(): # add a new thread or wait and retry if we hit our limit if len(self.imageCacheThreads) < self.imageCacheLimitThreads: newThread = image_cache_thread.image_cache_thread() - newThread.setUrl(self.double_urlencode(urlToAdd)) + newThread.setUrl(self.double_urlencode(url)) newThread.setHost(self.xbmc_host, self.xbmc_port) newThread.setAuth(self.xbmc_username, self.xbmc_password) newThread.start() @@ -519,7 +519,7 @@ class Artwork(): % (self.server, item_id, item_type)) return image - def getAllArtwork(self, item, parent_artwork=False): + def getAllArtwork(self, item, parentInfo=False): itemid = item['Id'] artworks = item['ImageTags'] @@ -566,7 +566,7 @@ class Artwork(): allartworks[art] = artwork # Process parent items if the main item is missing artwork - if parent_artwork: + if parentInfo: # Process parent backdrops if not allartworks['Backdrop']: diff --git a/resources/lib/clientinfo.py b/resources/lib/clientinfo.py index 8b3fe655..d5550388 100644 --- a/resources/lib/clientinfo.py +++ b/resources/lib/clientinfo.py @@ -9,7 +9,7 @@ import xbmc import xbmcaddon import xbmcvfs -import utils +from utils import Logging, window, settings ################################################################################################# @@ -19,14 +19,12 @@ class ClientInfo(): def __init__(self): + global log + log = Logging(self.__class__.__name__).log + self.addon = xbmcaddon.Addon() self.addonName = self.getAddonName() - def logMsg(self, msg, lvl=1): - - className = self.__class__.__name__ - utils.logMsg("%s %s" % (self.addonName, className), msg, lvl) - def getAddonName(self): # Used for logging @@ -42,11 +40,11 @@ class ClientInfo(): def getDeviceName(self): - if utils.settings('deviceNameOpt') == "false": + if settings('deviceNameOpt') == "false": # Use Kodi's deviceName deviceName = xbmc.getInfoLabel('System.FriendlyName').decode('utf-8') else: - deviceName = utils.settings('deviceName') + deviceName = settings('deviceName') deviceName = deviceName.replace("\"", "_") deviceName = deviceName.replace("/", "_") @@ -71,16 +69,18 @@ class ClientInfo(): def getDeviceId(self, reset=False): - clientId = utils.window('emby_deviceId') + clientId = window('emby_deviceId') if clientId: return clientId addon_path = self.addon.getAddonInfo('path').decode('utf-8') if os.path.supports_unicode_filenames: - GUID_file = xbmc.translatePath(os.path.join(addon_path, "machine_guid")).decode('utf-8') + path = os.path.join(addon_path, "machine_guid") else: - GUID_file = xbmc.translatePath(os.path.join(addon_path.encode("utf-8"), "machine_guid")).decode('utf-8') - + path = os.path.join(addon_path.encode('utf-8'), "machine_guid") + + GUID_file = xbmc.translatePath(path).decode('utf-8') + if reset and xbmcvfs.exists(GUID_file): # Reset the file xbmcvfs.delete(GUID_file) @@ -88,14 +88,14 @@ class ClientInfo(): GUID = xbmcvfs.File(GUID_file) clientId = GUID.read() if not clientId: - self.logMsg("Generating a new deviceid...", 1) + log("Generating a new deviceid...", 1) clientId = str("%012X" % uuid4()) GUID = xbmcvfs.File(GUID_file, 'w') GUID.write(clientId) GUID.close() - self.logMsg("DeviceId loaded: %s" % clientId, 1) - utils.window('emby_deviceId', value=clientId) + log("DeviceId loaded: %s" % clientId, 1) + window('emby_deviceId', value=clientId) return clientId \ No newline at end of file diff --git a/resources/lib/playbackutils.py b/resources/lib/playbackutils.py index 0afb0c8b..4da87f27 100644 --- a/resources/lib/playbackutils.py +++ b/resources/lib/playbackutils.py @@ -271,7 +271,7 @@ class PlaybackUtils(): def setArtwork(self, listItem): # Set up item and item info - allartwork = self.artwork.getAllArtwork(self.item, parent_artwork=True) + allartwork = self.artwork.getAllArtwork(self.item, parentInfo=True) # Set artwork for listitem arttypes = { diff --git a/resources/lib/playlist.py b/resources/lib/playlist.py index bcd34a46..1f0819b6 100644 --- a/resources/lib/playlist.py +++ b/resources/lib/playlist.py @@ -13,7 +13,7 @@ import playutils import playbackutils import embydb_functions as embydb import read_embyserver as embyserver -import utils +from utils import Logging, window, settings, language as lang, kodiSQL ################################################################################################# @@ -23,25 +23,21 @@ class Playlist(): def __init__(self): + global log + log = Logging(self.__class__.__name__).log + self.clientInfo = clientinfo.ClientInfo() self.addonName = self.clientInfo.getAddonName() - self.userid = utils.window('emby_currUser') - self.server = utils.window('emby_server%s' % self.userid) + self.userid = window('emby_currUser') + self.server = window('emby_server%s' % self.userid) self.emby = embyserver.Read_EmbyServer() - def logMsg(self, msg, lvl=1): - - self.className = self.__class__.__name__ - utils.logMsg("%s %s" % (self.addonName, self.className), msg, lvl) - def playAll(self, itemids, startat): - window = utils.window - - embyconn = utils.kodiSQL('emby') + embyconn = kodiSQL('emby') embycursor = embyconn.cursor() emby_db = embydb.Embydb_Functions(embycursor) @@ -49,8 +45,8 @@ class Playlist(): playlist = xbmc.PlayList(xbmc.PLAYLIST_VIDEO) playlist.clear() - self.logMsg("---*** PLAY ALL ***---", 1) - self.logMsg("Items: %s and start at: %s" % (itemids, startat), 1) + log("---*** PLAY ALL ***---", 1) + log("Items: %s and start at: %s" % (itemids, startat), 1) started = False window('emby_customplaylist', value="true") @@ -66,14 +62,14 @@ class Playlist(): mediatype = embydb_item[4] except TypeError: # Item is not found in our database, add item manually - self.logMsg("Item was not found in the database, manually adding item.", 1) + log("Item was not found in the database, manually adding item.", 1) item = self.emby.getItem(itemid) self.addtoPlaylist_xbmc(playlist, item) else: # Add to playlist self.addtoPlaylist(dbid, mediatype) - self.logMsg("Adding %s to playlist." % itemid, 1) + log("Adding %s to playlist." % itemid, 1) if not started: started = True @@ -84,12 +80,12 @@ class Playlist(): def modifyPlaylist(self, itemids): - embyconn = utils.kodiSQL('emby') + embyconn = kodiSQL('emby') embycursor = embyconn.cursor() emby_db = embydb.Embydb_Functions(embycursor) - self.logMsg("---*** ADD TO PLAYLIST ***---", 1) - self.logMsg("Items: %s" % itemids, 1) + log("---*** ADD TO PLAYLIST ***---", 1) + log("Items: %s" % itemids, 1) player = xbmc.Player() playlist = xbmc.PlayList(xbmc.PLAYLIST_VIDEO) @@ -107,7 +103,7 @@ class Playlist(): # Add to playlist self.addtoPlaylist(dbid, mediatype) - self.logMsg("Adding %s to playlist." % itemid, 1) + log("Adding %s to playlist." % itemid, 1) self.verifyPlaylist() embycursor.close() @@ -130,17 +126,17 @@ class Playlist(): else: pl['params']['item'] = {'file': url} - self.logMsg(xbmc.executeJSONRPC(json.dumps(pl)), 2) + log(xbmc.executeJSONRPC(json.dumps(pl)), 2) def addtoPlaylist_xbmc(self, playlist, item): playurl = playutils.PlayUtils(item).getPlayUrl() if not playurl: # Playurl failed - self.logMsg("Failed to retrieve playurl.", 1) + log("Failed to retrieve playurl.", 1) return - self.logMsg("Playurl: %s" % playurl) + log("Playurl: %s" % playurl) listitem = xbmcgui.ListItem() playbackutils.PlaybackUtils(item).setProperties(playurl, listitem) @@ -164,7 +160,7 @@ class Playlist(): else: pl['params']['item'] = {'file': url} - self.logMsg(xbmc.executeJSONRPC(json.dumps(pl)), 2) + log(xbmc.executeJSONRPC(json.dumps(pl)), 2) def verifyPlaylist(self): @@ -178,7 +174,7 @@ class Playlist(): 'playlistid': 1 } } - self.logMsg(xbmc.executeJSONRPC(json.dumps(pl)), 2) + log(xbmc.executeJSONRPC(json.dumps(pl)), 2) def removefromPlaylist(self, position): @@ -193,4 +189,4 @@ class Playlist(): 'position': position } } - self.logMsg(xbmc.executeJSONRPC(json.dumps(pl)), 2) + log(xbmc.executeJSONRPC(json.dumps(pl)), 2) \ No newline at end of file diff --git a/resources/lib/playutils.py b/resources/lib/playutils.py index a1de2948..ed9b57b0 100644 --- a/resources/lib/playutils.py +++ b/resources/lib/playutils.py @@ -7,7 +7,7 @@ import xbmcgui import xbmcvfs import clientinfo -import utils +from utils import Logging, window, settings, language as lang ################################################################################################# @@ -17,41 +17,37 @@ class PlayUtils(): def __init__(self, item): + global log + log = Logging(self.__class__.__name__).log + self.item = item self.clientInfo = clientinfo.ClientInfo() self.addonName = self.clientInfo.getAddonName() - self.userid = utils.window('emby_currUser') - self.server = utils.window('emby_server%s' % self.userid) - - def logMsg(self, msg, lvl=1): - - self.className = self.__class__.__name__ - utils.logMsg("%s %s" % (self.addonName, self.className), msg, lvl) + self.userid = window('emby_currUser') + self.server = window('emby_server%s' % self.userid) def getPlayUrl(self): - window = utils.window - playurl = None - if (self.item.get('Type') in ("Recording", "TvChannel") and - self.item.get('MediaSources') and self.item['MediaSources'][0]['Protocol'] == "Http"): + if (self.item.get('Type') in ("Recording", "TvChannel") and self.item.get('MediaSources') + and self.item['MediaSources'][0]['Protocol'] == "Http"): # Play LiveTV or recordings - self.logMsg("File protocol is http (livetv).", 1) + log("File protocol is http (livetv).", 1) playurl = "%s/emby/Videos/%s/live.m3u8?static=true" % (self.server, self.item['Id']) window('emby_%s.playmethod' % playurl, value="Transcode") elif self.item.get('MediaSources') and self.item['MediaSources'][0]['Protocol'] == "Http": # Only play as http, used for channels, or online hosting of content - self.logMsg("File protocol is http.", 1) + log("File protocol is http.", 1) playurl = self.httpPlay() window('emby_%s.playmethod' % playurl, value="DirectStream") elif self.isDirectPlay(): - self.logMsg("File is direct playing.", 1) + log("File is direct playing.", 1) playurl = self.directPlay() playurl = playurl.encode('utf-8') # Set playmethod property @@ -59,14 +55,14 @@ class PlayUtils(): elif self.isDirectStream(): - self.logMsg("File is direct streaming.", 1) + log("File is direct streaming.", 1) playurl = self.directStream() # Set playmethod property window('emby_%s.playmethod' % playurl, value="DirectStream") elif self.isTranscoding(): - self.logMsg("File is transcoding.", 1) + log("File is transcoding.", 1) playurl = self.transcoding() # Set playmethod property window('emby_%s.playmethod' % playurl, value="Transcode") @@ -88,21 +84,18 @@ class PlayUtils(): def isDirectPlay(self): - lang = utils.language - settings = utils.settings dialog = xbmcgui.Dialog() - # Requirement: Filesystem, Accessible path if settings('playFromStream') == "true": # User forcing to play via HTTP - self.logMsg("Can't direct play, play from HTTP enabled.", 1) + log("Can't direct play, play from HTTP enabled.", 1) return False videotrack = self.item['MediaSources'][0]['Name'] transcodeH265 = settings('transcodeH265') videoprofiles = [x['Profile'] for x in self.item['MediaSources'][0]['MediaStreams'] if 'Profile' in x] - transcodeHi10P = utils.settings('transcodeHi10P') + transcodeHi10P = settings('transcodeHi10P') if transcodeHi10P == "true" and "H264" in videotrack and "High 10" in videoprofiles: return False @@ -116,7 +109,7 @@ class PlayUtils(): '2': 720, '3': 1080 } - self.logMsg("Resolution is: %sP, transcode for resolution: %sP+" + log("Resolution is: %sP, transcode for resolution: %sP+" % (resolution, res[transcodeH265]), 1) if res[transcodeH265] <= resolution: return False @@ -124,19 +117,19 @@ class PlayUtils(): canDirectPlay = self.item['MediaSources'][0]['SupportsDirectPlay'] # Make sure direct play is supported by the server if not canDirectPlay: - self.logMsg("Can't direct play, server doesn't allow/support it.", 1) + log("Can't direct play, server doesn't allow/support it.", 1) return False location = self.item['LocationType'] if location == "FileSystem": # Verify the path if not self.fileExists(): - self.logMsg("Unable to direct play.") + log("Unable to direct play.", 1) try: count = int(settings('failCount')) except ValueError: count = 0 - self.logMsg("Direct play failed: %s times." % count, 1) + log("Direct play failed: %s times." % count, 1) if count < 2: # Let the user know that direct play failed @@ -192,23 +185,22 @@ class PlayUtils(): # Convert path to direct play path = self.directPlay() - self.logMsg("Verifying path: %s" % path, 1) + log("Verifying path: %s" % path, 1) if xbmcvfs.exists(path): - self.logMsg("Path exists.", 1) + log("Path exists.", 1) return True elif ":" not in path: - self.logMsg("Can't verify path, assumed linux. Still try to direct play.", 1) + log("Can't verify path, assumed linux. Still try to direct play.", 1) return True else: - self.logMsg("Failed to find file.", 1) + log("Failed to find file.", 1) return False def isDirectStream(self): - videotrack = self.item['MediaSources'][0]['Name'] transcodeH265 = utils.settings('transcodeH265') videoprofiles = [x['Profile'] for x in self.item['MediaSources'][0]['MediaStreams'] if 'Profile' in x] @@ -226,7 +218,7 @@ class PlayUtils(): '2': 720, '3': 1080 } - self.logMsg("Resolution is: %sP, transcode for resolution: %sP+" + log("Resolution is: %sP, transcode for resolution: %sP+" % (resolution, res[transcodeH265]), 1) if res[transcodeH265] <= resolution: return False @@ -239,7 +231,7 @@ class PlayUtils(): # Verify the bitrate if not self.isNetworkSufficient(): - self.logMsg("The network speed is insufficient to direct stream file.", 1) + log("The network speed is insufficient to direct stream file.", 1) return False return True @@ -258,15 +250,14 @@ class PlayUtils(): def isNetworkSufficient(self): - settings = self.getBitrate()*1000 try: sourceBitrate = int(self.item['MediaSources'][0]['Bitrate']) except (KeyError, TypeError): - self.logMsg("Bitrate value is missing.", 1) + log("Bitrate value is missing.", 1) else: - self.logMsg("The add-on settings bitrate is: %s, the video bitrate required is: %s" + log("The add-on settings bitrate is: %s, the video bitrate required is: %s" % (settings, sourceBitrate), 1) if settings < sourceBitrate: return False @@ -329,7 +320,6 @@ class PlayUtils(): def audioSubsPref(self, url, listitem): - lang = utils.language dialog = xbmcgui.Dialog() # For transcoding only # Present the list of audio to select from