4 from sqlobject import SQLObject, StringCol, UnicodeCol, IntCol, BoolCol, \
5 ForeignKey, DateCol, connectionForURI, sqlhub, dberrors
6 from .config import ml_conf
9 db_uri = ml_conf.get('database', '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)
22 db_file = os.path.join(d, 'm_librarian.sqlite')
23 if os.path.exists(db_file):
27 # make the database file/directory readable/writeable only by the user
32 except OSError: # Perhaps already exists
34 db_file = os.path.join(db_dir, 'm_librarian.sqlite')
36 db_uri = 'sqlite://%s' % db_file.replace(os.sep, '/')
38 sqlhub.processConnection = connectionForURI(db_uri)
41 class Author(SQLObject):
42 name = UnicodeCol(unique=True)
46 class Book(SQLObject):
47 author = ForeignKey('Author')
48 genre = ForeignKey('Genre')
56 ext = ForeignKey('Extension')
58 language = ForeignKey('Language')
61 class Extension(SQLObject):
62 name = StringCol(unique=True)
66 class Genre(SQLObject):
67 name = StringCol(unique=True)
72 class Language(SQLObject):
73 name = StringCol(unique=True)
80 except IndexError: # Table exists but is empty
82 except dberrors.Error:
83 for table in Author, Extension, Genre, Language, Book:
89 if __name__ == '__main__':
90 print "DB dirs:", db_dirs
92 print "DB URI:", db_uri