| 172 | self.commands = {} |
| 173 | |
| 174 | def collect_commands(self, addon): |
| 175 | for i in dir(addon): |
| 176 | if not i.startswith("__"): |
| 177 | o = getattr(addon, i) |
| 178 | try: |
| 179 | # hasattr is not enough, see https://github.com/mitmproxy/mitmproxy/issues/3794 |
| 180 | is_command = isinstance(getattr(o, "command_name", None), str) |
| 181 | except Exception: |
| 182 | pass # getattr may raise if o implements __getattr__. |
| 183 | else: |
| 184 | if is_command: |
| 185 | try: |
| 186 | self.add(o.command_name, o) |
| 187 | except exceptions.CommandError as e: |
| 188 | logging.warning( |
| 189 | f"Could not load command {o.command_name}: {e}" |
| 190 | ) |
| 191 | |
| 192 | def add(self, path: str, func: Callable): |
| 193 | self.commands[path] = Command(self, path, func) |