|
13 | 13 | import pathlib as _pathlib |
14 | 14 | import subprocess as _subprocess |
15 | 15 | import sys as _sys |
| 16 | +import tomllib as _tomllib |
16 | 17 |
|
17 | 18 | import setuptools as _setuptools |
18 | 19 |
|
@@ -58,16 +59,69 @@ class CompileProto(_setuptools.Command): |
58 | 59 | ] |
59 | 60 | """Options of the command.""" |
60 | 61 |
|
| 62 | + DEFAULT_OPTIONS: dict[str, str] = { |
| 63 | + "proto_path": "proto", |
| 64 | + "proto_glob": "*.proto", |
| 65 | + "include_paths": "submodules/api-common-protos", |
| 66 | + "py_path": "py", |
| 67 | + } |
| 68 | + |
61 | 69 | def initialize_options(self) -> None: |
62 | 70 | """Initialize options.""" |
63 | | - self.proto_path = "proto" |
64 | | - self.proto_glob = "*.proto" |
65 | | - self.include_paths = "submodules/api-common-protos" |
66 | | - self.py_path = "py" |
| 71 | + options = self._get_options_from_pyproject_toml(self.DEFAULT_OPTIONS) |
| 72 | + |
| 73 | + self.proto_path = options["proto_path"] |
| 74 | + self.proto_glob = options["proto_glob"] |
| 75 | + self.include_paths = options["include_paths"] |
| 76 | + self.py_path = options["py_path"] |
67 | 77 |
|
68 | 78 | def finalize_options(self) -> None: |
69 | 79 | """Finalize options.""" |
70 | 80 |
|
| 81 | + def _get_options_from_pyproject_toml( |
| 82 | + self, defaults: dict[str, str] |
| 83 | + ) -> dict[str, str]: |
| 84 | + """Get the options from the pyproject.toml file. |
| 85 | +
|
| 86 | + The options are read from the `[tool.frequenz-repo-config.setuptools.grpc_tools]` |
| 87 | + section of the pyproject.toml file. |
| 88 | +
|
| 89 | + Args: |
| 90 | + defaults: The default values for the options. |
| 91 | +
|
| 92 | + Returns: |
| 93 | + The options read from the pyproject.toml file. |
| 94 | + """ |
| 95 | + try: |
| 96 | + with _pathlib.Path("pyproject.toml").open("rb") as toml_file: |
| 97 | + pyproject_toml = _tomllib.load(toml_file) |
| 98 | + except FileNotFoundError: |
| 99 | + return defaults |
| 100 | + except (IOError, OSError) as err: |
| 101 | + print(f"WARNING: Failed to load pyproject.toml: {err}") |
| 102 | + return defaults |
| 103 | + |
| 104 | + try: |
| 105 | + config = pyproject_toml["tool"]["frequenz-repo-config"]["setuptools"][ |
| 106 | + "grpc_tools" |
| 107 | + ] |
| 108 | + except KeyError: |
| 109 | + return defaults |
| 110 | + |
| 111 | + known_keys = frozenset(defaults.keys()) |
| 112 | + config_keys = frozenset(config.keys()) |
| 113 | + if unknown_keys := config_keys - known_keys: |
| 114 | + print( |
| 115 | + "WARNING: There are some configuration keys in pyproject.toml we don't " |
| 116 | + "know about and will be ignored: " |
| 117 | + + ", ".join(f"'{k}'" for k in unknown_keys) |
| 118 | + ) |
| 119 | + |
| 120 | + if "include_paths" in config: |
| 121 | + config["include_paths"] = ",".join(config["include_paths"]) |
| 122 | + |
| 123 | + return dict(defaults, **{k: config[k] for k in (known_keys & config_keys)}) |
| 124 | + |
71 | 125 | def run(self) -> None: |
72 | 126 | """Compile the Python protobuf files.""" |
73 | 127 | include_paths = self.include_paths.split(",") |
|
0 commit comments