From 2479b69d6b6c7e61d5bb61a2a59e595caff7fb1f Mon Sep 17 00:00:00 2001 From: Oleg Broytman Date: Mon, 12 Aug 2024 19:02:43 +0300 Subject: [PATCH] Feat: Enable/disable images --- TODO | 4 --- enable.py | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++++ rescan_fs.py | 7 +++-- xsetbg.py | 8 +++--- xsetbg_db.py | 2 ++ 5 files changed, 88 insertions(+), 10 deletions(-) create mode 100755 enable.py diff --git a/TODO b/TODO index 7dacb2c..71bd2bf 100644 --- a/TODO +++ b/TODO @@ -2,7 +2,3 @@ OSD: Image title on screen. Should be in a real window to stay permanent. Min (and max?) image dimensions? - - -Rating (likes and dislikes) to show more interestin pictures -and skip less interesing? diff --git a/enable.py b/enable.py new file mode 100755 index 0000000..a1f2d00 --- /dev/null +++ b/enable.py @@ -0,0 +1,77 @@ +#! /usr/bin/env python3 +"""Enable/disable images + +This file is a part of XSetBg. + +""" + +import sys +from getopt import getopt, GetoptError + +from xsetbg_db import xsetbg_db + +if not xsetbg_db: + sys.exit("Error: no database found") + + +def usage(code=0): + sys.stderr.write("Usage: %s [-e|--enable] [-d|--disable] [-c|--clear]" + " [-i|--id=] [filename]\n" % sys.argv[0]) + sys.exit(code) + + +try: + options, arguments = getopt(sys.argv[1:], "edci:", + ["enable", "disable", "clear", + "id="]) +except GetoptError: + usage(1) + +enable = disable = clear = _id = filename = None + +for option, value in options: + if option in ("-e", "--enable"): + enable = True + if option in ("-d", "--disable"): + disable = True + if option in ("-c", "--clear"): + clear = True + if option in ("-i", "--id"): + _id = value + +if arguments: + if len(arguments) == 1: + try: + filename = arguments[0] + except ValueError: + usage(1) + else: + usage(1) + +if int(bool(enable)) + int(bool(disable)) + int(bool(clear)) != 1: + usage(1) + +if int(bool(_id)) + int(bool(filename)) != 1: + usage(1) + +if _id: + rows = list(xsetbg_db.selectBy(id=_id)) + if not rows: + sys.exit("No rows by id %s" % _id) +elif filename: + rows = list(xsetbg_db.selectBy(full_name=filename)) + if not rows: + sys.exit("No rows by filename %s" % filename) +else: + usage(1) + +row = rows[0] + +if enable: + row.show = True +elif disable: + row.show = False +elif clear: + row.show = None +else: + usage(1) diff --git a/rescan_fs.py b/rescan_fs.py index 24302fe..b8284a9 100755 --- a/rescan_fs.py +++ b/rescan_fs.py @@ -67,9 +67,11 @@ with SQLiteMassInsert() as txn: try: row = xsetbg_db.byFull_name(full_name) except SQLObjectNotFound: + image_and_show = is_image(full_name) values = {'full_name': full_name, - 'is_image': is_image(full_name), + 'is_image': image_and_show, 'flag': True, + 'show': image_and_show, } query = txn.sqlrepr(Insert(xsetbg_db.sqlmeta.table, values=values)) @@ -77,7 +79,8 @@ with SQLiteMassInsert() as txn: count_new += 1 else: if row.is_image is None: - row.is_image = is_image(full_name) + row.is_image = image_and_show = is_image(full_name) + row.show = image_and_show row.flag = True count_old += 1 count_del = xsetbg_db.select('flag IS NULL').count() diff --git a/xsetbg.py b/xsetbg.py index 825b20e..db2f2d0 100644 --- a/xsetbg.py +++ b/xsetbg.py @@ -73,7 +73,7 @@ else: if xsetbg_db.select( - '(is_image = 1) AND (last_shown IS NULL OR last_shown < %d)' % + '(is_image = 1) AND show AND (last_shown IS NULL OR last_shown < %d)' % (time() - min_delay)).count() == 0: error("No unshown images found. Run rescan_fs.py " "or decrease min_delay. Abort.") @@ -94,7 +94,7 @@ def change(force=False): try: timestamp = xsetbg_db.select( - '(is_image = 1) AND (last_shown IS NOT NULL)', + '(is_image = 1) AND show AND (last_shown IS NOT NULL)', orderBy='-last_shown')[0].last_shown current_time = time() @@ -105,13 +105,13 @@ def change(force=False): # Select a random image that has never been shown not_shown_select = xsetbg_db.select( - '(is_image = 1) AND (last_shown IS NULL)') + '(is_image = 1) AND show AND (last_shown IS NULL)') not_shown_count = not_shown_select.count() if not_shown_count: row = not_shown_select[random.randint(0, not_shown_count - 1)] else: old_shown_select = xsetbg_db.select( - '(is_image = 1) AND ' + '(is_image = 1) AND show AND ' '(last_shown IS NOT NULL AND last_shown < %d)' % current_time - min_delay) old_shown_count = old_shown_select.count() diff --git a/xsetbg_db.py b/xsetbg_db.py index ba5ab6c..638ce49 100755 --- a/xsetbg_db.py +++ b/xsetbg_db.py @@ -19,10 +19,12 @@ class XSetBg(SQLObject): last_shown = IntCol(default=None) # timestamp flag = BoolCol(default=None) is_image = BoolCol(default=None) + show = BoolCol(default=True) last_shown_idx = DatabaseIndex('last_shown') flag_idx = DatabaseIndex('flag') is_image_idx = DatabaseIndex('is_image') + show_idx = DatabaseIndex('show') def recreate_db(): -- 2.39.5