# Path: rebulk/rules.py
# class Rule(CustomRule):
#     """
#     Definition of a rule to apply
#     """
#     # pylint:disable=abstract-method
#     consequence = None
# 
#     def then(self, matches, when_response, context):
#         assert self.consequence
#         if is_iterable(self.consequence):
#             if not is_iterable(when_response):
#                 when_response = [when_response]
#             iterator = iter(when_response)
#             for cons in self.consequence:  #pylint: disable=not-an-iterable
#                 if inspect.isclass(cons):
#                     cons = cons()
#                 cons.then(matches, next(iterator), context)
#         else:
#             cons = self.consequence
#             if inspect.isclass(cons):
#                 cons = cons()  # pylint:disable=not-callable
#             cons.then(matches, when_response, context)
# 
# class RemoveMatch(Consequence):  # pylint: disable=abstract-method
#     """
#     Remove matches returned by then
#     """
#     def then(self, matches, when_response, context):
#         if is_iterable(when_response):
#             ret = []
#             when_response = list(when_response)
#             for match in when_response:
#                 if match in matches:
#                     matches.remove(match)
#                     ret.append(match)
#             return ret
#         if when_response in matches:
#             matches.remove(when_response)
#             return when_response
# 
# class CustomRule(Condition, Consequence, metaclass=ABCMeta):
#     """
#     Definition of a rule to apply
#     """
#     # pylint: disable=no-self-use, unused-argument, abstract-method
#     priority = 0
#     name = None
#     dependency = None
#     properties = {}
# 
#     def __init__(self, log_level=None):
#         self.defined_at = debug.defined_at()
#         if log_level is None and not hasattr(self, 'log_level'):
#             self.log_level = debug.LOG_LEVEL
# 
#     def enabled(self, context):
#         """
#         Disable rule.
# 
#         :param context:
#         :type context:
#         :return: True if rule is enabled, False if disabled
#         :rtype: bool
#         """
#         return True
# 
#     def __lt__(self, other):
#         return self.priority > other.priority
# 
#     def __repr__(self):
#         defined = ""
#         if self.defined_at:
#             defined = f"@{self.defined_at}"
#         return f"<{self.name if self.name else self.__class__.__name__}{defined}>"
# 
#     def __eq__(self, other):
#         return self.__class__ == other.__class__
# 
#     def __hash__(self):
#         return hash(self.__class__)


from rebulk.rules import Rule, RemoveMatch, CustomRule
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# pylint: disable=no-self-use, pointless-statement, missing-docstring, invalid-name, len-as-condition


class RemoveAllButLastYear(Rule):
    consequence = RemoveMatch
    def when(self, matches, context):
        entries = matches.named('year')
        return entries[:-1]


class PrefixedSuffixedYear(CustomRule):
    def when(self, matches, context):
        toRemove = []
        years = matches.named('year')
        for year in years:
            if not matches.previous(year, lambda p: p.name == 'yearPrefix') and \
                   not matches.next(year, lambda n: n.name == 'yearSuffix'):
                toRemove.append(year)
        return toRemove

    def then(self, matches, when_response, context):
        for to_remove in when_response:
            matches.remove(to_remove)



# Path: rebulk/rules.py
# class Rule(CustomRule):
#     """
#     Definition of a rule to apply
#     """
#     # pylint:disable=abstract-method
#     consequence = None
# 
#     def then(self, matches, when_response, context):
#         assert self.consequence
#         if is_iterable(self.consequence):
#             if not is_iterable(when_response):
#                 when_response = [when_response]
#             iterator = iter(when_response)
#             for cons in self.consequence:  #pylint: disable=not-an-iterable
#                 if inspect.isclass(cons):
#                     cons = cons()
#                 cons.then(matches, next(iterator), context)
#         else:
#             cons = self.consequence
#             if inspect.isclass(cons):
#                 cons = cons()  # pylint:disable=not-callable
#             cons.then(matches, when_response, context)
# 
# class RemoveMatch(Consequence):  # pylint: disable=abstract-method
#     """
#     Remove matches returned by then
#     """
#     def then(self, matches, when_response, context):
#         if is_iterable(when_response):
#             ret = []
#             when_response = list(when_response)
#             for match in when_response:
#                 if match in matches:
#                     matches.remove(match)
#                     ret.append(match)
#             return ret
#         if when_response in matches:
#             matches.remove(when_response)
#             return when_response
# 
# class CustomRule(Condition, Consequence, metaclass=ABCMeta):
#     """
#     Definition of a rule to apply
#     """
#     # pylint: disable=no-self-use, unused-argument, abstract-method
#     priority = 0
#     name = None
#     dependency = None
#     properties = {}
# 
#     def __init__(self, log_level=None):
#         self.defined_at = debug.defined_at()
#         if log_level is None and not hasattr(self, 'log_level'):
#             self.log_level = debug.LOG_LEVEL
# 
#     def enabled(self, context):
#         """
#         Disable rule.
# 
#         :param context:
#         :type context:
#         :return: True if rule is enabled, False if disabled
#         :rtype: bool
#         """
#         return True
# 
#     def __lt__(self, other):
#         return self.priority > other.priority
# 
#     def __repr__(self):
#         defined = ""
#         if self.defined_at:
#             defined = f"@{self.defined_at}"
#         return f"<{self.name if self.name else self.__class__.__name__}{defined}>"
# 
#     def __eq__(self, other):
#         return self.__class__ == other.__class__
# 
#     def __hash__(self):
#         return hash(self.__class__)


# Path: rebulk/test/rebulk_rules_module.py
from rebulk.rules import Rule, RemoveMatch, CustomRule

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# pylint: disable=no-self-use, pointless-statement, missing-docstring, invalid-name, len-as-condition


class RemoveAllButLastYear(Rule):
    consequence = RemoveMatch
    def when(self, matches, context):
        entries = matches.named('year')
        return entries[:-1]


class PrefixedSuffixedYear(CustomRule):
    def when(self, matches, context):
        toRemove = []
        years = matches.named('year')
        for year in years:
            if not matches.previous(year, lambda p: p.name == 'yearPrefix') and \
                   not matches.next(year, lambda n: n.name == 'yearSuffix'):
                toRemove.append(year)
        return toRemove

    def then(self, matches, when_response, context):
        for to_remove in when_response:
            matches.remove(to_remove)



class PrefixedSuffixedYearNoLambda(Rule):