Skip to content

Commit

Permalink
PEP 8 and readability formating
Browse files Browse the repository at this point in the history
- line break before keywords and operators
- max-line-length formating
- whitespace amount for inline comments
- code stacking for better readability
  • Loading branch information
madeddy committed Apr 4, 2024
1 parent 4ab7bdc commit d7ad578
Show file tree
Hide file tree
Showing 12 changed files with 345 additions and 245 deletions.
167 changes: 97 additions & 70 deletions decompiler/__init__.py

Large diffs are not rendered by default.

53 changes: 33 additions & 20 deletions decompiler/astdump.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ def __init__(self, out_file=None, no_pyexpr=False,
def dump(self, ast):
self.linenumber = 1
self.indent = 0
self.passed = [] # We'll keep a stack of objects which we've traversed here so we don't recurse endlessly on circular references
# We'll keep a stack of objects which we've traversed here so we don't recurse
# endlessly on circular references
self.passed = []
self.passed_where = []
self.print_ast(ast)

Expand Down Expand Up @@ -131,56 +133,67 @@ def should_print_key(self, ast, key):
elif key == 'serial':
ast.serial = 0
elif key == 'col_offset':
ast.col_offset = 0 # TODO maybe make this match?
ast.col_offset = 0 # TODO maybe make this match?
elif key == 'name' and type(ast.name) is tuple:
name = ast.name[0]
if isinstance(name, str):
name = name.encode('utf-8')
ast.name = (name.split(b'/')[-1], 0, 0)
elif key == 'location' and type(ast.location) is tuple:
if len(ast.location) == 4:
ast.location = (ast.location[0].split('/')[-1].split('\\')[-1], ast.location[1], ast.location[2], 0)
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)
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])
ast.location = (
ast.location[0].split('/')[-1].split('\\')[-1], ast.location[1])
elif key == 'loc' and type(ast.loc) is 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)):
elif (key == 'parameters'
and ast.parameters is None
and isinstance(ast, renpy.screenlang.ScreenLangScreen)):
# When no parameters exist, some versions of Ren'Py set parameters
# to None and some don't set it at all.
return False
elif (key == 'hide' and ast.hide is False and
(isinstance(ast, renpy.ast.Python) or
isinstance(ast, renpy.ast.Label))):
elif (key == 'hide'
and ast.hide is False
and (isinstance(ast, renpy.ast.Python)
or isinstance(ast, renpy.ast.Label))):
# When hide isn't set, some versions of Ren'Py set it to False and
# some don't set it at all.
return False
elif (key == 'attributes' and ast.attributes is None and
isinstance(ast, renpy.ast.Say)):
elif (key == 'attributes'
and ast.attributes is None
and isinstance(ast, renpy.ast.Say)):
# When no attributes are set, some versions of Ren'Py set it to None
# and some don't set it at all.
return False
elif (key == 'temporary_attributes' and ast.temporary_attributes is None and
isinstance(ast, renpy.ast.Say)):
elif (key == 'temporary_attributes'
and ast.temporary_attributes is None
and isinstance(ast, renpy.ast.Say)):
# When no temporary attributes are set, some versions of Ren'Py set
# it to None and some don't set it at all.
return False
elif (key == 'rollback' and ast.rollback == 'normal' and
isinstance(ast, renpy.ast.Say)):
elif (key == 'rollback'
and ast.rollback == 'normal'
and isinstance(ast, renpy.ast.Say)):
# When rollback is normal, some versions of Ren'Py set it to 'normal'
# and some don't set it at all.
return False
elif (key == 'block' and ast.block == [] and
isinstance(ast, renpy.ast.UserStatement)):
elif (key == 'block'
and ast.block == []
and isinstance(ast, renpy.ast.UserStatement)):
# When there's no block, some versions of Ren'Py set it to None
# and some don't set it at all.
return False
elif (key == 'store' and ast.store == 'store' and
isinstance(ast, renpy.ast.Python)):
elif (key == 'store'
and ast.store == 'store'
and isinstance(ast, renpy.ast.Python)):
# When a store isn't specified, some versions of Ren'Py set it to
# "store" and some don't set it at all.
return False
Expand Down
22 changes: 12 additions & 10 deletions decompiler/atldecompiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,11 @@ class ATLDecompiler(DecompilerBase):
dispatch = Dispatcher()

def dump(self, ast, indent_level=0, linenumber=1, skip_indent_until_write=False):
# At this point, the preceding ":" has been written, and indent hasn't been increased yet.
# There's no common syntax for starting an ATL node, and the base block that is created
# is just a RawBlock. normally RawBlocks are created witha block: statement so we cannot
# just reuse the node for that. Instead, we implement the top level node directly here
# At this point, the preceding ":" has been written, and indent hasn't been increased
# yet. There's no common syntax for starting an ATL node, and the base block that is
# created is just a RawBlock. normally RawBlocks are created witha block: statement
# so we cannot just reuse the node for that. Instead, we implement the top level node
# directly here
self.indent_level = indent_level
self.linenumber = linenumber
self.skip_indent_until_write = skip_indent_until_write
Expand Down Expand Up @@ -86,8 +87,9 @@ def print_atl_rawmulti(self, ast):
warp_words = WordConcatenator(False)

# warpers
# I think something changed about the handling of pause, that last special case doesn't look necessary anymore
# as a proper pause warper exists now but we'll keep it around for backwards compatability
# I think something changed about the handling of pause, that last special case
# doesn't look necessary anymore as a proper pause warper exists now but we'll
# keep it around for backwards compatability
if ast.warp_function:
warp_words.append("warp", ast.warp_function, ast.duration)
elif ast.warper:
Expand Down Expand Up @@ -168,8 +170,8 @@ def print_atl_rawchoice(self, ast):
self.write(f' {chance}')
self.write(":")
self.print_block(block)
if (self.index + 1 < len(self.block) and
isinstance(self.block[self.index + 1], renpy.atl.RawChoice)):
if (self.index + 1 < len(self.block)
and isinstance(self.block[self.index + 1], renpy.atl.RawChoice)):
self.indent()
self.write("pass")

Expand Down Expand Up @@ -204,8 +206,8 @@ def print_atl_rawparallel(self, ast):
self.indent()
self.write("parallel:")
self.print_block(block)
if (self.index + 1 < len(self.block) and
isinstance(self.block[self.index + 1], renpy.atl.RawParallel)):
if (self.index + 1 < len(self.block)
and isinstance(self.block[self.index + 1], renpy.atl.RawParallel)):
self.indent()
self.write("pass")

Expand Down
3 changes: 2 additions & 1 deletion decompiler/renpycompat.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,8 @@ def __setstate__(self, state):


def pickle_safe_loads(buffer: bytes):
return magic.safe_loads(buffer, CLASS_FACTORY, {"collections"}, encoding="ASCII", errors="strict")
return magic.safe_loads(
buffer, CLASS_FACTORY, {"collections"}, encoding="ASCII", errors="strict")


def pickle_safe_dumps(buffer: bytes):
Expand Down
Loading

0 comments on commit d7ad578

Please sign in to comment.