Coverage for src/su6/plugins.py: 100%

1"""

2 Provides a register decorator for third party plugins, and a `include_plugins` (used in cli.py) that loads them.

3"""

4 import typing

5 from dataclasses import dataclass

6 from importlib.metadata import EntryPoint, entry_points

7

8 from rich import print

9 from typer import Typer

10

11 from .core import (

12 AbstractConfig,

13 ApplicationState,

14 T_Command,

15 print_json,

16 run_tool,

17 state,

18 with_exit_code,

19)

20

21 __all__ = ["register", "run_tool", "PluginConfig", "print", "print_json"]

22

23

24 class PluginConfig(AbstractConfig):

25 """

26 Can be inherited in plugin to load in plugin-specific config.

27

28 The config class is a Singleton, which means multiple instances can be created, but they always have the same state.

29

30 Example:

31 @register()

32 class DemoConfig(PluginConfig):

33 required_arg: str

34 boolean_arg: bool

35 # ...

36

37

38 config = DemoConfig()

39

40 @register()

41 def with_arguments(required_arg: str, boolean_arg: bool = False) -> None:

42 config.update(required_arg=required_arg, boolean_arg=boolean_arg)

43 print(config)

44 """

45

46 extras: dict[str, typing.Any]

47 state: typing.Optional[ApplicationState] # only with @register(with_state=True) or after self.attach_state

48

49 def __init__(self, **kw: typing.Any) -> None:

50 """

51 Initial variables can be passed on instance creation.

52 """

53 super().__init__()

54 self.update(**kw)

55 self.extras = {}

56

57 def attach_extra(self, name: str, obj: typing.Any) -> None:

58 """

59 Add a non-annotated variable.

60 """

61 self.extras[name] = obj

62

63 def attach_state(self, global_state: ApplicationState) -> None:

64 """

65 Connect the global state to the plugin config.

66 """

67 self.state = global_state

68 self.attach_extra("state", global_state)

69

70 def _fields(self) -> typing.Generator[str, typing.Any, None]:

71 yield from self.__annotations__.keys()

72 if self.extras:

73 yield from self.extras.keys() # -> self.state in _values

74

75 def _get(self, key: str, strict: bool = True) -> typing.Any:

76 notfound = object()

77

78 value = getattr(self, key, notfound)

79 if value is not notfound:

80 return value

81

82 if self.extras:

83 value = self.extras.get(key, notfound)

84 if value is not notfound:

85 return value

86

87 if strict:

88 msg = f"{key} not found in `self {self.__class__.__name__}`"

89 if self.extras:

90 msg += f" or `extra's {self.extras.keys()}`"

91 raise KeyError(msg)

92

93 def _values(self) -> typing.Generator[typing.Any, typing.Any, None]:

94 yield from (self._get(k, False) for k in self._fields())

95

96 def __repr__(self) -> str:

97 """

98 Create a readable representation of this class with its data.

99

100 Stolen from dataclasses._repr_fn.

101 """

102 fields = self._fields()

103 values = self._values()

104 args = ", ".join([f"{f}={v!r}" for f, v in zip(fields, values)])

105 name = self.__class__.__qualname__

106 return f"{name}({args})"

107

108 def __str__(self) -> str:

109 """

110 Alias for repr.

111 """

112 return repr(self)

113

114

115 T_PluginConfig = typing.Type[PluginConfig]

116

117 U_Wrappable = typing.Union[T_PluginConfig, T_Command]

118 T_Wrappable = typing.TypeVar("T_Wrappable", T_PluginConfig, T_Command)

119

120

121@dataclass()

122 class Registration(typing.Generic[T_Wrappable]):

123 wrapped: T_Wrappable

124 args: tuple[typing.Any, ...]

125 kwargs: dict[str, typing.Any]

126

127 @property

128 def what(self) -> typing.Literal["command", "config"] | None:

129 if isinstance(self.wrapped, type) and issubclass(self.wrapped, PluginConfig):

130 return "config"

131 elif callable(self.wrapped):

132 return "command"

133

134

135# WeakValueDictionary() does not work since it removes the references too soon :(

136 registrations: dict[int, Registration[T_PluginConfig] | Registration[T_Command]] = {}

137

138

139 def _register(wrapped: T_Wrappable, *a: typing.Any, **kw: typing.Any) -> T_Wrappable:

140 registrations[id(wrapped)] = Registration(wrapped, a, kw)

141 return wrapped

142

143

144@typing.overload

145 def register(wrappable: T_Wrappable, *a_outer: typing.Any, **kw_outer: typing.Any) -> T_Wrappable:

146 """

147 If wrappable is passed, it returns the same type.

148

149 @register

150 def func(): ...

151

152 -> register(func) is called

153 """

154

155

156@typing.overload

157 def register(

158 wrappable: None = None, *a_outer: typing.Any, **kw_outer: typing.Any

159) -> typing.Callable[[T_Wrappable], T_Wrappable]:

160 """

161 If wrappable is None (empty), it returns a callback that will wrap the function later.

162

163 @register()

164 def func(): ...

165

166 -> register() is called

167 """

168

169

170 def register(

171 wrappable: T_Wrappable = None, *a_outer: typing.Any, **kw_outer: typing.Any

172) -> T_Wrappable | typing.Callable[[T_Wrappable], T_Wrappable]:

173 """

174 Register a top-level Plugin command or a Plugin Config.

175

176 Examples:

177 @register() # () are optional, but you can add Typer keyword arguments if needed.

178 def command():

179 # 'su6 command' is now registered!

180 ...

181

182 @register() # () are optional, but extra keyword arguments can be passed to configure the config.

183 class MyConfig(PluginConfig):

184 property: str

185 """

186

187 def inner(func: T_Wrappable) -> T_Wrappable:

188 return _register(func, *a_outer, **kw_outer)

189

190 if wrappable:

191 return inner(wrappable)

192 else:

193 return inner

194

195

196 T = typing.TypeVar("T")

197

198

199 class BoundMethodOf(typing.Protocol[T]):

200 """

201 Protocol to define properties that a bound method has.

202 """

203

204 __self__: T

205 __name__: str # noqa: A003 - property does exist on the class

206 __doc__: typing.Optional[str] # noqa: A003 - property does exist on the class

207

208 def __call__(self, a: int) -> str: # pragma: no cover

209 """

210 Indicates this Protocol type can be called.

211 """

212

213

214 Unbound = typing.Callable[..., typing.Any]

215

216

217 def unbind(meth: BoundMethodOf[typing.Any] | Unbound) -> typing.Optional[Unbound]:

218 """

219 Extract the original function (which has a different id) from a class method.

220 """

221 return getattr(meth, "__func__", None)

222

223

224@dataclass()

225 class PluginLoader:

226 app: Typer

227 with_exit_code: bool

228

229 def main(self) -> None:

230 """

231 Using importlib.metadata, discover available su6 plugins.

232

233 Example:

234 # pyproject.toml

235 # https://packaging.python.org/en/latest/guides/creating-and-discovering-plugins/#using-package-metadata

236 [project.entry-points."su6"]

237 demo = "su6_plugin_demo.cli" # <- CHANGE ME

238 """

239 discovered_plugins = entry_points(group="su6")

240 for plugin in discovered_plugins: # pragma: nocover

241 self._load_plugin(plugin)

242

243 self._cleanup()

244

245 def _cleanup(self) -> None:

246 # registrations.clear()

247 ...

248

249 def _load_plugin(self, plugin: EntryPoint) -> list[str]:

250 """

251 Look for typer instances and registered commands and configs in an Entrypoint.

252

253 [project.entry-points."su6"]

254 demo = "su6_plugin_demo.cli"

255

256 In this case, the entrypoint 'demo' is defined and points to the cli.py module,

257 which gets loaded with `plugin.load()` below.

258 """

259 result = []

260 plugin_module = plugin.load()

261

262 for item in dir(plugin_module):

263 if item.startswith("_"):

264 continue

265

266 possible_command = getattr(plugin_module, item)

267

268 # get method by id (in memory) or first unbind from class and then get by id

269 registration = registrations.get(id(possible_command)) or registrations.get(id(unbind(possible_command)))

270

271 if isinstance(possible_command, Typer):

272 result += self._add_subcommand(plugin.name, possible_command, plugin_module.__doc__)

273 elif registration and registration.what == "command":

274 result += self._add_command(plugin.name, typing.cast(Registration[T_Command], registration))

275 elif registration and registration.what == "config":

276 result += self._add_config(plugin.name, typing.cast(Registration[T_PluginConfig], registration))

277 # else: ignore

278

279 return result

280

281 def _add_command(self, _: str, registration: Registration[T_Command]) -> list[str]:

282 """

283 When a Command Registration is found, it is added to the top-level namespace.

284 """

285 if self.with_exit_code:

286 registration.wrapped = with_exit_code()(registration.wrapped)

287 # adding top-level commands

288 self.app.command(*registration.args, **registration.kwargs)(registration.wrapped)

289 return [f"command {_}"]

290

291 def _add_config(self, name: str, registration: Registration[T_PluginConfig]) -> list[str]:

292 """

293 When a Config Registration is found, the Singleton data is updated with config from pyproject.toml.

294

295 Example:

296 # pyproject.toml

297 [tool.su6.demo]

298 boolean-arg = true

299 optional-with-default = "overridden"

300

301 [tool.su6.demo.extra]

302 more = true

303 """

304 key = registration.kwargs.pop("config_key", name)

305

306 cls = registration.wrapped

307 inst = cls()

308

309 if registration.kwargs.pop("with_state", False):

310 inst.attach_state(state)

311

312 if registration.kwargs.pop("strict", True) is False:

313 inst._strict = False

314

315 state.attach_plugin_config(key, inst)

316 return [f"config {name}"]

317

318 def _add_subcommand(self, name: str, subapp: Typer, doc: str) -> list[str]:

319 self.app.add_typer(subapp, name=name, help=doc)

320 return [f"subcommand {name}"]

321

322

323 def include_plugins(app: Typer, _with_exit_code: bool = True) -> None:

324 """

325 Discover plugins using discover_plugins and add them to either global namespace or as a subcommand.

326

327 Args:

328 app: the top-level Typer app to append commands to

329 state: top-level application state

330 _with_exit_code: should the @with_exit_code decorator be applied to the return value of the command?

331 """

332 loader = PluginLoader(app, _with_exit_code)

333 loader.main()

334

335

336# todo:

337# - add to 'all'

338# - add to 'fix'