Coverage for aipyapp/cli/command/cmd_plugin.py: 0%

1

2 from ... import T

3 from .base import CommandMode, Completable

4 from .base_parser import ParserCommand

5 from .utils import print_table

6

7 class Command(ParserCommand):

8 name = 'plugin'

9 description = T('Plugin operations')

10 modes = [CommandMode.MAIN, CommandMode.TASK]

11

12 def add_subcommands(self, subparsers):

13 subparsers.add_parser('list', help=T('List available plugins'))

14

15 parser = subparsers.add_parser('show', help=T('Show plugin details'))

16 parser.add_argument('name', type=str, help=T('Plugin name'))

17

18 def get_subcommands(self):

19 if self.manager.is_main_mode():

20 subcommands = self.subcommands.copy()

21 subcommands.pop('show')

22 return subcommands

23 return super().get_subcommands()

24

25 def get_arg_values(self, arg, subcommand=None, partial_value=''):

26 if arg.name == 'name':

27 ctx = self.manager.context

28 return [Completable(plugin.name, plugin.description) for plugin in ctx.tm.plugin_manager]

29 return super().get_arg_values(arg, subcommand, partial_value)

30

31 def cmd_list(self, args, ctx):

32 """列出可用的插件"""

33 rows = []

34 for plugin in ctx.tm.plugin_manager:

35 rows.append([plugin.name, plugin.description, plugin.get_type().name, plugin.version, plugin.author])

36 print_table(rows, headers=[T('Name'), T('Description'), T('Type'), T('Version'), T('Author')], title=T('Available Plugins'))

37

38 def _get_first_line(self, doc: str) -> str:

39 """Get the first non-empty line of the docstring"""

40 return next((line for line in doc.split('\n') if line.strip()), '')

41

42 def cmd_show(self, args, ctx):

43 """显示插件详情"""

44 task = ctx.task

45 if not task:

46 ctx.console.print(T('Task mode only'), style='yellow')

47 return

48 plugin = task.plugins.get(args.name)

49 if not plugin:

50 ctx.console.print(f"[red]{T('Plugin not found')}: {args.name}[/red]")

51 return

52

53 rows = []

54 for name, func in plugin.get_handlers().items():

55 rows.append([T('Event'), name, self._get_first_line(func.__doc__)])

56 for name, func in plugin.get_functions().items():

57 rows.append([T('Function'), name, self._get_first_line(func.__doc__)])

58 print_table(rows, headers=[T('Type'), T('Name'), T('Description')], title=T('Plugin Details'))

59

60 def cmd(self, args, ctx):

61 self.cmd_list(args, ctx)