Skip to content

Commit 888e570

Browse files
committed
Added: separate script for yaml constructors (#150)
1 parent e805f5f commit 888e570

File tree

2 files changed

+57
-50
lines changed

2 files changed

+57
-50
lines changed
+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
from datetime import datetime, date, time, timedelta
2+
3+
4+
def get_yaml_constructors(**kwargs):
5+
"""
6+
This factory is used to build a dictionary of built-in and custom constructors that
7+
will be used in building the internal, dictionary representation of the form config.
8+
"""
9+
10+
# Default constructors for returning Python types
11+
def type_constructor_int(loader, node):
12+
return int
13+
14+
def type_constructor_str(loader, node):
15+
return str
16+
17+
def type_constructor_date(loader, node):
18+
return date
19+
20+
def type_constructor_datetime(loader, node):
21+
return datetime
22+
23+
def type_constructor_time(loader, node):
24+
return time
25+
26+
def type_constructor_timedelta(loader, node):
27+
return timedelta
28+
29+
def type_constructor_list(loader, node):
30+
return list
31+
32+
def type_constructor_tuple(loader, node):
33+
return tuple
34+
35+
def type_constructor_bytes(loader, node):
36+
return bytes
37+
38+
# We create a constructor mapping that we'll use later to
39+
# register the constructors.
40+
constructor_mapping = {
41+
'!int': type_constructor_int,
42+
'!str': type_constructor_str,
43+
'!date': type_constructor_date,
44+
'!type_datetime': type_constructor_datetime,
45+
'!type_time': type_constructor_time,
46+
'!type_timedelta': type_constructor_time,
47+
'!list': type_constructor_list,
48+
'!tuple': type_constructor_list,
49+
'!bytes': type_constructor_bytes,
50+
**kwargs,
51+
}
52+
return constructor_mapping

libreforms_fastapi/utils/pydantic_models.py

+5-50
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@
1818

1919
from pydantic.functional_validators import field_validator, model_validator
2020

21+
# We externalize the custom yaml constructors to create an easier entrypoint
22+
# to manage both default custom constructors, as well as end-user logic, see
23+
# https://github.com/signebedi/libreforms-fastapi/issues/150
24+
from libreforms_fastapi.utils.custom_yaml import get_yaml_constructors
25+
2126
class ImproperUsernameFormat(Exception):
2227
"""Raised when the username does not meet the regular expression defined in the app config"""
2328
pass
@@ -276,56 +281,6 @@ def password_pattern(cls, value):
276281
description: This is a file field
277282
"""
278283

279-
def get_yaml_constructors(**kwargs):
280-
"""
281-
This factory is used to build a dictionary of built-in and custom constructors that
282-
will be used in building the internal, dictionary representation of the form config.
283-
"""
284-
285-
# Default constructors for returning Python types
286-
def type_constructor_int(loader, node):
287-
return int
288-
289-
def type_constructor_str(loader, node):
290-
return str
291-
292-
def type_constructor_date(loader, node):
293-
return date
294-
295-
def type_constructor_datetime(loader, node):
296-
return datetime
297-
298-
def type_constructor_time(loader, node):
299-
return time
300-
301-
def type_constructor_timedelta(loader, node):
302-
return timedelta
303-
304-
def type_constructor_list(loader, node):
305-
return list
306-
307-
def type_constructor_tuple(loader, node):
308-
return tuple
309-
310-
def type_constructor_bytes(loader, node):
311-
return bytes
312-
313-
# We create a constructor mapping that we'll use later to
314-
# register the constructors.
315-
constructor_mapping = {
316-
'!int': type_constructor_int,
317-
'!str': type_constructor_str,
318-
'!date': type_constructor_date,
319-
'!type_datetime': type_constructor_datetime,
320-
'!type_time': type_constructor_time,
321-
'!type_timedelta': type_constructor_time,
322-
'!list': type_constructor_list,
323-
'!tuple': type_constructor_list,
324-
'!bytes': type_constructor_bytes,
325-
**kwargs,
326-
}
327-
return constructor_mapping
328-
329284
# Register the type constructors
330285
for key, value in get_yaml_constructors().items():
331286
yaml.add_constructor(key, value)

0 commit comments

Comments
 (0)