Skip to content

Commit

Permalink
Added: custom yaml loader to manage scope (#150)
Browse files Browse the repository at this point in the history
  • Loading branch information
signebedi committed Jun 22, 2024
1 parent 5df55ae commit 5a2d443
Showing 1 changed file with 57 additions and 39 deletions.
96 changes: 57 additions & 39 deletions libreforms_fastapi/utils/custom_yaml.py
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

0 comments on commit 5a2d443

Please sign in to comment.