]> git.phdru.name Git - xsetbg.git/commitdiff
Feat: Update `is_image`
authorOleg Broytman <phd@phdru.name>
Mon, 20 Aug 2018 14:49:33 +0000 (17:49 +0300)
committerOleg Broytman <phd@phdru.name>
Mon, 20 Aug 2018 14:49:33 +0000 (17:49 +0300)
rescan_fs.py

index 07d3fa31c07d6188353941416c4d69e709eb7c28..1b74f8f15213a4932f4e5326f134bf60e815183b 100755 (executable)
@@ -8,6 +8,7 @@ This file is a part of XSetBg.
 """
 
 import os
+import subprocess
 from sqlobject import SQLObjectNotFound
 from sqlobject.sqlbuilder import Insert, Update, Delete
 from xsetbg_conf import xsetbg_dir, xsetbg_conf
@@ -43,6 +44,15 @@ fs_encoding = xsetbg_conf.get("images", "fs_encoding")
 xsetbg_db = recreate_db()
 count_new = count_old = count_del = 0
 
+NULL = open(os.devnull, 'w')
+
+
+def is_image(full_path):
+    # Run `identify` from ImageMagic; convert retcode to bool
+    return not subprocess.call(['identify', full_path],
+                               stdout=NULL, stderr=subprocess.STDOUT)
+
+
 with SQLiteMassInsert() as txn:
     for image_dir in image_dirs:
         # List images in all subdirectories
@@ -53,12 +63,16 @@ with SQLiteMassInsert() as txn:
                     row = xsetbg_db.byFull_name(full_name)
                 except SQLObjectNotFound:
                     values = {'full_name': full_name.encode('utf-8'),
-                              'flag': True}
+                              'is_image': is_image(full_name),
+                              'flag': True,
+                              }
                     query = txn.sqlrepr(Insert(xsetbg_db.sqlmeta.table,
                                                values=values))
                     txn.query(query)
                     count_new += 1
                 else:
+                    if row.is_image is None:
+                        row.is_image = is_image(full_name)
                     row.flag = True
                     count_old += 1
     count_del = xsetbg_db.select('flag IS NULL').count()
@@ -67,6 +81,7 @@ with SQLiteMassInsert() as txn:
     query = txn.sqlrepr(Update(xsetbg_db.sqlmeta.table, {'flag': None}))
     txn.query(query)
 
+NULL.close()
 print "New images:", count_new
 print "Existing images:", count_old
 print "Removed images:", count_del