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.
10 __author__ = "Oleg Broytman <phd@phdru.name>"
11 __copyright__ = "Copyright (C) 2015 PhiloSoft Design"
12 __license__ = "GNU GPL"
15 from sqlobject import SQLObjectNotFound
16 from sqlobject.sqlbuilder import Insert, Update, Delete
17 from xsetbg_conf import xsetbg_dir, xsetbg_conf
18 from xsetbg_db import recreate_db, SQLiteMassInsert
20 if xsetbg_conf.has_option("images", "directory") or \
21 xsetbg_conf.has_option("images", "directory0") or \
22 xsetbg_conf.has_option("images", "directory1"):
24 if xsetbg_conf.has_option("images", "directory"):
25 image_dirs.append(xsetbg_conf.get("images", "directory"))
26 if xsetbg_conf.has_option("images", "directory0"):
27 image_dirs.append(xsetbg_conf.get("images", "directory0"))
28 if xsetbg_conf.has_option("images", "directory1"):
29 image_dirs.append(xsetbg_conf.get("images", "directory1"))
32 option = "directory%d" % i
33 if xsetbg_conf.has_option("images", option):
34 image_dirs.append(xsetbg_conf.get("images", option))
39 image_dirs = ["images"]
42 os.path.join(xsetbg_dir, os.path.expandvars(os.path.expanduser(dirname)))
43 for dirname in image_dirs
46 fs_encoding = xsetbg_conf.get("images", "fs_encoding")
47 xsetbg_db = recreate_db()
48 count_new = count_old = count_del = 0
50 with SQLiteMassInsert() as txn:
51 for image_dir in image_dirs:
52 # List images in all subdirectories
53 for dirpath, dirs, files in os.walk(image_dir):
55 full_name = os.path.join(dirpath, file).decode(fs_encoding)
57 row = xsetbg_db.byFull_name(full_name)
58 except SQLObjectNotFound:
59 values = {'full_name': full_name.encode('utf-8'),
61 query = txn.sqlrepr(Insert(xsetbg_db.sqlmeta.table,
68 count_del = xsetbg_db.select('flag IS NULL').count()
69 query = txn.sqlrepr(Delete(xsetbg_db.sqlmeta.table, 'flag IS NULL'))
71 query = txn.sqlrepr(Update(xsetbg_db.sqlmeta.table, {'flag': None}))
74 print "New images:", count_new
75 print "Existing images:", count_old
76 print "Removed images:", count_del