forked from wise-agents/wise-agents
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request wise-agents#310 from kabir/custom_yaml_loader
Custom yaml loader
- Loading branch information
Showing
14 changed files
with
100 additions
and
33 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
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
5 changes: 3 additions & 2 deletions
5
src/wiseagents/yaml_env/__init__.py → src/wiseagents/yaml/__init__.py
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,12 @@ | ||
# This is the __init__.py file for the wiseagents.yaml_env package | ||
# This is the __init__.py file for the wiseagents.yaml package | ||
|
||
# Import any modules or subpackages here | ||
from .yaml_utils import setup_yaml_for_env_vars | ||
from .wise_yaml_loader import WiseAgentsLoader | ||
|
||
|
||
# Define any necessary initialization code here | ||
|
||
# Optionally, you can define __all__ to specify the public interface of the package | ||
# __all__ = ['module1', 'module2', 'subpackage'] | ||
__all__ = ['setup_yaml_for_env_vars'] | ||
__all__ = ['setup_yaml_for_env_vars', 'WiseAgentsLoader'] |
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 |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import importlib | ||
import yaml | ||
|
||
from yaml.reader import Reader | ||
from yaml.scanner import Scanner | ||
from yaml.parser import Parser | ||
from yaml.composer import Composer | ||
from yaml.constructor import FullConstructor | ||
from yaml.resolver import Resolver | ||
|
||
|
||
class WiseAgentsLoader(Reader, Scanner, Parser, Composer, FullConstructor, Resolver): | ||
|
||
def __init__(self, stream): | ||
opened_file = False | ||
try: | ||
stream_copy = None | ||
if isinstance(stream, str): | ||
stream_copy = "" + stream | ||
elif isinstance(stream, bytes): | ||
stream_copy = b"" + stream | ||
else: | ||
opened_file = True | ||
stream_copy = open(getattr(stream, 'name', "<file>")) | ||
|
||
Reader.__init__(self, stream) | ||
Scanner.__init__(self) | ||
Parser.__init__(self) | ||
Composer.__init__(self) | ||
FullConstructor.__init__(self) | ||
Resolver.__init__(self) | ||
|
||
seen_classes = {} | ||
seen_packages = {} | ||
|
||
for token in yaml.scan(stream_copy): | ||
if type(token) is yaml.TagToken and token.value[0] == "!": | ||
if token.value in seen_classes.keys(): | ||
continue | ||
seen_classes[token.value] = True | ||
package_name = "" | ||
for part in token.value[1].split(".")[:-1]: | ||
package_name += part + "." | ||
package_name = package_name[:-1] | ||
if package_name in seen_packages.values(): | ||
continue | ||
seen_packages[package_name] = True | ||
importlib.import_module(package_name) | ||
|
||
finally: | ||
if opened_file: | ||
stream_copy.close() | ||
|
||
def construct_document(self, node): | ||
return super().construct_document(node) |
File renamed without changes.
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
2 changes: 1 addition & 1 deletion
2
tests/wiseagents/yaml_env/__init__.py → tests/wiseagents/yaml/__init__.py
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
4 changes: 2 additions & 2 deletions
4
tests/wiseagents/yaml_env/test_yaml_env.py → tests/wiseagents/yaml/test_yaml_env.py
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