|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
3 | 3 | import re |
| 4 | +from copy import deepcopy |
4 | 5 | from functools import partial |
5 | 6 | from typing import ( |
6 | 7 | TYPE_CHECKING, |
|
55 | 56 | YearFieldDescriptor, |
56 | 57 | YearmonthFieldDescriptor, |
57 | 58 | ) |
| 59 | +from ..metadata import Metadata |
58 | 60 |
|
59 | 61 | if TYPE_CHECKING: |
60 | 62 | from . import types |
@@ -150,7 +152,7 @@ def read_cell(self, cell: Any): |
150 | 152 | return cell_reader(cell) |
151 | 153 |
|
152 | 154 | def create_cell_reader(self) -> types.ICellReader: |
153 | | - value_reader = self.create_value_reader() |
| 155 | + value_reader = self.value_read_writer.create_value_reader() |
154 | 156 |
|
155 | 157 | # Create missing values |
156 | 158 | missing_values = self.missing_values |
@@ -272,7 +274,11 @@ def value_writer(cell: Any): |
272 | 274 | } |
273 | 275 |
|
274 | 276 | @classmethod |
275 | | - def from_descriptor(cls, source: Union[Dict[str, Any], FieldDescriptor]): |
| 277 | + def from_descriptor(cls, source: Union[Dict[str, Any], FieldDescriptor, str]): |
| 278 | + if isinstance(source, str): |
| 279 | + # Read descriptor from file |
| 280 | + source = Metadata.metadata_retrieve(source) |
| 281 | + |
276 | 282 | if isinstance(source, dict): |
277 | 283 | descriptor: FieldDescriptor = pydantic.TypeAdapter[FieldDescriptor]( |
278 | 284 | FieldDescriptor |
@@ -328,8 +334,37 @@ def from_descriptor(cls, source: Union[Dict[str, Any], FieldDescriptor]): |
328 | 334 | descriptor=descriptor, |
329 | 335 | ) |
330 | 336 |
|
331 | | - def to_descriptor(self): |
| 337 | + def to_descriptor(self) -> FieldDescriptor: |
332 | 338 | if self.descriptor: |
333 | 339 | return self.descriptor |
334 | 340 | else: |
335 | | - return AnyFieldDescriptor(name=self.name) |
| 341 | + raise Exception() |
| 342 | + |
| 343 | + ## Temporary metadata methods compatibility to be able to cooperate with, |
| 344 | + ## e.g. the Schema class which is still a Metadata child. |
| 345 | + ######################################################################### |
| 346 | + |
| 347 | + def to_copy(self): |
| 348 | + return deepcopy(self) |
| 349 | + |
| 350 | + @classmethod |
| 351 | + def metadata_import(cls, descriptor: Any, *args, **kwargs): |
| 352 | + return cls.from_descriptor(descriptor) |
| 353 | + |
| 354 | + @classmethod |
| 355 | + def metadata_select_class(cls, _: Any): |
| 356 | + return cls |
| 357 | + |
| 358 | + @classmethod |
| 359 | + def metadata_transform(cls, _: Any): |
| 360 | + return None |
| 361 | + |
| 362 | + @classmethod |
| 363 | + def metadata_validate(cls, *args, **kwargs): |
| 364 | + # TODO this one should actually be implemented for the Descriptor class |
| 365 | + return () |
| 366 | + |
| 367 | + def to_descriptor_source(self, *args, **kwargs) -> FieldDescriptor: |
| 368 | + # TODO should we support returning a source ? what is the use case for |
| 369 | + # this ? |
| 370 | + return self.to_descriptor() |
0 commit comments