This repository has been archived by the owner on Nov 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathsplitters.py
124 lines (95 loc) · 3.63 KB
/
splitters.py
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
"""
Used for converting a single .rsi file into mutiple smaller ones
"""
from rsi import Rsi
from typing import Dict, List, Optional
from abc import ABC
from pathlib import Path
import re
class RsiSplitter(ABC):
"""
This encapsulates all of the data needed to split a Rsi into several smaller Rsi somewhat cleanly
"""
def __init__(self, rsi: Rsi) -> None:
self.rsi = rsi
# Store the name to use for each rsi group
self.names: Dict[Rsi, str] = {}
def _split(self) -> List[Rsi]:
raise NotImplementedError
def split_to(self, path: Path, indents: Optional[int] = None) -> None:
for rsi in self._split():
rsi.license = self.rsi.license
rsi.copyright = self.rsi.copyright
rsi_path = path.joinpath(self.names[rsi]).with_suffix(".rsi")
rsi.write(rsi_path, indent=indents)
self.names.clear()
class SimpleSplitter(RsiSplitter):
"""
Split each Rsi state into its own Rsi
"""
def _split(self) -> List[Rsi]:
result = []
for name, state in self.rsi.states.items():
state_rsi = Rsi(self.rsi.size)
state_rsi.set_state(state, name)
result.append(state_rsi)
self.names[state_rsi] = state.name
return result
class HyphenSplitter(RsiSplitter):
"""
Split each rsi based on hyphens where Rsi states with the same prefix are grouped together
e.g. ak-20, ak-40, and ak-60 would all be grouped into ak.
"""
def _split(self) -> List[Rsi]:
groups: Dict[str, Rsi] = {}
for name, state in self.rsi.states.items():
prefix = name.split("-")[0]
suffix = name.split("-")[-1]
state_rsi = groups.setdefault(prefix, Rsi(self.rsi.size))
if prefix != suffix:
state.name = suffix
state_rsi.set_state(state, suffix)
self.names[state_rsi] = prefix
else:
state_rsi.set_state(state, name)
self.names[state_rsi] = name
return list(groups.values())
class UnderscoreSplitter(RsiSplitter):
"""
Like the hyphensplitter but for underscores.
"""
def _split(self) -> List[Rsi]:
groups: Dict[str, Rsi] = {}
for name, state in self.rsi.states.items():
prefix = name.split("_")[0]
suffix = name.split("_")[-1]
state_rsi = groups.setdefault(prefix, Rsi(self.rsi.size))
if prefix != suffix:
state.name = suffix
state_rsi.set_state(state, suffix)
self.names[state_rsi] = prefix
else:
state_rsi.set_state(state, name)
self.names[state_rsi] = name
return list(groups.values())
class NumberSplitter(RsiSplitter):
"""
Splits states based on the suffix number
e.g. infected, infected0, infected1 are all in the same rsi
"""
def _split(self) -> List[Rsi]:
groups: Dict[str, Rsi] = {}
pattern = re.compile("([^0-9]*)([0-9]*)")
for name, state in self.rsi.states.items():
match = pattern.match(name)
prefix = match.group(1) # type: ignore
suffix = match.group(2) if len(match.groups()) > 1 else "" # type: ignore
state_rsi = groups.setdefault(prefix, Rsi(self.rsi.size))
if prefix != suffix:
state.name = suffix
state_rsi.set_state(state, suffix)
self.names[state_rsi] = prefix
else:
state_rsi.set_state(state, name)
self.names[state_rsi] = name
return list(groups.values())