Reload settings on runtime.

Fix several issues.
This commit is contained in:
Schimon Jehudah 2024-03-07 19:06:31 +00:00
parent da48b429d3
commit 05168afa14
7 changed files with 48 additions and 40 deletions

View file

@ -70,6 +70,16 @@ class Config:
value = get_value('accounts', 'XMPP', key) value = get_value('accounts', 'XMPP', key)
settings['xmpp'][key] = value 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 = {} # self.settings = {}
# initiate an empty dict and the rest would be: # initiate an empty dict and the rest would be:
# settings['account'] = {} # settings['account'] = {}
@ -149,14 +159,6 @@ class ConfigJabberID:
settings[jid_bare][key] = value 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): def get_setting_value(db_file, key):
value = sqlite.get_setting_value(db_file, key) value = sqlite.get_setting_value(db_file, key)
if value: if value:

View file

@ -161,16 +161,19 @@ async def http(url):
except ClientError as e: except ClientError as e:
result = {'error': True, result = {'error': True,
'message': 'Error:' + str(e), 'message': 'Error:' + str(e),
'original_url': url} 'original_url': url,
'status_code': None}
except TimeoutError as e: except TimeoutError as e:
result = {'error': True, result = {'error': True,
'message': 'Timeout:' + str(e), 'message': 'Timeout:' + str(e),
'original_url': url} 'original_url': url,
'status_code': None}
except Exception as e: except Exception as e:
logging.error(e) logging.error(e)
result = {'error': True, result = {'error': True,
'message': 'Error:' + str(e), 'message': 'Error:' + str(e),
'original_url': url} 'original_url': url,
'status_code': None}
return result return result

View file

@ -2428,11 +2428,8 @@ async def set_setting_value(db_file, key_value):
.format(function_name, db_file, key, val)) .format(function_name, db_file, key, val))
if not val: if not val:
match key: logger.error('Missing value for key "{}" ({}).'.format(key, db_file))
case 'interval': return
val = 90
case 'quantum':
val = 3
async with DBLOCK: async with DBLOCK:
with create_connection(db_file) as conn: with create_connection(db_file) as conn:

View file

@ -284,7 +284,7 @@ async def check_updates(self, jid):
urls = await sqlite.get_active_feeds_url(db_file) urls = await sqlite.get_active_feeds_url(db_file)
for url in urls: for url in urls:
await action.scan(self, jid, db_file, url) await action.scan(self, jid, db_file, url)
val = self.default.setting['check'] val = self.settings['default']['check']
await asyncio.sleep(60 * float(val)) await asyncio.sleep(60 * float(val))
# Schedule to call this function again in 90 minutes # Schedule to call this function again in 90 minutes
# loop.call_at( # loop.call_at(

View file

@ -1,2 +1,2 @@
__version__ = '0.1.30' __version__ = '0.1.31'
__version_info__ = (0, 1, 30) __version_info__ = (0, 1, 31)

View file

@ -2752,7 +2752,6 @@ class Slixfeed(slixmpp.ClientXMPP):
i += 1 i += 1
value = self.settings[jid_bare]['quantum'] or self.settings['default']['quantum'] value = self.settings[jid_bare]['quantum'] or self.settings['default']['quantum']
value = str(value) value = str(value)
value = str(value)
options = form.add_field(var='quantum', options = form.add_field(var='quantum',
ftype='list-single', ftype='list-single',
label='Amount', label='Amount',
@ -2767,7 +2766,6 @@ class Slixfeed(slixmpp.ClientXMPP):
i += 1 i += 1
value = self.settings[jid_bare]['archive'] or self.settings['default']['archive'] value = self.settings[jid_bare]['archive'] or self.settings['default']['archive']
value = str(value) value = str(value)
value = str(value)
options = form.add_field(var='archive', options = form.add_field(var='archive',
ftype='list-single', ftype='list-single',
label='Archive', label='Archive',
@ -2795,7 +2793,7 @@ class Slixfeed(slixmpp.ClientXMPP):
jid_full = str(session['from']) jid_full = str(session['from'])
function_name = sys._getframe().f_code.co_name function_name = sys._getframe().f_code.co_name
logger.debug('{}: jid_full: {}' logger.debug('{}: jid_full: {}'
.format(function_name, jid_full)) .format(function_name, jid_full))
jid_bare = session['from'].bare jid_bare = session['from'].bare
form = payload form = payload
jid_file = jid_bare jid_file = jid_bare
@ -2808,14 +2806,23 @@ class Slixfeed(slixmpp.ClientXMPP):
key = value key = value
val = values[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) val = int(val)
if key == 'interval':
if val < 1: val = 1 if val < 1: val = 1
val = val * 60 val = val * 60
is_enabled = self.settings[jid_bare]['enabled'] or self.settings['default']['enabled'] 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): str(is_enabled) == 0):
logger.info('Slixfeed has been enabled for {}'.format(jid_bare)) logger.info('Slixfeed has been enabled for {}'.format(jid_bare))
status_type = 'available' status_type = 'available'
@ -2826,7 +2833,8 @@ class Slixfeed(slixmpp.ClientXMPP):
key_list = ['check', 'status', 'interval'] key_list = ['check', 'status', 'interval']
await task.start_tasks_xmpp(self, jid_bare, key_list) 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): str(is_enabled) == 1):
logger.info('Slixfeed has been disabled for {}'.format(jid_bare)) logger.info('Slixfeed has been disabled for {}'.format(jid_bare))
key_list = ['interval', 'status'] key_list = ['interval', 'status']
@ -2836,10 +2844,8 @@ class Slixfeed(slixmpp.ClientXMPP):
XmppPresence.send(self, jid_bare, status_message, XmppPresence.send(self, jid_bare, status_message,
status_type=status_type) status_type=status_type)
# These three ilnes (getting value after setting it) might be removed await Config.set_setting_value(self.settings, jid_bare, db_file, key, val)
await config.set_setting_value(db_file, key, val) val = self.settings[jid_bare][key]
val = sqlite.get_setting_value(db_file, key)
val = val[0]
# if key == 'enabled': # if key == 'enabled':
# if str(setting.enabled) == 0: # if str(setting.enabled) == 0:

View file

@ -386,7 +386,7 @@ async def message(self, message):
response = 'Value may not be greater than 500.' response = 'Value may not be greater than 500.'
else: else:
db_file = config.get_pathname_to_database(jid_file) 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 ' response = ('Maximum archived items has '
'been set to {}.' 'been set to {}.'
.format(val)) .format(val))
@ -702,7 +702,7 @@ async def message(self, message):
try: try:
val = int(val) val = int(val)
db_file = config.get_pathname_to_database(jid_file) 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 # NOTE Perhaps this should be replaced by functions
# clean and start # clean and start
await task.refresh_task(self, jid_bare, task.task_send, await task.refresh_task(self, jid_bare, task.task_send,
@ -736,7 +736,7 @@ async def message(self, message):
try: try:
val = int(val) val = int(val)
db_file = config.get_pathname_to_database(jid_file) 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: if val == 0: # if not val:
response = 'Summary length limit is disabled.' response = 'Summary length limit is disabled.'
else: else:
@ -780,21 +780,21 @@ async def message(self, message):
db_file = config.get_pathname_to_database(jid_file) db_file = config.get_pathname_to_database(jid_file)
key = 'media' key = 'media'
val = 0 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.' response = 'Media is disabled.'
XmppMessage.send_reply(self, message, response) XmppMessage.send_reply(self, message, response)
case 'media on': case 'media on':
db_file = config.get_pathname_to_database(jid_file) db_file = config.get_pathname_to_database(jid_file)
key = 'media' key = 'media'
val = 1 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.' response = 'Media is enabled.'
XmppMessage.send_reply(self, message, response) XmppMessage.send_reply(self, message, response)
case 'new': case 'new':
db_file = config.get_pathname_to_database(jid_file) db_file = config.get_pathname_to_database(jid_file)
key = 'old' key = 'old'
val = 0 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.' response = 'Only new items of newly added feeds be delivered.'
XmppMessage.send_reply(self, message, response) XmppMessage.send_reply(self, message, response)
case _ if message_lowercase.startswith('next'): case _ if message_lowercase.startswith('next'):
@ -809,7 +809,7 @@ async def message(self, message):
db_file = config.get_pathname_to_database(jid_file) db_file = config.get_pathname_to_database(jid_file)
key = 'old' key = 'old'
val = 1 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.' response = 'All items of newly added feeds be delivered.'
XmppMessage.send_reply(self, message, response) XmppMessage.send_reply(self, message, response)
case _ if message_lowercase.startswith('quantum'): case _ if message_lowercase.startswith('quantum'):
@ -822,7 +822,7 @@ async def message(self, message):
# 'Every update will contain {} news items.' # 'Every update will contain {} news items.'
# ).format(response) # ).format(response)
db_file = config.get_pathname_to_database(jid_file) 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.' response = ('Next update will contain {} news items.'
.format(val)) .format(val))
except: except:
@ -1019,7 +1019,7 @@ async def message(self, message):
key = 'enabled' key = 'enabled'
val = 1 val = 1
db_file = config.get_pathname_to_database(jid_file) 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_type = 'available'
status_message = '📫️ Welcome back!' status_message = '📫️ Welcome back!'
XmppPresence.send(self, jid_bare, status_message, status_type=status_type) XmppPresence.send(self, jid_bare, status_message, status_type=status_type)
@ -1105,7 +1105,7 @@ async def message(self, message):
key = 'enabled' key = 'enabled'
val = 0 val = 0
db_file = config.get_pathname_to_database(jid_file) 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'] key_list = ['interval', 'status']
task.clean_tasks_xmpp(self, jid_bare, key_list) task.clean_tasks_xmpp(self, jid_bare, key_list)
status_type = 'xa' status_type = 'xa'