Skip to content

Commit

Permalink
Switch string formatting to f-strings (CensoredUsername#207)
Browse files Browse the repository at this point in the history
* Port of main app to f-string formating
- magic.py is omitted because of py2 compat

* Port of un.rpyc to f-string formating
- codegen.py is omitted because of py2 compat

* Format inside f-strings with conversion flags
  • Loading branch information
madeddy authored Mar 26, 2024
1 parent 3b5c5ff commit ff3b46d
Show file tree
Hide file tree
Showing 12 changed files with 175 additions and 174 deletions.
127 changes: 63 additions & 64 deletions decompiler/__init__.py

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion decompiler/astdump.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def print_ast(self, ast):
except ValueError:
pass
else:
self.p('<circular reference to object on line %d>' % self.passed_where[i])
self.p(f'<circular reference to object on line {self.passed_where[i]}>')
return
self.passed.append(ast)
self.passed_where.append(self.linenumber)
Expand Down
16 changes: 8 additions & 8 deletions decompiler/atldecompiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def print_atl_rawmulti(self, ast):

# circles
if ast.circles != "0":
words.append("circles %s" % ast.circles)
words.append(f'circles {ast.circles}')

# splines
spline_words = WordConcatenator(False)
Expand Down Expand Up @@ -165,7 +165,7 @@ def print_atl_rawchoice(self, ast):
self.indent()
self.write("choice")
if chance != "1.0":
self.write(" %s" % chance)
self.write(f' {chance}')
self.write(":")
self.print_block(block)
if (self.index + 1 < len(self.block) and
Expand All @@ -176,25 +176,25 @@ def print_atl_rawchoice(self, ast):
@dispatch(renpy.atl.RawContainsExpr)
def print_atl_rawcontainsexpr(self, ast):
self.indent()
self.write("contains %s" % ast.expression)
self.write(f'contains {ast.expression}')

@dispatch(renpy.atl.RawEvent)
def print_atl_rawevent(self, ast):
self.indent()
self.write("event %s" % ast.name)
self.write(f'event {ast.name}')

@dispatch(renpy.atl.RawFunction)
def print_atl_rawfunction(self, ast):
self.indent()
self.write("function %s" % ast.expr)
self.write(f'function {ast.expr}')

@dispatch(renpy.atl.RawOn)
def print_atl_rawon(self, ast):
for name, block in sorted(ast.handlers.items(),
key=lambda i: i[1].loc[1]):
self.advance_to_block(block)
self.indent()
self.write("on %s:" % name)
self.write(f'on {name}:')
self.print_block(block)

@dispatch(renpy.atl.RawParallel)
Expand All @@ -214,9 +214,9 @@ def print_atl_rawrepeat(self, ast):
self.indent()
self.write("repeat")
if ast.repeats:
self.write(" %s" % ast.repeats) # not sure if this is even a string
self.write(f' {ast.repeats}') # not sure if this is even a string

@dispatch(renpy.atl.RawTime)
def print_atl_rawtime(self, ast):
self.indent()
self.write("time %s" % ast.time)
self.write(f'time {ast.time}')
42 changes: 20 additions & 22 deletions decompiler/sl2decompiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def print_screen(self, ast):

# Print the screen statement and create the block
self.indent()
self.write("screen %s" % ast.name)
self.write(f'screen {ast.name}')
# If we have parameters, print them.
if ast.parameters:
self.write(reconstruct_paraminfo(ast.parameters))
Expand Down Expand Up @@ -97,7 +97,7 @@ def _print_if(self, ast, keyword):
if condition is None:
self.write("else")
else:
self.write("%s %s" % (keyword(), condition))
self.write(f'{keyword()} {condition}')

# Every condition has a block of type slast.SLBlock
self.print_block(block, immediate_block=True)
Expand Down Expand Up @@ -142,10 +142,10 @@ def print_for(self, ast):

self.indent()
if hasattr(ast, "index_expression") and ast.index_expression is not None:
self.write("for %sindex %s in %s:" % (variable, ast.index_expression, ast.expression))
self.write(f'for {variable}index {ast.index_expression} in {ast.expression}:')

else:
self.write("for %sin %s:" % (variable, ast.expression))
self.write(f'for {variable}in {ast.expression}:')

# for doesn't contain a block, but just a list of child nodes
self.print_nodes(children, 1)
Expand Down Expand Up @@ -173,7 +173,7 @@ def print_python(self, ast):
with self.increase_indent():
self.write_lines(split_logical_lines(code))
else:
self.write("$ %s" % code)
self.write(f'$ {code}')

@dispatch(sl2.slast.SLPass)
def print_pass(self, ast):
Expand All @@ -188,17 +188,17 @@ def print_use(self, ast):
self.write("use ")
args = reconstruct_arginfo(ast.args)
if isinstance(ast.target, PyExpr):
self.write("expression %s" % ast.target)
self.write(f'expression {ast.target}')
if args:
self.write(" pass ")
else:
self.write("%s" % ast.target)
self.write(f'{ast.target}')

self.write("%s" % args)
self.write(f'{args}')
if hasattr(ast, 'id') and ast.id is not None:
self.write(" id %s" % ast.id)
self.write(f' id {ast.id}')

if hasattr(ast, 'block') and ast.block:
if hasattr(ast, "block") and ast.block:
self.print_block(ast.block)

@dispatch(sl2.slast.SLTransclude)
Expand All @@ -210,7 +210,7 @@ def print_transclude(self, ast):
def print_default(self, ast):
# A default statement
self.indent()
self.write("default %s = %s" % (ast.variable, ast.expression))
self.write(f'default {ast.variable} = {ast.expression}')

@dispatch(sl2.slast.SLDisplayable)
def print_displayable(self, ast, has_block=False):
Expand All @@ -223,7 +223,8 @@ def print_displayable(self, ast, has_block=False):
if nameAndChildren is None and self.options.sl_custom_names:
# check if we have a name registered for this displayable
nameAndChildren = self.options.sl_custom_names.get(ast.displayable.__name__)
self.print_debug("Substituted '{}' as the name for displayable {}".format(nameAndChildren[0], ast.displayable))
self.print_debug(
f'Substituted "{nameAndChildren[0]}" as the name for displayable {ast.displayable}')

if nameAndChildren is None:
# This is a (user-defined) displayable we don't know about.
Expand All @@ -232,13 +233,10 @@ def print_displayable(self, ast, has_block=False):
# print a debug message
nameAndChildren = (ast.style, 'many')
self.print_debug(
"""Warning: Encountered a user-defined displayable of type '{}'.
f'''Warning: Encountered a user-defined displayable of type "{ast.displayable}".
Unfortunately, the name of user-defined displayables is not recorded in the compiled file.
For now the style name '{}' will be substituted.
To check if this is correct, find the corresponding renpy.register_sl_displayable call.""".format(
ast.displayable, ast.style
)
)
For now the style name "{ast.style}" will be substituted.
To check if this is correct, find the corresponding renpy.register_sl_displayable call.''') # noqa

(name, children) = nameAndChildren
self.indent()
Expand Down Expand Up @@ -335,9 +333,9 @@ def print_displayable(self, ast, has_block=False):
def sort_keywords_and_children(self, node, immediate_block=False, ignore_children=False):
# sorts the contents of a SL statement that has keywords and children
# returns a list of sorted contents.
#
#
# node is either a SLDisplayable, a SLScreen or a SLBlock
#
#
# before this point, the name and any positional arguments of the statement have been
# emitted, but the block itself has not been created yet.
# immediate_block: bool, if True, nothing is on the first line
Expand All @@ -346,7 +344,7 @@ def sort_keywords_and_children(self, node, immediate_block=False, ignore_childre
# get all the data we need from the node
keywords = node.keyword
children = [] if ignore_children else node.children

# first linenumber where we can insert content that doesn't have a clear lineno
block_lineno = node.location[1]
start_lineno = (block_lineno + 1) if immediate_block else block_lineno
Expand Down Expand Up @@ -560,7 +558,7 @@ def print_keyword_or_child(self, item, first_line=False, has_block=False):

for name, value in item[2]:
self.write(sep())
self.write("%s %s" % (name, value))
self.write(f'{name} {value}')

if ty == "keywords_atl":
assert not has_block, "cannot start a block on the same line as an at transform block"
Expand Down
52 changes: 26 additions & 26 deletions decompiler/testcasedecompiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,92 +53,92 @@ def print_python(self, ast):
with self.increase_indent():
self.write_lines(split_logical_lines(code[1:]))
else:
self.write("$ %s" % code)
self.write(f'$ {code}')

@dispatch(testast.If)
def print_if(self, ast):
self.indent()
self.write('if %s:' % ast.condition)
self.write(f'if {ast.condition}:')
self.print_nodes(ast.block, extra_indent=1)

@dispatch(testast.Assert)
def print_assert(self, ast):
self.indent()
self.write('assert %s' % ast.expr)
self.write(f'assert {ast.expr}')

@dispatch(testast.Jump)
def print_jump(self, ast):
self.indent()
self.write('jump %s' % ast.target)
self.write(f'jump {ast.target}')

@dispatch(testast.Call)
def print_call(self, ast):
self.indent()
self.write('call %s' % ast.target)
self.write(f'call {ast.target}')

@dispatch(testast.Action)
def print_action(self, ast):
self.indent()
self.write('run %s' % ast.expr)
self.write(f'run {ast.expr}')

@dispatch(testast.Pause)
def print_pause(self, ast):
self.indent()
self.write('pause %s' % ast.expr)
self.write(f'pause {ast.expr}')

@dispatch(testast.Label)
def print_label(self, ast):
self.indent()
self.write('label %s' % ast.name)
self.write(f'label {ast.name}')

@dispatch(testast.Type)
def print_type(self, ast):
self.indent()
if len(ast.keys[0]) == 1:
self.write('type "%s"' % string_escape(''.join(ast.keys)))
self.write(f'type "{string_escape("".join(ast.keys))}"')
else:
self.write('type %s' % ast.keys[0])
self.write(f'type {ast.keys[0]}')
if ast.pattern is not None:
self.write(' pattern "%s"' % string_escape(ast.pattern))
self.write(f' pattern "{string_escape(ast.pattern)}"')
if hasattr(ast, 'position') and ast.position is not None:
self.write(' pos %s' % ast.position)
self.write(f' pos {ast.position}')

@dispatch(testast.Drag)
def print_drag(self, ast):
self.indent()
self.write('drag %s' % ast.points)
self.write(f'drag {ast.points}')
if ast.button != 1:
self.write(' button %d' % ast.button)
self.write(f' button {ast.button}')
if ast.pattern is not None:
self.write(' pattern "%s"' % string_escape(ast.pattern))
self.write(f' pattern "{string_escape(ast.pattern)}"')
if ast.steps != 10:
self.write(' steps %d' % ast.steps)
self.write(f' steps {ast.steps}')

@dispatch(testast.Move)
def print_move(self, ast):
self.indent()
self.write('move %s' % ast.position)
self.write(f'move {ast.position}')
if ast.pattern is not None:
self.write(' pattern "%s"' % string_escape(ast.pattern))
self.write(f' pattern "{string_escape(ast.pattern)}"')

@dispatch(testast.Click)
def print_click(self, ast):
self.indent()
if ast.pattern is not None:
self.write('"%s"' % string_escape(ast.pattern))
self.write(f'"{string_escape(ast.pattern)}"')
else:
self.write('click')
self.write("click")
if hasattr(ast, 'button') and ast.button != 1:
self.write(' button %d' % ast.button)
self.write(f' button {ast.button}')
if hasattr(ast, 'position') and ast.position is not None:
self.write(' pos %s' % ast.position)
self.write(f' pos {ast.position}')
if hasattr(ast, 'always') and ast.always:
self.write(' always')
self.write(" always")

@dispatch(testast.Scroll)
def print_scroll(self, ast):
self.indent()
self.write('scroll "%s"' % string_escape(ast.pattern))
self.write(f'scroll "{string_escape(ast.pattern)}"')

@dispatch(testast.Until)
def print_until(self, ast):
Expand All @@ -147,6 +147,6 @@ def print_until(self, ast):
# Go to right's line number now since we can't go to it after we print left.
self.advance_to_line(ast.right.linenumber)
self.print_node(ast.left)
self.write(' until ')
self.write(" until ")
self.skip_indent_until_write = True
self.print_node(ast.right)
self.print_node(ast.right)
4 changes: 2 additions & 2 deletions decompiler/translate.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def unique_identifier(self, label, digest):
break

i += 1
suffix = "_{0}".format(i)
suffix = f'_{i}'

return identifier

Expand All @@ -69,7 +69,7 @@ def create_translate(self, block):
elif isinstance(i, renpy.ast.UserStatement):
code = i.line
else:
raise Exception("Don't know how to get canonical code for a %s" % str(type(i)))
raise Exception(f'Don\'t know how to get canonical code for a {type(i)!s}')
md5.update(code.encode("utf-8") + b"\r\n")

digest = md5.hexdigest()[:8]
Expand Down
Loading

0 comments on commit ff3b46d

Please sign in to comment.