3 __all__ = ['Author', 'Book', 'Extension', 'Genre', 'Language',
4 'init_db', 'insert_name',
8 from sqlobject import SQLObject, StringCol, UnicodeCol, IntCol, BoolCol, \
9 ForeignKey, DateCol, RelatedJoin, \
10 connectionForURI, sqlhub, SQLObjectNotFound, dberrors
11 from .config import ml_conf
14 db_uri = ml_conf.get('database', '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)
27 db_file = os.path.join(d, 'm_librarian.sqlite')
28 if os.path.exists(db_file):
32 # make the database file/directory readable/writeable only by the user
37 except OSError: # Perhaps already exists
39 db_file = os.path.join(db_dir, 'm_librarian.sqlite')
41 db_uri = 'sqlite://%s' % db_file.replace(os.sep, '/')
43 sqlhub.processConnection = connectionForURI(db_uri)
46 class Author(SQLObject):
47 name = UnicodeCol(unique=True)
49 books = RelatedJoin('Book', otherColumn='book_id')
52 class Book(SQLObject):
53 authors = RelatedJoin('Author')
54 genres = RelatedJoin('Genre')
63 extension = ForeignKey('Extension')
65 language = ForeignKey('Language')
68 class Extension(SQLObject):
69 name = StringCol(unique=True)
73 class Genre(SQLObject):
74 name = StringCol(unique=True)
77 books = RelatedJoin('Book', otherColumn='book_id')
80 class Language(SQLObject):
81 name = StringCol(unique=True)
88 except IndexError: # Table exists but is empty
90 except dberrors.Error:
91 for table in Author, Extension, Genre, Language, Book:
97 def insert_name(table, name):
99 return table.byName(name)
100 except SQLObjectNotFound:
101 return table(name=name, count=0)
105 print "DB dirs:", db_dirs
107 print "DB URI:", db_uri
109 if __name__ == '__main__':