]> git.phdru.name Git - xsetbg.git/blob - xsetbg_db.py
Search xsetbg.db in XDG directories
[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 PhiloSoft Design"
8 __license__ = "GNU GPL"
9
10 __all__ = ['xsetbg_db_path', 'xsetbg_db']
11
12 import anydbm
13 import os
14 import shelve
15 from xsetbg_conf import xsetbg_conf
16
17 db_dirs = []
18 try:
19     db_file = xsetbg_conf.get('xsetbg', 'database')
20 except:
21     db_file = None
22
23 if not db_file:
24     if 'XDG_CACHE_HOME' in os.environ:
25         db_dirs.append(os.environ['XDG_CACHE_HOME'])
26     home_cache = os.path.expanduser('~/.cache')
27     if home_cache not in db_dirs:
28         db_dirs.append(home_cache)
29     db_dirs.append(os.path.dirname(os.path.abspath(__file__)))
30
31     for d in db_dirs:
32         db_file = os.path.join(d, 'xsetbg.db')
33         if os.path.exists(db_file):
34             break
35     else:
36         raise RuntimeError("Cannot find xsetbg.db; searched %s", db_dirs)
37
38 xsetbg_db_path = db_file
39
40 try:
41     xsetbg_db = shelve.open(xsetbg_db_path, 'r')
42 except anydbm.error:
43     xsetbg_db = None
44
45 if __name__ == '__main__':
46     print "DB dirs:", db_dirs
47     print "DB file:", db_file