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

1 import json

2

3 from rich.tree import Tree

4 from rich.syntax import Syntax

5

6 from ... import T

7 from .base import Completable

8 from .base_parser import ParserCommand

9 from .utils import print_records

10

11 class RoleCommand(ParserCommand):

12 name = 'role'

13 description = T('Role operations')

14

15 def add_subcommands(self, subparsers):

16 # List subcommand

17 subparsers.add_parser('list', help=T('List available roles'))

18

19 # Show subcommand

20 show_parser = subparsers.add_parser('show', help=T('Show role details'))

21 show_parser.add_argument('role', type=str, help=T('Role name'))

22

23 # Use subcommand

24 use_parser = subparsers.add_parser('use', help=T('Use a role'))

25 use_parser.add_argument('role', type=str, help=T('Role name'))

26

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

28 if subcommand in ['show', 'use'] and arg.name == 'role':

29 ctx = self.manager.context

30 return [Completable(name, role.short) for name, role in ctx.tm.role_manager.roles.items()]

31 return super().get_arg_values(arg, subcommand)

32

33 def cmd_list(self, args, ctx):

34 rows = ctx.tm.list_roles()

35 print_records(rows)

36

37 def cmd_show(self, args, ctx):

38 role_name = args.role.lower()

39 role = ctx.tm.role_manager.roles.get(role_name)

40 if not role:

41 self.log.error(T('Role not found').format(args.role))

42 return

43

44 from rich.table import Table

45 from rich import print

46

47 # 基本信息表格

48 basic_table = Table(title=f"{T('Role Information')}: {role.name}", show_lines=True)

49 basic_table.add_column(T('Property'), style="bold cyan", justify="right")

50 basic_table.add_column(T('Value'), style="bold white", justify="left")

51

52 basic_table.add_row(T('Role name'), role.name)

53 basic_table.add_row(T('Role description'), role.short)

54 basic_table.add_row(T('Tips count'), str(len(role.tips)))

55 basic_table.add_row(T('Environment variables count'), str(len(role.envs)))

56 basic_table.add_row(T('Package dependencies count'), str(len(role.packages)))

57 basic_table.add_row(T('Plugins count'), str(len(role.plugins)))

58

59 print(basic_table)

60

61 # 详细描述

62 if role.detail:

63 from rich.panel import Panel

64 detail_panel = Panel(role.detail, title=T('Role detail'), border_style="blue")

65 print(detail_panel)

66

67 # 提示信息表格

68 if role.tips:

69 tips_table = Table(title=T('Tips'), show_lines=True)

70 tips_table.add_column(T('Name'), style="bold green", justify="left")

71 tips_table.add_column(T('Description'), style="bold white", justify="left")

72

73 for tip_name, tip in role.tips.items():

74 tips_table.add_row(tip_name, tip.short)

75

76 print(tips_table)

77

78 # 环境变量表格

79 if role.envs:

80 env_table = Table(title=T('Environment variables'), show_lines=True)

81 env_table.add_column(T('Name'), style="bold yellow", justify="left")

82 env_table.add_column(T('Description'), style="bold white", justify="left")

83 env_table.add_column(T('Value'), style="dim", justify="left")

84

85 for env_name, (value, desc) in role.envs.items():

86 # 截断过长的值

87 display_value = value[:50] + "..." if len(value) > 50 else value

88 env_table.add_row(env_name, desc, display_value)

89

90 print(env_table)

91

92 # 包依赖表格

93 if role.packages:

94 pkg_table = Table(title=T('Package dependencies'), show_lines=True)

95 pkg_table.add_column(T('Language'), style="bold magenta", justify="left")

96 pkg_table.add_column(T('Packages'), style="bold white", justify="left")

97

98 for lang, packages in role.packages.items():

99 pkg_table.add_row(lang, ', '.join(packages))

100

101 print(pkg_table)

102

103 # 插件表格

104 if role.plugins:

105 tree = Tree(T('Plugins'))

106 for plugin_name, plugin_config in role.plugins.items():

107 t = tree.add(plugin_name)

108 t.add(Syntax(json.dumps(plugin_config, ensure_ascii=False, indent=2), "json", word_wrap=True))

109

110 print(tree)

111

112 def cmd_use(self, args, ctx):

113 success = ctx.tm.use(role=args.role)

114 if success:

115 self.log.info(f'Use {args.role} role')

116 else:

117 self.log.error(T('Failed to use role').format(args.role))

118

119 def cmd(self, args, ctx):

120 self.cmd_list(args, ctx)