]> git.phdru.name Git - m_librarian.git/blob - m_librarian/db.py
Fix Db schema
[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 sqlhub.processConnection = connectionForURI(db_uri)
44
45
46 class Author(SQLObject):
47     name = UnicodeCol(unique=True)
48     count = IntCol()
49     books = RelatedJoin('Book', otherColumn='book_id')
50
51
52 class Book(SQLObject):
53     authors = RelatedJoin('Author')
54     genres = RelatedJoin('Genre')
55     title = UnicodeCol()
56     series = UnicodeCol()
57     ser_no = IntCol()
58     archive = StringCol()
59     file = StringCol()
60     size = IntCol()
61     lib_id = StringCol()
62     deleted = BoolCol()
63     extension = ForeignKey('Extension')
64     date = DateCol()
65     language = ForeignKey('Language')
66
67
68 class Extension(SQLObject):
69     name = StringCol(unique=True)
70     count = IntCol()
71
72
73 class Genre(SQLObject):
74     name = StringCol(unique=True)
75     title = UnicodeCol()
76     count = IntCol()
77     books = RelatedJoin('Book', otherColumn='book_id')
78
79
80 class Language(SQLObject):
81     name = StringCol(unique=True)
82     count = IntCol()
83
84
85 def init_db():
86     try:
87         Book.select()[0]
88     except IndexError:  # Table exists but is empty
89         return
90     except dberrors.Error:
91         for table in Author, Extension, Genre, Language, Book:
92             table.createTable()
93     else:
94         return
95
96
97 def insert_name(table, name):
98     try:
99         return table.byName(name)
100     except SQLObjectNotFound:
101         return table(name=name, count=0)
102
103
104 def test():
105     print "DB dirs:", db_dirs
106     if db_uri:
107         print "DB URI:", db_uri
108
109 if __name__ == '__main__':
110     test()