|
| 1 | +#@+leo-ver=5-thin |
| 2 | +#@+node:ekr.20251125174431.1: * @file plugins/leo_plugin.py |
| 3 | +"""A plugin for pyflakes that improves testing of ast.ATTRIBUTE nodes.""" |
| 4 | + |
| 5 | +import ast |
| 6 | +import os |
| 7 | + |
| 8 | +#@+others |
| 9 | +#@+node:ekr.20251126064813.1: ** leo_plugin: funcToMethod |
| 10 | +def funcToMethod(f: Callable, theClass: object, name: str = None) -> None: |
| 11 | + """ |
| 12 | + From the Python Cookbook... |
| 13 | +
|
| 14 | + The following method allows you to add a function as a method of |
| 15 | + any class. That is, it converts the function to a method of the |
| 16 | + class. The method just added is available instantly to all |
| 17 | + existing instances of the class, and to all instances created in |
| 18 | + the future. |
| 19 | +
|
| 20 | + The function's first argument should be self. |
| 21 | +
|
| 22 | + The newly created method has the same name as the function unless |
| 23 | + the optional name argument is supplied, in which case that name is |
| 24 | + used as the method name. |
| 25 | + """ |
| 26 | + setattr(theClass, name or f.__name__, f) |
| 27 | + |
| 28 | +#@+node:ekr.20251126060205.1: ** leo_plugin: patched_ATTRIBUTE |
| 29 | +def patched_ATTRIBUTE(self, node) -> None: |
| 30 | + if isinstance(node.ctx, ast.Load): |
| 31 | + if isinstance(node.value, ast.Name): |
| 32 | + if 1: ### Don't put this in production code! |
| 33 | + print(f"patched_ATTRIBUTE: load {node.value.id}.{node.attr}") |
| 34 | + self.handleChildren(node) |
| 35 | +#@+node:ekr.20251126054330.1: ** leo_plugin: register |
| 36 | +def register(pyflakes) -> None: |
| 37 | + """Register the leo_plugin plugin.""" |
| 38 | + if 0: |
| 39 | + path, extension = os.path.splitext(__file__) |
| 40 | + print(f"V6: {os.path.basename(path)}.register: pyflakes: {pyflakes!r}") |
| 41 | + |
| 42 | + # Patch pyflakes.ATTRIBUTE. |
| 43 | + funcToMethod(patched_ATTRIBUTE, pyflakes.__class__, 'ATTRIBUTE') |
| 44 | +#@-others |
| 45 | +#@-leo |
0 commit comments