Coverage for /opt/conda/envs/apienv/lib/python3.10/site-packages/daiquiri/core/schema/components/h5grove.py: 60%

58 statements  

« prev     ^ index     » next       coverage.py v7.6.5, created at 2024-11-15 02:12 +0000

1from marshmallow import Schema, fields, ValidationError 

2 

3 

4class DTypeField(fields.Field): 

5 def _deserialize(self, value, attr, data, **kwargs): 

6 if isinstance(value, str) or isinstance(value, dict): 

7 return value 

8 

9 raise ValidationError("Dtype should be str or dict") 

10 

11 

12class ValueField(fields.Field): 

13 def _deserialize(self, value, attr, data, **kwargs): 

14 if isinstance(value, str) or isinstance(value, float) or isinstance(value, int): 

15 return value 

16 

17 if isinstance(value, list): 

18 return [self._deserialize(v, attr, data, **kwargs) for v in value] 

19 

20 raise ValidationError("Value should be str, number or list of those.") 

21 

22 

23class AttrMetaSchema(Schema): 

24 dtype = DTypeField(required=True) 

25 name = fields.Str(required=True) 

26 shape = fields.List(fields.Int(), required=True) 

27 

28 

29class BaseMetaSchema(Schema): 

30 name = fields.Str(required=True) 

31 type = fields.Str(required=True) 

32 

33 

34class ExternalLinkMetaSchema(BaseMetaSchema): 

35 target_file = fields.Str(required=True) 

36 target_path = fields.Str(required=True) 

37 

38 

39class SoftLinkMetaSchema(BaseMetaSchema): 

40 target_path = fields.Str(required=True) 

41 

42 

43class DatasetMetaSchema(BaseMetaSchema): 

44 attributes = fields.List(fields.Nested(AttrMetaSchema)) 

45 chunks = fields.List(fields.Int(), allow_none=True) 

46 dtype = DTypeField(required=True) 

47 filters = fields.Dict(allow_none=True) 

48 shape = fields.List(fields.Int(), required=True) 

49 

50 

51class GroupMetaSchema(BaseMetaSchema): 

52 attributes = fields.List(fields.Nested(AttrMetaSchema), required=True) 

53 children = fields.Dict(fields.Nested(lambda: MetaSchema()), required=True) 

54 

55 

56class MetaSchema(BaseMetaSchema): 

57 external_link_schema = ExternalLinkMetaSchema() 

58 soft_link_schema = ExternalLinkMetaSchema() 

59 dataset_schema = DatasetMetaSchema() 

60 group_schema = GroupMetaSchema() 

61 

62 def _get_data_schema(self, data): 

63 if data["type"] == "external_link": 

64 return self.external_link_schema 

65 if data["type"] == "soft_link": 

66 return self.soft_link_schema 

67 if data["type"] == "dataset": 

68 return self.dataset_schema 

69 if data["type"] == "group": 

70 return self.group_schema 

71 

72 return super() 

73 

74 def load(self, data, *args, **kwargs): 

75 data_schema = self._get_data_schema(data) 

76 return data_schema.load(data, *args, **kwargs) 

77 

78 def dump(self, data, *args, **kwargs): 

79 data_schema = self._get_data_schema(data) 

80 return data_schema.dump(data, *args, **kwargs) 

81 

82 def validate(self, data, *args, **kwargs): 

83 data_schema = self._get_data_schema(data) 

84 return data_schema.validate(data, *args, **kwargs)