Skip to content

Commit

Permalink
Codechange: Various non-formatting related flake8 fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
LordAro authored and FLHerne committed Nov 7, 2020
1 parent 9232fd9 commit a1d23b6
Show file tree
Hide file tree
Showing 22 changed files with 125 additions and 121 deletions.
14 changes: 11 additions & 3 deletions .flake8
Expand Up @@ -2,6 +2,14 @@
max-line-length = 120
inline-quotes = double

# We use 'black' for coding style; these next two warnings are not PEP-8
# compliant.
ignore = E203, W503
# We use 'black' for coding style; these next two warnings are not PEP-8 compliant.
# E241 is so we can align data tables in a pleasing way (black fixes issues outside of nofmt blocks).
ignore = E203, E241, W503
per-file-ignores =
# Ignore unused imports
nml/expression/__init__.py:F401
# Block formatting
nml/actions/real_sprite.py:E126,E131
nml/palette.py:E126,E131

exclude = generated
17 changes: 8 additions & 9 deletions nml/actions/action0.py
Expand Up @@ -337,7 +337,9 @@ def adjust_value(value, org_value, unit, ottd_convert_func):
value = expression.ConstantNumeric(int(value.value + 1), value.pos)
higher_value = value

if abs(ottd_convert_func(lower_value, unit) - org_value.value) < abs(ottd_convert_func(higher_value, unit) - org_value.value):
lower_value_ottd = abs(ottd_convert_func(lower_value, unit) - org_value.value)
higher_value_ottd = abs(ottd_convert_func(higher_value, unit) - org_value.value)
if lower_value_ottd < higher_value_ottd:
return lower_value
return higher_value

Expand Down Expand Up @@ -425,8 +427,6 @@ def get_property_info_list(feature, name):
@return: A list of dictionaries with property information
@rtype: C{list} of C{dict}
"""
global properties

#Validate feature
assert feature in range (0, len(properties)) #guaranteed by item
if properties[feature] is None:
Expand Down Expand Up @@ -490,7 +490,7 @@ def parse_property_value(prop_info, value, unit = None, size_bit = None):

# Divide by conversion factor specified by unit
if unit is not None:
if not 'unit_type' in prop_info or unit.type != prop_info['unit_type']:
if 'unit_type' not in prop_info or unit.type != prop_info['unit_type']:
raise generic.ScriptError("Invalid unit for property", value.pos)
unit_mul, unit_div = unit.convert, 1
if isinstance(unit_mul, tuple):
Expand Down Expand Up @@ -580,7 +580,7 @@ def parse_property(prop_info, value_list, feature, id):
value = expression.ConstantNumeric(0)

elif isinstance(value, expression.String):
if not 'string' in prop_info: raise generic.ScriptError("String used as value for non-string property: " + str(prop_info['num']), value.pos)
if 'string' not in prop_info: raise generic.ScriptError("String used as value for non-string property: " + str(prop_info['num']), value.pos)
string_range = prop_info['string']
stringid, string_actions = action4.get_string_action4s(feature, string_range, value, id)
value = expression.ConstantNumeric(stringid)
Expand Down Expand Up @@ -616,7 +616,6 @@ def validate_prop_info_list(prop_info_list, pos_list, feature):
@param feature: Feature of the associated item
@type feature: C{int}
"""
global properties
first_warnings = [(info, pos_list[i]) for i, info in enumerate(prop_info_list) if 'first' in info and i != 0]
for info, pos in first_warnings:
for prop_name, prop_info in properties[feature].items():
Expand Down Expand Up @@ -670,14 +669,14 @@ def parse_property_block(prop_list, feature, id, size):

for prop_info, value_list in zip(prop_info_list, value_list_list):
if 'test_function' in prop_info and not prop_info['test_function'](*value_list): continue
properties, extra_actions, mods, extra_append_actions = parse_property(prop_info, value_list, feature, id)
props, extra_actions, mods, extra_append_actions = parse_property(prop_info, value_list, feature, id)
action_list.extend(extra_actions)
action_list_append.extend(extra_append_actions)
for mod in mods:
act6.modify_bytes(mod[0], mod[1], mod[2] + offset)
for p in properties:
for p in props:
offset += p.get_size()
action0.prop_list.extend(properties)
action0.prop_list.extend(props)

if len(act6.modifications) > 0: action_list.append(act6)
if len(action0.prop_list) != 0:
Expand Down
6 changes: 3 additions & 3 deletions nml/actions/action0properties.py
Expand Up @@ -15,7 +15,7 @@

import itertools
from nml import generic, nmlop
from nml.expression import (BinOp, ConstantNumeric, ConstantFloat, Array, StringLiteral,
from nml.expression import (ConstantNumeric, ConstantFloat, Array, StringLiteral,
Identifier, ProduceCargo, AcceptCargo, parse_string_to_dword)

tilelayout_names = {}
Expand Down Expand Up @@ -473,7 +473,7 @@ def speed_fraction(value):
#

def aircraft_is_heli(value):
if isinstance(value, ConstantNumeric) and not value.value in (0, 2, 3):
if isinstance(value, ConstantNumeric) and value.value not in (0, 2, 3):
raise generic.ScriptError("Invalid value for aircraft_type", value.pos)
return nmlop.AND(value, 2).reduce()

Expand Down Expand Up @@ -594,7 +594,7 @@ def mt_house_old_id(value, num_ids, size_bit):
# For substitute / override properties
# Set value for tile i (0 .. 3) to (value + i)
# Also validate that the size of the old house matches
if isinstance(value, ConstantNumeric) and not value.value in old_houses[size_bit]:
if isinstance(value, ConstantNumeric) and value.value not in old_houses[size_bit]:
raise generic.ScriptError("Substitute / override house type must have the same size as the newly defined house.", value.pos)
ret = [value]
for i in range(1, num_ids):
Expand Down
6 changes: 3 additions & 3 deletions nml/actions/action2layout.py
Expand Up @@ -234,7 +234,7 @@ def set_param(self, name, value):
assert isinstance(value, expression.Expression)
name = name.value

if not name in self.params:
if name not in self.params:
raise generic.ScriptError("Unknown sprite parameter '{}'".format(name), value.pos)
if self.is_set(name):
raise generic.ScriptError("Sprite parameter '{}' can be set only once per sprite.".format(name), value.pos)
Expand Down Expand Up @@ -289,7 +289,7 @@ def _validate_recolour_mode(self, name, value):
if not isinstance(value, expression.ConstantNumeric):
raise generic.ScriptError("Expected a compile-time constant.", value.pos)

if not value.value in (0, 1, 2):
if value.value not in (0, 1, 2):
raise generic.ScriptError("Value of 'recolour_mode' must be RECOLOUR_NONE, RECOLOUR_TRANSPARENT or RECOLOUR_REMAP.")
return value.value

Expand Down Expand Up @@ -356,7 +356,7 @@ def _validate_hide_sprite(self, name, value):
value = expression.Not(value)
try:
value = value.reduce()
except:
except generic.ScriptError:
pass
self.create_register(name, value)
return None
Expand Down
6 changes: 2 additions & 4 deletions nml/actions/action2production.py
Expand Up @@ -14,7 +14,7 @@
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA."""

from nml.actions import action2, action2var, action6, actionD
from nml import expression, nmlop, global_constants
from nml import expression, nmlop, global_constants, generic

class Action2Production(action2.Action2):
"""
Expand Down Expand Up @@ -161,16 +161,14 @@ def get_production_v2_actions(produce):
@type produce: L{Produce2}
"""
action_list = []
act6 = action6.Action6()
action6.free_parameters.save()

result_list = []
varact2parser = action2var.Varaction2Parser(0x0A)

def resolve_cargoitem(item):
cargolabel = item.name.value
if cargolabel not in global_constants.cargo_numbers:
raise generic.ScriptError("Cargo label {0} not found in your cargo table".format(cargolabel), pos)
raise generic.ScriptError("Cargo label {0} not found in your cargo table".format(cargolabel), produce.pos)
cargoindex = global_constants.cargo_numbers[cargolabel]
valueregister = resolve_prodcb_register(item.value, varact2parser)
return (cargoindex, valueregister)
Expand Down
2 changes: 1 addition & 1 deletion nml/actions/action7.py
Expand Up @@ -222,7 +222,7 @@ def parse_conditional_block(cond_list):
# actions (like action6) can be skipped safely
for block in blocks:
block['param_dst'], block['cond_actions'], block['cond_type'], block['cond_value'], block['cond_value_size'] = parse_conditional(block['expr'])
if not 'last_block' in block:
if 'last_block' not in block:
block['action_list'] = [actionD.ActionD(expression.ConstantNumeric(param_skip_all), expression.ConstantNumeric(0xFF), nmlop.ASSIGN, expression.ConstantNumeric(0), expression.ConstantNumeric(0))]
else:
block['action_list'] = []
Expand Down
2 changes: 1 addition & 1 deletion nml/actions/actionA.py
Expand Up @@ -13,7 +13,7 @@
with NML; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA."""

from nml import expression, nmlop
from nml import nmlop
from nml.actions import base_action, real_sprite, actionD, action6

class ActionA(base_action.BaseAction):
Expand Down
4 changes: 2 additions & 2 deletions nml/actions/actionB.py
Expand Up @@ -79,7 +79,7 @@ def parse_error_block(error):
msg_string = error.msg
grfstrings.validate_string(msg_string)
langs.extend(grfstrings.get_translations(msg_string))
for l in langs: assert l is not None
for lang in langs: assert lang is not None
else:
custom_msg = False
msg = error.msg.reduce_constant().value
Expand All @@ -89,7 +89,7 @@ def parse_error_block(error):
if isinstance(error.data, expression.String):
grfstrings.validate_string(error.data)
langs.extend(grfstrings.get_translations(error.data))
for l in langs: assert l is not None
for lang in langs: assert lang is not None
elif not isinstance(error.data, expression.StringLiteral):
raise generic.ScriptError("Error parameter 3 'data' should be the identifier of a custom sting", error.data.pos)

Expand Down
22 changes: 11 additions & 11 deletions nml/actions/real_sprite.py
Expand Up @@ -296,8 +296,8 @@ def __init__(self, mapping, label = None, poslist = None):

def debug_print(self, indentation):
generic.print_dbg(indentation, 'Recolour sprite, mapping:')
for assignment in self.mapping:
generic.print_dbg(indentation + 2, '{}: {};'.format(assignment.name, assignment.value))
for recolour in self.mapping:
generic.print_dbg(indentation + 2, '{}: {};'.format(recolour.name, recolour.value))

def get_labels(self):
labels = {}
Expand All @@ -319,8 +319,8 @@ def expand(self, default_file, default_mask_file, poslist, id_dict):
def __str__(self):
ret = "" if self.label is None else str(self.label) + ": "
ret += "recolour_sprite {\n"
for assignment in self.mapping:
ret += '{}: {};'.format(assignment.name, assignment.value)
for recolour in self.mapping:
ret += '{}: {};'.format(recolour.name, recolour.value)
ret += "}"
return ret

Expand All @@ -333,13 +333,13 @@ def __init__(self, sprite):
def prepare_output(self, sprite_num):
SpriteAction.prepare_output(self, sprite_num)
colour_mapping = {}
for assignment in self.sprite.mapping:
if assignment.value.max is not None and assignment.name.max.value - assignment.name.min.value != assignment.value.max.value - assignment.value.min.value:
raise generic.ScriptError("From and to ranges in a recolour block need to have the same size", assignment.pos)
for i in range(assignment.name.max.value - assignment.name.min.value + 1):
idx = assignment.name.min.value + i
val = assignment.value.min.value
if assignment.value.max is not None:
for recolour in self.sprite.mapping:
if recolour.value.max is not None and recolour.name.max.value - recolour.name.min.value != recolour.value.max.value - recolour.value.min.value:
raise generic.ScriptError("From and to ranges in a recolour block need to have the same size", recolour.pos)
for i in range(recolour.name.max.value - recolour.name.min.value + 1):
idx = recolour.name.min.value + i
val = recolour.value.min.value
if recolour.value.max is not None:
val += i
colour_mapping[idx] = val
for i in range(256):
Expand Down
2 changes: 1 addition & 1 deletion nml/ast/font.py
Expand Up @@ -13,7 +13,7 @@
with NML; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA."""

from nml import expression, generic, expression
from nml import expression, generic
from nml.actions import action12
from nml.ast import base_statement, sprite_container

Expand Down
2 changes: 1 addition & 1 deletion nml/ast/item.py
Expand Up @@ -13,7 +13,7 @@
with NML; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA."""

from nml import expression, generic, global_constants, unit
from nml import expression, generic, global_constants
from nml.ast import base_statement, general
from nml.actions import action0, action2, action2var, action3

Expand Down
3 changes: 1 addition & 2 deletions nml/editors/extract_tables.py
Expand Up @@ -30,8 +30,7 @@
block_names_table = sorted(temp1)



##Create list of properties and variables
# Create list of properties and variables
var_tables = [
action2var_variables.varact2_globalvars,
action2var_variables.varact2vars_vehicles,
Expand Down
10 changes: 5 additions & 5 deletions nml/editors/kate.py
Expand Up @@ -97,11 +97,11 @@
<!-- Preprocessor commands starting with a hash - Main switch for preprocessor -->
<context attribute="Error" lineEndContext="#pop" name="AfterHash">
<!-- define, elif, else, endif, error, if, ifdef, ifndef, include, include_next, line, pragma, undef, warning -->
<RegExpr attribute="Preprocessor" context="Preprocessor" String="#\s*if(?:def|ndef)?(?=\s+\S)" insensitive="true" beginRegion="PP" firstNonSpace="true" />
<RegExpr attribute="Preprocessor" context="Preprocessor" String="#\s*endif" insensitive="true" endRegion="PP" firstNonSpace="true" />
<RegExpr attribute="Preprocessor" context="Define" String="#\s*define.*((?=\\))" insensitive="true" firstNonSpace="true" />
<RegExpr attribute="Preprocessor" context="Preprocessor" String="#\s*(?:el(?:se|if)|include(?:_next)?|define|undef|line|error|warning|pragma)" insensitive="true" firstNonSpace="true" />
<RegExpr attribute="Preprocessor" context="Preprocessor" String="#\s+[0-9]+" insensitive="true" firstNonSpace="true" />
<RegExpr attribute="Preprocessor" context="Preprocessor" String="#\\s*if(?:def|ndef)?(?=\\s+\\S)" insensitive="true" beginRegion="PP" firstNonSpace="true" />
<RegExpr attribute="Preprocessor" context="Preprocessor" String="#\\s*endif" insensitive="true" endRegion="PP" firstNonSpace="true" />
<RegExpr attribute="Preprocessor" context="Define" String="#\\s*define.*((?=\\))" insensitive="true" firstNonSpace="true" />
<RegExpr attribute="Preprocessor" context="Preprocessor" String="#\\s*(?:el(?:se|if)|include(?:_next)?|define|undef|line|error|warning|pragma)" insensitive="true" firstNonSpace="true" />
<RegExpr attribute="Preprocessor" context="Preprocessor" String="#\\s+[0-9]+" insensitive="true" firstNonSpace="true" />
</context>
<!-- Preprocessor instructions -->
<context attribute="Preprocessor" lineEndContext="#pop" name="Preprocessor">
Expand Down
3 changes: 2 additions & 1 deletion nml/editors/notepadpp.py
Expand Up @@ -71,7 +71,8 @@
</NotepadPlus>
"""

#Build np++ xml file

# Build np++ xml file
def write_file(fname):
handle = open(fname, "w")

Expand Down

0 comments on commit a1d23b6

Please sign in to comment.