6 __author__ = "Oleg Broytman <phd@phdru.name>"
7 __copyright__ = "Copyright (C) 2014, 2015 PhiloSoft Design"
8 __license__ = "GNU GPL"
10 __all__ = ['xsetbg_db_path', 'xsetbg_db']
13 from sqlobject import SQLObject, connectionForURI, sqlhub, \
14 UnicodeCol, IntCol, BoolCol, DatabaseIndex, dberrors
15 from xsetbg_conf import xsetbg_conf
17 # octal; -rw-------; make the database file(s) readable only by the user
21 class XSetBg(SQLObject):
22 full_name = UnicodeCol(alternateID=True)
23 last_shown = IntCol(default=None) # timestamp
24 flag = BoolCol(default=None)
26 last_shown_idx = DatabaseIndex('last_shown')
27 flag_idx = DatabaseIndex('flag')
34 xsetbg_db.createTable()
38 class SQLiteMassInsert(object):
40 self.connection = sqlhub.processConnection
43 connection = self.connection
44 connection.query("PRAGMA synchronous=OFF")
45 connection.query("PRAGMA count_changes=OFF")
46 connection.query("PRAGMA journal_mode=MEMORY")
47 connection.query("PRAGMA temp_store=MEMORY")
49 txn = connection.transaction()
50 sqlhub.processConnection = txn
53 def __exit__(self, *args, **kw):
54 sqlhub.processConnection.commit()
55 connection = sqlhub.processConnection = self.connection
56 connection.query("VACUUM %s" % xsetbg_db.sqlmeta.table)
61 xsetbg_db_path = xsetbg_conf.get('xsetbg', 'database')
65 if not xsetbg_db_path:
66 if 'XDG_CACHE_HOME' in os.environ:
67 db_dirs.append(os.environ['XDG_CACHE_HOME'])
68 home_cache = os.path.expanduser('~/.cache')
69 if home_cache not in db_dirs:
70 db_dirs.append(home_cache)
71 db_dirs.append(os.path.dirname(os.path.abspath(__file__)))
74 xsetbg_db_path = os.path.join(d, 'xsetbg.sqlite')
75 if os.path.exists(xsetbg_db_path):
79 sqlhub.processConnection = \
80 connectionForURI('sqlite:///%s' % xsetbg_db_path)
84 xsetbg_db = XSetBg # Table exists but is empty
85 except dberrors.Error:
92 if __name__ == '__main__':
93 print "DB dirs:", db_dirs
94 print "DB file:", xsetbg_db_path