Find deprecated escape sequences. Checks for deprecated escape sequences in ``*.py files``. If `root` is a file, that file is checked, if `root` is a directory all ``*.py`` files found in a recursive descent are checked. If a deprecated escape sequence is found, the file and line w
(root)
| 10 | |
| 11 | |
| 12 | def main(root): |
| 13 | """Find deprecated escape sequences. |
| 14 | |
| 15 | Checks for deprecated escape sequences in ``*.py files``. If `root` is a |
| 16 | file, that file is checked, if `root` is a directory all ``*.py`` files |
| 17 | found in a recursive descent are checked. |
| 18 | |
| 19 | If a deprecated escape sequence is found, the file and line where found is |
| 20 | printed. Note that for multiline strings the line where the string ends is |
| 21 | printed and the error(s) are somewhere in the body of the string. |
| 22 | |
| 23 | Parameters |
| 24 | ---------- |
| 25 | root : str |
| 26 | File or directory to check. |
| 27 | Returns |
| 28 | ------- |
| 29 | None |
| 30 | |
| 31 | """ |
| 32 | import ast |
| 33 | import tokenize |
| 34 | import warnings |
| 35 | from pathlib import Path |
| 36 | |
| 37 | count = 0 |
| 38 | base = Path(root) |
| 39 | paths = base.rglob("*.py") if base.is_dir() else [base] |
| 40 | for path in paths: |
| 41 | # use tokenize to auto-detect encoding on systems where no |
| 42 | # default encoding is defined (e.g. LANG='C') |
| 43 | with tokenize.open(str(path)) as f: |
| 44 | with warnings.catch_warnings(record=True) as w: |
| 45 | warnings.simplefilter('always') |
| 46 | tree = ast.parse(f.read()) |
| 47 | if w: |
| 48 | print("file: ", str(path)) |
| 49 | for e in w: |
| 50 | print('line: ', e.lineno, ': ', e.message) |
| 51 | print() |
| 52 | count += len(w) |
| 53 | print("Errors Found", count) |
| 54 | |
| 55 | |
| 56 | if __name__ == "__main__": |
no test coverage detected