Coverage for slidge/command/__init__.py: 88%

8 statements  

« prev     ^ index     » next       coverage.py v7.15.2, created at 2026-08-18 04:30 +0000

1""" 

2This module implements an unified API to define :term:`adhoc <Ad-hoc Command>` 

3or :term:`chatbot <Chatbot Command>` commands. Subclass a :class:`Command`, 

4:class:`ContactCommand` or :class:`MUCCommand`, and list it in your gateway's 

5:attr:`.BaseGateway.COMMANDS`. 

6""" 

7 

8from types import ModuleType 

9 

10from . import admin, register, user 

11from .base import ( 

12 Command, 

13 CommandAccess, 

14 CommandBase, 

15 CommandResponseType, 

16 Confirmation, 

17 ContactCommand, 

18 Form, 

19 FormField, 

20 MUCCommand, 

21 SearchResult, 

22 TableResult, 

23) 

24 

25BUILTIN_COMMANDS: tuple[type[CommandBase], ...] = ( 

26 admin.ListUsers, 

27 admin.SlidgeInfo, 

28 admin.DeleteUser, 

29 admin.ChangeLoglevel, 

30 admin.Exec, 

31 register.Register, 

32 user.Search, 

33 user.SyncContacts, 

34 user.ListContacts, 

35 user.ListGroups, 

36 user.ListSpaces, 

37 user.ListRoomsInSpace, 

38 user.Login, 

39 user.CreateGroup, 

40 user.Preferences, 

41 user.Unregister, 

42 user.LeaveGroup, 

43 user.InviteInGroups, 

44) 

45""" 

46The commands slidge provides out of the box. They are registered by default 

47via :attr:`.BaseGateway.COMMANDS`. 

48""" 

49 

50 

51def commands_from_module(module: ModuleType) -> tuple[type[CommandBase], ...]: 

52 """Collect all :class:`.CommandBase` subclasses defined in ``module``. 

53 

54 Does not include classes which are imported from another module or abstract classes. 

55 """ 

56 return tuple( 

57 obj 

58 for obj in vars(module).values() 

59 if isinstance(obj, type) 

60 and issubclass(obj, CommandBase) 

61 and obj.__module__ == module.__name__ 

62 and not any(x is NotImplemented for x in (obj.NAME, obj.NODE, obj.CHAT_COMMAND)) 

63 ) 

64 

65 

66__all__ = ( 

67 "BUILTIN_COMMANDS", 

68 "Command", 

69 "CommandAccess", 

70 "CommandBase", 

71 "CommandResponseType", 

72 "Confirmation", 

73 "ContactCommand", 

74 "Form", 

75 "FormField", 

76 "MUCCommand", 

77 "SearchResult", 

78 "TableResult", 

79 "commands_from_module", 

80)