-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added: custom yaml loader to manage scope (#150)
- Loading branch information
Showing
1 changed file
with
57 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,70 @@ | ||
import yaml | ||
from datetime import datetime, date, time, timedelta | ||
|
||
|
||
def get_yaml_constructors(**kwargs): | ||
""" | ||
This factory is used to build a dictionary of built-in and custom constructors that | ||
will be used in building the internal, dictionary representation of the form config. | ||
""" | ||
def get_all_users(): | ||
pass | ||
|
||
# Default constructors for returning Python types | ||
def type_constructor_int(loader, node): | ||
return int | ||
def get_all_groups(): | ||
pass | ||
|
||
def type_constructor_str(loader, node): | ||
return str | ||
def get_all_forms(form_name): | ||
pass | ||
|
||
def type_constructor_date(loader, node): | ||
return date | ||
class CustomFullLoader(yaml.FullLoader): | ||
def __init__(self, stream): | ||
super().__init__(stream) | ||
|
||
def type_constructor_datetime(loader, node): | ||
return datetime | ||
# Register the type constructors | ||
for key, value in self.get_yaml_constructors().items(): | ||
self.add_constructor(key, value) | ||
|
||
def type_constructor_time(loader, node): | ||
return time | ||
def get_yaml_constructors(self, **kwargs): | ||
""" | ||
This factory is used to build a dictionary of built-in and custom constructors that | ||
will be used in serialize the internal, dictionary representation of the form config. | ||
""" | ||
|
||
def type_constructor_timedelta(loader, node): | ||
return timedelta | ||
# Default constructors for returning Python types | ||
def type_constructor_int(loader, node): | ||
return int | ||
|
||
def type_constructor_list(loader, node): | ||
return list | ||
def type_constructor_str(loader, node): | ||
return str | ||
|
||
def type_constructor_tuple(loader, node): | ||
return tuple | ||
def type_constructor_date(loader, node): | ||
return date | ||
|
||
def type_constructor_bytes(loader, node): | ||
return bytes | ||
def type_constructor_datetime(loader, node): | ||
return datetime | ||
|
||
# We create a constructor mapping that we'll use later to | ||
# register the constructors. | ||
constructor_mapping = { | ||
'!int': type_constructor_int, | ||
'!str': type_constructor_str, | ||
'!date': type_constructor_date, | ||
'!type_datetime': type_constructor_datetime, | ||
'!type_time': type_constructor_time, | ||
'!type_timedelta': type_constructor_time, | ||
'!list': type_constructor_list, | ||
'!tuple': type_constructor_list, | ||
'!bytes': type_constructor_bytes, | ||
**kwargs, | ||
} | ||
return constructor_mapping | ||
def type_constructor_time(loader, node): | ||
return time | ||
|
||
def type_constructor_timedelta(loader, node): | ||
return timedelta | ||
|
||
def type_constructor_list(loader, node): | ||
return list | ||
|
||
def type_constructor_tuple(loader, node): | ||
return tuple | ||
|
||
def type_constructor_bytes(loader, node): | ||
return bytes | ||
|
||
# We create a constructor mapping that we'll use later to | ||
# register the constructors. | ||
constructor_mapping = { | ||
'!int': type_constructor_int, | ||
'!str': type_constructor_str, | ||
'!date': type_constructor_date, | ||
'!datetime': type_constructor_datetime, | ||
'!time': type_constructor_time, | ||
'!timedelta': type_constructor_timedelta, | ||
'!list': type_constructor_list, | ||
'!tuple': type_constructor_list, | ||
'!bytes': type_constructor_bytes, | ||
**kwargs, | ||
} | ||
return constructor_mapping |