Skip to content

Commit

Permalink
impr: detect invalid config changes (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
nalgeon committed Apr 10, 2023
1 parent b7d0784 commit 6b3b36e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
18 changes: 9 additions & 9 deletions bot/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,27 +251,27 @@ async def config_handle(update: Update, context: CallbackContext):
await message.reply_text(text, parse_mode=ParseMode.HTML)
return

property = parts[1]
value = config.get_value(property)
value = value if value is not None else "(empty)"

if len(parts) == 2:
# view config property (`/config {property}`)
property = parts[1]
value = config.get_value(property) or "(empty)"
await message.reply_text(f"`{value}`", parse_mode=ParseMode.MARKDOWN)
return

# change config property (`/config {property} {value}`)
property = parts[1]
old_value = config.get_value(property) or "(empty)"
value = " ".join(parts[2:])
has_changed, is_immediate = config.set_value(property, value)
# change config property (`/config {property} {new_value}`)
new_value = " ".join(parts[2:])
has_changed, is_immediate = config.set_value(property, new_value)

if not has_changed:
text = f"✗ The `{property}` property already equals to `{value}`"
text = f"✗ The `{property}` property already equals to `{new_value}`"
await message.reply_text(text, parse_mode=ParseMode.MARKDOWN)
return

config.save()
Filters.reload()
text = f"✓ Changed the `{property}` property: `{old_value}` → `{value}`"
text = f"✓ Changed the `{property}` property: `{value}` → `{new_value}`"
if not is_immediate:
text += "\n❗️Restart the bot for changes to take effect."
await message.reply_text(text, parse_mode=ParseMode.MARKDOWN)
Expand Down
5 changes: 4 additions & 1 deletion bot/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,11 @@ def set_value(self, property: str, value: str) -> tuple[bool, bool]:
- `has_changed` = True if the value has actually changed, False otherwise.
- `is_immediate` = True if the change takes effect immediately, False otherwise.
"""
try:
val = yaml.safe_load(value)
except Exception:
raise ValueError(f"Invalid value: {value}")

val = yaml.safe_load(value)
old_val = self.get_value(property)
if val == old_val:
return False, False
Expand Down
4 changes: 4 additions & 0 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,10 @@ def test_dict_value_does_not_exist(self):
value = self.config.get_value("shortcuts.bugfix")
self.assertEqual(value, "Fix bugs in the code")

def test_invalid_value(self):
with self.assertRaises(ValueError):
self.config.set_value("imagine", '"on')

def test_has_changed(self):
has_changed, _ = self.config.set_value("imagine", "on")
self.assertTrue(has_changed)
Expand Down

0 comments on commit 6b3b36e

Please sign in to comment.