use new DatabaseCon context class

This commit is contained in:
shaun 2016-11-05 13:19:57 +11:00
parent a58b644062
commit 881b3f8e70
4 changed files with 183 additions and 209 deletions

View file

@ -6,6 +6,7 @@ import logging
import sqlite3
from contextlib import closing
import sys
import traceback
import xbmc
import xbmcaddon
@ -97,7 +98,8 @@ class DatabaseConn(object):
def __enter__(self):
# Open the connection
self.path = self._SQL(self.db_file)
log.info("opening database: %s", self.path)
log.info("opening: %s", self.path)
#traceback.print_stack()
if settings('dblock') == "true":
self.conn = sqlite3.connect(self.path, isolation_level=None, timeout=self.timeout)
@ -131,7 +133,7 @@ class DatabaseConn(object):
self.conn.commit()
log.info("commit: %s", self.path)
log.info("close: %s", self.path)
log.info("closing: %s", self.path)
self.conn.close()

View file

@ -28,6 +28,8 @@ import playbackutils as pbutils
import playutils
import api
from utils import window, settings, dialog, language as lang
from database import DatabaseConn
from contextlib import closing
#################################################################################################
@ -234,11 +236,10 @@ def deleteItem():
log.info("Unknown type, unable to proceed.")
return
embyconn = utils.kodiSQL('emby')
embycursor = embyconn.cursor()
emby_db = embydb.Embydb_Functions(embycursor)
with DatabaseConn('emby') as conn:
with closing(conn.cursor()) as cursor:
emby_db = embydb.Embydb_Functions(cursor)
item = emby_db.getItem_byKodiId(dbId, itemType)
embycursor.close()
try:
itemId = item[0]
@ -422,11 +423,10 @@ def getThemeMedia():
return
# Get every user view Id
embyconn = utils.kodiSQL('emby')
embycursor = embyconn.cursor()
emby_db = embydb.Embydb_Functions(embycursor)
with DatabaseConn('emby') as conn:
with closing(conn.cursor()) as cursor:
emby_db = embydb.Embydb_Functions(cursor)
viewids = emby_db.getViews()
embycursor.close()
# Get Ids with Theme Videos
itemIds = {}

View file

@ -13,6 +13,8 @@ import embydb_functions as embydb
import playbackutils as pbutils
from utils import window, settings, kodiSQL
from ga_client import log_error
from database import DatabaseConn
from contextlib import closing
#################################################################################################
@ -165,11 +167,10 @@ class KodiMonitor(xbmc.Monitor):
item_id = None
conn = kodiSQL('emby')
cursor = conn.cursor()
with DatabaseConn('emby') as conn:
with closing(conn.cursor()) as cursor:
emby_db = embydb.Embydb_Functions(cursor)
db_item = emby_db.getItem_byKodiId(kodi_id, item_type)
cursor.close()
try:
item_id = db_item[0]

View file

@ -12,7 +12,6 @@ import xbmcgui
import xbmcvfs
import api
import database
import utils
import clientinfo
import downloadutils
@ -25,6 +24,7 @@ import videonodes
from objects import Movies, MusicVideos, TVShows, Music
from utils import window, settings, language as lang, should_stop
from ga_client import GoogleAnalytics
from database import DatabaseConn
from contextlib import closing
##################################################################################################
@ -57,7 +57,6 @@ class LibrarySync(threading.Thread):
self.monitor = xbmc.Monitor()
self.clientInfo = clientinfo.ClientInfo()
self.database = database.DatabaseConn
self.doUtils = downloadutils.DownloadUtils().downloadUrl
self.user = userclient.UserClient()
self.emby = embyserver.Read_EmbyServer()
@ -235,11 +234,10 @@ class LibrarySync(threading.Thread):
# Add sources
utils.sourcesXML()
embyconn = utils.kodiSQL('emby')
embycursor = embyconn.cursor()
# use emby and video DBs
with DatabaseConn('emby') as conn_emby, DatabaseConn('video') as conn_video:
with closing(conn_emby.cursor()) as cursor_emby, closing(conn_video.cursor()) as cursor_video:
# content sync: movies, tvshows, musicvideos, music
kodiconn = utils.kodiSQL('video')
kodicursor = kodiconn.cursor()
if manualrun:
message = "Manual sync"
@ -278,8 +276,7 @@ class LibrarySync(threading.Thread):
starttotal = datetime.now()
# Set views
self.maintainViews(embycursor, kodicursor)
embyconn.commit()
self.maintainViews(cursor_emby, cursor_video)
# Sync video library
process = {
@ -294,7 +291,7 @@ class LibrarySync(threading.Thread):
continue
startTime = datetime.now()
completed = process[itemtype](embycursor, kodicursor, pDialog)
completed = process[itemtype](cursor_emby, kodicursor, pDialog)
if not completed:
xbmc.executebuiltin('InhibitIdleShutdown(false)')
utils.setScreensaver(value=screensaver)
@ -302,30 +299,23 @@ class LibrarySync(threading.Thread):
if pDialog:
pDialog.close()
embycursor.close()
kodicursor.close()
return False
else:
self.dbCommit(kodiconn)
embyconn.commit()
elapsedTime = datetime.now() - startTime
log.info("SyncDatabase (finished %s in: %s)"
% (itemtype, str(elapsedTime).split('.')[0]))
else:
# Close the Kodi cursor
kodicursor.close()
# sync music
# use emby and music
if music_enabled:
if repair and 'music' not in repair_list:
pass
else:
musicconn = utils.kodiSQL('music')
musiccursor = musicconn.cursor()
with DatabaseConn('emby') as conn_emby, DatabaseConn('music') as conn_music:
with closing(conn_emby.cursor()) as cursor_emby, closing(conn_music.cursor()) as cursor_music:
startTime = datetime.now()
completed = self.music(embycursor, musiccursor, pDialog)
completed = self.music(cursor_emby, cursor_music, pDialog)
if not completed:
xbmc.executebuiltin('InhibitIdleShutdown(false)')
utils.setScreensaver(value=screensaver)
@ -333,25 +323,21 @@ class LibrarySync(threading.Thread):
if pDialog:
pDialog.close()
embycursor.close()
musiccursor.close()
return False
else:
musicconn.commit()
embyconn.commit()
elapsedTime = datetime.now() - startTime
log.info("SyncDatabase (finished music in: %s)"
% (str(elapsedTime).split('.')[0]))
musiccursor.close()
if pDialog:
pDialog.close()
emby_db = embydb.Embydb_Functions(embycursor)
with DatabaseConn('emby') as conn_emby:
with closing(conn_emby.cursor()) as cursor_emby:
emby_db = embydb.Embydb_Functions(cursor_emby)
current_version = emby_db.get_version(self.clientInfo.get_version())
window('emby_version', current_version)
embyconn.commit()
embycursor.close()
settings('SyncInstallRunDone', value="true")
@ -376,19 +362,11 @@ class LibrarySync(threading.Thread):
def refreshViews(self):
embyconn = utils.kodiSQL('emby')
embycursor = embyconn.cursor()
kodiconn = utils.kodiSQL('video')
kodicursor = kodiconn.cursor()
with DatabaseConn('emby') as conn_emby, DatabaseConn('video') as conn_video:
with closing(conn_emby.cursor()) as cursor_emby, closing(conn_video.cursor()) as cursor_video:
# Compare views, assign correct tags to items
self.maintainViews(embycursor, kodicursor)
self.maintainViews(cursor_emby, cursor_video)
self.dbCommit(kodiconn)
kodicursor.close()
embyconn.commit()
embycursor.close()
def maintainViews(self, embycursor, kodicursor):
@ -737,19 +715,17 @@ class LibrarySync(threading.Thread):
def incrementalSync(self):
embyconn = utils.kodiSQL('emby')
embycursor = embyconn.cursor()
kodiconn = utils.kodiSQL('video')
kodicursor = kodiconn.cursor()
emby_db = embydb.Embydb_Functions(embycursor)
with DatabaseConn('emby') as conn_emby, DatabaseConn('video') as conn_video:
with closing(conn_emby.cursor()) as cursor_emby, closing(conn_video.cursor()) as cursor_video:
emby_db = embydb.Embydb_Functions(cursor_emby)
pDialog = None
update_embydb = False
if self.refresh_views:
# Received userconfig update
self.refresh_views = False
self.maintainViews(embycursor, kodicursor)
embycursor.commit()
self.maintainViews(cursor_emby, cursor_video)
self.forceLibraryUpdate = True
update_embydb = True
@ -775,7 +751,7 @@ class LibrarySync(threading.Thread):
listItems = list(process[process_type])
del process[process_type][:] # Reset class list
items_process = itemtypes.Items(embycursor, kodicursor)
items_process = itemtypes.Items(cursor_emby, cursor_video)
update = False
# Prepare items according to process process_type
@ -809,13 +785,11 @@ class LibrarySync(threading.Thread):
if update_embydb:
update_embydb = False
log.info("Updating emby database.")
embyconn.commit()
self.saveLastSync()
if self.forceLibraryUpdate:
# Force update the Kodi library
self.forceLibraryUpdate = False
self.dbCommit(kodiconn)
log.info("Updating video library.")
window('emby_kodiScan', value="true")
@ -824,9 +798,6 @@ class LibrarySync(threading.Thread):
if pDialog:
pDialog.close()
kodicursor.close()
embycursor.close()
def compareDBVersion(self, current, minimum):
# It returns True is database is up to date. False otherwise.
@ -849,7 +820,7 @@ class LibrarySync(threading.Thread):
def _verify_emby_database(self):
# Create the tables for the emby database
with self.database('emby') as conn:
with DatabaseConn('emby') as conn:
with closing(conn.cursor()) as cursor:
# emby, view, version
cursor.execute(
@ -907,18 +878,18 @@ class LibrarySync(threading.Thread):
if (window('emby_dbCheck') != "true" and settings('SyncInstallRunDone') == "true"):
# Verify the validity of the database
log.info("Doing DB Version Check")
with DatabaseConn('emby') as conn:
with closing(conn.cursor()) as cursor:
embyconn = utils.kodiSQL('emby')
embycursor = embyconn.cursor()
emby_db = embydb.Embydb_Functions(embycursor)
emby_db = embydb.Embydb_Functions(cursor)
currentVersion = emby_db.get_version()
###$ Begin migration $###
if not currentVersion:
currentVersion = emby_db.get_version(settings('dbCreatedWithVersion') or self.clientInfo.get_version())
embyconn.commit()
log.info("Migration of database version completed")
###$ End migration $###
embycursor.close()
window('emby_version', value=currentVersion)
minVersion = window('emby_minDBVersion')