Skip to content

Commit 0f84a4e

Browse files
committed
Modified: simplified constructors during partial initialization (#150)
1 parent 1f47f11 commit 0f84a4e

File tree

1 file changed

+51
-52
lines changed

1 file changed

+51
-52
lines changed

libreforms_fastapi/utils/custom_yaml.py

+51-52
Original file line numberDiff line numberDiff line change
@@ -27,62 +27,61 @@ def unknown_constructor(loader, suffix, node):
2727
return str("")
2828
self.add_multi_constructor('', unknown_constructor)
2929

30-
# Register the type constructors
31-
for key, value in get_basic_yaml_constructors(initialize_full_loader=self.initialize_full_loader).items():
32-
self.add_constructor(key, value)
30+
else:
3331

34-
# If we want the full loader initialized, then we will expect that doc_db,
35-
# session, User, and Group are not None, then add these as data constructors,
36-
# see https://github.com/signebedi/libreforms-fastapi/issues/150.
32+
# Register the type constructors
33+
for key, value in get_basic_yaml_constructors().items():
34+
self.add_constructor(key, value)
3735

38-
def data_constructor_all_usernames(loader, node):
39-
if self.initialize_full_loader and User is not None and session is not None:
40-
return [""]+[x.to_dict(just_the_basics=True)['username'] for x in session.query(User).all()]
41-
else:
42-
return [""]
36+
# If we want the full loader initialized, then we will expect that doc_db,
37+
# session, User, and Group are not None, then add these as data constructors,
38+
# see https://github.com/signebedi/libreforms-fastapi/issues/150.
4339

44-
self.add_constructor('!all_usernames', data_constructor_all_usernames)
45-
46-
def data_constructor_all_group_names(loader, node):
47-
if self.initialize_full_loader and Group is not None and session is not None:
48-
return [""]+[x.to_dict()['name'] for x in session.query(Group).all()]
49-
else:
50-
return [""]
51-
52-
self.add_constructor('!all_groups', data_constructor_all_group_names)
40+
def data_constructor_all_usernames(loader, node):
41+
if self.initialize_full_loader and User is not None and session is not None:
42+
return [""]+[x.to_dict(just_the_basics=True)['username'] for x in session.query(User).all()]
43+
else:
44+
return [""]
5345

54-
def data_constructor_dynamic_forms(form_name, initialize_full_loader, doc_db):
46+
self.add_constructor('!all_usernames', data_constructor_all_usernames)
5547

56-
def dynamic_method(loader, data):
57-
if self.initialize_full_loader and doc_db is not None:
58-
return [""]+doc_db._get_existing_document_ids(form_name)
48+
def data_constructor_all_group_names(loader, node):
49+
if self.initialize_full_loader and Group is not None and session is not None:
50+
return [""]+[x.to_dict()['name'] for x in session.query(Group).all()]
5951
else:
6052
return [""]
6153

62-
return dynamic_method
63-
64-
# This little block of code is going to give us a world of difficulty
65-
# when we don't pass doc_db but want to validate the yaml ... how do we
66-
# access the form names other than doc_db._get_form_names()so we can
67-
# remove `if doc_db is not None:`?
68-
if doc_db is not None:
69-
for form_name in doc_db._get_form_names():
70-
self.add_constructor(
71-
f'!all_forms_{form_name}',
72-
data_constructor_dynamic_forms(
73-
form_name,
74-
self.initialize_full_loader,
75-
doc_db=doc_db
54+
self.add_constructor('!all_groups', data_constructor_all_group_names)
55+
56+
def data_constructor_dynamic_forms(form_name, initialize_full_loader, doc_db):
57+
58+
def dynamic_method(loader, data):
59+
if self.initialize_full_loader and doc_db is not None:
60+
return [""]+doc_db._get_existing_document_ids(form_name)
61+
else:
62+
return [""]
63+
64+
return dynamic_method
65+
66+
# This little block of code is going to give us a world of difficulty
67+
# when we don't pass doc_db but want to validate the yaml ... how do we
68+
# access the form names other than doc_db._get_form_names()so we can
69+
# remove `if doc_db is not None:`?
70+
if doc_db is not None:
71+
for form_name in doc_db._get_form_names():
72+
self.add_constructor(
73+
f'!all_forms_{form_name}',
74+
data_constructor_dynamic_forms(
75+
form_name,
76+
self.initialize_full_loader,
77+
doc_db=doc_db
78+
)
7679
)
77-
)
7880

7981
return CustomFullLoader
8082

8183

82-
def get_basic_yaml_constructors(
83-
initialize_full_loader: bool = False,
84-
**kwargs
85-
):
84+
def get_basic_yaml_constructors(**kwargs):
8685
"""
8786
This factory is used to build a dictionary of built-in and custom constructors that
8887
will be used in serialize the internal, dictionary representation of the form config.
@@ -92,31 +91,31 @@ def get_basic_yaml_constructors(
9291
def type_constructor_int(loader, node):
9392
# If initialize_full_loader is not True, then we really
9493
# don't need to manage any special constructors at all.
95-
return int if initialize_full_loader else ""
94+
return int
9695

9796
def type_constructor_str(loader, node):
98-
return str if initialize_full_loader else ""
97+
return str
9998

10099
def type_constructor_date(loader, node):
101-
return date if initialize_full_loader else ""
100+
return date
102101

103102
def type_constructor_datetime(loader, node):
104-
return datetime if initialize_full_loader else ""
103+
return datetime
105104

106105
def type_constructor_time(loader, node):
107-
return time if initialize_full_loader else ""
106+
return time
108107

109108
def type_constructor_timedelta(loader, node):
110-
return timedelta if initialize_full_loader else ""
109+
return timedelta
111110

112111
def type_constructor_list(loader, node):
113-
return list if initialize_full_loader else ""
112+
return list
114113

115114
def type_constructor_tuple(loader, node):
116-
return tuple if initialize_full_loader else ""
115+
return tuple
117116

118117
def type_constructor_bytes(loader, node):
119-
return bytes if initialize_full_loader else ""
118+
return bytes
120119

121120
# We create a constructor mapping that we'll use later to
122121
# register the constructors.

0 commit comments

Comments
 (0)