]> git.phdru.name Git - xsetbg.git/blobdiff - xsetbg_db.py
Fix(DB): Fix column encoding
[xsetbg.git] / xsetbg_db.py
old mode 100644 (file)
new mode 100755 (executable)
index 54bc62e..ba5ab6c
@@ -1,21 +1,93 @@
+#! /usr/bin/env python3
 """XSetBg database
 
 """
 
-__author__ = "Oleg Broytman <phd@phdru.name>"
-__copyright__ = "Copyright (C) 2014 PhiloSoft Design"
-__license__ = "GNU GPL"
+import os
+from sqlobject import SQLObject, connectionForURI, sqlhub, \
+    UnicodeCol, IntCol, BoolCol, DatabaseIndex, dberrors
+from xsetbg_conf import xsetbg_conf
 
 __all__ = ['xsetbg_db_path', 'xsetbg_db']
 
-import anydbm
-import os
-import shelve
-from xsetbg_conf import xsetbg_conf
+# octal; -rw-------; make the database file(s) readable only by the user
+os.umask(0o066)
+
+
+class XSetBg(SQLObject):
+    full_name = UnicodeCol(alternateID=True)
+    last_shown = IntCol(default=None)  # timestamp
+    flag = BoolCol(default=None)
+    is_image = BoolCol(default=None)
+
+    last_shown_idx = DatabaseIndex('last_shown')
+    flag_idx = DatabaseIndex('flag')
+    is_image_idx = DatabaseIndex('is_image')
+
+
+def recreate_db():
+    global xsetbg_db
+    if not xsetbg_db:
+        xsetbg_db = XSetBg
+        xsetbg_db.createTable()
+    return xsetbg_db
 
-xsetbg_db_path = os.path.expanduser(xsetbg_conf.get('xsetbg', 'database'))
 
+class SQLiteMassInsert(object):
+    def __init__(self):
+        self.connection = sqlhub.processConnection
+
+    def __enter__(self):
+        connection = self.connection
+        connection.query("PRAGMA synchronous=OFF")
+        connection.query("PRAGMA count_changes=OFF")
+        connection.query("PRAGMA journal_mode=MEMORY")
+        connection.query("PRAGMA temp_store=MEMORY")
+
+        txn = connection.transaction()
+        sqlhub.processConnection = txn
+        return txn
+
+    def __exit__(self, *args, **kw):
+        sqlhub.processConnection.commit(close=True)
+        connection = sqlhub.processConnection = self.connection
+        connection.query("VACUUM")
+        connection.close()
+
+
+db_dirs = []
 try:
-    xsetbg_db = shelve.open(xsetbg_db_path, 'r')
-except anydbm.error:
+    xsetbg_db_path = xsetbg_conf.get('xsetbg', 'database')
+except Exception:
+    xsetbg_db_path = None
+
+if not xsetbg_db_path:
+    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)
+    db_dirs.append(os.path.dirname(os.path.abspath(__file__)))
+
+    for d in db_dirs:
+        xsetbg_db_path = os.path.join(d, 'xsetbg.sqlite')
+        if os.path.exists(xsetbg_db_path):
+            break
+
+if xsetbg_db_path:
+    sqlhub.processConnection = \
+        connectionForURI('sqlite:///%s' % xsetbg_db_path)
+    try:
+        XSetBg.select()[0]
+    except IndexError:
+        xsetbg_db = XSetBg  # Table exists but is empty
+    except dberrors.Error:
+        xsetbg_db = None
+    else:
+        xsetbg_db = XSetBg
+else:
     xsetbg_db = None
+
+if __name__ == '__main__':
+    print("DB dirs:", db_dirs)
+    print("DB file:", xsetbg_db_path)