From c50c290c3ee76e7c811b483c26b2330618386551 Mon Sep 17 00:00:00 2001 From: Ruud Date: Mon, 6 Oct 2014 17:28:10 +0200 Subject: [PATCH] Database helper --- couchpotato/core/database.py | 4 +-- couchpotato/core/helpers/database.py | 26 +++++++++++++++++++ couchpotato/core/media/_base/media/index.py | 17 ++++++------ couchpotato/core/media/_base/media/main.py | 2 +- couchpotato/core/media/movie/_base/main.py | 2 +- .../media/movie/providers/info/_modifier.py | 2 +- couchpotato/core/notifications/core/index.py | 6 ++--- couchpotato/core/notifications/core/main.py | 2 +- couchpotato/core/plugins/category/index.py | 2 +- couchpotato/core/plugins/dashboard.py | 2 +- couchpotato/core/plugins/profile/index.py | 2 +- couchpotato/core/plugins/quality/index.py | 3 +-- couchpotato/core/plugins/quality/main.py | 2 +- couchpotato/core/plugins/release/index.py | 4 +-- couchpotato/core/plugins/release/main.py | 2 +- couchpotato/core/settings.py | 2 +- couchpotato/runner.py | 2 +- 17 files changed, 53 insertions(+), 29 deletions(-) create mode 100644 couchpotato/core/helpers/database.py diff --git a/couchpotato/core/database.py b/couchpotato/core/database.py index 944dca64..d597abfb 100644 --- a/couchpotato/core/database.py +++ b/couchpotato/core/database.py @@ -3,12 +3,12 @@ import os import time import traceback from sqlite3 import OperationalError +from CodernityDB3.index import Index -from CodernityDB.database import RecordNotFound -from CodernityDB.index import IndexException, IndexNotFoundException, IndexConflict from couchpotato import CPLog from couchpotato.api import addApiView from couchpotato.core.event import addEvent, fireEvent, fireEventAsync +from couchpotato.core.helpers.database import IndexException, IndexNotFoundException, IndexConflict, RecordNotFound from couchpotato.core.helpers.encoding import toUnicode, sp from couchpotato.core.helpers.variable import getImdb, tryInt, randomString diff --git a/couchpotato/core/helpers/database.py b/couchpotato/core/helpers/database.py new file mode 100644 index 00000000..d0825eb0 --- /dev/null +++ b/couchpotato/core/helpers/database.py @@ -0,0 +1,26 @@ + +from six import PY2 + + +if PY2: + from CodernityDB.database_super_thread_safe import SuperThreadSafeDatabase + from CodernityDB.index import IndexException, IndexConflict, IndexNotFoundException + from CodernityDB.database import RecordNotFound, RecordDeleted + from CodernityDB.hash_index import HashIndex + from CodernityDB.tree_index import MultiTreeBasedIndex, TreeBasedIndex +else: + from CodernityDB3.database_super_thread_safe import SuperThreadSafeDatabase + from CodernityDB3.index import IndexException, IndexConflict, IndexNotFoundException + from CodernityDB3.database import RecordNotFound, RecordDeleted + from CodernityDB3.hash_index import HashIndex + from CodernityDB3.tree_index import MultiTreeBasedIndex, TreeBasedIndex + +SuperThreadSafeDatabase = SuperThreadSafeDatabase +IndexException = IndexException +IndexNotFoundException = IndexNotFoundException +IndexConflict = IndexConflict +RecordNotFound = RecordNotFound +HashIndex = HashIndex +MultiTreeBasedIndex = MultiTreeBasedIndex +TreeBasedIndex = TreeBasedIndex +RecordDeleted = RecordDeleted diff --git a/couchpotato/core/media/_base/media/index.py b/couchpotato/core/media/_base/media/index.py index b40e162b..08279c69 100644 --- a/couchpotato/core/media/_base/media/index.py +++ b/couchpotato/core/media/_base/media/index.py @@ -1,14 +1,14 @@ from string import ascii_letters from hashlib import md5 +from couchpotato.core.helpers.database import MultiTreeBasedIndex, TreeBasedIndex -from CodernityDB.tree_index import MultiTreeBasedIndex, TreeBasedIndex from couchpotato.core.helpers.encoding import toUnicode, simplifyString class MediaIndex(MultiTreeBasedIndex): _version = 3 - custom_header = """from CodernityDB.tree_index import MultiTreeBasedIndex""" + custom_header = """from couchpotato.core.helpers.database import MultiTreeBasedIndex""" def __init__(self, *args, **kwargs): kwargs['key_format'] = '32s' @@ -62,10 +62,11 @@ class MediaTypeIndex(TreeBasedIndex): class TitleSearchIndex(MultiTreeBasedIndex): - _version = 1 + _version = 2 - custom_header = """from CodernityDB.tree_index import MultiTreeBasedIndex -from itertools import izip + custom_header = """from couchpotato.core.helpers.database import MultiTreeBasedIndex +try: from itertools import izip +except: izip = zip from couchpotato.core.helpers.encoding import simplifyString""" def __init__(self, *args, **kwargs): @@ -101,7 +102,7 @@ from couchpotato.core.helpers.encoding import simplifyString""" class TitleIndex(TreeBasedIndex): _version = 4 - custom_header = """from CodernityDB.tree_index import TreeBasedIndex + custom_header = """from couchpotato.core.helpers.database import TreeBasedIndex from string import ascii_letters from couchpotato.core.helpers.encoding import toUnicode, simplifyString""" @@ -134,7 +135,7 @@ from couchpotato.core.helpers.encoding import toUnicode, simplifyString""" class StartsWithIndex(TreeBasedIndex): _version = 3 - custom_header = """from CodernityDB.tree_index import TreeBasedIndex + custom_header = """from couchpotato.core.helpers.database import TreeBasedIndex from string import ascii_letters from couchpotato.core.helpers.encoding import toUnicode, simplifyString""" @@ -180,7 +181,7 @@ class MediaChildrenIndex(TreeBasedIndex): class MediaTagIndex(MultiTreeBasedIndex): _version = 2 - custom_header = """from CodernityDB.tree_index import MultiTreeBasedIndex""" + custom_header = """from couchpotato.core.helpers.database import MultiTreeBasedIndex""" def __init__(self, *args, **kwargs): kwargs['key_format'] = '32s' diff --git a/couchpotato/core/media/_base/media/main.py b/couchpotato/core/media/_base/media/main.py index aa1ee382..160d3192 100755 --- a/couchpotato/core/media/_base/media/main.py +++ b/couchpotato/core/media/_base/media/main.py @@ -3,10 +3,10 @@ import time import traceback from string import ascii_lowercase -from CodernityDB.database import RecordNotFound, RecordDeleted from couchpotato import tryInt, get_db from couchpotato.api import addApiView from couchpotato.core.event import fireEvent, fireEventAsync, addEvent +from couchpotato.core.helpers.database import RecordNotFound, RecordDeleted from couchpotato.core.helpers.encoding import toUnicode from couchpotato.core.helpers.variable import splitString, getImdb, getTitle from couchpotato.core.logger import CPLog diff --git a/couchpotato/core/media/movie/_base/main.py b/couchpotato/core/media/movie/_base/main.py index 1b0881b5..a1f33c5c 100755 --- a/couchpotato/core/media/movie/_base/main.py +++ b/couchpotato/core/media/movie/_base/main.py @@ -1,10 +1,10 @@ import traceback import time -from CodernityDB.database import RecordNotFound from couchpotato import get_db from couchpotato.api import addApiView from couchpotato.core.event import fireEvent, fireEventAsync, addEvent +from couchpotato.core.helpers.database import RecordNotFound from couchpotato.core.helpers.encoding import toUnicode from couchpotato.core.helpers.variable import splitString, getTitle, getImdb, getIdentifier from couchpotato.core.logger import CPLog diff --git a/couchpotato/core/media/movie/providers/info/_modifier.py b/couchpotato/core/media/movie/providers/info/_modifier.py index f6a3089b..41d66ce3 100644 --- a/couchpotato/core/media/movie/providers/info/_modifier.py +++ b/couchpotato/core/media/movie/providers/info/_modifier.py @@ -1,9 +1,9 @@ import copy import traceback -from CodernityDB.database import RecordNotFound from couchpotato import get_db from couchpotato.core.event import addEvent, fireEvent +from couchpotato.core.helpers.database import RecordNotFound from couchpotato.core.helpers.variable import mergeDicts, randomString from couchpotato.core.logger import CPLog from couchpotato.core.plugins.base import Plugin diff --git a/couchpotato/core/notifications/core/index.py b/couchpotato/core/notifications/core/index.py index c7985b5b..735efa13 100644 --- a/couchpotato/core/notifications/core/index.py +++ b/couchpotato/core/notifications/core/index.py @@ -1,10 +1,10 @@ -from CodernityDB.tree_index import TreeBasedIndex +from couchpotato.core.helpers.database import TreeBasedIndex class NotificationIndex(TreeBasedIndex): _version = 1 - custom_header = """from CodernityDB.tree_index import TreeBasedIndex + custom_header = """from couchpotato.core.helpers.database import TreeBasedIndex import time""" def __init__(self, *args, **kwargs): @@ -22,7 +22,7 @@ import time""" class NotificationUnreadIndex(TreeBasedIndex): _version = 1 - custom_header = """from CodernityDB.tree_index import TreeBasedIndex + custom_header = """from couchpotato.core.helpers.database import TreeBasedIndex import time""" def __init__(self, *args, **kwargs): diff --git a/couchpotato/core/notifications/core/main.py b/couchpotato/core/notifications/core/main.py index fa8e9a7e..d01ed453 100644 --- a/couchpotato/core/notifications/core/main.py +++ b/couchpotato/core/notifications/core/main.py @@ -3,11 +3,11 @@ import threading import time import traceback import uuid -from CodernityDB.database import RecordDeleted from couchpotato import get_db from couchpotato.api import addApiView, addNonBlockApiView from couchpotato.core.event import addEvent, fireEvent +from couchpotato.core.helpers.database import RecordDeleted from couchpotato.core.helpers.encoding import toUnicode from couchpotato.core.helpers.variable import tryInt, splitString from couchpotato.core.logger import CPLog diff --git a/couchpotato/core/plugins/category/index.py b/couchpotato/core/plugins/category/index.py index 6445de3c..1c8a3eaf 100644 --- a/couchpotato/core/plugins/category/index.py +++ b/couchpotato/core/plugins/category/index.py @@ -1,4 +1,4 @@ -from CodernityDB.tree_index import TreeBasedIndex +from couchpotato.core.helpers.database import TreeBasedIndex class CategoryIndex(TreeBasedIndex): diff --git a/couchpotato/core/plugins/dashboard.py b/couchpotato/core/plugins/dashboard.py index afead443..9f61a78d 100644 --- a/couchpotato/core/plugins/dashboard.py +++ b/couchpotato/core/plugins/dashboard.py @@ -1,10 +1,10 @@ import random as rndm import time -from CodernityDB.database import RecordDeleted from couchpotato import get_db from couchpotato.api import addApiView from couchpotato.core.event import fireEvent +from couchpotato.core.helpers.database import RecordDeleted from couchpotato.core.helpers.variable import splitString, tryInt from couchpotato.core.logger import CPLog from couchpotato.core.plugins.base import Plugin diff --git a/couchpotato/core/plugins/profile/index.py b/couchpotato/core/plugins/profile/index.py index c2bf9445..0a1c64b4 100644 --- a/couchpotato/core/plugins/profile/index.py +++ b/couchpotato/core/plugins/profile/index.py @@ -1,4 +1,4 @@ -from CodernityDB.tree_index import TreeBasedIndex +from couchpotato.core.helpers.database import TreeBasedIndex class ProfileIndex(TreeBasedIndex): diff --git a/couchpotato/core/plugins/quality/index.py b/couchpotato/core/plugins/quality/index.py index 78043972..3e0d5fa1 100644 --- a/couchpotato/core/plugins/quality/index.py +++ b/couchpotato/core/plugins/quality/index.py @@ -1,6 +1,5 @@ from hashlib import md5 - -from CodernityDB.hash_index import HashIndex +from couchpotato.core.helpers.database import HashIndex class QualityIndex(HashIndex): diff --git a/couchpotato/core/plugins/quality/main.py b/couchpotato/core/plugins/quality/main.py index f3fbefef..39245768 100644 --- a/couchpotato/core/plugins/quality/main.py +++ b/couchpotato/core/plugins/quality/main.py @@ -2,10 +2,10 @@ from math import fabs, ceil import traceback import re -from CodernityDB.database import RecordNotFound from couchpotato import get_db from couchpotato.api import addApiView from couchpotato.core.event import addEvent, fireEvent +from couchpotato.core.helpers.database import RecordNotFound from couchpotato.core.helpers.encoding import toUnicode, ss from couchpotato.core.helpers.variable import mergeDicts, getExt, tryInt, splitString, tryFloat from couchpotato.core.logger import CPLog diff --git a/couchpotato/core/plugins/release/index.py b/couchpotato/core/plugins/release/index.py index 8265fe33..bd328088 100644 --- a/couchpotato/core/plugins/release/index.py +++ b/couchpotato/core/plugins/release/index.py @@ -1,7 +1,5 @@ from hashlib import md5 - -from CodernityDB.hash_index import HashIndex -from CodernityDB.tree_index import TreeBasedIndex +from couchpotato.core.helpers.database import TreeBasedIndex, HashIndex class ReleaseIndex(TreeBasedIndex): diff --git a/couchpotato/core/plugins/release/main.py b/couchpotato/core/plugins/release/main.py index 815cb404..1a359e3b 100644 --- a/couchpotato/core/plugins/release/main.py +++ b/couchpotato/core/plugins/release/main.py @@ -3,10 +3,10 @@ import os import time import traceback -from CodernityDB.database import RecordDeleted, RecordNotFound from couchpotato import md5, get_db from couchpotato.api import addApiView from couchpotato.core.event import fireEvent, addEvent +from couchpotato.core.helpers.database import RecordDeleted, RecordNotFound from couchpotato.core.helpers.encoding import toUnicode, sp from couchpotato.core.helpers.variable import getTitle, tryInt from couchpotato.core.logger import CPLog diff --git a/couchpotato/core/settings.py b/couchpotato/core/settings.py index 4315ec18..16432479 100644 --- a/couchpotato/core/settings.py +++ b/couchpotato/core/settings.py @@ -2,9 +2,9 @@ from __future__ import with_statement import ConfigParser from hashlib import md5 -from CodernityDB.hash_index import HashIndex from couchpotato.api import addApiView from couchpotato.core.event import addEvent, fireEvent +from couchpotato.core.helpers.database import HashIndex from couchpotato.core.helpers.encoding import toUnicode from couchpotato.core.helpers.variable import mergeDicts, tryInt, tryFloat diff --git a/couchpotato/runner.py b/couchpotato/runner.py index f753f82d..6743cf29 100644 --- a/couchpotato/runner.py +++ b/couchpotato/runner.py @@ -11,12 +11,12 @@ import re import tarfile import shutil -from CodernityDB.database_super_thread_safe import SuperThreadSafeDatabase from argparse import ArgumentParser from cache import FileSystemCache from couchpotato import KeyHandler, LoginHandler, LogoutHandler from couchpotato.api import NonBlockHandler, ApiHandler from couchpotato.core.event import fireEventAsync, fireEvent +from couchpotato.core.helpers.database import SuperThreadSafeDatabase from couchpotato.core.helpers.encoding import sp from couchpotato.core.helpers.variable import getDataDir, tryInt, getFreeSpace import requests