Skip to content

Commit

Permalink
Merge pull request #2220 from Exirel/plugins-rules-lgtm-conflicts
Browse files Browse the repository at this point in the history
plugins.rules: fix conflicting attributes in base classes
  • Loading branch information
dgw authored Dec 27, 2021
2 parents c1f32aa + 77ff78a commit 6305e40
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 16 deletions.
2 changes: 1 addition & 1 deletion docs/source/plugin/internals.rst
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,6 @@ sopel.plugins.rules
:members:
:undoc-members:

.. autoclass:: NamedRuleMixin
.. autoclass:: AbstractNamedRule
:members:
:undoc-members:
36 changes: 21 additions & 15 deletions sopel/plugins/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ class AbstractRule(abc.ABC):
"""
@classmethod
@abc.abstractclassmethod
@abc.abstractmethod
def from_callable(cls: Type[TypedRule], settings, handler) -> TypedRule:
"""Instantiate a rule object from ``settings`` and ``handler``.
Expand Down Expand Up @@ -1063,15 +1063,20 @@ def execute(self, bot, trigger):
return exit_code


class NamedRuleMixin:
"""Mixin for named rules.
class AbstractNamedRule(Rule):
"""Abstract base class for named rules.
A named rule is invoked by using a specific word, and is usually known
as a "command". For example, the command "hello" is triggered by using
the word "hello" with some sort of prefix or context.
A named rule can be invoked by using one of its aliases, also.
"""
def __init__(self, name, aliases=None, **kwargs):
super().__init__([], **kwargs)
self._name = name
self._aliases = tuple(aliases) if aliases is not None else tuple()

@property
def name(self):
return self._name
Expand Down Expand Up @@ -1133,8 +1138,15 @@ def escape_name(self, name):

return name

@abc.abstractmethod
def get_rule_regex(self):
"""Make the rule regex for this named rule.
:return: a compiled regex for this named rule and its aliases
"""


class Command(NamedRuleMixin, Rule):
class Command(AbstractNamedRule):
"""Command rule definition.
A command rule (or simply "a command") is a named rule, i.e. it has a known
Expand Down Expand Up @@ -1196,11 +1208,9 @@ def __init__(self,
help_prefix=COMMAND_DEFAULT_HELP_PREFIX,
aliases=None,
**kwargs):
super().__init__([], **kwargs)
self._name = name
super().__init__(name, aliases=aliases, **kwargs)
self._prefix = prefix
self._help_prefix = help_prefix
self._aliases = tuple(aliases) if aliases is not None else tuple()
self._regexes = (self.get_rule_regex(),)

def __str__(self):
Expand Down Expand Up @@ -1259,7 +1269,7 @@ def get_rule_regex(self):
return re.compile(pattern, re.IGNORECASE | re.VERBOSE)


class NickCommand(NamedRuleMixin, Rule):
class NickCommand(AbstractNamedRule):
"""Nickname Command rule definition.
A nickname command rule is a named rule with a twist: instead of a prefix,
Expand Down Expand Up @@ -1319,13 +1329,11 @@ def from_callable(cls, settings, handler):
return cls(**kwargs)

def __init__(self, nick, name, nick_aliases=None, aliases=None, **kwargs):
super().__init__([], **kwargs)
super().__init__(name, aliases=aliases, **kwargs)
self._nick = nick
self._name = name
self._nick_aliases = (tuple(nick_aliases)
if nick_aliases is not None
else tuple())
self._aliases = tuple(aliases) if aliases is not None else tuple()
self._regexes = (self.get_rule_regex(),)

def __str__(self):
Expand Down Expand Up @@ -1381,7 +1389,7 @@ def get_rule_regex(self):
self._nick_aliases)


class ActionCommand(NamedRuleMixin, Rule):
class ActionCommand(AbstractNamedRule):
"""Action Command rule definition.
An action command rule is a named rule that can be triggered only when the
Expand Down Expand Up @@ -1434,9 +1442,7 @@ def from_callable(cls, settings, handler):
return cls(**kwargs)

def __init__(self, name, aliases=None, **kwargs):
super().__init__([], **kwargs)
self._name = name
self._aliases = tuple(aliases) if aliases is not None else tuple()
super().__init__(name, aliases=aliases, **kwargs)
self._regexes = (self.get_rule_regex(),)

def __str__(self):
Expand Down

0 comments on commit 6305e40

Please sign in to comment.