Skip to content

XetoCLI

XetoCLI

A client interface to Haxall's Xeto CLI tools for type checking and analysis of Haystack records.

This API is experimental and subject to change in future releases.

XetoCLI can be directly imported as follows:

from phable import XetoCLI
Source code in src/phable/xeto_cli.py
10
11
12
13
14
15
16
17
18
19
20
21
22
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
56
57
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
class XetoCLI:
    """A client interface to Haxall's Xeto CLI tools for type checking and analysis
    of Haystack records.

    This API is experimental and subject to change in future releases.

    `XetoCLI` can be directly imported as follows:

    ```python
    from phable import XetoCLI
    ```
    """

    def __init__(
        self,
        *,
        io_format: Literal["json", "zinc"] = "zinc",
    ):
        """Initialize a `XetoCLI` instance.

        Parameters:
            io_format:
                Data serialization format for communication with Haxall. Either `json`
                or `zinc`. Defaults to `zinc`.
        """
        self._io_format = io_format
        self._encoder = PH_CODECS[io_format].encoder
        self._decoder = PH_CODECS[io_format].decoder

    def validate(self, recs: list[dict[str, Any]], graph: bool = True) -> Grid:
        """Validate records against Xeto type specifications.

        This method executes the Haxall `xeto validate` command to determine whether
        the provided records conform to their declared Xeto types. It returns a grid
        of validation errors and warnings, including any type mismatches or missing
        required tags.

        **Example:**

        ```python
        from phable import Marker, Number, Ref, XetoCLI

        cli = XetoCLI()
        recs = [
            {
                "dis": "Site 1",
                "site": Marker(),
                "area": Number(1_000, "square_foot"),
                "spec": Ref("ph::Site"),
            }
        ]
        result = cli.validate(recs)
        ```

        Parameters:
            recs:
                List of Haystack record dictionaries to analyze. Each record should
                specify a Xeto type with a `spec` tag.
            graph:
                If `True`, also checks the graph of query references between
                records, such as an equip's required points. Defaults to `True`.

        Returns:
            `Grid` with one row per validation error or warning (columns `id`, `level`,
            `slot`, `msg`). The grid has no rows when every record is valid.
        """

        with tempfile.NamedTemporaryFile(
            mode="w", suffix=f".{self._io_format}", delete=False
        ) as temp_file:
            temp_file.write(self._encoder(recs))
            temp_file.flush()
            temp_file_path = temp_file.name

        try:
            cli_stdout = _exec_localhost_cmd(self._io_format, graph, temp_file_path)
            decoded_str = self._decoder(cli_stdout)
            assert isinstance(decoded_str, Grid)
            return decoded_str
        finally:
            try:
                os.unlink(temp_file_path)
            except OSError:
                pass

__init__

__init__(*, io_format='zinc')

Initialize a XetoCLI instance.

Parameters:

Name Type Description Default
io_format typing.Literal['json', 'zinc']

Data serialization format for communication with Haxall. Either json or zinc. Defaults to zinc.

'zinc'
Source code in src/phable/xeto_cli.py
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
def __init__(
    self,
    *,
    io_format: Literal["json", "zinc"] = "zinc",
):
    """Initialize a `XetoCLI` instance.

    Parameters:
        io_format:
            Data serialization format for communication with Haxall. Either `json`
            or `zinc`. Defaults to `zinc`.
    """
    self._io_format = io_format
    self._encoder = PH_CODECS[io_format].encoder
    self._decoder = PH_CODECS[io_format].decoder

validate

validate(recs, graph=True)

Validate records against Xeto type specifications.

This method executes the Haxall xeto validate command to determine whether the provided records conform to their declared Xeto types. It returns a grid of validation errors and warnings, including any type mismatches or missing required tags.

Example:

from phable import Marker, Number, Ref, XetoCLI

cli = XetoCLI()
recs = [
    {
        "dis": "Site 1",
        "site": Marker(),
        "area": Number(1_000, "square_foot"),
        "spec": Ref("ph::Site"),
    }
]
result = cli.validate(recs)

Parameters:

Name Type Description Default
recs list[dict[str, typing.Any]]

List of Haystack record dictionaries to analyze. Each record should specify a Xeto type with a spec tag.

required
graph bool

If True, also checks the graph of query references between records, such as an equip's required points. Defaults to True.

True

Returns:

Type Description
phable.Grid

Grid with one row per validation error or warning (columns id, level,

phable.Grid

slot, msg). The grid has no rows when every record is valid.

Source code in src/phable/xeto_cli.py
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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
def validate(self, recs: list[dict[str, Any]], graph: bool = True) -> Grid:
    """Validate records against Xeto type specifications.

    This method executes the Haxall `xeto validate` command to determine whether
    the provided records conform to their declared Xeto types. It returns a grid
    of validation errors and warnings, including any type mismatches or missing
    required tags.

    **Example:**

    ```python
    from phable import Marker, Number, Ref, XetoCLI

    cli = XetoCLI()
    recs = [
        {
            "dis": "Site 1",
            "site": Marker(),
            "area": Number(1_000, "square_foot"),
            "spec": Ref("ph::Site"),
        }
    ]
    result = cli.validate(recs)
    ```

    Parameters:
        recs:
            List of Haystack record dictionaries to analyze. Each record should
            specify a Xeto type with a `spec` tag.
        graph:
            If `True`, also checks the graph of query references between
            records, such as an equip's required points. Defaults to `True`.

    Returns:
        `Grid` with one row per validation error or warning (columns `id`, `level`,
        `slot`, `msg`). The grid has no rows when every record is valid.
    """

    with tempfile.NamedTemporaryFile(
        mode="w", suffix=f".{self._io_format}", delete=False
    ) as temp_file:
        temp_file.write(self._encoder(recs))
        temp_file.flush()
        temp_file_path = temp_file.name

    try:
        cli_stdout = _exec_localhost_cmd(self._io_format, graph, temp_file_path)
        decoded_str = self._decoder(cli_stdout)
        assert isinstance(decoded_str, Grid)
        return decoded_str
    finally:
        try:
            os.unlink(temp_file_path)
        except OSError:
            pass