Change DatabaseConn to return the cursor instead

This commit is contained in:
angelblue05 2016-11-07 17:32:40 -06:00
parent 13e4ff5128
commit 6d0b2b96bc
8 changed files with 218 additions and 244 deletions

View file

@ -15,7 +15,6 @@ import requests
import image_cache_thread
from utils import window, settings, dialog, language as lang, JSONRPC
from database import DatabaseConn
from contextlib import closing
##################################################################################################
@ -166,49 +165,47 @@ class Artwork(object):
def _cache_all_video_entries(self, pdialog):
with DatabaseConn('video') as conn:
with closing(conn.cursor()) as cursor_video:
cursor_video.execute("SELECT url FROM art WHERE media_type != 'actor'") # dont include actors
result = cursor_video.fetchall()
total = len(result)
log.info("Image cache sync about to process %s images", total)
cursor_video.close()
with DatabaseConn('video') as cursor_video:
count = 0
for url in result:
cursor_video.execute("SELECT url FROM art WHERE media_type != 'actor'") # dont include actors
result = cursor_video.fetchall()
total = len(result)
log.info("Image cache sync about to process %s images", total)
cursor_video.close()
if pdialog.iscanceled():
break
count = 0
for url in result:
percentage = int((float(count) / float(total))*100)
message = "%s of %s (%s)" % (count, total, len(self.image_cache_threads))
pdialog.update(percentage, "%s %s" % (lang(33045), message))
self.cache_texture(url[0])
count += 1
if pdialog.iscanceled():
break
percentage = int((float(count) / float(total))*100)
message = "%s of %s (%s)" % (count, total, len(self.image_cache_threads))
pdialog.update(percentage, "%s %s" % (lang(33045), message))
self.cache_texture(url[0])
count += 1
def _cache_all_music_entries(self, pdialog):
with DatabaseConn('music') as conn:
with closing(conn.cursor()) as cursor_music:
with DatabaseConn('music') as cursor_music:
cursor_music.execute("SELECT url FROM art")
result = cursor_music.fetchall()
total = len(result)
log.info("Image cache sync about to process %s images", total)
cursor_music.execute("SELECT url FROM art")
result = cursor_music.fetchall()
total = len(result)
log.info("Image cache sync about to process %s images", total)
count = 0
for url in result:
count = 0
for url in result:
if pdialog.iscanceled():
break
if pdialog.iscanceled():
break
percentage = int((float(count) / float(total))*100)
message = "%s of %s" % (count, total)
pdialog.update(percentage, "%s %s" % (lang(33045), message))
self.cache_texture(url[0])
count += 1
percentage = int((float(count) / float(total))*100)
message = "%s of %s" % (count, total)
pdialog.update(percentage, "%s %s" % (lang(33045), message))
self.cache_texture(url[0])
count += 1
@classmethod
def delete_cache(cls):
@ -230,14 +227,13 @@ class Artwork(object):
log.debug("deleted: %s", filename)
# remove all existing data from texture DB
with DatabaseConn('texture') as conn:
with closing(conn.cursor()) as cursor_texture:
cursor_texture.execute('SELECT tbl_name FROM sqlite_master WHERE type="table"')
rows = cursor_texture.fetchall()
for row in rows:
table_name = row[0]
if table_name != "version":
cursor_texture.execute("DELETE FROM " + table_name)
with DatabaseConn('texture') as cursor_texture:
cursor_texture.execute('SELECT tbl_name FROM sqlite_master WHERE type="table"')
rows = cursor_texture.fetchall()
for row in rows:
table_name = row[0]
if table_name != "version":
cursor_texture.execute("DELETE FROM " + table_name)
def _add_worker_image_thread(self, url):
@ -431,28 +427,26 @@ class Artwork(object):
@classmethod
def delete_cached_artwork(cls, url):
# Only necessary to remove and apply a new backdrop or poster
with DatabaseConn('texture') as conn:
with closing(conn.cursor()) as cursor_texture:
with DatabaseConn('texture') as cursor_texture:
try:
cursor_texture.execute("SELECT cachedurl FROM texture WHERE url = ?", (url,))
cached_url = cursor_texture.fetchone()[0]
except TypeError:
log.info("Could not find cached url")
except OperationalError:
log.info("Database is locked. Skip deletion process.")
else: # Delete thumbnail as well as the entry
thumbnails = xbmc.translatePath("special://thumbnails/%s" % cached_url).decode('utf-8')
log.info("Deleting cached thumbnail: %s", thumbnails)
xbmcvfs.delete(thumbnails)
try:
cursor_texture.execute("SELECT cachedurl FROM texture WHERE url = ?", (url,))
cached_url = cursor_texture.fetchone()[0]
except TypeError:
log.info("Could not find cached url")
cursor_texture.execute("DELETE FROM texture WHERE url = ?", (url,))
except OperationalError:
log.info("Database is locked. Skip deletion process.")
else: # Delete thumbnail as well as the entry
thumbnails = xbmc.translatePath("special://thumbnails/%s" % cached_url).decode('utf-8')
log.info("Deleting cached thumbnail: %s", thumbnails)
xbmcvfs.delete(thumbnails)
try:
cursor_texture.execute("DELETE FROM texture WHERE url = ?", (url,))
except OperationalError:
log.debug("Issue deleting url from cache. Skipping.")
log.debug("Issue deleting url from cache. Skipping.")
def get_people_artwork(self, people):
# append imageurl if existing

View file

@ -14,7 +14,6 @@ import musicutils as musicutils
from utils import settings, dialog, language as lang
from dialogs import context
from database import DatabaseConn
from contextlib import closing
#################################################################################################
@ -89,14 +88,13 @@ class ContextMenu(object):
if not item_id and kodi_id and item_type:
with DatabaseConn('emby') as conn:
with closing(conn.cursor()) as cursor:
emby_db = embydb.Embydb_Functions(cursor)
item = emby_db.getItem_byKodiId(kodi_id, item_type)
try:
item_id = item[0]
except TypeError:
pass
with DatabaseConn('emby') as cursor:
emby_db = embydb.Embydb_Functions(cursor)
item = emby_db.getItem_byKodiId(kodi_id, item_type)
try:
item_id = item[0]
except TypeError:
pass
return item_id
@ -165,28 +163,27 @@ class ContextMenu(object):
def _rate_song(self):
with DatabaseConn('music') as conn:
with closing(conn.cursor()) as cursor_music:
query = "SELECT rating FROM song WHERE idSong = ?"
cursor_music.execute(query, (self.kodi_id,))
try:
value = cursor_music.fetchone()[0]
current_value = int(round(float(value), 0))
except TypeError:
pass
else:
new_value = dialog("numeric", 0, lang(30411), str(current_value))
if new_value > -1:
with DatabaseConn('music') as cursor_music:
query = "SELECT rating FROM song WHERE idSong = ?"
cursor_music.execute(query, (self.kodi_id,))
try:
value = cursor_music.fetchone()[0]
current_value = int(round(float(value), 0))
except TypeError:
pass
else:
new_value = dialog("numeric", 0, lang(30411), str(current_value))
if new_value > -1:
new_value = int(new_value)
if new_value > 5:
new_value = 5
new_value = int(new_value)
if new_value > 5:
new_value = 5
if settings('enableUpdateSongRating') == "true":
musicutils.updateRatingToFile(new_value, self.api.get_file_path())
if settings('enableUpdateSongRating') == "true":
musicutils.updateRatingToFile(new_value, self.api.get_file_path())
query = "UPDATE song SET rating = ? WHERE idSong = ?"
cursor_music.execute(query, (new_value, self.kodi_id,))
query = "UPDATE song SET rating = ? WHERE idSong = ?"
cursor_music.execute(query, (new_value, self.kodi_id,))
def _delete_item(self):

View file

@ -4,7 +4,6 @@
import logging
import sqlite3
from contextlib import closing
import sys
import traceback
@ -98,7 +97,8 @@ class DatabaseConn(object):
self.conn = sqlite3.connect(self.path, timeout=self.timeout)
log.info("opened: %s - %s", self.path, id(self.conn))
return self.conn
self.cursor = self.conn.cursor()
return self.conn.cursor()
def _SQL(self, media_type):
@ -126,6 +126,7 @@ class DatabaseConn(object):
log.info("commit: %s", self.path)
log.info("closing: %s - %s", self.path, id(self.conn))
self.cursor.close()
self.conn.close()
@ -156,39 +157,36 @@ def db_reset():
# 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)
with DatabaseConn('video') 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:
with DatabaseConn('music') 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")
# Wipe the emby database
log.warn("Resetting the Emby database.")
with DatabaseConn('emby') 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)):

View file

@ -30,7 +30,6 @@ import api
from views import Playlist, VideoNodes
from utils import window, settings, dialog, language as lang
from database import DatabaseConn
from contextlib import closing
#################################################################################################
@ -235,10 +234,9 @@ def deleteItem():
log.info("Unknown type, unable to proceed.")
return
with DatabaseConn('emby') as conn:
with closing(conn.cursor()) as cursor:
emby_db = embydb.Embydb_Functions(cursor)
item = emby_db.getItem_byKodiId(dbId, itemType)
with DatabaseConn('emby') as cursor:
emby_db = embydb.Embydb_Functions(cursor)
item = emby_db.getItem_byKodiId(dbId, itemType)
try:
itemId = item[0]
@ -422,10 +420,9 @@ def getThemeMedia():
return
# Get every user view Id
with DatabaseConn('emby') as conn:
with closing(conn.cursor()) as cursor:
emby_db = embydb.Embydb_Functions(cursor)
viewids = emby_db.getViews()
with DatabaseConn('emby') as cursor:
emby_db = embydb.Embydb_Functions(cursor)
viewids = emby_db.getViews()
# Get Ids with Theme Videos
itemIds = {}

View file

@ -8,7 +8,6 @@ import read_embyserver as embyserver
from objects import Movies, MusicVideos, TVShows, Music
from utils import settings
from database import DatabaseConn
from contextlib import closing
#################################################################################################
@ -62,38 +61,37 @@ class Items(object):
# this is going to open a music connection even if it is not needed but
# I feel that is better than trying to sort out the login yourself
with DatabaseConn('music') as conn:
with closing(conn.cursor()) as cursor_music:
with DatabaseConn('music') as cursor_music:
for itemtype in items:
for itemtype in items:
# Safety check
if not itemtypes.get(itemtype):
# We don't process this type of item
continue
# Safety check
if not itemtypes.get(itemtype):
# We don't process this type of item
continue
itemlist = items[itemtype]
if not itemlist:
# The list to process is empty
continue
itemlist = items[itemtype]
if not itemlist:
# The list to process is empty
continue
if itemtype in ('MusicAlbum', 'MusicArtist', 'AlbumArtist', 'Audio'):
if self.music_enabled:
items_process = itemtypes[itemtype](embycursor, cursor_music, pdialog) # see note above
else:
# Music is not enabled, do not proceed with itemtype
continue
if itemtype in ('MusicAlbum', 'MusicArtist', 'AlbumArtist', 'Audio'):
if self.music_enabled:
items_process = itemtypes[itemtype](embycursor, cursor_music, pdialog) # see note above
else:
update_videolibrary = True
items_process = itemtypes[itemtype](embycursor, kodicursor, pdialog)
# Music is not enabled, do not proceed with itemtype
continue
else:
update_videolibrary = True
items_process = itemtypes[itemtype](embycursor, kodicursor, pdialog)
if process == "added":
items_process.add_all(itemtype, itemlist)
elif process == "remove":
items_process.remove_all(itemtype, itemlist)
else:
process_items = self.emby.getFullItems(itemlist)
items_process.process_all(itemtype, process, process_items, total)
if process == "added":
items_process.add_all(itemtype, itemlist)
elif process == "remove":
items_process.remove_all(itemtype, itemlist)
else:
process_items = self.emby.getFullItems(itemlist)
items_process.process_all(itemtype, process, process_items, total)
return (True, update_videolibrary)

View file

@ -14,7 +14,6 @@ import playbackutils as pbutils
from utils import window, settings
from ga_client import log_error
from database import DatabaseConn
from contextlib import closing
#################################################################################################
@ -167,10 +166,9 @@ class KodiMonitor(xbmc.Monitor):
item_id = None
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)
with DatabaseConn('emby') as cursor:
emby_db = embydb.Embydb_Functions(cursor)
db_item = emby_db.getItem_byKodiId(kodi_id, item_type)
try:
item_id = db_item[0]

View file

@ -14,6 +14,7 @@ import xbmcvfs
import api
import utils
import clientinfo
import database
import downloadutils
import itemtypes
import embydb_functions as embydb
@ -23,8 +24,6 @@ import views
from objects import Movies, MusicVideos, TVShows, Music
from utils import window, settings, language as lang, should_stop
from ga_client import GoogleAnalytics
import database
from contextlib import closing
##################################################################################################
@ -233,8 +232,8 @@ class LibrarySync(threading.Thread):
utils.sourcesXML()
# use emby and video DBs
with database.DatabaseConn('emby') as conn_emby, database.DatabaseConn('video') as conn_video:
with closing(conn_emby.cursor()) as cursor_emby, closing(conn_video.cursor()) as cursor_video:
with database.DatabaseConn('emby') as cursor_emby:
with database.DatabaseConn('video') as cursor_video:
# content sync: movies, tvshows, musicvideos, music
if manualrun:
@ -275,7 +274,7 @@ class LibrarySync(threading.Thread):
# Set views
views.Views(cursor_emby, cursor_video).maintain()
conn_emby.commit()
cursor_emby.connection.commit()
#self.maintainViews(cursor_emby, cursor_video)
# Sync video library
@ -312,8 +311,8 @@ class LibrarySync(threading.Thread):
if repair and 'music' not in repair_list:
pass
else:
with database.DatabaseConn('emby') as conn_emby, database.DatabaseConn('music') as conn_music:
with closing(conn_emby.cursor()) as cursor_emby, closing(conn_music.cursor()) as cursor_music:
with database.DatabaseConn('emby') as cursor_emby:
with database.DatabaseConn('music') as cursor_music:
startTime = datetime.now()
completed = self.music(cursor_emby, cursor_music, pDialog)
if not completed:
@ -332,10 +331,9 @@ class LibrarySync(threading.Thread):
if pDialog:
pDialog.close()
with database.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())
with database.DatabaseConn('emby') 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)
@ -362,8 +360,8 @@ class LibrarySync(threading.Thread):
def refreshViews(self):
with database.DatabaseConn('emby') as conn_emby, database.DatabaseConn() as conn_video:
with closing(conn_emby.cursor()) as cursor_emby, closing(conn_video.cursor()) as cursor_video:
with database.DatabaseConn('emby') as cursor_emby:
with database.DatabaseConn() as cursor_video:
# Compare views, assign correct tags to items
views.Views(cursor_emby, cursor_video).maintain()
@ -515,8 +513,8 @@ class LibrarySync(threading.Thread):
# do a lib update if any items in list
totalUpdates = len(self.addedItems) + len(self.updateItems) + len(self.userdataItems) + len(self.removeItems)
if totalUpdates > 0:
with database.DatabaseConn('emby') as conn_emby, database.DatabaseConn('video') as conn_video:
with closing(conn_emby.cursor()) as cursor_emby, closing(conn_video.cursor()) as cursor_video:
with database.DatabaseConn('emby') as cursor_emby:
with database.DatabaseConn('video') as cursor_video:
emby_db = embydb.Embydb_Functions(cursor_emby)
@ -610,18 +608,17 @@ class LibrarySync(threading.Thread):
def _verify_emby_database(self):
# Create the tables for the emby database
with database.DatabaseConn('emby') as conn:
with closing(conn.cursor()) as cursor:
# emby, view, version
cursor.execute(
"""CREATE TABLE IF NOT EXISTS emby(
emby_id TEXT UNIQUE, media_folder TEXT, emby_type TEXT, media_type TEXT,
kodi_id INTEGER, kodi_fileid INTEGER, kodi_pathid INTEGER, parent_id INTEGER,
checksum INTEGER)""")
cursor.execute(
"""CREATE TABLE IF NOT EXISTS view(
view_id TEXT UNIQUE, view_name TEXT, media_type TEXT, kodi_tagid INTEGER)""")
cursor.execute("CREATE TABLE IF NOT EXISTS version(idVersion TEXT)")
with database.DatabaseConn('emby') as cursor:
# emby, view, version
cursor.execute(
"""CREATE TABLE IF NOT EXISTS emby(
emby_id TEXT UNIQUE, media_folder TEXT, emby_type TEXT, media_type TEXT,
kodi_id INTEGER, kodi_fileid INTEGER, kodi_pathid INTEGER, parent_id INTEGER,
checksum INTEGER)""")
cursor.execute(
"""CREATE TABLE IF NOT EXISTS view(
view_id TEXT UNIQUE, view_name TEXT, media_type TEXT, kodi_tagid INTEGER)""")
cursor.execute("CREATE TABLE IF NOT EXISTS version(idVersion TEXT)")
def run(self):
@ -669,16 +666,14 @@ 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 database.DatabaseConn('emby') as conn:
with closing(conn.cursor()) as cursor:
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())
log.info("Migration of database version completed")
###$ End migration $###
with database.DatabaseConn('emby') as cursor:
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())
log.info("Migration of database version completed")
###$ End migration $###
window('emby_version', value=currentVersion)

View file

@ -13,7 +13,6 @@ import embydb_functions as embydb
import read_embyserver as embyserver
from utils import window, JSONRPC
from database import DatabaseConn
from contextlib import closing
#################################################################################################
@ -31,75 +30,73 @@ class Playlist(object):
def play_all(self, item_ids, start_at):
with DatabaseConn('emby') as conn:
with closing(conn.cursor()) as cursor:
emby_db = embydb.Embydb_Functions(cursor)
with DatabaseConn('emby') as cursor:
emby_db = embydb.Embydb_Functions(cursor)
player = xbmc.Player()
playlist = xbmc.PlayList(xbmc.PLAYLIST_VIDEO)
playlist.clear()
player = xbmc.Player()
playlist = xbmc.PlayList(xbmc.PLAYLIST_VIDEO)
playlist.clear()
log.info("---*** PLAY ALL ***---")
log.info("Items: %s and start at: %s", item_ids, start_at)
log.info("---*** PLAY ALL ***---")
log.info("Items: %s and start at: %s", item_ids, start_at)
started = False
window('emby_customplaylist', value="true")
started = False
window('emby_customplaylist', value="true")
if start_at:
# Seek to the starting position
window('emby_customplaylist.seektime', str(start_at))
if start_at:
# Seek to the starting position
window('emby_customplaylist.seektime', str(start_at))
for item_id in item_ids:
for item_id in item_ids:
log.info("Adding %s to playlist", item_id)
item = emby_db.getItem_byId(item_id)
try:
db_id = item[0]
media_type = item[4]
log.info("Adding %s to playlist", item_id)
item = emby_db.getItem_byId(item_id)
try:
db_id = item[0]
media_type = item[4]
except TypeError:
# Item is not found in our database, add item manually
log.info("Item was not found in the database, manually adding item")
item = self.emby.getItem(item_id)
self.add_to_xbmc_playlist(playlist, item)
except TypeError:
# Item is not found in our database, add item manually
log.info("Item was not found in the database, manually adding item")
item = self.emby.getItem(item_id)
self.add_to_xbmc_playlist(playlist, item)
else: # Add to playlist
self.add_to_playlist(db_id, media_type)
else: # Add to playlist
self.add_to_playlist(db_id, media_type)
if not started:
started = True
player.play(playlist)
if not started:
started = True
player.play(playlist)
self.verify_playlist()
self.verify_playlist()
def modify_playlist(self, item_ids):
with DatabaseConn('emby') as conn:
with closing(conn.cursor()) as cursor:
emby_db = embydb.Embydb_Functions(cursor)
with DatabaseConn('emby') as cursor:
emby_db = embydb.Embydb_Functions(cursor)
log.info("---*** ADD TO PLAYLIST ***---")
log.info("Items: %s", item_ids)
log.info("---*** ADD TO PLAYLIST ***---")
log.info("Items: %s", item_ids)
playlist = xbmc.PlayList(xbmc.PLAYLIST_VIDEO)
playlist = xbmc.PlayList(xbmc.PLAYLIST_VIDEO)
for item_id in item_ids:
for item_id in item_ids:
log.info("Adding %s to playlist", item_id)
item = emby_db.getItem_byId(item_id)
try:
db_id = item[0]
media_type = item[4]
log.info("Adding %s to playlist", item_id)
item = emby_db.getItem_byId(item_id)
try:
db_id = item[0]
media_type = item[4]
except TypeError:
# Item is not found in our database, add item manually
item = self.emby.getItem(item_id)
self.add_to_xbmc_playlist(playlist, item)
except TypeError:
# Item is not found in our database, add item manually
item = self.emby.getItem(item_id)
self.add_to_xbmc_playlist(playlist, item)
else: # Add to playlist
self.add_to_playlist(db_id, media_type)
else: # Add to playlist
self.add_to_playlist(db_id, media_type)
self.verify_playlist()
self.verify_playlist()
return playlist