diff --git a/setup.py b/setup.py index 352b9e3..ca97a79 100755 --- a/setup.py +++ b/setup.py @@ -18,7 +18,7 @@ from setuptools import find_packages reqs = [ 'flask>=0.11', - 'pony>=0.7.2', + 'pony>=0.7.6', 'Pillow', 'requests>=1.0.0', 'mutagen>=1.33' diff --git a/supysonic/cli.py b/supysonic/cli.py index 34f6f80..f41f0a5 100755 --- a/supysonic/cli.py +++ b/supysonic/cli.py @@ -14,9 +14,10 @@ import getpass import sys import time +from pony.orm import db_session from pony.orm import ObjectNotFound -from .db import Folder, User, db_session +from .db import Folder, User from .managers.folder import FolderManager from .managers.user import UserManager from .scanner import Scanner diff --git a/supysonic/db.py b/supysonic/db.py index db7b009..260a4f3 100644 --- a/supysonic/db.py +++ b/supysonic/db.py @@ -17,7 +17,6 @@ from pony.orm import Database, Required, Optional, Set, PrimaryKey, LongStr from pony.orm import ObjectNotFound from pony.orm import buffer from pony.orm import min, max, avg, sum, exists -from pony.orm import db_session as pony_session from uuid import UUID, uuid4 from .py23 import dict, strtype @@ -32,6 +31,11 @@ def now(): db = Database() +@db.on_connect(provider = 'sqlite') +def sqlite_case_insensitive_like(db, connection): + cursor = connection.cursor() + cursor.execute('PRAGMA case_sensitive_like = OFF') + class PathMixin(object): @classmethod def get(cls, *args, **kwargs): @@ -470,26 +474,6 @@ class Playlist(db.Entity): self.tracks = ','.join(t for t in tracks if t) -class DBSessionContextWrapper(object): - def __ensure_sqlite_case_insensitive_like(self): - if db.provider.dialect == 'SQLite': - db.execute('PRAGMA case_sensitive_like = OFF') - - def __call__(self, func, *args, **kwargs): - def new_func(*args, **kwargs): - self.__ensure_sqlite_case_insensitive_like() - return func(*args, **kwargs) - return pony_session(new_func, *args, **kwargs) - - def __enter__(self): - pony_session.__enter__() - self.__ensure_sqlite_case_insensitive_like() - - def __exit__(self, *args, **kwargs): - pony_session.__exit__(*args, **kwargs) - -db_session = DBSessionContextWrapper() - def parse_uri(database_uri): if not isinstance(database_uri, strtype): raise TypeError('Expecting a string') diff --git a/supysonic/scanner.py b/supysonic/scanner.py index ccf3038..1c2f718 100644 --- a/supysonic/scanner.py +++ b/supysonic/scanner.py @@ -12,11 +12,12 @@ import mimetypes import mutagen import time +from pony.orm import db_session + from .covers import find_cover_in_folder from .db import Folder, Artist, Album, Track, User from .db import StarredFolder, StarredArtist, StarredAlbum, StarredTrack from .db import RatingFolder, RatingTrack -from .db import db_session from .py23 import strtype class StatsDetails(object): diff --git a/supysonic/watcher.py b/supysonic/watcher.py index adafa9a..2f6d794 100644 --- a/supysonic/watcher.py +++ b/supysonic/watcher.py @@ -11,12 +11,13 @@ import logging import time from logging.handlers import TimedRotatingFileHandler +from pony.orm import db_session from signal import signal, SIGTERM, SIGINT from threading import Thread, Condition, Timer from watchdog.observers import Observer from watchdog.events import PatternMatchingEventHandler -from .db import init_database, release_database, db_session, Folder +from .db import init_database, release_database, Folder from .py23 import dict from .scanner import Scanner diff --git a/supysonic/web.py b/supysonic/web.py index 2814612..ac5259d 100644 --- a/supysonic/web.py +++ b/supysonic/web.py @@ -12,9 +12,10 @@ import mimetypes from flask import Flask from os import makedirs, path +from pony.orm import db_session from .config import IniConfig -from .db import init_database, db_session +from .db import init_database def create_application(config = None): global app diff --git a/tests/api/test_album_songs.py b/tests/api/test_album_songs.py index 0a8583e..e77d5e7 100644 --- a/tests/api/test_album_songs.py +++ b/tests/api/test_album_songs.py @@ -10,7 +10,9 @@ import uuid -from supysonic.db import Folder, Artist, Album, Track, db_session +from pony.orm import db_session + +from supysonic.db import Folder, Artist, Album, Track from .apitestbase import ApiTestBase diff --git a/tests/api/test_annotation.py b/tests/api/test_annotation.py index b552912..52f81f7 100644 --- a/tests/api/test_annotation.py +++ b/tests/api/test_annotation.py @@ -10,7 +10,9 @@ import uuid -from supysonic.db import Folder, Artist, Album, Track, User, ClientPrefs, db_session +from pony.orm import db_session + +from supysonic.db import Folder, Artist, Album, Track, User, ClientPrefs from .apitestbase import ApiTestBase diff --git a/tests/api/test_browse.py b/tests/api/test_browse.py index 69e0761..05ca9cb 100644 --- a/tests/api/test_browse.py +++ b/tests/api/test_browse.py @@ -12,8 +12,9 @@ import time import uuid from lxml import etree +from pony.orm import db_session -from supysonic.db import Folder, Artist, Album, Track, db_session +from supysonic.db import Folder, Artist, Album, Track from .apitestbase import ApiTestBase diff --git a/tests/api/test_media.py b/tests/api/test_media.py index c5c4526..26b43f7 100644 --- a/tests/api/test_media.py +++ b/tests/api/test_media.py @@ -13,8 +13,9 @@ import uuid from io import BytesIO from PIL import Image +from pony.orm import db_session -from supysonic.db import Folder, Artist, Album, Track, db_session +from supysonic.db import Folder, Artist, Album, Track from .apitestbase import ApiTestBase diff --git a/tests/api/test_playlist.py b/tests/api/test_playlist.py index aace508..0e9b063 100644 --- a/tests/api/test_playlist.py +++ b/tests/api/test_playlist.py @@ -10,7 +10,9 @@ import uuid -from supysonic.db import Folder, Artist, Album, Track, Playlist, User, db_session +from pony.orm import db_session + +from supysonic.db import Folder, Artist, Album, Track, Playlist, User from .apitestbase import ApiTestBase diff --git a/tests/api/test_search.py b/tests/api/test_search.py index ec62db2..8216257 100644 --- a/tests/api/test_search.py +++ b/tests/api/test_search.py @@ -11,9 +11,9 @@ import time import unittest -from pony.orm import commit +from pony.orm import db_session, commit -from supysonic.db import Folder, Artist, Album, Track, db_session +from supysonic.db import Folder, Artist, Album, Track from .apitestbase import ApiTestBase diff --git a/tests/api/test_transcoding.py b/tests/api/test_transcoding.py index 4a85373..0b3f959 100644 --- a/tests/api/test_transcoding.py +++ b/tests/api/test_transcoding.py @@ -10,7 +10,9 @@ import unittest -from supysonic.db import Folder, Track, db_session +from pony.orm import db_session + +from supysonic.db import Folder, Track from supysonic.managers.folder import FolderManager from supysonic.scanner import Scanner diff --git a/tests/base/test_cli.py b/tests/base/test_cli.py index d05c364..b0e11d0 100644 --- a/tests/base/test_cli.py +++ b/tests/base/test_cli.py @@ -15,13 +15,14 @@ import tempfile import unittest from contextlib import contextmanager +from pony.orm import db_session try: # Don't use io.StringIO on py2, it only accepts unicode and the CLI spits strs from StringIO import StringIO except ImportError: from io import StringIO -from supysonic.db import Folder, User, init_database, release_database, db_session +from supysonic.db import Folder, User, init_database, release_database from supysonic.cli import SupysonicCLI from ..testbase import TestConfig diff --git a/tests/base/test_db.py b/tests/base/test_db.py index a3ebe51..8421f7f 100644 --- a/tests/base/test_db.py +++ b/tests/base/test_db.py @@ -13,10 +13,10 @@ import unittest import uuid from collections import namedtuple +from pony.orm import db_session from supysonic import db -db_session = db.db_session date_regex = re.compile(r'^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}$') class DbTestCase(unittest.TestCase): diff --git a/tests/base/test_scanner.py b/tests/base/test_scanner.py index b570c73..74be660 100644 --- a/tests/base/test_scanner.py +++ b/tests/base/test_scanner.py @@ -15,14 +15,12 @@ import tempfile import unittest from contextlib import contextmanager -from pony.orm import commit +from pony.orm import db_session, commit from supysonic import db from supysonic.managers.folder import FolderManager from supysonic.scanner import Scanner -db_session = db.db_session - class ScannerTestCase(unittest.TestCase): def setUp(self): db.init_database('sqlite:', True) diff --git a/tests/base/test_watcher.py b/tests/base/test_watcher.py index 8cf63a5..2c19253 100644 --- a/tests/base/test_watcher.py +++ b/tests/base/test_watcher.py @@ -18,9 +18,10 @@ import unittest from contextlib import contextmanager from hashlib import sha1 +from pony.orm import db_session from threading import Thread -from supysonic.db import init_database, release_database, db_session, Track, Artist +from supysonic.db import init_database, release_database, Track, Artist from supysonic.managers.folder import FolderManager from supysonic.watcher import SupysonicWatcher diff --git a/tests/frontend/test_folder.py b/tests/frontend/test_folder.py index 0d88a1d..c42107c 100644 --- a/tests/frontend/test_folder.py +++ b/tests/frontend/test_folder.py @@ -10,7 +10,9 @@ import uuid -from supysonic.db import Folder, db_session +from pony.orm import db_session + +from supysonic.db import Folder from .frontendtestbase import FrontendTestBase diff --git a/tests/frontend/test_login.py b/tests/frontend/test_login.py index f5eb3fc..5dde4ff 100644 --- a/tests/frontend/test_login.py +++ b/tests/frontend/test_login.py @@ -11,7 +11,9 @@ import uuid -from supysonic.db import User, db_session +from pony.orm import db_session + +from supysonic.db import User from .frontendtestbase import FrontendTestBase diff --git a/tests/frontend/test_playlist.py b/tests/frontend/test_playlist.py index a5df907..67bdb22 100644 --- a/tests/frontend/test_playlist.py +++ b/tests/frontend/test_playlist.py @@ -10,7 +10,9 @@ import uuid -from supysonic.db import Folder, Artist, Album, Track, Playlist, User, db_session +from pony.orm import db_session + +from supysonic.db import Folder, Artist, Album, Track, Playlist, User from .frontendtestbase import FrontendTestBase diff --git a/tests/frontend/test_user.py b/tests/frontend/test_user.py index 2bcdd7f..67f72c3 100644 --- a/tests/frontend/test_user.py +++ b/tests/frontend/test_user.py @@ -11,8 +11,9 @@ import uuid from flask import escape +from pony.orm import db_session -from supysonic.db import User, ClientPrefs, db_session +from supysonic.db import User, ClientPrefs from .frontendtestbase import FrontendTestBase diff --git a/tests/issue101.py b/tests/issue101.py index c089315..98b68a5 100644 --- a/tests/issue101.py +++ b/tests/issue101.py @@ -12,7 +12,9 @@ import shutil import tempfile import unittest -from supysonic.db import init_database, release_database, db_session +from pony.orm import db_session + +from supysonic.db import init_database, release_database from supysonic.db import Folder from supysonic.managers.folder import FolderManager from supysonic.scanner import Scanner diff --git a/tests/managers/test_manager_folder.py b/tests/managers/test_manager_folder.py index c6ac034..8430479 100644 --- a/tests/managers/test_manager_folder.py +++ b/tests/managers/test_manager_folder.py @@ -18,9 +18,7 @@ import tempfile import unittest import uuid -from pony.orm import ObjectNotFound - -db_session = db.db_session +from pony.orm import db_session, ObjectNotFound class FolderManagerTestCase(unittest.TestCase): def setUp(self): diff --git a/tests/managers/test_manager_user.py b/tests/managers/test_manager_user.py index d723b11..082cdb2 100644 --- a/tests/managers/test_manager_user.py +++ b/tests/managers/test_manager_user.py @@ -17,11 +17,9 @@ import io import unittest import uuid -from pony.orm import commit +from pony.orm import db_session, commit from pony.orm import ObjectNotFound -db_session = db.db_session - class UserManagerTestCase(unittest.TestCase): def setUp(self): # Create an empty sqlite database in memory diff --git a/tests/testbase.py b/tests/testbase.py index 6a294f2..d93b477 100644 --- a/tests/testbase.py +++ b/tests/testbase.py @@ -14,7 +14,9 @@ import shutil import unittest import tempfile -from supysonic.db import init_database, release_database, db_session +from pony.orm import db_session + +from supysonic.db import init_database, release_database from supysonic.config import DefaultConfig from supysonic.managers.user import UserManager from supysonic.web import create_application