7 from sqlobject import SQLObject, connectionForURI, sqlhub, \
8 UnicodeCol, IntCol, BoolCol, DatabaseIndex, dberrors
9 from xsetbg_conf import xsetbg_conf
11 __all__ = ['xsetbg_db_path', 'xsetbg_db']
13 # octal; -rw-------; make the database file(s) readable only by the user
17 class XSetBg(SQLObject):
18 full_name = UnicodeCol(alternateID=True)
19 last_shown = IntCol(default=None) # timestamp
20 flag = BoolCol(default=None)
22 last_shown_idx = DatabaseIndex('last_shown')
23 flag_idx = DatabaseIndex('flag')
30 xsetbg_db.createTable()
34 class SQLiteMassInsert(object):
36 self.connection = sqlhub.processConnection
39 connection = self.connection
40 connection.query("PRAGMA synchronous=OFF")
41 connection.query("PRAGMA count_changes=OFF")
42 connection.query("PRAGMA journal_mode=MEMORY")
43 connection.query("PRAGMA temp_store=MEMORY")
45 txn = connection.transaction()
46 sqlhub.processConnection = txn
49 def __exit__(self, *args, **kw):
50 sqlhub.processConnection.commit()
51 connection = sqlhub.processConnection = self.connection
52 connection.query("VACUUM %s" % xsetbg_db.sqlmeta.table)
57 xsetbg_db_path = xsetbg_conf.get('xsetbg', 'database')
61 if not xsetbg_db_path:
62 if 'XDG_CACHE_HOME' in os.environ:
63 db_dirs.append(os.environ['XDG_CACHE_HOME'])
64 home_cache = os.path.expanduser('~/.cache')
65 if home_cache not in db_dirs:
66 db_dirs.append(home_cache)
67 db_dirs.append(os.path.dirname(os.path.abspath(__file__)))
70 xsetbg_db_path = os.path.join(d, 'xsetbg.sqlite')
71 if os.path.exists(xsetbg_db_path):
75 sqlhub.processConnection = \
76 connectionForURI('sqlite:///%s' % xsetbg_db_path)
80 xsetbg_db = XSetBg # Table exists but is empty
81 except dberrors.Error:
88 if __name__ == '__main__':
89 print "DB dirs:", db_dirs
90 print "DB file:", xsetbg_db_path