Exemplo n.º 1
0
    def __init__(self):
        # Dynamically constructs the JSON schema and the Draft7Validator used in the CommandParser
        # IMPORTANT: validate schema with Draft7Validator.check_schema(schema) before using it!

        inflector = Inflector()
        commands = 'commands'
        current_path = dirname(abspath(__file__))
        excluded_files = ['__init__.py']
        modules = [
            file_name[:-3] for file_name in listdir(commands)
            if isfile(join(join(current_path, commands), file_name))
            and file_name not in excluded_files
        ]
        schema = {
            "$schema": "https://json-schema.org/schema#",
            "type": "object",
            "properties": {
                "name": {
                    "type": "string",
                    "enum": modules
                },
                "args": {
                    "type": "object"
                }
            },
            "required": ["name"]
        }
        classes = dict()
        for module_name in modules:
            classes[module_name] = getattr(
                import_module(f'{commands}.{module_name}'),
                inflector.camelize(module_name))
        self.validator = Draft7Validator(schema)
        self.classes = classes