33from odin import ResourceAdapter , resources
44from odin .exceptions import CodecDecodeError
55from odin .resources import ResourceBase
6- from odin .utils import getmeta
76
87try :
98 import toml
@@ -21,17 +20,20 @@ def load(fp, resource=None, full_clean=True, default_to_not_supplied=False):
2120 """
2221 Load a resource from a TOML encoded file.
2322
24- If a ``resource`` value is supplied it is used as the base resource for the supplied YAML. If one is not
25- supplied, a resource type field ``$`` is used to obtain the type represented by the dictionary. A
26- ``ValidationError`` will be raised if either of these values are supplied and not compatible. It is valid for a
27- type to be supplied in the file to be a child object from within the inheritance tree.
23+ If a ``resource`` value is supplied it is used as the base resource for the
24+ supplied YAML. If one is not supplied, a resource type field ``$`` is used to
25+ obtain the type represented by the dictionary. A ``ValidationError`` will be
26+ raised if either of these values are supplied and not compatible. It is valid for a
27+ type to be supplied in the file to be a child object from within the inheritance
28+ tree.
2829
2930 :param fp: a file pointer to read TOML data format.
30- :param resource: A resource type, resource name or list of resources and names to use as the base for creating a
31- resource. If a list is supplied the first item will be used if a resource type is not supplied.
31+ :param resource: A resource type, resource name or list of resources and names to
32+ use as the base for creating a resource. If a list is supplied the first item
33+ will be used if a resource type is not supplied.
3234 :param full_clean: Do a full clean of the object as part of the loading process.
33- :param default_to_not_supplied: Used for loading partial resources. Any fields not supplied are replaced with
34- NOT_SUPPLIED.
35+ :param default_to_not_supplied: Used for loading partial resources. Any fields not
36+ supplied are replaced with NOT_SUPPLIED.
3537 :returns: A resource object or object graph of resources loaded from file.
3638
3739 """
@@ -53,17 +55,19 @@ def load(fp, resource=None, full_clean=True, default_to_not_supplied=False):
5355def loads (s , resource = None , full_clean = True , default_to_not_supplied = False ):
5456 """Load a resource from a TOML encoded string.
5557
56- If a ``resource`` value is supplied it is used as the base resource for the supplied YAML. If one is not
57- supplied, a resource type field ``$`` is used to obtain the type represented by the dictionary. A
58- ``ValidationError`` will be raised if either of these values are supplied and not compatible. It is valid for a
59- type to be supplied in the file to be a child object from within the inheritance tree.
58+ If a ``resource`` value is supplied it is used as the base resource for the
59+ supplied YAML. If one is not supplied, a resource type field ``$`` is used to
60+ obtain the type represented by the dictionary. A ``ValidationError`` will be raised
61+ if either of these values are supplied and not compatible. It is valid for a type
62+ to be supplied in the file to be a child object from within the inheritance tree.
6063
6164 :param s: a string containing TOML.
62- :param resource: A resource type, resource name or list of resources and names to use as the base for creating a
63- resource. If a list is supplied the first item will be used if a resource type is not supplied.
65+ :param resource: A resource type, resource name or list of resources and names to
66+ use as the base for creating a resource. If a list is supplied the first item
67+ will be used if a resource type is not supplied.
6468 :param full_clean: Do a full clean of the object as part of the loading process.
65- :param default_to_not_supplied: Used for loading partial resources. Any fields not supplied are replaced with
66- NOT_SUPPLIED.
69+ :param default_to_not_supplied: Used for loading partial resources. Any fields not
70+ supplied are replaced with NOT_SUPPLIED.
6771 :returns: A resource object or object graph of resources loaded from file.
6872
6973 """
@@ -97,11 +101,7 @@ def __init__(
97101 self .include_type_field = include_type_field
98102
99103 def resource_to_dict (self , v ):
100- resource_dict = v .to_dict (self .include_virtual_fields )
101- if self .include_type_field :
102- meta = getmeta (v )
103- resource_dict [meta .type_field ] = meta .resource_name
104- return resource_dict
104+ return v .to_dict (self .include_virtual_fields , self .include_type_field )
105105
106106 def dump_value (self , v ):
107107 if isinstance (v , ResourceBase | ResourceAdapter ):
@@ -133,7 +133,8 @@ def dump(
133133
134134 :param resource: The root resource to dump to a JSON encoded file.
135135 :param fp: The file pointer that represents the output file.
136- :param encoder: Encoder to use serializing to a string; default is the :py:class:`OdinEncoder`.
136+ :param encoder: Encoder to use serializing to a string; default is the
137+ :py:class:`OdinEncoder`.
137138 :param include_virtual_fields: Include virtual fields in the output
138139 :param kwargs: Additional keyword arguments for the encoder.
139140 """
@@ -154,7 +155,8 @@ def dumps(
154155 """Dump to a TOML encoded file.
155156
156157 :param resource: The root resource to dump to a JSON encoded file.
157- :param encoder: Encoder to use serializing to a string; default is the :py:class:`OdinEncoder`.
158+ :param encoder: Encoder to use serializing to a string; default is the
159+ :py:class:`OdinEncoder`.
158160 :param include_virtual_fields: Include virtual fields in the output
159161 :param kwargs: Additional keyword arguments for the encoder.
160162 :returns: TOML encoded string.
0 commit comments