Public Access
extractor erstmal fertig
- er findet fast alle tabellen - quellcode wird entpackt wenn er in einer variable ist
This commit is contained in:
+31
-8
@@ -1,5 +1,6 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import re
|
||||
import ast
|
||||
import argparse
|
||||
import json
|
||||
@@ -28,10 +29,13 @@ DEFAULT_OUTPUT = "data"
|
||||
|
||||
class RegisterExtractor(ast.NodeVisitor):
|
||||
|
||||
def __init__(self, filename):
|
||||
def __init__(self, filename, source):
|
||||
self.filename = filename
|
||||
self.source = source
|
||||
self.results = []
|
||||
self.model_names = {}
|
||||
self.table_vars = {}
|
||||
self.alias_vars = {}
|
||||
|
||||
def visit_Module(self, node):
|
||||
for item in node.body:
|
||||
@@ -103,8 +107,22 @@ class RegisterExtractor(ast.NodeVisitor):
|
||||
continue
|
||||
|
||||
table = self.resolve_table_call(node.value)
|
||||
|
||||
if table:
|
||||
self.table_vars[target.id] = table
|
||||
elif (
|
||||
isinstance(node.value, ast.Call)
|
||||
and isinstance(node.value.func, ast.Attribute)
|
||||
):
|
||||
table = self.resolve_table_object(node.value.func.value)
|
||||
if table:
|
||||
self.table_vars[target.id] = table
|
||||
|
||||
# Ausdruck für spätere Auflösung merken
|
||||
self.alias_vars[target.id] = ast.get_source_segment(
|
||||
self.source, node.value
|
||||
)
|
||||
|
||||
elif isinstance(node.value, ast.Constant):
|
||||
self.table_vars[target.id] = node.value.value
|
||||
|
||||
@@ -256,9 +274,6 @@ class RegisterExtractor(ast.NodeVisitor):
|
||||
if condition:
|
||||
op["condition"] = condition
|
||||
operations.append(op)
|
||||
|
||||
if not op['tables']:
|
||||
print(op)
|
||||
|
||||
for child in ast.iter_child_nodes(node):
|
||||
self.walk_expression(child, operations, phase, condition)
|
||||
@@ -374,7 +389,14 @@ class RegisterExtractor(ast.NodeVisitor):
|
||||
kwargs[kw.arg] = ast.unparse(kw.value)
|
||||
except Exception:
|
||||
kwargs[kw.arg] = "<unknown>"
|
||||
|
||||
code = ast.get_source_segment(self.source, call)
|
||||
|
||||
for name, value in self.alias_vars.items():
|
||||
code = re.sub(
|
||||
rf"\b{re.escape(name)}\b",
|
||||
f"({value})",
|
||||
code,
|
||||
)
|
||||
return {
|
||||
"object": obj,
|
||||
"operation": operation,
|
||||
@@ -382,18 +404,19 @@ class RegisterExtractor(ast.NodeVisitor):
|
||||
"line": call.lineno,
|
||||
"args": args,
|
||||
"kwargs": kwargs,
|
||||
"code": ast.unparse(call),
|
||||
"code": code,
|
||||
}
|
||||
|
||||
def analyse_file(path):
|
||||
|
||||
try:
|
||||
tree = ast.parse(path.read_text(encoding="utf-8"))
|
||||
source = path.read_text(encoding="utf-8")
|
||||
tree = ast.parse(source)
|
||||
except Exception as e:
|
||||
print(f"Cannot parse {path}: {e}")
|
||||
return []
|
||||
|
||||
visitor = RegisterExtractor(str(path))
|
||||
visitor = RegisterExtractor(str(path), source)
|
||||
visitor.visit(tree)
|
||||
|
||||
results = [r for r in visitor.results if r["operations"]]
|
||||
|
||||
Reference in New Issue
Block a user