2 """Rescan filesystem and update database
4 Rescan images directories; remove unknown images from DB; add new images.
6 This file is a part of XSetBg.
11 from sqlobject import SQLObjectNotFound
12 from sqlobject.sqlbuilder import Insert, Update, Delete
13 from xsetbg_conf import xsetbg_dir, xsetbg_conf
14 from xsetbg_db import recreate_db, SQLiteMassInsert
16 if xsetbg_conf.has_option("images", "directory") or \
17 xsetbg_conf.has_option("images", "directory0") or \
18 xsetbg_conf.has_option("images", "directory1"):
20 if xsetbg_conf.has_option("images", "directory"):
21 image_dirs.append(xsetbg_conf.get("images", "directory"))
22 if xsetbg_conf.has_option("images", "directory0"):
23 image_dirs.append(xsetbg_conf.get("images", "directory0"))
24 if xsetbg_conf.has_option("images", "directory1"):
25 image_dirs.append(xsetbg_conf.get("images", "directory1"))
28 option = "directory%d" % i
29 if xsetbg_conf.has_option("images", option):
30 image_dirs.append(xsetbg_conf.get("images", option))
35 image_dirs = ["images"]
38 os.path.join(xsetbg_dir, os.path.expandvars(os.path.expanduser(dirname)))
39 for dirname in image_dirs
42 fs_encoding = xsetbg_conf.get("images", "fs_encoding")
43 xsetbg_db = recreate_db()
44 count_new = count_old = count_del = 0
46 with SQLiteMassInsert() as txn:
47 for image_dir in image_dirs:
48 # List images in all subdirectories
49 for dirpath, dirs, files in os.walk(image_dir):
51 full_name = os.path.join(dirpath, file).decode(fs_encoding)
53 row = xsetbg_db.byFull_name(full_name)
54 except SQLObjectNotFound:
55 values = {'full_name': full_name.encode('utf-8'),
57 query = txn.sqlrepr(Insert(xsetbg_db.sqlmeta.table,
64 count_del = xsetbg_db.select('flag IS NULL').count()
65 query = txn.sqlrepr(Delete(xsetbg_db.sqlmeta.table, 'flag IS NULL'))
67 query = txn.sqlrepr(Update(xsetbg_db.sqlmeta.table, {'flag': None}))
70 print "New images:", count_new
71 print "Existing images:", count_old
72 print "Removed images:", count_del