File: rebulk_rules_module.py

package info (click to toggle)
python-rebulk 3.3.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 752 kB
  • sloc: python: 7,497; makefile: 3
file content (38 lines) | stat: -rw-r--r-- 1,313 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# pylint: disable=pointless-statement, missing-docstring, invalid-name, len-as-condition
from rebulk.rules import Rule, RemoveMatch, CustomRule


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):
    consequence = RemoveMatch
    def when(self, matches, context):
        toRemove = []
        years = matches.named('year')
        for year in years:
            if not [m for m in matches.previous(year) if m.name == 'yearPrefix'] and \
                    not [m for m in matches.next(year) if m.name == 'yearSuffix']:
                toRemove.append(year)
        return toRemove