Skip to content

Commit 3122583

Browse files
committed
clean(lint)
1 parent 88279c0 commit 3122583

File tree

2 files changed

+118
-82
lines changed

2 files changed

+118
-82
lines changed

.pylintrc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ max-line-length=98
33

44
[MESSAGES CONTROL]
55
disable=
6-
fixme, # TO DOs are not errors
7-
bad-continuation, # be compatible to black
6+
fixme, # TO DOs are not errors.
7+
consider-using-f-string, # We're not on Python >= 3.6 yet.
8+
bad-continuation, # Be compatible to black.
89

910
[REPORT]
1011
score=no

stm32loader/main.py

Lines changed: 115 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,26 @@
2121
"""Flash firmware to STM32 microcontrollers over a serial connection."""
2222

2323

24-
import os
25-
import sys
26-
import copy
2724
import argparse
2825
import atexit
26+
import copy
27+
import os
28+
import sys
29+
30+
try:
31+
from progress.bar import ChargingBar as progress_bar
32+
except ImportError:
33+
progress_bar = None
2934

3035
from stm32loader import __version__, bootloader
3136
from stm32loader.uart import SerialConnection
3237

3338
DEFAULT_VERBOSITY = 5
3439

3540

36-
class HelpFormatter(
37-
argparse.RawDescriptionHelpFormatter,
38-
argparse.ArgumentDefaultsHelpFormatter
39-
):
41+
class HelpFormatter(argparse.RawDescriptionHelpFormatter, argparse.ArgumentDefaultsHelpFormatter):
42+
"""Custom help formatter -- don't print confusing default values."""
43+
4044
def _get_help_string(self, action):
4145
action = copy.copy(action)
4246
# Don't show "(default: None)" for arguments without defaults,
@@ -54,8 +58,10 @@ def tweak_action(action):
5458
if action.dest == "port":
5559
action.required = True
5660
return action
61+
5762
return super()._format_actions_usage(map(tweak_action, actions), groups)
5863

64+
5965
class Stm32Loader:
6066
"""Main application: parse arguments and handle commands."""
6167

@@ -75,97 +81,139 @@ def debug(self, level, message):
7581
def parse_arguments(self, arguments):
7682
"""Parse the list of command-line arguments."""
7783
parser = argparse.ArgumentParser(
78-
epilog='\n'.join([
79-
'examples:',
80-
' %(prog)s -p COM7 -f F1',
81-
' %(prog)s -e -w -v example/main.bin',
82-
]),
83-
formatter_class=HelpFormatter)
84+
epilog="\n".join(
85+
[
86+
"examples:",
87+
" %(prog)s -p COM7 -f F1",
88+
" %(prog)s -e -w -v example/main.bin",
89+
]
90+
),
91+
formatter_class=HelpFormatter,
92+
)
8493

8594
data_file_arg = parser.add_argument(
86-
"data_file", metavar="FILE.BIN", type=str, nargs="?",
87-
help="file to read from or store to flash")
95+
"data_file",
96+
metavar="FILE.BIN",
97+
type=str,
98+
nargs="?",
99+
help="file to read from or store to flash",
100+
)
88101

89102
parser.add_argument(
90-
"-e", "--erase", action="store_true",
91-
help="erase (note: this is required on previously written memory)")
103+
"-e",
104+
"--erase",
105+
action="store_true",
106+
help="erase (note: this is required on previously written memory)",
107+
)
92108

93109
parser.add_argument(
94-
"-u", "--unprotect", action="store_true",
95-
help="unprotect in case erase fails")
110+
"-u", "--unprotect", action="store_true", help="unprotect in case erase fails"
111+
)
96112

97113
parser.add_argument(
98-
"-w", "--write", action="store_true",
99-
help="write file content to flash")
114+
"-w", "--write", action="store_true", help="write file content to flash"
115+
)
100116

101117
parser.add_argument(
102-
"-v", "--verify", action="store_true",
103-
help="verify flash content versus local file (recommended)")
118+
"-v",
119+
"--verify",
120+
action="store_true",
121+
help="verify flash content versus local file (recommended)",
122+
)
104123

105124
parser.add_argument(
106-
"-r", "--read", action="store_true",
107-
help="read from flash and store in local file")
125+
"-r", "--read", action="store_true", help="read from flash and store in local file"
126+
)
108127

109128
length_arg = parser.add_argument(
110-
"-l", "--length", action="store", type=int,
111-
help="length of read")
129+
"-l", "--length", action="store", type=int, help="length of read"
130+
)
112131

113132
default_port = os.environ.get("STM32LOADER_SERIAL_PORT")
114133
port_arg = parser.add_argument(
115-
"-p", "--port", action="store", type=str, # morally required=True
134+
"-p",
135+
"--port",
136+
action="store",
137+
type=str, # morally required=True
116138
default=default_port,
117-
help="serial port" +
118-
("" if default_port else " (default: $STM32LOADER_SERIAL_PORT)"))
139+
help=(
140+
"serial port" + ("" if default_port else " (default: $STM32LOADER_SERIAL_PORT)")
141+
),
142+
)
119143

120144
parser.add_argument(
121-
"-b", "--baud", action="store", type=int, default=115200,
122-
help="baudrate")
145+
"-b", "--baud", action="store", type=int, default=115200, help="baudrate"
146+
)
123147

124148
address_arg = parser.add_argument(
125-
"-a", "--address", action="store", type=int, default=0x08000000,
126-
help="target address")
149+
"-a", "--address", action="store", type=int, default=0x08000000, help="target address"
150+
)
127151

128152
parser.add_argument(
129-
"-g", "--go-address", action="store", type=int, metavar="ADDRESS",
130-
help="start executing from address (0x08000000, usually)")
153+
"-g",
154+
"--go-address",
155+
action="store",
156+
type=int,
157+
metavar="ADDRESS",
158+
help="start executing from address (0x08000000, usually)",
159+
)
131160

132161
default_family = os.environ.get("STM32LOADER_FAMILY")
133162
parser.add_argument(
134-
"-f", "--family", action="store", type=str,
163+
"-f",
164+
"--family",
165+
action="store",
166+
type=str,
135167
default=default_family,
136-
help="device family to read out device UID and flash size; "
137-
"e.g F1 for STM32F1xx" +
138-
("" if default_family else " (default: $STM32LOADER_FAMILY)"))
168+
help=(
169+
"device family to read out device UID and flash size; "
170+
"e.g F1 for STM32F1xx"
171+
+ ("" if default_family else " (default: $STM32LOADER_FAMILY)")
172+
),
173+
)
139174

140175
parser.add_argument(
141-
"-V", "--verbose", dest="verbosity", action="store_const", const=10,
176+
"-V",
177+
"--verbose",
178+
dest="verbosity",
179+
action="store_const",
180+
const=10,
142181
default=DEFAULT_VERBOSITY,
143-
help="verbose mode")
182+
help="verbose mode",
183+
)
144184

145185
parser.add_argument(
146-
"-q", "--quiet", dest="verbosity", action="store_const", const=0,
147-
help="quiet mode")
186+
"-q", "--quiet", dest="verbosity", action="store_const", const=0, help="quiet mode"
187+
)
148188

149189
parser.add_argument(
150-
"-s", "--swap-rts-dtr", action="store_true",
151-
help="swap RTS and DTR: use RTS for reset and DTR for boot0")
190+
"-s",
191+
"--swap-rts-dtr",
192+
action="store_true",
193+
help="swap RTS and DTR: use RTS for reset and DTR for boot0",
194+
)
152195

153196
parser.add_argument(
154-
"-R", "--reset-active-high", action="store_true",
155-
help="make reset active high")
197+
"-R", "--reset-active-high", action="store_true", help="make reset active high"
198+
)
156199

157200
parser.add_argument(
158-
"-B", "--boot0-active-low", action="store_true",
159-
help="make boot0 active low")
201+
"-B", "--boot0-active-low", action="store_true", help="make boot0 active low"
202+
)
160203

161204
parser.add_argument(
162-
"-n", "--no-progress", action="store_true",
163-
help="don't show progress bar")
205+
"-n", "--no-progress", action="store_true", help="don't show progress bar"
206+
)
164207

165208
parser.add_argument(
166-
"-P", "--parity", action="store", type=str, default="even",
209+
"-P",
210+
"--parity",
211+
action="store",
212+
type=str,
213+
default="even",
167214
choices=self.PARITY.keys(),
168-
help='parity: "even" for STM32, "none" for BlueNRG')
215+
help='parity: "even" for STM32, "none" for BlueNRG',
216+
)
169217

170218
parser.add_argument("--version", action="version", version=__version__)
171219

@@ -186,11 +234,13 @@ def parse_arguments(self, arguments):
186234

187235
if not self.configuration.port:
188236
port_arg.required = True
189-
atexit.register(lambda: print(
190-
"{}: note: you can also set the environment "
191-
"variable STM32LOADER_SERIAL_PORT".format(parser.prog),
192-
file=sys.stderr,
193-
))
237+
atexit.register(
238+
lambda: print(
239+
"{}: note: you can also set the environment "
240+
"variable STM32LOADER_SERIAL_PORT".format(parser.prog),
241+
file=sys.stderr,
242+
)
243+
)
194244

195245
if self.configuration.read or self.configuration.write or self.configuration.verify:
196246
data_file_arg.nargs = None
@@ -237,9 +287,7 @@ def connect(self):
237287
show_progress = self._get_progress_bar(self.configuration.no_progress)
238288

239289
self.stm32 = bootloader.Stm32Bootloader(
240-
serial_connection,
241-
verbosity=self.configuration.verbosity,
242-
show_progress=show_progress
290+
serial_connection, verbosity=self.configuration.verbosity, show_progress=show_progress
243291
)
244292

245293
try:
@@ -284,9 +332,7 @@ def perform_commands(self):
284332
if self.configuration.write:
285333
self.stm32.write_memory_data(self.configuration.address, binary_data)
286334
if self.configuration.verify:
287-
read_data = self.stm32.read_memory_data(
288-
self.configuration.address, len(binary_data)
289-
)
335+
read_data = self.stm32.read_memory_data(self.configuration.address, len(binary_data))
290336
try:
291337
bootloader.Stm32Bootloader.verify_data(read_data, binary_data)
292338
print("Verification OK")
@@ -331,7 +377,8 @@ def read_device_uid(self):
331377
flash_size, device_uid = self.stm32.get_flash_size_and_uid_f4()
332378
except bootloader.CommandError as e:
333379
self.debug(
334-
0, "Something was wrong with reading chip family data: " + str(e),
380+
0,
381+
"Something was wrong with reading chip family data: " + str(e),
335382
)
336383
return
337384

@@ -341,22 +388,10 @@ def read_device_uid(self):
341388

342389
@staticmethod
343390
def _get_progress_bar(no_progress=False):
344-
if no_progress:
345-
return None
346-
desired_progress_bar = None
347-
try:
348-
from progress.bar import ( # pylint: disable=import-outside-toplevel
349-
ChargingBar as desired_progress_bar,
350-
)
351-
except ImportError:
352-
# progress module is a package dependency,
353-
# but not strictly required
354-
pass
355-
356-
if not desired_progress_bar:
391+
if no_progress or not progress_bar:
357392
return None
358393

359-
return bootloader.ShowProgress(desired_progress_bar)
394+
return bootloader.ShowProgress(progress_bar)
360395

361396

362397
def main(*args, **kwargs):

0 commit comments

Comments
 (0)