]> git.phdru.name Git - xsetbg.git/blob - xsetbg_db.py
Fix flake8 warnings
[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, connectionForURI, sqlhub, \
14     StringCol, UnicodeCol, IntCol, BoolCol, DatabaseIndex, dberrors
15 from xsetbg_conf import xsetbg_conf
16
17
18 class SqliteSequence(SQLObject):
19     class sqlmeta:
20         idName = 'rowid'
21     name = StringCol(unique=True)
22     seq = IntCol()
23
24
25 class XSetBg(SQLObject):
26     full_name = UnicodeCol(alternateID=True)
27     last_shown = IntCol(default=None)  # timestamp
28     flag = BoolCol(default=None)
29
30     last_shown_idx = DatabaseIndex('last_shown')
31     flag_idx = DatabaseIndex('flag')
32
33
34 db_dirs = []
35 try:
36     xsetbg_db_path = xsetbg_conf.get('xsetbg', 'database')
37 except:
38     xsetbg_db_path = None
39
40 if not xsetbg_db_path:
41     if 'XDG_CACHE_HOME' in os.environ:
42         db_dirs.append(os.environ['XDG_CACHE_HOME'])
43     home_cache = os.path.expanduser('~/.cache')
44     if home_cache not in db_dirs:
45         db_dirs.append(home_cache)
46     db_dirs.append(os.path.dirname(os.path.abspath(__file__)))
47
48     for d in db_dirs:
49         xsetbg_db_path = os.path.join(d, 'xsetbg.sqlite')
50         if os.path.exists(xsetbg_db_path):
51             break
52
53 if xsetbg_db_path:
54     sqlhub.processConnection = \
55         connectionForURI('sqlite:///%s' % xsetbg_db_path)
56     try:
57         XSetBg.select()[0]
58     except IndexError:
59         xsetbg_db = XSetBg  # Table exists but is empty
60     except dberrors.Error:
61         xsetbg_db = None
62     else:
63         xsetbg_db = XSetBg
64 else:
65     xsetbg_db = None
66
67 if __name__ == '__main__':
68     print "DB dirs:", db_dirs
69     print "DB file:", xsetbg_db_path