Пример #1
0
def test_config_copy():
    cfg = Config(Schema_04())
    cp = cfg.copy()
    assert cfg.data == cp

    cp["updated"] = True
    assert cfg.data != cp
Пример #2
0
def test_apply_argparse_03_invalid():
    """
    Test of the apply_argparse function for Schema 03.
    Here we provide additional attributes outside of the schema.
    These should be marked as invalid.
    """
    config = Config(Schema_03())
    parser = argparse.ArgumentParser("new-parser")
    argparse_options(parser, Schema_03())

    parser.add_argument("--new-attribute-int", type=int)
    parser.add_argument("--new-attribute-str", type=str)

    args = parser.parse_args(
        [
            "--new-attribute-int",
            "123",
            "--new-attribute-str",
            "not good",
        ]
    )

    config = apply_argparse(args, config)

    # New arguments should not get added to config
    assert not config.get("new_attribute_int", False)
    assert not config.get("new_attribute_str", False)
Пример #3
0
def test_config_mapping():
    cfg = Config(Schema_04())
    cp = cfg.copy()
    assert cfg.data == cp
    assert len(cfg) == len(cp)

    for key in cfg:
        assert cp[key] == cfg[key]
Пример #4
0
def test_apply_argparse_15():
    """
    Test of the apply_argparse function for Schema 15.
    Here we have a doubly nested override.
    """
    schema_15 = Schema_15()

    config = Config(schema_15)

    parser = argparse.ArgumentParser("new-parser")
    argparse_options(parser, schema_15)

    args = parser.parse_args(
        [
            "--nested-schema.time-duration-attr",
            "2y 2d 2h 2m 2s 2ms",
            "--nested-schema.float-attr",
            "9.87",
            "--nested-schema.schema-attr.int-attr",
            "90210",
            "--nested-schema.schema-attr.str-attr",
            "updated",
        ]
    )
    apply_argparse(args, config)

    # Config should be overwritten by arguments
    assert config["nested_schema"]["float_attr"] == 9.87
    assert config["nested_schema"]["time_duration_attr"] == 63295322.002
    assert config["nested_schema"]["schema_attr"]["int_attr"] == 90210
    assert config["nested_schema"]["schema_attr"]["str_attr"] == "updated"
Пример #5
0
def test_apply_argparse_10_invalid():
    """
    Test of the apply_argparse function for Schema 10.
    Four nested schema options are passed to the config.
    """
    schema_10 = Schema_10()
    with open(
        os.path.join(os.path.dirname(__file__), "data", "defaults", "in.01.json")
    ) as fh:
        config_data = json.load(fh)

    config = Config(schema_10, config_data)

    parser = argparse.ArgumentParser("new-parser")
    argparse_options(parser, schema_10)

    parser.add_argument("--schema-attr.new-attr-int", type=int)
    parser.add_argument("--schema-attr.new-attr-str", type=str)

    args = parser.parse_args(
        [
            "--schema-attr.new-attr-int",
            "2222",
            "--schema-attr.new-attr-str",
            "hello world",
        ]
    )

    apply_argparse(args, config)

    # New arguments should not get added to config
    assert not config["schema_attr"].get("new_attribute_int", False)
    assert not config["schema_attr"].get("new_attribute_str", False)
Пример #6
0
def apply_arg(original_key: str, args: Namespace, config: Config) -> None:
    """
    Function for applying arguments to a config. Applies to nested
    configs as well.

    **Arguments**

    - original_key (`str`): the name of the argument in the argparse Namespace
    - args (`argparse.Namespace`): the entire Namespace, ie the result of the parser
    - config (`Config`): the config object

    """
    from confu.schema import Attribute, Schema

    schema = config._schema

    path = original_key.split("__")

    arg_data = getattr(args, original_key)

    if len(path) > 1:
        data = config.data

        current_schema = schema._attr

        for key in path:

            if isinstance(current_schema.get(key), Schema):
                current_schema = current_schema.get(key)._attr

            elif isinstance(current_schema.get(key), Attribute):
                attribute = current_schema.get(key)

                # If we cannot find the attribute in the schema
                # we don't add it
                if attribute is None:
                    return

        for key in path[:-1]:
            if key not in data:
                data[key] = {}
            data = data[key]
        data[path[-1]] = arg_data

    else:
        attribute = schema._attr.get(original_key)

        # If we cannot find the attribute in the schema
        # we don't add it
        if attribute is None:
            return

        config.data[original_key] = arg_data
Пример #7
0
def test_apply_argparse_03():
    """
    Test of the apply_argparse function for Schema 03.
    Two nested schema options are passed to the config.
    """
    config = Config(Schema_03())
    parser = argparse.ArgumentParser("new-parser")
    argparse_options(parser, Schema_03())

    args = parser.parse_args(
        [
            "--str-attr",
            "donkey",
            "--list-attr-int",
            "1,2,3",
            "--list-attr-str",
            "1,2,3",
            "--int-attr",
            "999",
            "--float-attr",
            "365.2",
            "--bool-attr",
            "--bool-attr-w-dflt",
            "--no-bool-attr-w-dflt-yes",
            "--nested.int-attr-choices",
            "2",
            "--nested.int-attr",
            "3",
        ]
    )

    config = apply_argparse(args, config)

    # Config should be overwritten by arguments
    assert config["str_attr"] == "donkey"
    assert config["list_attr_int"] == [1, 2, 3]
    assert config["list_attr_str"] == ["1", "2", "3"]
    assert config["int_attr"] == 999
    assert config["bool_attr"] is True
    assert config["bool_attr_w_dflt"] is True
    assert config["bool_attr_w_dflt_yes"] is False
    assert config["nested"]["int_attr_choices"] == 2
    assert config["nested"]["int_attr"] == 3
Пример #8
0
def test_apply_argparse_10():
    """
    Test of the apply_argparse function for Schema 10.
    Four nested schema options are passed to the config.
    """
    schema_10 = Schema_10()
    with open(
        os.path.join(os.path.dirname(__file__), "data", "defaults", "in.01.json")
    ) as fh:
        config_data = json.load(fh)

    config = Config(schema_10, config_data)

    parser = argparse.ArgumentParser("new-parser")
    argparse_options(parser, schema_10)

    args = parser.parse_args(
        [
            "--schema-attr.int-attr",
            "2222",
            "--schema-attr.str-attr",
            "hello world",
            "--schema-attr.str-attr-nd",
            "defaults empty string",
            "--schema-attr.str-attr-null",
            "not null",
        ]
    )

    apply_argparse(args, config)

    # Config should be overwritten by arguments
    assert config["schema_attr"]["int_attr"] == 2222
    assert config["schema_attr"]["str_attr"] == "hello world"
    assert config["schema_attr"]["str_attr_nd"] == "defaults empty string"
    assert config["schema_attr"]["str_attr_null"] == "not null"
Пример #9
0
def test_config_init():
    cfg = Config(Schema_04())

    # check internal cache
    data = cfg.data
    assert data == cfg.data
Пример #10
0
def test_get_nested():
    cfg = Config(Schema_04())
    data = cfg.data
    assert cfg.get_nested("nested") == {"int_attr_choices": 1}
    assert cfg.get_nested("nested.nonexistant") == None
Пример #11
0
def test_empty_config():
    cfg = Config(Schema_04())

    assert len(cfg)
Пример #12
0

class Example_Schema(Schema):
    int_attr = Int(default=123)
    str_attr = Str(default="test")
    str_attr_null = Str(default=None)
    list_attr_w_default = List(item=Int(), default=[1, 2, 3])
    nested = Nested_Schema()


class Simple_Example_Schema(Schema):
    str_attr = Str(default="test")


# config without kwargs
cfg = Config(Example_Schema())
print(cfg.data)

# config with data and meta kwrags
cfg_kwargs = Config(
    Simple_Example_Schema(),
    data={
        "int_attr": 42,
        "list_attr": [1, 2, 3]
    },
    meta={"meta_attr": "meta data"},
)
print(cfg_kwargs.data)
print(cfg_kwargs.meta)

# copy the data of a schema