File: utils.py

package info (click to toggle)
elasticsearch-curator 8.0.21-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 2,716 kB
  • sloc: python: 17,838; makefile: 159; sh: 156
file content (31 lines) | stat: -rw-r--r-- 1,048 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
"""Singleton Utils Module"""

import json
from click import BadParameter
from es_client.helpers.utils import ensure_list


# Click functions require ctx and param to be passed positionally even if not used
# pylint: disable=unused-argument
def json_to_dict(ctx, param, value):
    """Convert JSON to dictionary"""
    if value is None:
        return {}
    try:
        return json.loads(value)
    except ValueError as exc:
        raise BadParameter(f'Invalid JSON for {param}: {value}') from exc


# Click functions require ctx and param to be passed positionally even if not used
# pylint: disable=unused-argument
def validate_filter_json(ctx, param, value):
    """Validate the JSON provided from the command-line"""
    # The "None" edge case should only be for optional filters, like alias add/remove
    if value is None:
        return value
    try:
        filter_list = ensure_list(json.loads(value))
        return filter_list
    except ValueError as exc:
        raise BadParameter(f'Filter list is invalid JSON: {value}') from exc