From d3ce04ab10a6f270aa20d7bcb5e1be72871a5970 Mon Sep 17 00:00:00 2001 From: shaun Date: Sat, 5 Nov 2016 11:15:28 +1100 Subject: [PATCH] move DB reset function to the databse file use the new DB connection context for all DB reset actions --- default.py | 3 +- resources/lib/database.py | 100 ++++++++++++++++++++++++++++++++++- resources/lib/librarysync.py | 2 +- resources/lib/utils.py | 95 +-------------------------------- 4 files changed, 102 insertions(+), 98 deletions(-) diff --git a/default.py b/default.py index e52c524b..eae867f6 100644 --- a/default.py +++ b/default.py @@ -23,6 +23,7 @@ import entrypoint import loghandler from utils import window, dialog, language as lang from ga_client import GoogleAnalytics +import database ################################################################################################# @@ -80,7 +81,7 @@ class Main(object): import utils modes = { - 'reset': utils.reset, + 'reset': database.db_reset, 'resetauth': entrypoint.resetAuth, 'play': entrypoint.doPlayback, 'passwords': utils.passwordsXML, diff --git a/resources/lib/database.py b/resources/lib/database.py index d36ca24d..86708a8f 100644 --- a/resources/lib/database.py +++ b/resources/lib/database.py @@ -4,10 +4,16 @@ import logging import sqlite3 +from contextlib import closing +import sys import xbmc +import xbmcaddon +import xbmcgui +import xbmcplugin +import xbmcvfs -from utils import window, should_stop, settings +from utils import window, should_stop, settings, language, deletePlaylists, deleteNodes ################################################################################################# @@ -120,8 +126,98 @@ class DatabaseConn(object): if self.commit_on_close == True and changes: log.info("number of rows updated: %s", changes) - kodi_commit() + if self.db_file == "video": + kodi_commit() self.conn.commit() log.info("close: %s", self.path) self.conn.close() + + +def db_reset(): + + dialog = xbmcgui.Dialog() + + if not dialog.yesno(language(29999), language(33074)): + return + + # first stop any db sync + window('emby_online', value="reset") + window('emby_shouldStop', value="true") + count = 10 + while window('emby_dbScan') == "true": + log.info("Sync is running, will retry: %s..." % count) + count -= 1 + if count == 0: + dialog.ok(language(29999), language(33085)) + return + xbmc.sleep(1000) + + # Clean up the playlists + deletePlaylists() + + # Clean up the video nodes + deleteNodes() + + # Wipe the kodi databases + log.warn("Resetting the Kodi video database.") + with DatabaseConn('video') as conn: + with closing(conn.cursor()) as cursor: + cursor.execute('SELECT tbl_name FROM sqlite_master WHERE type="table"') + rows = cursor.fetchall() + for row in rows: + tablename = row[0] + if tablename != "version": + cursor.execute("DELETE FROM " + tablename) + + if settings('enableMusic') == "true": + log.warn("Resetting the Kodi music database.") + with DatabaseConn('music') as conn: + with closing(conn.cursor()) as cursor: + cursor.execute('SELECT tbl_name FROM sqlite_master WHERE type="table"') + rows = cursor.fetchall() + for row in rows: + tablename = row[0] + if tablename != "version": + cursor.execute("DELETE FROM " + tablename) + + # Wipe the emby database + log.warn("Resetting the Emby database.") + with DatabaseConn('emby') as conn: + with closing(conn.cursor()) as cursor: + cursor.execute('SELECT tbl_name FROM sqlite_master WHERE type="table"') + rows = cursor.fetchall() + for row in rows: + tablename = row[0] + if tablename != "version": + cursor.execute("DELETE FROM " + tablename) + cursor.execute('DROP table IF EXISTS emby') + cursor.execute('DROP table IF EXISTS view') + cursor.execute("DROP table IF EXISTS version") + + # Offer to wipe cached thumbnails + if dialog.yesno(language(29999), language(33086)): + log.warn("Resetting all cached artwork") + # Remove all existing textures first + import artwork + artwork.Artwork().delete_cache() + + # reset the install run flag + settings('SyncInstallRunDone', value="false") + + # Remove emby info + resp = dialog.yesno(language(29999), language(33087)) + if resp: + import connectmanager + # Delete the settings + addon = xbmcaddon.Addon() + addondir = xbmc.translatePath( + "special://profile/addon_data/plugin.video.emby/").decode('utf-8') + dataPath = "%ssettings.xml" % addondir + xbmcvfs.delete(dataPath) + connectmanager.ConnectManager().clear_data() + + dialog.ok(heading=language(29999), line1=language(33088)) + xbmc.executebuiltin('RestartApp') + return xbmcplugin.setResolvedUrl(int(sys.argv[1]), False, xbmcgui.ListItem()) + \ No newline at end of file diff --git a/resources/lib/librarysync.py b/resources/lib/librarysync.py index a6e2cb90..58801f3a 100644 --- a/resources/lib/librarysync.py +++ b/resources/lib/librarysync.py @@ -933,7 +933,7 @@ class LibrarySync(threading.Thread): log.warn("Database version is out of date! USER IGNORED!") dialog.ok(lang(29999), lang(33023)) else: - utils.reset() + database.db_reset() break diff --git a/resources/lib/utils.py b/resources/lib/utils.py index b4145e76..b82a167f 100644 --- a/resources/lib/utils.py +++ b/resources/lib/utils.py @@ -2,7 +2,6 @@ ################################################################################################# - import inspect import json import logging @@ -15,6 +14,7 @@ import unicodedata import xml.etree.ElementTree as etree from datetime import datetime + import xbmc import xbmcaddon import xbmcgui @@ -280,99 +280,6 @@ def profiling(sortby="cumulative"): ################################################################################################# # Addon utilities -def reset(): - - dialog = xbmcgui.Dialog() - - if not dialog.yesno(language(29999), language(33074)): - return - - # first stop any db sync - window('emby_online', value="reset") - window('emby_shouldStop', value="true") - count = 10 - while window('emby_dbScan') == "true": - log.info("Sync is running, will retry: %s..." % count) - count -= 1 - if count == 0: - dialog.ok(language(29999), language(33085)) - return - xbmc.sleep(1000) - - # Clean up the playlists - deletePlaylists() - - # Clean up the video nodes - deleteNodes() - - # Wipe the kodi databases - log.warn("Resetting the Kodi video database.") - connection = kodiSQL('video') - cursor = connection.cursor() - cursor.execute('SELECT tbl_name FROM sqlite_master WHERE type="table"') - rows = cursor.fetchall() - for row in rows: - tablename = row[0] - if tablename != "version": - cursor.execute("DELETE FROM " + tablename) - connection.commit() - cursor.close() - - if settings('enableMusic') == "true": - log.warn("Resetting the Kodi music database.") - connection = kodiSQL('music') - cursor = connection.cursor() - cursor.execute('SELECT tbl_name FROM sqlite_master WHERE type="table"') - rows = cursor.fetchall() - for row in rows: - tablename = row[0] - if tablename != "version": - cursor.execute("DELETE FROM " + tablename) - connection.commit() - cursor.close() - - # Wipe the emby database - log.warn("Resetting the Emby database.") - connection = kodiSQL('emby') - cursor = connection.cursor() - cursor.execute('SELECT tbl_name FROM sqlite_master WHERE type="table"') - rows = cursor.fetchall() - for row in rows: - tablename = row[0] - if tablename != "version": - cursor.execute("DELETE FROM " + tablename) - cursor.execute('DROP table IF EXISTS emby') - cursor.execute('DROP table IF EXISTS view') - cursor.execute("DROP table IF EXISTS version") - connection.commit() - cursor.close() - - # Offer to wipe cached thumbnails - if dialog.yesno(language(29999), language(33086)): - log.warn("Resetting all cached artwork") - # Remove all existing textures first - import artwork - artwork.Artwork().delete_cache() - - # reset the install run flag - settings('SyncInstallRunDone', value="false") - - # Remove emby info - resp = dialog.yesno(language(29999), language(33087)) - if resp: - import connectmanager - # Delete the settings - addon = xbmcaddon.Addon() - addondir = xbmc.translatePath( - "special://profile/addon_data/plugin.video.emby/").decode('utf-8') - dataPath = "%ssettings.xml" % addondir - xbmcvfs.delete(dataPath) - connectmanager.ConnectManager().clear_data() - - dialog.ok(heading=language(29999), line1=language(33088)) - xbmc.executebuiltin('RestartApp') - return xbmcplugin.setResolvedUrl(int(sys.argv[1]), False, xbmcgui.ListItem()) - def sourcesXML(): # To make Master lock compatible path = xbmc.translatePath("special://profile/").decode('utf-8')