import inspect
import re


def kps_from_string(s):
    """
    Converts a simple KP operation from a string format to JSON

    Example:
    kp0 biolink:ChemicalSubstance biolink:treats biolink:Disease

    """
    s = inspect.cleandoc(s)
    kp_re = r"(?P<name>.*) (?P<subject>.*) (?P<predicate>.*) (?P<object>.*)"
    kps = {}
    for line in s.splitlines():
        match_kp = re.search(kp_re, line)
        if not match_kp:
            raise ValueError(f"Invalid line: {line}")
        name = match_kp.group("name")
        if name not in kps:
            kps[name] = {
                "url": f"http://{name}",
                "infores": name,
                "maturity": "development",
                "operations": [],
            }
        kps[name]["operations"].append(
            {
                "subject_category": match_kp.group("subject"),
                "predicate": match_kp.group("predicate"),
                "object_category": match_kp.group("object"),
            }
        )
    return kps