def test_registrable_factory_roundtrip_alias(make_aliased_classes): A, B = make_aliased_classes txt = """a: !a_class.some_factory akw1: 8 akw2: !b_ bkw1: 2 bkw2: hello world """ txt_default_alias = """a: !a_class.some_factory akw1: 8 akw2: !b_ bkw1: 2 bkw2: hello world """ config = yaml.load(txt) a = config['a'] assert a.akw1 == 8 assert a.akw2 is not None assert hasattr(a.akw2, "bkw1") assert a.akw2.bkw1 == 2 assert isinstance(a, A) with StringIO() as s: yaml.dump(config, s) assert s.getvalue() == txt_default_alias
def test_registrable_roundtrip(make_classes): A, B = make_classes txt = """a: !A akw1: 8 akw2: !B bkw1: 2 bkw2: hello world """ config = yaml.load(txt) with StringIO() as s: yaml.dump(config, s) assert s.getvalue() == txt
def test_registrable_dump_new_class(make_new_classes): A, B = make_new_classes txt = """!a_class akw1: 8 akw2: !b_class bkw1: 2 bkw2: hello world """ b = B(2, "hello world") a = A(8, b) with StringIO() as s: yaml.dump(a, s) assert s.getvalue() == txt
def test_component_dumping_made_in_code(make_classes_2): A, B = make_classes_2 txt_expected = """!A akw1: 8 akw2: !B bkw1: 1 bkw2: test bkw3: 99 """ b_custom = B.compile(bkw1=1, bkw2='test') a_custom = A.compile(akw1=8, akw2=b_custom) with StringIO() as stream: yaml.dump(a_custom, stream) assert txt_expected == stream.getvalue()
def test_registrable_roundtrip_new_default(make_new_classes): A, B = make_new_classes txt = """a: !a_class akw1: 8 akw2: !b_ bkw1: 2 bkw2: hello world """ txt_default_alias = """a: !a_class akw1: 8 akw2: !b_ bkw1: 2 bkw2: hello world """ config = yaml.load(txt) with StringIO() as s: yaml.dump(config, s) assert s.getvalue() == txt_default_alias
def test_component_dumping_with_defaults_and_comments(make_classes_2): A, B = make_classes_2 txt = """!A akw1: 8 # Comment Here akw2: !B bkw1: 1 bkw2: !!str test """ txt_expected = """!A akw1: 8 akw2: !B bkw1: 1 bkw2: test bkw3: 99 """ a_schema = yaml.load(txt) a = a_schema() with StringIO() as stream: yaml.dump(a, stream) assert txt_expected == stream.getvalue()
def first_parse(self) -> Tuple[str, Dict]: """Check if valid YAML file and also load config In this first parse the runnable does not get compiled because it could be a custom Runnable, so it needs the extensions to be imported first. """ if not os.path.exists(self.yaml_file): raise FileNotFoundError( f"Configuration file '{self.yaml_file}' not found. Terminating." ) with open(self.yaml_file, 'r') as f: content = f.read() try: yamls = list(yaml.load_all(content)) except TypeError as e: raise error.ParsingRunnableError( f"Syntax error compiling the runnable: {str(e)}") if len(yamls) > 2: raise ValueError( f"{self.yaml_file} should contain an (optional) extensions sections" + " and the main runnable object.") extensions: Dict[str, str] = {} if len(yamls) == 2: extensions = dict(yamls[0]) # We want self.content to be a string with the raw content # We will precompile later once all extensions are registered. with StringIO() as stream: yaml.dump(yamls[-1], stream) content = stream.getvalue() return content, extensions
def test_component_dumping_factory(make_instances): a = make_instances() config = "!A {}\n" with StringIO() as stream: yaml.dump(a, stream) assert config == stream.getvalue()