]> git.phdru.name Git - m_librarian.git/blob - m_librarian/db.py
9a5eb0af3e65dd95c5b0a2045e6b2c892ed51b2b
[m_librarian.git] / m_librarian / db.py
1 #! /usr/bin/env python
2
3 __all__ = ['Author', 'Book', 'Extension', 'Genre', 'Language',
4            'init_db', 'insert_name',
5            ]
6
7 import os
8 from sqlobject import SQLObject, StringCol, UnicodeCol, IntCol, BoolCol, \
9     ForeignKey, DateCol, RelatedJoin, \
10     connectionForURI, sqlhub, SQLObjectNotFound, dberrors
11 from .config import ml_conf
12
13 try:
14     db_uri = ml_conf.get('database', 'URI')
15 except:
16     db_uri = None
17
18 db_dirs = []
19 if not db_uri:
20     if 'XDG_CACHE_HOME' in os.environ:
21         db_dirs.append(os.environ['XDG_CACHE_HOME'])
22     home_cache = os.path.expanduser('~/.cache')
23     if home_cache not in db_dirs:
24         db_dirs.append(home_cache)
25
26     for d in db_dirs:
27         db_file = os.path.join(d, 'm_librarian.sqlite')
28         if os.path.exists(db_file):
29             break
30     else:
31         # octal; -rw-------;
32         # make the database file/directory readable/writeable only by the user
33         os.umask(0066)
34         db_dir = db_dirs[0]
35         try:
36             os.makedirs(db_dir)
37         except OSError:  # Perhaps already exists
38             pass
39         db_file = os.path.join(db_dir, 'm_librarian.sqlite')
40
41     db_uri = 'sqlite://%s' % db_file.replace(os.sep, '/')
42
43
44 sqlhub.processConnection = connection = connectionForURI(db_uri)
45
46 if connection.dbName == 'sqlite':
47     # Speedup SQLite connection
48     connection.query("PRAGMA synchronous=OFF")
49     connection.query("PRAGMA count_changes=OFF")
50     connection.query("PRAGMA journal_mode=MEMORY")
51     connection.query("PRAGMA temp_store=MEMORY")
52
53
54 class Author(SQLObject):
55     name = UnicodeCol(unique=True)
56     count = IntCol()
57     books = RelatedJoin('Book', otherColumn='book_id')
58
59
60 class Book(SQLObject):
61     authors = RelatedJoin('Author')
62     genres = RelatedJoin('Genre')
63     title = UnicodeCol()
64     series = UnicodeCol()
65     ser_no = IntCol()
66     archive = StringCol()
67     file = StringCol()
68     size = IntCol()
69     lib_id = StringCol()
70     deleted = BoolCol()
71     extension = ForeignKey('Extension')
72     date = DateCol()
73     language = ForeignKey('Language')
74
75
76 class Extension(SQLObject):
77     name = StringCol(unique=True)
78     count = IntCol()
79
80
81 class Genre(SQLObject):
82     name = StringCol(unique=True)
83     title = UnicodeCol()
84     count = IntCol()
85     books = RelatedJoin('Book', otherColumn='book_id')
86
87
88 class Language(SQLObject):
89     name = StringCol(unique=True)
90     count = IntCol()
91
92
93 def init_db():
94     try:
95         Book.select()[0]
96     except IndexError:  # Table exists but is empty
97         return
98     except dberrors.Error:
99         for table in Author, Extension, Genre, Language, Book:
100             table.createTable()
101     else:
102         return
103
104
105 def insert_name(table, name):
106     try:
107         return table.byName(name)
108     except SQLObjectNotFound:
109         return table(name=name, count=0)
110
111
112 def test():
113     print "DB dirs:", db_dirs
114     if db_uri:
115         print "DB URI:", db_uri
116
117 if __name__ == '__main__':
118     test()