|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | +__author__ = "heider" |
| 5 | +__doc__ = r""" |
| 6 | +
|
| 7 | + TODO: IMPLEMENT |
| 8 | +
|
| 9 | + Created on 9/2/22 |
| 10 | + """ |
| 11 | + |
| 12 | +__all__ = ["recursive_detect_import_aliasing"] |
| 13 | + |
| 14 | +from pathlib import Path |
| 15 | +from typing import Iterable, Callable, Optional, Sequence, Mapping, List |
| 16 | + |
| 17 | +from warg.os_utilities.filtering import negate, is_python_module, is_python_package |
| 18 | + |
| 19 | + |
| 20 | +def has_import_aliases(path: Path, *, verbose: bool = False) -> bool: |
| 21 | + """ |
| 22 | +
|
| 23 | + :param verbose: |
| 24 | + :type verbose: |
| 25 | + :param path: |
| 26 | + :type path: |
| 27 | + :return: |
| 28 | + :rtype: |
| 29 | + """ |
| 30 | + if path.is_file(): |
| 31 | + with open(path) as f: |
| 32 | + for ln, l in enumerate(f.readlines()): |
| 33 | + if "import" in l and "as" in l: |
| 34 | + if verbose: |
| 35 | + print(f"Found alias at line {ln}: {l}") |
| 36 | + return True |
| 37 | + return False |
| 38 | + |
| 39 | + |
| 40 | +def recursive_detect_import_aliasing( |
| 41 | + path: Path, |
| 42 | + file_extensions: Sequence[str] = (".py", ".txt", ".yaml"), |
| 43 | + *, |
| 44 | + exclusion_filter: Optional[Iterable[Callable]] = (negate(is_python_package),), |
| 45 | + verbose: bool = True, |
| 46 | +) -> None: |
| 47 | + """we do not like them!""" |
| 48 | + path = Path(path) |
| 49 | + file_extensions = [f".{f.lstrip('.')}" for f in file_extensions] |
| 50 | + |
| 51 | + for child in path.iterdir(): |
| 52 | + if child.is_dir(): |
| 53 | + if exclusion_filter is None or not any( |
| 54 | + flt(child) for flt in exclusion_filter |
| 55 | + ): |
| 56 | + recursive_detect_import_aliasing( |
| 57 | + child, |
| 58 | + file_extensions=file_extensions, |
| 59 | + exclusion_filter=exclusion_filter, |
| 60 | + verbose=verbose, |
| 61 | + ) |
| 62 | + else: |
| 63 | + if verbose: |
| 64 | + print( |
| 65 | + f"{child} was excluded, filters:\n{[(flt.__name__, flt(child)) for flt in exclusion_filter]}" |
| 66 | + ) |
| 67 | + elif child.is_file(): |
| 68 | + if child.suffix in file_extensions: |
| 69 | + if has_import_aliases(child, verbose=verbose): |
| 70 | + print(child) |
| 71 | + |
| 72 | + |
| 73 | +COMMON_ALIAS_REPLACEMENTS = { |
| 74 | + "plt.": "pyplot.", |
| 75 | + "import matplotlib.pyplot as plt": "from matplotlib import pyplot", |
| 76 | + "np.": "numpy.", |
| 77 | + "import numpy as np": "import numpy", |
| 78 | +} |
| 79 | + |
| 80 | + |
| 81 | +def auto_replace_aliases( |
| 82 | + path: Path, |
| 83 | + replacement_mapping: Mapping = COMMON_ALIAS_REPLACEMENTS, |
| 84 | + *, |
| 85 | + verbose: bool = False, |
| 86 | +) -> List[str]: |
| 87 | + """ |
| 88 | + replaces plt. with pyplot. and changes import to from matplotlib import pyplot. for numpy numpy. becomes numpy. and so on |
| 89 | + """ |
| 90 | + path = Path(path) |
| 91 | + |
| 92 | + working_copy = [] |
| 93 | + |
| 94 | + if path.is_file(): |
| 95 | + with open(path) as f: |
| 96 | + for ln, line in enumerate(f.readlines()): |
| 97 | + line_copy = line |
| 98 | + for k, v in COMMON_ALIAS_REPLACEMENTS.items(): |
| 99 | + if k in line: |
| 100 | + if verbose: |
| 101 | + print(f"Found {k} at line {ln}: {line}") |
| 102 | + line_copy = line_copy.replace(k, v) |
| 103 | + if verbose: |
| 104 | + print(f"{line} -> {line_copy}") |
| 105 | + working_copy.append(line_copy) |
| 106 | + return working_copy |
| 107 | + |
| 108 | + |
| 109 | +if __name__ == "__main__": |
| 110 | + recursive_detect_import_aliasing(Path(__file__).parent.parent / "exclude") |
| 111 | + |
| 112 | + import numpy as np # for demonstration |
| 113 | + import matplotlib.pyplot as plt |
| 114 | + |
| 115 | + print(plt.__doc__[0]) |
| 116 | + print(np.__version__) |
| 117 | + |
| 118 | + print(auto_replace_aliases(Path(__file__))) |
0 commit comments