• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2024 The Bazel Authors. All rights reserved.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#    http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14"""Implementation of py_api."""
15
16_PY_COMMON_API_LABEL = Label("//python/private/api:py_common_api")
17
18ApiImplInfo = provider(
19    doc = "Provider to hold an API implementation",
20    fields = {
21        "impl": """
22:type: struct
23
24The implementation of the API being provided. The object it contains
25will depend on the target that is providing the API struct.
26""",
27    },
28)
29
30def _py_common_get(ctx):
31    """Get the py_common API instance.
32
33    NOTE: to use this function, the rule must have added `py_common.API_ATTRS`
34    to its attributes.
35
36    Args:
37        ctx: {type}`ctx` current rule ctx
38
39    Returns:
40        {type}`PyCommonApi`
41    """
42
43    # A generic provider is used to decouple the API implementations from
44    # the loading phase of the rules using an implementation.
45    return ctx.attr._py_common_api[ApiImplInfo].impl
46
47py_common = struct(
48    get = _py_common_get,
49    API_ATTRS = {
50        "_py_common_api": attr.label(
51            default = _PY_COMMON_API_LABEL,
52            providers = [ApiImplInfo],
53        ),
54    },
55)
56