Skip to content

core

Core somesy functions.

INPUT_FILES_ORDERED module-attribute

INPUT_FILES_ORDERED = [
    ".somesy.toml",
    "somesy.toml",
    "pyproject.toml",
    "package.json",
    "Project.toml",
    "fpm.toml",
    "Cargo.toml",
]

Input files ordered by priority for discovery.

discover_input

discover_input(input_file: Optional[Path] = None) -> Path

Check given input file path. If not given, find somesy configuration file path from default list.

Parameters:

Name Type Description Default
input_file Optional[Path]

somesy configuration file path. Defaults to None.

None

Raises:

Type Description
FileNotFoundError

Raised if no somesy input file found from cli input or the defaults.

Returns:

Type Description
Path

somesy configuration file path.

Source code in src/somesy/core/core.py
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
def discover_input(input_file: Optional[Path] = None) -> Path:
    """Check given input file path. If not given, find somesy configuration file path from default list.

    Args:
        input_file: somesy configuration file path. Defaults to None.

    Raises:
        FileNotFoundError: Raised if no somesy input file found from cli input or the defaults.

    Returns:
        somesy configuration file path.
    """
    if input_file:
        if input_file.is_file():
            logger.info(f"Using provided file '{input_file}' as somesy input file.")
            return input_file
        else:
            msg = f"Passed file '{input_file}' does not exist. Searching for usable somesy input file..."
            logger.verbose(msg)

    for filename in INPUT_FILES_ORDERED:
        input_file = Path(filename)
        if input_file.is_file():
            try:
                get_input_content(input_file)
            except RuntimeError:
                continue

            msg = f"Using '{input_file}' as somesy input file."
            logger.verbose(msg)
            return input_file

    raise FileNotFoundError("No somesy input file found.")

get_input_content

get_input_content(
    path: Path, *, no_unwrap: bool = False
) -> Dict[str, Any]

Read contents of a supported somesy input file.

Given a path to a TOML file, this function reads the file and returns its content as a TOMLDocument object. The function checks if the file is a valid somesy input file by checking its name and content.

Parameters:

Name Type Description Default
path Path

path to the input file

required
no_unwrap bool

if True, the function returns the TOMLDocument object instead of unwrapping it

False

Returns:

Type Description
Dict[str, Any]

the content of the input file as a TOMLDocument object

Raises:

Type Description
ValueError

if the input file is not a valid somesy input file or if the file is not a TOML file.

RuntimeError

if the input file does not contain a somesy input section at expected key

Source code in src/somesy/core/core.py
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
def get_input_content(path: Path, *, no_unwrap: bool = False) -> Dict[str, Any]:
    """Read contents of a supported somesy input file.

    Given a path to a TOML file, this function reads the file and returns its content as a TOMLDocument object.
    The function checks if the file is a valid somesy input file by checking its name and content.

    Args:
        path (Path): path to the input file
        no_unwrap (bool): if True, the function returns the TOMLDocument object instead of unwrapping it

    Returns:
        the content of the input file as a TOMLDocument object

    Raises:
        ValueError: if the input file is not a valid somesy input file or if the file is not a TOML file.
        RuntimeError: if the input file does not contain a somesy input section at expected key
    """
    logger.debug(f"Path {path}")
    # somesy.toml / .somesy.toml
    if path.suffix == ".toml" and "somesy" in path.name:
        with open(path, "r") as f:
            ret = tomlkit.load(f)
            return ret if no_unwrap else ret.unwrap()

    # pyproject.toml or fpm.toml
    if (path.suffix == ".toml" and "pyproject" in path.name) or path.name in [
        "Project.toml",
        "fpm.toml",
    ]:
        with open(path, "r") as f:
            input_content = tomlkit.load(f)
            if "tool" in input_content and "somesy" in input_content["tool"]:
                return input_content["tool"]["somesy"].unwrap()
            else:
                raise RuntimeError(
                    "No tool.somesy section found in pyproject.toml file!"
                )

    # Cargo.toml
    if path.name == "Cargo.toml":
        with open(path, "r") as f:
            input_content = tomlkit.load(f)
            if (
                "package" in input_content
                and "metadata" in input_content["package"]
                and "somesy" in input_content["package"]["metadata"]
            ):
                return input_content["package"]["metadata"]["somesy"].unwrap()
            else:
                raise RuntimeError(
                    "No package.somesy section found in Cargo.toml file!"
                )

    # package.json
    if path.suffix == ".json" and "package" in path.name:
        with open(path, "r") as f:
            input_content = json.load(f)
            if "somesy" in input_content:
                return input_content["somesy"]
            else:
                raise RuntimeError("No somesy section found in package.json file!")

    # no match:
    raise ValueError("Unsupported input file.")