Skip to content

Commit 1f74ac9

Browse files
WIP attempt
1 parent de38ae2 commit 1f74ac9

File tree

6 files changed

+51
-8
lines changed

6 files changed

+51
-8
lines changed

frictionless/detector/detector.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ def detect_schema(
328328

329329
# Handle name/empty
330330
for index, name in enumerate(names):
331-
names[index] = name or f"field{index+1}"
331+
names[index] = name or f"field{index + 1}"
332332

333333
# Deduplicate names
334334
if len(names) != len(set(names)):
@@ -386,6 +386,7 @@ def detect_schema(
386386
if not is_field_missing_value:
387387
_, notes = runner["field"].read_cell(source)
388388
runner["score"] += 1 if not notes else -1
389+
389390
if max_score[index] > 0 and runner["score"] >= (
390391
max_score[index] * self.field_confidence
391392
):

frictionless/fields/geojson.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def format(self):
1818

1919
# TODO: use different value_readers based on format (see string)
2020
def create_value_reader(self):
21-
validator_for = platform.jsonschema.field_descriptor_validators.validator_for # type: ignore
21+
validator_for = platform.jsonschema_validators.validator_for # type: ignore
2222
validators = { # type: ignore
2323
"default": validator_for(settings.GEOJSON_PROFILE)(settings.GEOJSON_PROFILE),
2424
"topojson": validator_for(settings.TOPOJSON_PROFILE)(

frictionless/metadata/metadata.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ def metadata_select_class(cls, type: Optional[str]) -> Type[Metadata]:
413413
return cls
414414

415415
@classmethod
416-
def metadata_select_property_class(cls, name: str) -> Optional[Type[Metadata]]:
416+
def metadata_select_property_class(cls, name: str) -> Optional[type]:
417417
"""Defines the class to use with a given property's metadata
418418
419419
Complex properties are likely to have their own python class,

frictionless/schema/field.py

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
import re
4+
from copy import deepcopy
45
from functools import partial
56
from typing import (
67
TYPE_CHECKING,
@@ -55,6 +56,7 @@
5556
YearFieldDescriptor,
5657
YearmonthFieldDescriptor,
5758
)
59+
from ..metadata import Metadata
5860

5961
if TYPE_CHECKING:
6062
from . import types
@@ -150,7 +152,7 @@ def read_cell(self, cell: Any):
150152
return cell_reader(cell)
151153

152154
def create_cell_reader(self) -> types.ICellReader:
153-
value_reader = self.create_value_reader()
155+
value_reader = self.value_read_writer.create_value_reader()
154156

155157
# Create missing values
156158
missing_values = self.missing_values
@@ -272,7 +274,11 @@ def value_writer(cell: Any):
272274
}
273275

274276
@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+
276282
if isinstance(source, dict):
277283
descriptor: FieldDescriptor = pydantic.TypeAdapter[FieldDescriptor](
278284
FieldDescriptor
@@ -328,8 +334,37 @@ def from_descriptor(cls, source: Union[Dict[str, Any], FieldDescriptor]):
328334
descriptor=descriptor,
329335
)
330336

331-
def to_descriptor(self):
337+
def to_descriptor(self) -> FieldDescriptor:
332338
if self.descriptor:
333339
return self.descriptor
334340
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()

frictionless/schema/schema.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ class Schema(Metadata, metaclass=Factory):
7979
"""
8080

8181
def __attrs_post_init__(self):
82+
print(self.fields)
8283
for field in self.fields:
8384
field.schema = self
8485
super().__attrs_post_init__()
@@ -325,6 +326,8 @@ def to_summary(self) -> str:
325326

326327
@classmethod
327328
def metadata_select_property_class(cls, name: str):
329+
if name == "fields":
330+
return Field
328331
return None
329332

330333
# TODO: handle invalid structure

frictionless/table/row.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,11 @@ def to_list(self, *, json: bool = False, types: Optional[List[str]] = None):
223223
return result
224224

225225
def to_dict(
226-
self, *, csv: bool = False, json: bool = False, types: Optional[List[str]] = None
226+
self,
227+
*,
228+
csv: bool = False,
229+
json: bool = False,
230+
types: Optional[List[str]] = None,
227231
) -> Dict[str, Any]:
228232
"""
229233
Parameters:

0 commit comments

Comments
 (0)