Skip to content

Commit abca125

Browse files
phlogistonjohnmergify[bot]
authored andcommitted
sambacc/commands: add include function to help clarify import reasons
Avoid importing with noqa comments when importing a file just for the commands it provides. Add a function to "include" (aka import for commands) to the command builder type. Signed-off-by: John Mulligan <[email protected]>
1 parent fbed586 commit abca125

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

sambacc/commands/cli.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
from collections import namedtuple
2020
import argparse
21+
import importlib
22+
import inspect
2123
import logging
2224
import typing
2325

@@ -174,6 +176,30 @@ def dict(self) -> dict[str, Command]:
174176
"""Return a dict mapping command names to Command object."""
175177
return {c.name: c for c in self._commands}
176178

179+
def include(
180+
self, modname: str, *, package: str = "", check: bool = True
181+
) -> None:
182+
"""Import a python module to add commands to this command builder.
183+
If check is true and no new commands are added by the import, raise an
184+
error.
185+
"""
186+
if modname.startswith(".") and not package:
187+
package = "sambacc.commands"
188+
mod = importlib.import_module(modname, package=package)
189+
if not check:
190+
return
191+
loaded_fns = {c.cmd_func for c in self._commands}
192+
mod_fns = {fn for _, fn in inspect.getmembers(mod, inspect.isfunction)}
193+
if not mod_fns.intersection(loaded_fns):
194+
raise Fail(f"import from {modname} did not add any new commands")
195+
196+
def include_multiple(
197+
self, modnames: typing.Iterable[str], *, package: str = ""
198+
) -> None:
199+
"""Run the include function on multiple module names."""
200+
for modname in modnames:
201+
self.include(modname, package=package)
202+
177203

178204
class Context(typing.Protocol):
179205
"""Protocol type for CLI Context.

0 commit comments

Comments
 (0)