1
1
# -*- coding: utf-8 -*-
2
2
3
- from peewee import OperationalError , IntegrityError
3
+ import peewee
4
+ import sqlite3
4
5
from .bookmark import Bookmark , TemporaryBookmark , BookmarkBaseModel , db
5
6
6
7
import logging
8
+
7
9
logging .basicConfig (level = logging .INFO )
8
10
logger = logging .getLogger (__name__ )
9
11
10
-
11
12
TABLES = {
12
13
False : Bookmark ,
13
14
True : TemporaryBookmark ,
14
15
}
15
16
16
17
17
18
class BookmarkManager (BookmarkBaseModel ):
18
-
19
19
@staticmethod
20
20
def connect ():
21
21
try :
@@ -24,7 +24,9 @@ def connect():
24
24
db .create_tables ([Bookmark , TemporaryBookmark ], safe = True )
25
25
logger .info ('Table Bookmark and TemporaryBookmark create/updates '
26
26
'successfully!' )
27
- except OperationalError as exc :
27
+ except peewee .OperationalError as exc :
28
+ logger .exception (exc )
29
+ except sqlite3 .OperationalError as exc :
28
30
logger .exception (exc )
29
31
30
32
@staticmethod
@@ -41,7 +43,7 @@ def add_bookmark(name, path, page, data=None, table=Bookmark):
41
43
comic_page = page , page_data = data )
42
44
q .execute ()
43
45
logger .info ('%s item inserted.' % table .__class__ )
44
- except IntegrityError :
46
+ except peewee . IntegrityError :
45
47
q = table .update (comic_page = page , page_data = data ).where (
46
48
table .comic_path == path )
47
49
q .execute ()
@@ -55,26 +57,38 @@ def remove_bookmark(path, table=Bookmark):
55
57
q = table .delete ().where (table .comic_path == path )
56
58
q .execute ()
57
59
logger .info ('Bookmark deleted.' )
58
- except IntegrityError :
60
+ except peewee . IntegrityError :
59
61
logger .exception ('Bookmark not find!' )
60
62
BookmarkManager .close ()
61
63
62
64
@staticmethod
63
65
def get_bookmarks (rows_number , table = Bookmark ):
64
- query = table .select ().order_by (table .comic_id .desc ()).limit (
65
- rows_number )
66
- return list (query )
66
+ try :
67
+ query = table .select ().order_by (table .comic_id .desc ()).limit (
68
+ rows_number )
69
+ return list (query )
70
+ except peewee .OperationalError as exc :
71
+ logger .exception (exc )
72
+ return []
67
73
68
74
@staticmethod
69
75
def get_bookmark_by_path (path , table = Bookmark ):
70
- BookmarkManager .connect ()
71
- bk_list = table .select ().where (table .comic_path == path )
72
- BookmarkManager .close ()
73
- return bk_list [0 ] if bk_list else None
76
+ try :
77
+ BookmarkManager .connect ()
78
+ bk_list = table .select ().where (table .comic_path == path )
79
+ BookmarkManager .close ()
80
+ return bk_list [0 ] if bk_list else None
81
+ except peewee .OperationalError as exc :
82
+ logger .exception (exc )
83
+ return None
74
84
75
85
@staticmethod
76
86
def is_bookmark (path , table = Bookmark ):
77
- BookmarkManager .connect ()
78
- bk_list = table .select ().where (table .comic_path == path )
79
- BookmarkManager .close ()
80
- return True if bk_list else False
87
+ try :
88
+ BookmarkManager .connect ()
89
+ bk_list = table .select ().where (table .comic_path == path )
90
+ BookmarkManager .close ()
91
+ return True if bk_list else False
92
+ except peewee .OperationalError as exc :
93
+ logger .exception (exc )
94
+ return False
0 commit comments