]> git.phdru.name Git - xsetbg.git/blob - xsetbg_db.py
Starting to switch XSetBg to SQLite instead of bsddb
[xsetbg.git] / xsetbg_db.py
1 #! /usr/bin/env python
2 """XSetBg database
3
4 """
5
6 __author__ = "Oleg Broytman <phd@phdru.name>"
7 __copyright__ = "Copyright (C) 2014, 2015 PhiloSoft Design"
8 __license__ = "GNU GPL"
9
10 __all__ = ['xsetbg_db_path', 'xsetbg_db']
11
12 import os
13 from sqlobject import SQLObject, StringCol, IntCol, BoolCol, dberrors
14 from xsetbg_conf import xsetbg_conf
15
16
17 class XSetbg(SQLObject):
18     path = StringCol(alternateID=True)
19     last_shown = IntCol(default=None) # timestamp
20     flag = BoolCol(default=None)
21
22
23 db_dirs = []
24 try:
25     xsetbg_db_path = xsetbg_conf.get('xsetbg', 'database')
26 except:
27     xsetbg_db_path = None
28
29 if not xsetbg_db_path:
30     if 'XDG_CACHE_HOME' in os.environ:
31         db_dirs.append(os.environ['XDG_CACHE_HOME'])
32     home_cache = os.path.expanduser('~/.cache')
33     if home_cache not in db_dirs:
34         db_dirs.append(home_cache)
35     db_dirs.append(os.path.dirname(os.path.abspath(__file__)))
36
37     for d in db_dirs:
38         xsetbg_db_path = os.path.join(d, 'xsetbg.sqlite')
39         if os.path.exists(xsetbg_db_path):
40             break
41
42 if xsetbg_db_path:
43     try:
44         XSetbg.setConnection('sqlite:///' + xsetbg_db_path)
45         XSetbg.select()[0]
46     except dberrors.Error:
47         xsetbg_db = None
48     else:
49         xsetbg_db = XSetbg
50 else:
51     xsetbg_db = None
52
53 if __name__ == '__main__':
54     print "DB dirs:", db_dirs
55     print "DB file:", xsetbg_db_path