예제 #1
0
def test_locate_root_level_entry():
    settings = {"auths": {"chaos.example.com": {"type": "bearer"}}}
    parent, entry, k, i = locate_settings_entry(settings, 'auths')
    assert parent == settings
    assert entry == settings['auths']
    assert k == 'auths'
    assert i == None
def test_locate_dotted_entry():
    settings = {"auths": {"chaos.example.com": {"type": "bearer"}}}
    parent, entry, k, i = locate_settings_entry(settings, "auths.chaos\\.example\\.com")
    assert parent == settings["auths"]
    assert entry == {"type": "bearer"}
    assert k == "chaos.example.com"
    assert i is None
def test_locate_root_level_entry():
    settings = {"auths": {"chaos.example.com": {"type": "bearer"}}}
    parent, entry, k, i = locate_settings_entry(settings, "auths")
    assert parent == settings
    assert entry == settings["auths"]
    assert k == "auths"
    assert i is None
예제 #4
0
def test_locate_indexed_entry():
    settings = {
        "auths": {
            "chaos.example.com": {
                "type":
                "bearer",
                "headers": [
                    {
                        "name": "X-Client",
                        "value": "blah"
                    },
                    {
                        "name": "X-For",
                        "value": "other"
                    },
                ],
            }
        }
    }
    parent, entry, k, i = locate_settings_entry(
        settings, "auths.chaos\\.example\\.com.headers[1]")
    assert parent == settings["auths"]["chaos.example.com"]["headers"]
    assert entry == {"name": "X-For", "value": "other"}
    assert k is None
    assert i == 1
예제 #5
0
def test_locate_dotted_entry():
    settings = {"auths": {"chaos.example.com": {"type": "bearer"}}}
    parent, entry, k, i = locate_settings_entry(settings,
                                                'auths.chaos\\.example\\.com')
    assert parent == settings['auths']
    assert entry == {"type": "bearer"}
    assert k == 'chaos.example.com'
    assert i == None
예제 #6
0
def remove_settings_value(ctx: click.Context, key: str):
    """
    Remove a settings key and its children.

    The key must be dotted path to its location in the settings file.
    """
    if not os.path.isfile(ctx.obj["settings_path"]):
        ctx.exit(1)

    settings = load_settings(ctx.obj["settings_path"]) or {}
    item = locate_settings_entry(settings, key)
    if not item:
        ctx.exit(1)
    parent, entry, key_tail, index = item

    if key_tail is not None:
        parent.pop(key_tail, None)
    elif index is not None:
        parent.remove(parent[index])
    save_settings(settings, ctx.obj["settings_path"])
예제 #7
0
def get_settings_value(ctx: click.Context, key: str, fmt: str = "json"):
    """
    Show a settings value.

    The key must be dotted path to its location in the settings file.
    """
    if not os.path.isfile(ctx.obj["settings_path"]):
        ctx.exit(1)

    settings = load_settings(ctx.obj["settings_path"]) or {}
    item = locate_settings_entry(settings, key)
    if not item:
        ctx.exit(1)
    parent, entry, key_tail, index = item

    if fmt == "json":
        click.echo(json.dumps(entry, indent=2))
    elif fmt == "string":
        click.echo(str(entry))
    elif fmt == "yaml":
        click.echo(yaml.dump(entry, indent=2))
예제 #8
0
def test_locate_dotted_key_from_indexed_entry():
    settings = {
        "auths": {
            "chaos.example.com": {
                "type":
                "bearer",
                "headers": [{
                    "name": "X-Client",
                    "value": "blah"
                }, {
                    "name": "X-For",
                    "value": "other"
                }]
            }
        }
    }
    parent, entry, k, i = locate_settings_entry(
        settings, 'auths.chaos\\.example\\.com.headers[1].name')
    assert parent == settings['auths']["chaos.example.com"]["headers"][1]
    assert entry == "X-For"
    assert k == "name"
    assert i == None
예제 #9
0
def set_settings_value(ctx: click.Context, key: str, value: str = None):
    """
    Set a settings value.
    The value must be a valid JSON string so that it can be interpreted
    with the appropriate type.

    The key must be dotted path to its location in the settings file.
    """
    if not os.path.isfile(ctx.obj["settings_path"]):
        ctx.exit(1)

    settings = load_settings(ctx.obj["settings_path"]) or {}
    item = locate_settings_entry(settings, key)
    if not item:
        ctx.exit(1)
    parent, entry, key_tail, index = item

    value = json.loads(value)
    if key_tail is not None:
        parent[key_tail] = value
    elif index is not None:
        parent[index] = value
    save_settings(settings, ctx.obj["settings_path"])
예제 #10
0
def test_cannot_locate_dotted_entry():
    settings = {"auths": {"chaos.example.com": {"type": "bearer"}}}
    assert locate_settings_entry(settings, 'auths.chaos.example.com') == None