scripts: Add rewrite_find_enums.py from issue

This commit is contained in:
Florian Bruhin 2022-04-19 18:51:44 +02:00
parent ef9e4a6663
commit d30afc3b0d
1 changed files with 39 additions and 0 deletions

View File

@ -0,0 +1,39 @@
import sys
import pathlib
from typed_ast import ast3
def add_parents(tree):
for node in ast3.walk(tree):
for child in ast.iter_child_nodes(node):
child.parent = node
def find_enums(tree):
for node in ast3.walk(tree):
if not isinstance(node, ast3.Assign):
continue
if node.type_comment is None:
continue
if '.' not in node.type_comment:
continue
if not node.type_comment.startswith("'"):
continue
comment = node.type_comment.strip("'")
mod, cls = comment.rsplit(".", maxsplit=1)
assert len(node.targets) == 1
name = node.targets[0].id
yield (mod, cls, name)
def main():
for filename in sys.argv[1:]:
tree = ast3.parse(pathlib.Path(filename).read_text())
for mod, cls, name in find_enums(tree):
old = f"{mod}.{name}"
new = f"{mod}.{cls}.{name}"
print(f"{old} {new}")
if __name__ == '__main__':
main()