Created
August 18, 2019 00:01
-
-
Save zhammer/4d8e8d3bea2f4da1ad7a842b0ed281c8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import re | |
import sys | |
from pathlib import Path | |
from typing import List, Tuple | |
import libcst as cst | |
from libcst import CSTVisitorT | |
class UrlLibTransformer(cst.CSTTransformer): | |
def visit_Import(self, node: cst.Import) -> None: | |
print("import", [name.name.value for name in node.names]) | |
def leave_Import(self, node: cst.Import, updated_node: cst.Import) -> cst.Import: | |
return updated_node | |
def visit_ImportFrom(self, node: cst.ImportFrom) -> None: | |
print("from", node.module.value, "import", [name.name.value for name in node.names]) | |
def leave_ImportFrom(self, node: cst.ImportFrom, updated_node: cst.ImportFrom) -> cst.ImportFrom: | |
return updated_node | |
def lint_file(visitor: CSTVisitorT, filename: str) -> None: | |
with open(filename, 'r') as python_file: | |
python_source = python_file.read() | |
source_tree = cst.parse_module(python_source) | |
visited_tree = source_tree.visit(visitor) | |
if not visited_tree.deep_equals(source_tree): | |
with open(filename, 'w') as python_file: | |
python_file.write(visited_tree.code) | |
def collect_files(base: str) -> Tuple[str, ...]: | |
def is_python_file(path: str): | |
return os.path.isfile(path) and re.search(r"\.pyi?$", path) | |
if is_python_file(base): | |
return (base,) | |
if os.path.isdir(base): | |
python_files: List[str] = [] | |
for root, dirs, filenames in os.walk(base): | |
full_filenames = (f"{root}/{filename}" for filename in filenames) | |
python_files += [full_filename for full_filename in full_filenames if is_python_file(full_filename)] | |
return tuple(python_files) | |
return tuple() | |
if __name__ == '__main__': | |
base = sys.argv[1] | |
python_files = collect_files(base) | |
for python_file in python_files: | |
lint_file(UrlLibTransformer(), python_file) |
Author
zhammer
commented
Aug 18, 2019
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment