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)
21 is_image = BoolCol(default=None)
23 last_shown_idx = DatabaseIndex('last_shown')
24 flag_idx = DatabaseIndex('flag')
25 is_image_idx = DatabaseIndex('is_image')
32 xsetbg_db.createTable()
36 class SQLiteMassInsert(object):
38 self.connection = sqlhub.processConnection
41 connection = self.connection
42 connection.query("PRAGMA synchronous=OFF")
43 connection.query("PRAGMA count_changes=OFF")
44 connection.query("PRAGMA journal_mode=MEMORY")
45 connection.query("PRAGMA temp_store=MEMORY")
47 txn = connection.transaction()
48 sqlhub.processConnection = txn
51 def __exit__(self, *args, **kw):
52 sqlhub.processConnection.commit()
53 connection = sqlhub.processConnection = self.connection
54 connection.query("VACUUM")
59 xsetbg_db_path = xsetbg_conf.get('xsetbg', 'database')
63 if not xsetbg_db_path:
64 if 'XDG_CACHE_HOME' in os.environ:
65 db_dirs.append(os.environ['XDG_CACHE_HOME'])
66 home_cache = os.path.expanduser('~/.cache')
67 if home_cache not in db_dirs:
68 db_dirs.append(home_cache)
69 db_dirs.append(os.path.dirname(os.path.abspath(__file__)))
72 xsetbg_db_path = os.path.join(d, 'xsetbg.sqlite')
73 if os.path.exists(xsetbg_db_path):
77 sqlhub.processConnection = \
78 connectionForURI('sqlite:///%s' % xsetbg_db_path)
82 xsetbg_db = XSetBg # Table exists but is empty
83 except dberrors.Error:
90 if __name__ == '__main__':
91 print("DB dirs:", db_dirs)
92 print("DB file:", xsetbg_db_path)