From 43361961bbbc10e73a9b3d5ddce53a0f0ed7ada8 Mon Sep 17 00:00:00 2001 From: jesopo Date: Sat, 22 Dec 2018 09:33:23 +0000 Subject: [PATCH] Move out logic to case-insensitively find a badge (badges.py) --- modules/badges.py | 32 ++++++++++++-------------------- 1 file changed, 12 insertions(+), 20 deletions(-) diff --git a/modules/badges.py b/modules/badges.py index 49c435a2..bd1d2454 100644 --- a/modules/badges.py +++ b/modules/badges.py @@ -22,6 +22,12 @@ class Module(ModuleManager.BaseModule): def _days_since(self, now: datetime.date, dt: datetime.datetime): return (now-dt.date()).days + def _find_badge(self, badges, badge): + badge_lower = badge.lower() + for badge_name in badges.keys(): + if badge_name.lower() == badge_lower: + return badge_name + def _get_badges(self, user): return user.get_setting("badges", {}) def _set_badges(self, user, badges): @@ -39,11 +45,7 @@ class Module(ModuleManager.BaseModule): now = self._round_up_day(self._now()) - found_badge = None - for badge_name in badges.keys(): - if badge_name.lower() == badge_lower: - found_badge = badge_name - break + found_badge = self._find_badge(badges, badge) if found_badge: dt = self._parse_datetime(badges[found_badge]) @@ -86,6 +88,7 @@ class Module(ModuleManager.BaseModule): for badge_name in badges.keys(): if badge_name.lower() == badge_lower: raise utils.EventError("You already have a '%s' badge" % badge) + badges[badge] = self._format_datetime(self._now()) self._set_badges(event["user"], badges) event["stdout"].write("Added '%s' badge" % badge) @@ -100,11 +103,8 @@ class Module(ModuleManager.BaseModule): badge_lower = badge.lower() badges = self._get_badges(event["user"]) - found_badge = None - for badge_name in badges.keys(): - if badge_name.lower() == badge_lower: - found_badge = badge_name - break + found_badge = self._find_badge(badges, badge) + if found_badge: del badges[found_badge] self._set_badges(event["user"], badges) @@ -122,11 +122,7 @@ class Module(ModuleManager.BaseModule): badge_lower = badge.lower() badges = self._get_badges(event["user"]) - found_badge = None - for badge_name in badges.keys(): - if badge_name.lower() == badge_lower: - found_badge = badge_name - break + found_badge = self._find_badge(badges, badge) if found_badge: badges[found_badge] = self._format_datetime(self._now()) @@ -145,11 +141,7 @@ class Module(ModuleManager.BaseModule): badge_lower = badge.lower() badges = self._get_badges(event["user"]) - found_badge = None - for badge_name in badges.keys(): - if badge_name.lower() == badge_lower: - found_badge = badge_name - break + found_badge = self._find_badge(badges, badge) if not found_badge: raise utils.EventError("You have no '%s' badge" % badge)