]> git.phdru.name Git - m_librarian.git/blob - m_librarian/db.py
Extend db structure for Book; add Genre
[m_librarian.git] / m_librarian / db.py
1 #! /usr/bin/env python
2
3 import os
4 from sqlobject import SQLObject, StringCol, UnicodeCol, IntCol, BoolCol, \
5     ForeignKey, DateCol, connectionForURI, sqlhub
6 from .config import ml_conf
7
8 try:
9     db_uri = ml_conf.get('database', 'URI')
10 except:
11     db_uri = None
12
13 db_dirs = []
14 if not db_uri:
15     if 'XDG_CACHE_HOME' in os.environ:
16         db_dirs.append(os.environ['XDG_CACHE_HOME'])
17     home_cache = os.path.expanduser('~/.cache')
18     if home_cache not in db_dirs:
19         db_dirs.append(home_cache)
20
21     for d in db_dirs:
22         db_file = os.path.join(d, 'm_librarian.sqlite')
23         if os.path.exists(db_file):
24             break
25     else:
26         # octal; -rw-------;
27         # make the database file/directory readable/writeable only by the user
28         os.umask(0066)
29         db_dir = db_dirs[0]
30         try:
31             os.makedirs(db_dir)
32         except OSError:  # Perhaps already exists
33             pass
34         db_file = os.path.join(db_dir, 'm_librarian.sqlite')
35         db_uri = 'sqlite://%s' % db_file.replace(os.sep, '/')
36
37 sqlhub.processConnection = connectionForURI(db_uri)
38
39
40 class Author(SQLObject):
41     name = UnicodeCol()
42     count = IntCol()
43
44
45 class Genre(SQLObject):
46     name = StringCol()
47     title = UnicodeCol()
48     count = IntCol()
49
50
51 class Book(SQLObject):
52     author = ForeignKey('Author')
53     genre = ForeignKey('Genre')
54     title = UnicodeCol()
55     series = UnicodeCol()
56     ser_no = IntCol()
57     file = StringCol()
58     size = IntCol()
59     lib_id = StringCol()
60     deleted = BoolCol()
61     ext = ForeignKey('Extension')
62     date = DateCol()
63     language = ForeignKey('Language')
64
65
66 class Extension(SQLObject):
67     name = StringCol()
68     count = IntCol()
69
70
71 class Language(SQLObject):
72     name = StringCol()
73     count = IntCol()
74
75
76 if __name__ == '__main__':
77     print "DB dirs:", db_dirs
78     if db_uri:
79         print "DB URI:", db_uri