-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor YAML loading to use add_representer
- Loading branch information
Paul Prescod
committed
Jun 6, 2022
1 parent
7844d2c
commit 19944a0
Showing
6 changed files
with
69 additions
and
48 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
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
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
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
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
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,11 +1,44 @@ | ||
from yaml import SafeDumper | ||
from typing import Callable | ||
from yaml import SafeDumper, SafeLoader | ||
from yaml.representer import Representer | ||
from collections import defaultdict | ||
|
||
|
||
class SnowfakeryDumper(SafeDumper): | ||
class SnowfakeryContinuationDumper(SafeDumper): | ||
pass | ||
|
||
|
||
SnowfakeryContinuationDumper.add_representer( | ||
defaultdict, SnowfakeryContinuationDumper.represent_dict | ||
) | ||
|
||
|
||
def hydrate(cls, data): | ||
obj = cls.__new__(cls) | ||
obj.__setstate__(data) | ||
obj.restore_from_continuation(data) | ||
return obj | ||
|
||
|
||
# Evaluate whether its cleaner for functions to bypass register_for_continuation | ||
# and go directly to SnowfakeryContinuationDumper.add_representer. | ||
# | ||
# | ||
|
||
|
||
def represent_continuation(dumper: SnowfakeryContinuationDumper, data): | ||
if isinstance(data, dict): | ||
return Representer.represent_dict(dumper, data) | ||
else: | ||
return Representer.represent_object(dumper, data) | ||
|
||
|
||
def register_for_continuation(cls, dump_transformer: Callable = lambda x: x): | ||
SnowfakeryContinuationDumper.add_representer( | ||
cls, lambda self, data: represent_continuation(self, dump_transformer(data)) | ||
) | ||
SafeLoader.add_constructor( | ||
f"tag:yaml.org,2002:python/object/apply:{cls.__module__}.{cls.__name__}", | ||
lambda loader, node: cls._from_continuation( | ||
loader.construct_mapping(node.value[0]) | ||
), | ||
) |