From 05168afa14ede47725384c21b8e88bbd2295b142 Mon Sep 17 00:00:00 2001 From: Schimon Jehudah Date: Thu, 7 Mar 2024 19:06:31 +0000 Subject: [PATCH] Reload settings on runtime. Fix several issues. --- slixfeed/config.py | 20 +++++++++++--------- slixfeed/fetch.py | 9 ++++++--- slixfeed/sqlite.py | 7 ++----- slixfeed/task.py | 2 +- slixfeed/version.py | 4 ++-- slixfeed/xmpp/client.py | 26 ++++++++++++++++---------- slixfeed/xmpp/process.py | 20 ++++++++++---------- 7 files changed, 48 insertions(+), 40 deletions(-) diff --git a/slixfeed/config.py b/slixfeed/config.py index fee878c..e7ac7ae 100644 --- a/slixfeed/config.py +++ b/slixfeed/config.py @@ -69,7 +69,17 @@ class Config: for key in ('operator', 'reconnect_timeout', 'type'): value = get_value('accounts', 'XMPP', key) settings['xmpp'][key] = value - + + + async def set_setting_value(settings, jid_bare, db_file, key, val): + key = key.lower() + key_val = [key, val] + settings[jid_bare][key] = val + if sqlite.is_setting_key(db_file, key): + await sqlite.update_setting_value(db_file, key_val) + else: + await sqlite.set_setting_value(db_file, key_val) + # self.settings = {} # initiate an empty dict and the rest would be: # settings['account'] = {} @@ -149,14 +159,6 @@ class ConfigJabberID: settings[jid_bare][key] = value -async def set_setting_value(db_file, key, val): - key = key.lower() - if sqlite.is_setting_key(db_file, key): - await sqlite.update_setting_value(db_file, [key, val]) - else: - await sqlite.set_setting_value(db_file, [key, val]) - - def get_setting_value(db_file, key): value = sqlite.get_setting_value(db_file, key) if value: diff --git a/slixfeed/fetch.py b/slixfeed/fetch.py index 63f7ddd..b7fdc52 100644 --- a/slixfeed/fetch.py +++ b/slixfeed/fetch.py @@ -161,16 +161,19 @@ async def http(url): except ClientError as e: result = {'error': True, 'message': 'Error:' + str(e), - 'original_url': url} + 'original_url': url, + 'status_code': None} except TimeoutError as e: result = {'error': True, 'message': 'Timeout:' + str(e), - 'original_url': url} + 'original_url': url, + 'status_code': None} except Exception as e: logging.error(e) result = {'error': True, 'message': 'Error:' + str(e), - 'original_url': url} + 'original_url': url, + 'status_code': None} return result diff --git a/slixfeed/sqlite.py b/slixfeed/sqlite.py index c25b735..34bc9d2 100644 --- a/slixfeed/sqlite.py +++ b/slixfeed/sqlite.py @@ -2428,11 +2428,8 @@ async def set_setting_value(db_file, key_value): .format(function_name, db_file, key, val)) if not val: - match key: - case 'interval': - val = 90 - case 'quantum': - val = 3 + logger.error('Missing value for key "{}" ({}).'.format(key, db_file)) + return async with DBLOCK: with create_connection(db_file) as conn: diff --git a/slixfeed/task.py b/slixfeed/task.py index f32de83..398441a 100644 --- a/slixfeed/task.py +++ b/slixfeed/task.py @@ -284,7 +284,7 @@ async def check_updates(self, jid): urls = await sqlite.get_active_feeds_url(db_file) for url in urls: await action.scan(self, jid, db_file, url) - val = self.default.setting['check'] + val = self.settings['default']['check'] await asyncio.sleep(60 * float(val)) # Schedule to call this function again in 90 minutes # loop.call_at( diff --git a/slixfeed/version.py b/slixfeed/version.py index 8721af7..f49fe39 100644 --- a/slixfeed/version.py +++ b/slixfeed/version.py @@ -1,2 +1,2 @@ -__version__ = '0.1.30' -__version_info__ = (0, 1, 30) +__version__ = '0.1.31' +__version_info__ = (0, 1, 31) diff --git a/slixfeed/xmpp/client.py b/slixfeed/xmpp/client.py index 54e5fa8..b58f89f 100644 --- a/slixfeed/xmpp/client.py +++ b/slixfeed/xmpp/client.py @@ -2752,7 +2752,6 @@ class Slixfeed(slixmpp.ClientXMPP): i += 1 value = self.settings[jid_bare]['quantum'] or self.settings['default']['quantum'] value = str(value) - value = str(value) options = form.add_field(var='quantum', ftype='list-single', label='Amount', @@ -2767,7 +2766,6 @@ class Slixfeed(slixmpp.ClientXMPP): i += 1 value = self.settings[jid_bare]['archive'] or self.settings['default']['archive'] value = str(value) - value = str(value) options = form.add_field(var='archive', ftype='list-single', label='Archive', @@ -2795,7 +2793,7 @@ class Slixfeed(slixmpp.ClientXMPP): jid_full = str(session['from']) function_name = sys._getframe().f_code.co_name logger.debug('{}: jid_full: {}' - .format(function_name, jid_full)) + .format(function_name, jid_full)) jid_bare = session['from'].bare form = payload jid_file = jid_bare @@ -2808,14 +2806,23 @@ class Slixfeed(slixmpp.ClientXMPP): key = value val = values[value] - if key == 'interval': + if key in ('enabled', 'media', 'old'): + if val == True: + val = 1 + elif val == False: + val = 0 + + if key in ('archive', 'interval', 'quantum'): val = int(val) + + if key == 'interval': if val < 1: val = 1 val = val * 60 is_enabled = self.settings[jid_bare]['enabled'] or self.settings['default']['enabled'] - if (key == 'enabled' and val == 1 and + if (key == 'enabled' and + val == 1 and str(is_enabled) == 0): logger.info('Slixfeed has been enabled for {}'.format(jid_bare)) status_type = 'available' @@ -2826,7 +2833,8 @@ class Slixfeed(slixmpp.ClientXMPP): key_list = ['check', 'status', 'interval'] await task.start_tasks_xmpp(self, jid_bare, key_list) - if (key == 'enabled' and val == 0 and + if (key == 'enabled' and + val == 0 and str(is_enabled) == 1): logger.info('Slixfeed has been disabled for {}'.format(jid_bare)) key_list = ['interval', 'status'] @@ -2836,10 +2844,8 @@ class Slixfeed(slixmpp.ClientXMPP): XmppPresence.send(self, jid_bare, status_message, status_type=status_type) - # These three ilnes (getting value after setting it) might be removed - await config.set_setting_value(db_file, key, val) - val = sqlite.get_setting_value(db_file, key) - val = val[0] + await Config.set_setting_value(self.settings, jid_bare, db_file, key, val) + val = self.settings[jid_bare][key] # if key == 'enabled': # if str(setting.enabled) == 0: diff --git a/slixfeed/xmpp/process.py b/slixfeed/xmpp/process.py index 7f02383..3cedfa5 100644 --- a/slixfeed/xmpp/process.py +++ b/slixfeed/xmpp/process.py @@ -386,7 +386,7 @@ async def message(self, message): response = 'Value may not be greater than 500.' else: db_file = config.get_pathname_to_database(jid_file) - await config.set_setting_value(db_file, key, val) + await Config.set_setting_value(self.settings, jid_bare, db_file, key, val) response = ('Maximum archived items has ' 'been set to {}.' .format(val)) @@ -702,7 +702,7 @@ async def message(self, message): try: val = int(val) db_file = config.get_pathname_to_database(jid_file) - await config.set_setting_value(db_file, key, val) + await Config.set_setting_value(self.settings, jid_bare, db_file, key, val) # NOTE Perhaps this should be replaced by functions # clean and start await task.refresh_task(self, jid_bare, task.task_send, @@ -736,7 +736,7 @@ async def message(self, message): try: val = int(val) db_file = config.get_pathname_to_database(jid_file) - await config.set_setting_value(db_file, key, val) + await Config.set_setting_value(self.settings, jid_bare, db_file, key, val) if val == 0: # if not val: response = 'Summary length limit is disabled.' else: @@ -780,21 +780,21 @@ async def message(self, message): db_file = config.get_pathname_to_database(jid_file) key = 'media' val = 0 - await config.set_setting_value(db_file, key, val) + await Config.set_setting_value(self.settings, jid_bare, db_file, key, val) response = 'Media is disabled.' XmppMessage.send_reply(self, message, response) case 'media on': db_file = config.get_pathname_to_database(jid_file) key = 'media' val = 1 - await config.set_setting_value(db_file, key, val) + await Config.set_setting_value(self.settings, jid_bare, db_file, key, val) response = 'Media is enabled.' XmppMessage.send_reply(self, message, response) case 'new': db_file = config.get_pathname_to_database(jid_file) key = 'old' val = 0 - await config.set_setting_value(db_file, key, val) + await Config.set_setting_value(self.settings, jid_bare, db_file, key, val) response = 'Only new items of newly added feeds be delivered.' XmppMessage.send_reply(self, message, response) case _ if message_lowercase.startswith('next'): @@ -809,7 +809,7 @@ async def message(self, message): db_file = config.get_pathname_to_database(jid_file) key = 'old' val = 1 - await config.set_setting_value(db_file, key, val) + await Config.set_setting_value(self.settings, jid_bare, db_file, key, val) response = 'All items of newly added feeds be delivered.' XmppMessage.send_reply(self, message, response) case _ if message_lowercase.startswith('quantum'): @@ -822,7 +822,7 @@ async def message(self, message): # 'Every update will contain {} news items.' # ).format(response) db_file = config.get_pathname_to_database(jid_file) - await config.set_setting_value(db_file, key, val) + await Config.set_setting_value(self.settings, jid_bare, db_file, key, val) response = ('Next update will contain {} news items.' .format(val)) except: @@ -1019,7 +1019,7 @@ async def message(self, message): key = 'enabled' val = 1 db_file = config.get_pathname_to_database(jid_file) - await config.set_setting_value(db_file, key, val) + await Config.set_setting_value(self.settings, jid_bare, db_file, key, val) status_type = 'available' status_message = '📫️ Welcome back!' XmppPresence.send(self, jid_bare, status_message, status_type=status_type) @@ -1105,7 +1105,7 @@ async def message(self, message): key = 'enabled' val = 0 db_file = config.get_pathname_to_database(jid_file) - await config.set_setting_value(db_file, key, val) + await Config.set_setting_value(self.settings, jid_bare, db_file, key, val) key_list = ['interval', 'status'] task.clean_tasks_xmpp(self, jid_bare, key_list) status_type = 'xa'