Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rename src.ast⇒ast_util, find AST end points with asttokens, move rep…
…orting out of ast_parse and into functions which know src/dst explicitly (don't surmise and confuse the control flow), begin src.editor to execute edit agenda
- Loading branch information
Showing
7 changed files
with
145 additions
and
76 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
from asttokens import ASTTokens | ||
from ast import Import as IType, ImportFrom as IFType, walk | ||
|
||
def get_imports(source_filepath, index_list=None): | ||
with open(source_filepath, 'r') as f: | ||
source = f.read() | ||
fl = source.split('\n') | ||
a = ASTTokens(source, parse=True) | ||
imports = [t for t in walk(a.tree) if type(t) in (IType, IFType)] | ||
if index_list is not None: | ||
imports = [] | ||
for (n, n_i) in index_list: | ||
return [imports[i] for i in index_list] | ||
return imports | ||
|
||
def count_imported_names(nodes): | ||
""" | ||
Return an integer for a single node (0 if not an import statement), | ||
else return a list of integers for a list of AST nodes. | ||
""" | ||
if type(nodes) is not list: | ||
if type(nodes) in [IType, IFType]: | ||
return len(nodes.names) | ||
else: | ||
assert ast.stmt in type(nodes).mro(), f"{nodes} is not an AST statement" | ||
return 0 | ||
counts = [] | ||
for node in nodes: | ||
if type(node) in [IType, IFType]: | ||
c = len(node.names) | ||
counts.append(c) | ||
else: | ||
assert ast.stmt in type(nodes).mro(), f"{nodes} is not an AST statement" | ||
counts.append(0) | ||
return counts | ||
|
||
def locate_import_ends(source_filepath, index_list=None): | ||
ends = [] | ||
nodes = get_imports(source_filepath, index_list) | ||
for n in nodes: | ||
end = {} | ||
end["line"], end["index"] = n.last_token.end[0] | ||
ends.append(end) | ||
return ends |
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
from src.ast_tokens import get_imports, count_imported_names, locate_import_ends | ||
|
||
def edit_defs(src_path, dst_path, imports, defs, edit_agenda): | ||
for mvdef in edit_agenda.get("move"): | ||
# Move the mvdefs out of the source file | ||
pass | ||
for cpdef in edit_agenda.get("copy"): | ||
# Copy the cpdefs over | ||
pass | ||
for rmdef in edit_agenda.get("lose"): | ||
# Just comment out the rmdefs for now | ||
pass | ||
return |