| 214 | |
| 215 | @dc.dataclass(slots=True, repr=False) |
| 216 | class Destination: |
| 217 | name: str |
| 218 | type: str |
| 219 | clinic: Clinic |
| 220 | buffers: BufferSeries = dc.field(init=False, default_factory=BufferSeries) |
| 221 | filename: str = dc.field(init=False) # set in __post_init__ |
| 222 | |
| 223 | args: dc.InitVar[tuple[str, ...]] = () |
| 224 | |
| 225 | def __post_init__(self, args: tuple[str, ...]) -> None: |
| 226 | valid_types = ('buffer', 'file', 'suppress') |
| 227 | if self.type not in valid_types: |
| 228 | fail( |
| 229 | f"Invalid destination type {self.type!r} for {self.name}, " |
| 230 | f"must be {', '.join(valid_types)}" |
| 231 | ) |
| 232 | extra_arguments = 1 if self.type == "file" else 0 |
| 233 | if len(args) < extra_arguments: |
| 234 | fail(f"Not enough arguments for destination " |
| 235 | f"{self.name!r} new {self.type!r}") |
| 236 | if len(args) > extra_arguments: |
| 237 | fail(f"Too many arguments for destination {self.name!r} new {self.type!r}") |
| 238 | if self.type =='file': |
| 239 | d = {} |
| 240 | filename = self.clinic.filename |
| 241 | d['path'] = filename |
| 242 | dirname, basename = os.path.split(filename) |
| 243 | if not dirname: |
| 244 | dirname = '.' |
| 245 | d['dirname'] = dirname |
| 246 | d['basename'] = basename |
| 247 | d['basename_root'], d['basename_extension'] = os.path.splitext(filename) |
| 248 | self.filename = args[0].format_map(d) |
| 249 | |
| 250 | def __repr__(self) -> str: |
| 251 | if self.type == 'file': |
| 252 | type_repr = f"type='file' file={self.filename!r}" |
| 253 | else: |
| 254 | type_repr = f"type={self.type!r}" |
| 255 | return f"<clinic.Destination {self.name!r} {type_repr}>" |
| 256 | |
| 257 | def clear(self) -> None: |
| 258 | if self.type != 'buffer': |
| 259 | fail(f"Can't clear destination {self.name!r}: it's not of type 'buffer'") |
| 260 | self.buffers.clear() |
| 261 | |
| 262 | def dump(self) -> str: |
| 263 | return self.buffers.dump() |
| 264 | |
| 265 | |
| 266 | DestinationDict = dict[str, Destination] |
searching dependent graphs…