|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
from __future__ import unicode_literals |
|
|
|
import sys |
|
import inspect |
|
import codegen |
|
import ast as py_ast |
|
import renpy |
|
|
|
def pprint(out_file, ast, decompile_python=False, comparable=False, no_pyexpr=False): |
|
|
|
|
|
AstDumper(out_file, decompile_python=decompile_python, comparable=comparable, no_pyexpr=no_pyexpr).dump(ast) |
|
|
|
class AstDumper(object): |
|
""" |
|
An object which handles the walking of a tree of python objects |
|
it will create a human-readable representation of all interesting |
|
attributes and write this to a given stream |
|
""" |
|
MAP_OPEN = {list: '[', tuple: '(', set: '{', frozenset: 'frozenset({'} |
|
MAP_CLOSE = {list: ']', tuple: ')', set: '}', frozenset: '})'} |
|
|
|
def __init__(self, out_file=None, decompile_python=False, no_pyexpr=False, |
|
comparable=False, indentation=u' '): |
|
self.indentation = indentation |
|
self.out_file = out_file or sys.stdout |
|
self.decompile_python = decompile_python |
|
self.comparable = comparable |
|
self.no_pyexpr = no_pyexpr |
|
|
|
def dump(self, ast): |
|
self.linenumber = 1 |
|
self.indent = 0 |
|
self.passed = [] |
|
self.passed_where = [] |
|
self.print_ast(ast) |
|
|
|
def print_ast(self, ast): |
|
|
|
try: |
|
i = self.passed.index(ast) |
|
except ValueError: |
|
pass |
|
else: |
|
self.p('<circular reference to object on line %d>' % self.passed_where[i]) |
|
return |
|
self.passed.append(ast) |
|
self.passed_where.append(self.linenumber) |
|
if isinstance(ast, (list, tuple, set, frozenset)): |
|
self.print_list(ast) |
|
elif isinstance(ast, renpy.ast.PyExpr): |
|
self.print_pyexpr(ast) |
|
elif isinstance(ast, dict): |
|
self.print_dict(ast) |
|
elif isinstance(ast, (str, unicode)): |
|
self.print_string(ast) |
|
elif isinstance(ast, (int, long, bool)) or ast is None: |
|
self.print_other(ast) |
|
elif inspect.isclass(ast): |
|
self.print_class(ast) |
|
elif isinstance(ast, object): |
|
self.print_object(ast) |
|
else: |
|
self.print_other(ast) |
|
self.passed_where.pop() |
|
self.passed.pop() |
|
|
|
def print_list(self, ast): |
|
|
|
if type(ast) not in (list, tuple, set, frozenset): |
|
self.p(repr(type(ast))) |
|
|
|
for k in (list, tuple, set, frozenset): |
|
if isinstance(ast, k): |
|
klass = k |
|
|
|
else: |
|
klass = ast.__class__ |
|
|
|
self.p(self.MAP_OPEN[klass]) |
|
|
|
self.ind(1, ast) |
|
for i, obj in enumerate(ast): |
|
self.print_ast(obj) |
|
if i+1 != len(ast): |
|
self.p(',') |
|
self.ind() |
|
self.ind(-1, ast) |
|
self.p(self.MAP_CLOSE[klass]) |
|
|
|
def print_dict(self, ast): |
|
|
|
if type(ast) != dict: |
|
self.p(repr(type(ast))) |
|
|
|
self.p('{') |
|
|
|
self.ind(1, ast) |
|
for i, key in enumerate(ast): |
|
self.print_ast(key) |
|
self.p(': ') |
|
self.print_ast(ast[key]) |
|
if i+1 != len(ast): |
|
self.p(',') |
|
self.ind() |
|
self.ind(-1, ast) |
|
self.p('}') |
|
|
|
def should_print_key(self, ast, key): |
|
if key.startswith('_') or not hasattr(ast, key) or inspect.isroutine(getattr(ast, key)): |
|
return False |
|
elif not self.comparable: |
|
return True |
|
elif key == 'serial': |
|
ast.serial = 0 |
|
elif key == 'col_offset': |
|
ast.col_offset = 0 |
|
elif key == 'name' and type(ast.name) == tuple: |
|
name = ast.name[0] |
|
if isinstance(name, unicode): |
|
name = name.encode('utf-8') |
|
ast.name = (name.split(b'/')[-1], 0, 0) |
|
elif key == 'location' and type(ast.location) == tuple: |
|
if len(ast.location) == 4: |
|
ast.location = (ast.location[0].split('/')[-1].split('\\')[-1], ast.location[1], ast.location[2], 0) |
|
elif len(ast.location) == 3: |
|
ast.location = (ast.location[0].split('/')[-1].split('\\')[-1], ast.location[1], 0) |
|
elif len(ast.location) == 2: |
|
ast.location = (ast.location[0].split('/')[-1].split('\\')[-1], ast.location[1]) |
|
elif key == 'loc' and type(ast.loc) == tuple: |
|
ast.loc = (ast.loc[0].split('/')[-1].split('\\')[-1], ast.loc[1]) |
|
elif key == 'filename': |
|
ast.filename = ast.filename.split('/')[-1].split('\\')[-1] |
|
elif (key == 'parameters' and ast.parameters is None and |
|
isinstance(ast, renpy.screenlang.ScreenLangScreen)): |
|
|
|
|
|
return False |
|
elif (key == 'hide' and ast.hide == False and |
|
(isinstance(ast, renpy.ast.Python) or |
|
isinstance(ast, renpy.ast.Label))): |
|
|
|
|
|
return False |
|
elif (key == 'attributes' and ast.attributes is None and |
|
isinstance(ast, renpy.ast.Say)): |
|
|
|
|
|
return False |
|
elif (key == 'temporary_attributes' and ast.temporary_attributes is None and |
|
isinstance(ast, renpy.ast.Say)): |
|
|
|
|
|
return False |
|
elif (key == 'rollback' and ast.rollback == 'normal' and |
|
isinstance(ast, renpy.ast.Say)): |
|
|
|
|
|
return False |
|
elif (key == 'block' and ast.block == [] and |
|
isinstance(ast, renpy.ast.UserStatement)): |
|
|
|
|
|
return False |
|
elif (key == 'store' and ast.store == 'store' and |
|
isinstance(ast, renpy.ast.Python)): |
|
|
|
|
|
return False |
|
elif key == 'translatable' and isinstance(ast, renpy.ast.UserStatement): |
|
|
|
|
|
return False |
|
elif key == 'hotspot' and isinstance(ast, renpy.sl2.slast.SLDisplayable): |
|
|
|
|
|
return False |
|
return True |
|
|
|
def print_object(self, ast): |
|
|
|
|
|
|
|
self.p('<') |
|
self.p(str(ast.__class__)[8:-2] if hasattr(ast, '__class__') else str(ast)) |
|
|
|
if isinstance(ast, py_ast.Module) and self.decompile_python: |
|
self.p('.code = ') |
|
self.print_ast(codegen.to_source(ast, unicode(self.indentation))) |
|
self.p('>') |
|
return |
|
|
|
keys = list(i for i in dir(ast) if self.should_print_key(ast, i)) |
|
if keys: |
|
self.p(' ') |
|
self.ind(1, keys) |
|
for i, key in enumerate(keys): |
|
self.p('.') |
|
self.p(str(key)) |
|
self.p(' = ') |
|
self.print_ast(getattr(ast, key)) |
|
if i+1 != len(keys): |
|
self.p(',') |
|
self.ind() |
|
self.ind(-1, keys) |
|
self.p('>') |
|
|
|
def print_pyexpr(self, ast): |
|
if not self.no_pyexpr: |
|
self.print_object(ast) |
|
self.p(' = ') |
|
self.print_string(ast) |
|
|
|
def print_class(self, ast): |
|
|
|
self.p('<class ') |
|
self.p(str(ast)[8:-2]) |
|
self.p('>') |
|
|
|
def print_string(self, ast): |
|
|
|
|
|
if b'\n' in ast: |
|
astlist = ast.split(b'\n') |
|
if isinstance(ast, unicode): |
|
self.p('u') |
|
self.p('"""') |
|
self.p(self.escape_string(astlist.pop(0))) |
|
for i, item in enumerate(astlist): |
|
self.p('\n') |
|
self.p(self.escape_string(item)) |
|
self.p('"""') |
|
self.ind() |
|
|
|
else: |
|
self.p(repr(ast)) |
|
|
|
def escape_string(self, string): |
|
|
|
if isinstance(string, unicode): |
|
return repr(string)[2:-1] |
|
elif isinstance(string, str): |
|
return repr(string)[1:-1] |
|
else: |
|
return string |
|
|
|
def print_other(self, ast): |
|
|
|
self.p(repr(ast)) |
|
|
|
def ind(self, diff_indent=0, ast=None): |
|
|
|
|
|
|
|
if ast is None or len(ast) > 1: |
|
self.indent += diff_indent |
|
self.p(u'\n' + self.indentation * self.indent) |
|
|
|
def p(self, string): |
|
|
|
string = unicode(string) |
|
self.linenumber += string.count('\n') |
|
self.out_file.write(string) |
|
|