Skip to content

GetAllCommandsService

GetAllCommandsService

Bases: ServiceInterface

Get all commands tool.

Source code in myalias/core/services/get_all_command_service.py
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
class GetAllCommandsService(ServiceInterface):
    """Get all commands tool."""

    def execute(self):
        """
        Get all commands file in path myalias/commands.

        Returns:
            List of commands.

        """

        commands = []

        for root, dirs, files in os.walk('myalias/commands'):
            for file in files:
                if file.endswith('.py'):
                    file_path = os.path.join(root, file)
                    module_name = os.path.splitext(file_path)[0].replace(
                        '/', '.'
                    )
                    try:
                        module = importlib.import_module(module_name)
                        for attr_name in dir(module):
                            attr = getattr(module, attr_name)
                            if (
                                isinstance(attr, type)
                                and issubclass(attr, CommandInterface)
                                and attr != CommandInterface
                            ):
                                name = attr_name.replace('Command', '')
                                name = ''.join(
                                    x if x.islower() else '-' + x.lower()
                                    for x in name
                                ).lstrip('-')

                                description = attr.__doc__
                                objCommand = {
                                    'name': name,
                                    'description': description,
                                    'args': attr.execute.__code__.co_varnames,
                                    'instance': attr,
                                }
                                commands.append(objCommand)
                    except ImportError:
                        pass

        return commands

execute()

Get all commands file in path myalias/commands.

Returns:

Type Description

List of commands.

Source code in myalias/core/services/get_all_command_service.py
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
def execute(self):
    """
    Get all commands file in path myalias/commands.

    Returns:
        List of commands.

    """

    commands = []

    for root, dirs, files in os.walk('myalias/commands'):
        for file in files:
            if file.endswith('.py'):
                file_path = os.path.join(root, file)
                module_name = os.path.splitext(file_path)[0].replace(
                    '/', '.'
                )
                try:
                    module = importlib.import_module(module_name)
                    for attr_name in dir(module):
                        attr = getattr(module, attr_name)
                        if (
                            isinstance(attr, type)
                            and issubclass(attr, CommandInterface)
                            and attr != CommandInterface
                        ):
                            name = attr_name.replace('Command', '')
                            name = ''.join(
                                x if x.islower() else '-' + x.lower()
                                for x in name
                            ).lstrip('-')

                            description = attr.__doc__
                            objCommand = {
                                'name': name,
                                'description': description,
                                'args': attr.execute.__code__.co_varnames,
                                'instance': attr,
                            }
                            commands.append(objCommand)
                except ImportError:
                    pass

    return commands