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)
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:

View file

@ -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

View file

@ -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:

View file

@ -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(

View file

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

View file

@ -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:

View file

@ -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'