3 __all__ = ['Author', 'Book', 'Extension', 'Genre', 'Language',
8 from sqlobject import SQLObject, StringCol, UnicodeCol, IntCol, BoolCol, \
9 ForeignKey, DateCol, connectionForURI, sqlhub, dberrors
10 from .config import ml_conf
13 db_uri = ml_conf.get('database', 'URI')
19 if 'XDG_CACHE_HOME' in os.environ:
20 db_dirs.append(os.environ['XDG_CACHE_HOME'])
21 home_cache = os.path.expanduser('~/.cache')
22 if home_cache not in db_dirs:
23 db_dirs.append(home_cache)
26 db_file = os.path.join(d, 'm_librarian.sqlite')
27 if os.path.exists(db_file):
31 # make the database file/directory readable/writeable only by the user
36 except OSError: # Perhaps already exists
38 db_file = os.path.join(db_dir, 'm_librarian.sqlite')
40 db_uri = 'sqlite://%s' % db_file.replace(os.sep, '/')
42 sqlhub.processConnection = connectionForURI(db_uri)
45 class Author(SQLObject):
46 name = UnicodeCol(unique=True)
50 class Book(SQLObject):
51 author = ForeignKey('Author')
52 genre = ForeignKey('Genre')
60 ext = ForeignKey('Extension')
62 language = ForeignKey('Language')
65 class Extension(SQLObject):
66 name = StringCol(unique=True)
70 class Genre(SQLObject):
71 name = StringCol(unique=True)
76 class Language(SQLObject):
77 name = StringCol(unique=True)
84 except IndexError: # Table exists but is empty
86 except dberrors.Error:
87 for table in Author, Extension, Genre, Language, Book:
94 print "DB dirs:", db_dirs
96 print "DB URI:", db_uri
98 if __name__ == '__main__':