avwx.parsing.sanitization.cleaners.base

Cleaning base classes.

 1"""Cleaning base classes."""
 2
 3from __future__ import annotations
 4
 5
 6class Cleaner:
 7    """Base Cleaner type."""
 8
 9    # Set to True if no more cleaners should check this item
10    should_break: bool = False
11
12
13class SingleItem(Cleaner):
14    """Cleaner looks at a single item."""
15
16    def can_handle(self, item: str) -> bool:
17        """Return True if the element can and needs to be cleaned."""
18        raise NotImplementedError
19
20
21class DoubleItem(Cleaner):
22    """Cleaner that looks at two neighboring items."""
23
24    def can_handle(self, first: str, second: str) -> bool:
25        """Return True if neighboring pairs need to be cleaned."""
26        raise NotImplementedError
27
28
29class RemoveItem(SingleItem):
30    """Sanitization should remove item if handled."""
31
32    should_break = True
33
34
35class CleanItem(SingleItem):
36    """Sanitization should clean/replace item if handled."""
37
38    def clean(self, item: str) -> str:
39        """Clean the raw string."""
40        raise NotImplementedError
41
42
43class CleanPair(DoubleItem):
44    """Sanitization should clean both paired items."""
45
46    def clean(self, first: str, second: str) -> tuple[str, str]:
47        """Clean both raw strings."""
48        raise NotImplementedError
49
50
51class SplitItem(Cleaner):
52    """Sanitization should split the item in two at an index if handled"""
53
54    def split_at(self, item: str) -> int | None:
55        """Return the string index where the item should be split."""
56        raise NotImplementedError
57
58
59class CombineItems(Cleaner):
60    """Sanitization should combine two different items if handled."""
61
62    def can_handle(self, first: str, second: str) -> bool:
63        """Return True if both elements can and need to be combined."""
64        raise NotImplementedError
65
66
67CleanerListType = list[type[CleanItem] | type[CleanPair] | type[RemoveItem] | type[SplitItem] | type[CombineItems]]
class Cleaner:
 7class Cleaner:
 8    """Base Cleaner type."""
 9
10    # Set to True if no more cleaners should check this item
11    should_break: bool = False

Base Cleaner type.

should_break: bool = False
class SingleItem(Cleaner):
14class SingleItem(Cleaner):
15    """Cleaner looks at a single item."""
16
17    def can_handle(self, item: str) -> bool:
18        """Return True if the element can and needs to be cleaned."""
19        raise NotImplementedError

Cleaner looks at a single item.

def can_handle(self, item: str) -> bool:
17    def can_handle(self, item: str) -> bool:
18        """Return True if the element can and needs to be cleaned."""
19        raise NotImplementedError

Return True if the element can and needs to be cleaned.

Inherited Members
Cleaner
should_break
class DoubleItem(Cleaner):
22class DoubleItem(Cleaner):
23    """Cleaner that looks at two neighboring items."""
24
25    def can_handle(self, first: str, second: str) -> bool:
26        """Return True if neighboring pairs need to be cleaned."""
27        raise NotImplementedError

Cleaner that looks at two neighboring items.

def can_handle(self, first: str, second: str) -> bool:
25    def can_handle(self, first: str, second: str) -> bool:
26        """Return True if neighboring pairs need to be cleaned."""
27        raise NotImplementedError

Return True if neighboring pairs need to be cleaned.

Inherited Members
Cleaner
should_break
class RemoveItem(SingleItem):
30class RemoveItem(SingleItem):
31    """Sanitization should remove item if handled."""
32
33    should_break = True

Sanitization should remove item if handled.

should_break = True
Inherited Members
SingleItem
can_handle
class CleanItem(SingleItem):
36class CleanItem(SingleItem):
37    """Sanitization should clean/replace item if handled."""
38
39    def clean(self, item: str) -> str:
40        """Clean the raw string."""
41        raise NotImplementedError

Sanitization should clean/replace item if handled.

def clean(self, item: str) -> str:
39    def clean(self, item: str) -> str:
40        """Clean the raw string."""
41        raise NotImplementedError

Clean the raw string.

class CleanPair(DoubleItem):
44class CleanPair(DoubleItem):
45    """Sanitization should clean both paired items."""
46
47    def clean(self, first: str, second: str) -> tuple[str, str]:
48        """Clean both raw strings."""
49        raise NotImplementedError

Sanitization should clean both paired items.

def clean(self, first: str, second: str) -> tuple[str, str]:
47    def clean(self, first: str, second: str) -> tuple[str, str]:
48        """Clean both raw strings."""
49        raise NotImplementedError

Clean both raw strings.

class SplitItem(Cleaner):
52class SplitItem(Cleaner):
53    """Sanitization should split the item in two at an index if handled"""
54
55    def split_at(self, item: str) -> int | None:
56        """Return the string index where the item should be split."""
57        raise NotImplementedError

Sanitization should split the item in two at an index if handled

def split_at(self, item: str) -> int | None:
55    def split_at(self, item: str) -> int | None:
56        """Return the string index where the item should be split."""
57        raise NotImplementedError

Return the string index where the item should be split.

Inherited Members
Cleaner
should_break
class CombineItems(Cleaner):
60class CombineItems(Cleaner):
61    """Sanitization should combine two different items if handled."""
62
63    def can_handle(self, first: str, second: str) -> bool:
64        """Return True if both elements can and need to be combined."""
65        raise NotImplementedError

Sanitization should combine two different items if handled.

def can_handle(self, first: str, second: str) -> bool:
63    def can_handle(self, first: str, second: str) -> bool:
64        """Return True if both elements can and need to be combined."""
65        raise NotImplementedError

Return True if both elements can and need to be combined.

Inherited Members
Cleaner
should_break
CleanerListType = list[type[CleanItem] | type[CleanPair] | type[RemoveItem] | type[SplitItem] | type[CombineItems]]