From b0be7aba8bd6646c6327da5d46bcbcb28ef2aef5 Mon Sep 17 00:00:00 2001 From: Oleg Broytman Date: Sun, 20 Dec 2015 01:02:15 +0300 Subject: [PATCH] Add database schema --- m_librarian/db.py | 62 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100755 m_librarian/db.py diff --git a/m_librarian/db.py b/m_librarian/db.py new file mode 100755 index 0000000..e78f8f1 --- /dev/null +++ b/m_librarian/db.py @@ -0,0 +1,62 @@ +#! /usr/bin/env python + +import os +from sqlobject import SQLObject, StringCol, UnicodeCol, IntCol, \ + connectionForURI, sqlhub +from .config import ml_conf + +try: + db_uri = ml_conf.get('database', 'URI') +except: + db_uri = None + +db_dirs = [] +if not db_uri: + if 'XDG_CACHE_HOME' in os.environ: + db_dirs.append(os.environ['XDG_CACHE_HOME']) + home_cache = os.path.expanduser('~/.cache') + if home_cache not in db_dirs: + db_dirs.append(home_cache) + + for d in db_dirs: + db_file = os.path.join(d, 'm_librarian.sqlite') + if os.path.exists(db_file): + break + else: + # octal; -rw-------; + # make the database file/directory readable/writeable only by the user + os.umask(0066) + db_dir = db_dirs[0] + try: + os.makedirs(db_dir) + except OSError: # Perhaps already exists + pass + db_file = os.path.join(db_dir, 'm_librarian.sqlite') + db_uri = 'sqlite://%s' % db_file.replace(os.sep, '/') + +sqlhub.processConnection = connectionForURI(db_uri) + + +class Author(SQLObject): + name = UnicodeCol() + count = IntCol() + + +class Book(SQLObject): + name = UnicodeCol() + + +class Extension(SQLObject): + name = StringCol() + count = IntCol() + + +class Language(SQLObject): + name = StringCol() + count = IntCol() + + +if __name__ == '__main__': + print "DB dirs:", db_dirs + if db_uri: + print "DB URI:", db_uri -- 2.39.5