def visit_py_files(src, visitor): for file in Path(src).glob("**/*.py"): with suppress(SyntaxError): with fopen(file) as f: content = f.read() content = content.replace("\r\n", "\n").replace("\r", "\n").strip() if not content.endswith("\n"): content += "\n" tree = ast.parse(content) visitor.filename = file visitor.visit(tree)
def visit_py_files(src, visitor): for file in find_py_files(src): with suppress(SyntaxError): fname = os.path.join(*file) with fopen(fname) as f: content = f.read() content = content.replace("\r\n", "\n").replace("\r", "\n").strip() if not content.endswith("\n"): content += "\n" tree = ast.parse(content) visitor.filename = file[-1] visitor.visit(tree)
def parse_file(fname): """Parse a python file into an AST. This is a very thin wrapper around ast.parse TODO: Handle encodings other than the default for Python 2 (issue #26) """ try: with fopen(fname) as f: fstr = f.read() except IOError: if fname != 'stdin': raise sys.stdout.write('\nReading from stdin:\n\n') fstr = sys.stdin.read() fstr = fstr.replace('\r\n', '\n').replace('\r', '\n') if not fstr.endswith('\n'): fstr += '\n' return ast.parse(fstr, filename=fname)