Skip to content

Commit 5912241

Browse files
committed
Clean up all lint warnings in targets
1 parent 59d35e3 commit 5912241

File tree

1 file changed

+97
-44
lines changed

1 file changed

+97
-44
lines changed

tools/targets/__init__.py

Lines changed: 97 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,8 @@
2222
import shutil
2323
import inspect
2424
import sys
25+
from collections import namedtuple
2526
from copy import copy
26-
from inspect import getmro
27-
from collections import namedtuple, Mapping
2827
from future.utils import raise_from
2928
from tools.resources import FileType
3029
from tools.targets.LPC import patch
@@ -45,16 +44,21 @@
4544
"Cortex-M4F": ["M4", "CORTEX_M", "RTOS_M4_M7", "LIKE_CORTEX_M4", "CORTEX"],
4645
"Cortex-M7": ["M7", "CORTEX_M", "RTOS_M4_M7", "LIKE_CORTEX_M7", "CORTEX"],
4746
"Cortex-M7F": ["M7", "CORTEX_M", "RTOS_M4_M7", "LIKE_CORTEX_M7", "CORTEX"],
48-
"Cortex-M7FD": ["M7", "CORTEX_M", "RTOS_M4_M7", "LIKE_CORTEX_M7", "CORTEX"],
47+
"Cortex-M7FD": ["M7", "CORTEX_M", "RTOS_M4_M7", "LIKE_CORTEX_M7",
48+
"CORTEX"],
4949
"Cortex-A9": ["A9", "CORTEX_A", "LIKE_CORTEX_A9", "CORTEX"],
5050
"Cortex-M23": ["M23", "CORTEX_M", "LIKE_CORTEX_M23", "CORTEX"],
51-
"Cortex-M23-NS": ["M23", "M23_NS", "CORTEX_M", "LIKE_CORTEX_M23", "CORTEX"],
51+
"Cortex-M23-NS": ["M23", "M23_NS", "CORTEX_M", "LIKE_CORTEX_M23",
52+
"CORTEX"],
5253
"Cortex-M33": ["M33", "CORTEX_M", "LIKE_CORTEX_M33", "CORTEX"],
53-
"Cortex-M33-NS": ["M33", "M33_NS", "CORTEX_M", "LIKE_CORTEX_M33", "CORTEX"],
54+
"Cortex-M33-NS": ["M33", "M33_NS", "CORTEX_M", "LIKE_CORTEX_M33",
55+
"CORTEX"],
5456
"Cortex-M33F": ["M33", "CORTEX_M", "LIKE_CORTEX_M33", "CORTEX"],
55-
"Cortex-M33F-NS": ["M33", "M33_NS", "CORTEX_M", "LIKE_CORTEX_M33", "CORTEX"],
57+
"Cortex-M33F-NS": ["M33", "M33_NS", "CORTEX_M", "LIKE_CORTEX_M33",
58+
"CORTEX"],
5659
"Cortex-M33FE": ["M33", "CORTEX_M", "LIKE_CORTEX_M33", "CORTEX"],
57-
"Cortex-M33FE-NS": ["M33", "M33_NS", "CORTEX_M", "LIKE_CORTEX_M33", "CORTEX"]
60+
"Cortex-M33FE-NS": ["M33", "M33_NS", "CORTEX_M", "LIKE_CORTEX_M33",
61+
"CORTEX"]
5862
}
5963

6064
CORE_ARCH = {
@@ -78,16 +82,20 @@
7882
"Cortex-M33FE-NS": 8,
7983
}
8084

81-
################################################################################
85+
###############################################################################
8286
# Generic Target class that reads and interprets the data in targets.json
8387

88+
8489
class HookError(Exception):
8590
""" A simple class that represents all the exceptions associated with
8691
hooking
8792
"""
8893
pass
8994

95+
9096
CACHES = {}
97+
98+
9199
def cached(func):
92100
"""A simple decorator used for automatically caching data returned by a
93101
function
@@ -102,9 +110,15 @@ def wrapper(*args, **kwargs):
102110

103111
# Cumulative attributes can have values appended to them, so they
104112
# need to be computed differently than regular attributes
105-
CUMULATIVE_ATTRIBUTES = ['extra_labels', 'macros', 'device_has', 'features', 'components']
106113

107-
default_build_tools_metadata = {u'version':0, u'public':False}
114+
115+
CUMULATIVE_ATTRIBUTES = [
116+
'extra_labels', 'macros', 'device_has', 'features', 'components'
117+
]
118+
119+
120+
default_build_tools_metadata = {u'version': 0, u'public': False}
121+
108122

109123
def get_resolution_order(json_data, target_name, order, level=0):
110124
""" Return the order in which target descriptions are searched for
@@ -128,22 +142,29 @@ def get_resolution_order(json_data, target_name, order, level=0):
128142
def target(name, json_data):
129143
"""Construct a target object"""
130144
if name.startswith("_"):
131-
raise Exception("Invalid target name '%s' specified, target name should not start with '_'" % name)
132-
145+
raise Exception(
146+
"Invalid target name '%s' specified,"
147+
" target name should not start with '_'" % name
148+
)
133149
try:
134150
resolution_order = get_resolution_order(json_data, name, [])
135151
except KeyError as exc:
136152
raise_from(NotSupportedException(
137153
"target {} has an incomplete target definition".format(name)
138154
), exc)
139155
resolution_order_names = [tgt for tgt, _ in resolution_order]
140-
141-
return Target(name=name,
142-
json_data={key: value for key, value in json_data.items()
143-
if key in resolution_order_names},
144-
resolution_order=resolution_order,
145-
resolution_order_names=resolution_order_names,
146-
build_tools_metadata=json_data.get("__build_tools_metadata__", default_build_tools_metadata))
156+
return Target(
157+
name=name,
158+
json_data={key: value for key, value in json_data.items()
159+
if key in resolution_order_names},
160+
resolution_order=resolution_order,
161+
resolution_order_names=resolution_order_names,
162+
build_tools_metadata=json_data.get(
163+
"__build_tools_metadata__",
164+
default_build_tools_metadata
165+
)
166+
)
167+
147168

148169
def generate_py_target(new_targets, name):
149170
"""Add one or more new target(s) represented as a Python dictionary
@@ -158,15 +179,21 @@ def generate_py_target(new_targets, name):
158179
total_data = {}
159180
total_data.update(new_targets)
160181
total_data.update(base_targets)
161-
162182
return target(name, total_data)
163183

164-
class Target(namedtuple("Target", "name json_data resolution_order resolution_order_names build_tools_metadata")):
184+
185+
class Target(namedtuple(
186+
"Target",
187+
"name json_data resolution_order "
188+
"resolution_order_names build_tools_metadata"
189+
)):
165190
"""An object to represent a Target (MCU/Board)"""
166191

167192
# Default location of the 'targets.json' file
168193
__targets_json_location_default = os.path.join(
169-
os.path.dirname(os.path.abspath(__file__)), '..', '..', 'targets', 'targets.json')
194+
os.path.dirname(os.path.abspath(__file__)),
195+
'..', '..', 'targets', 'targets.json'
196+
)
170197

171198
# Current/new location of the 'targets.json' file
172199
__targets_json_location = None
@@ -188,8 +215,10 @@ def get_json_target_data():
188215
for extra_target in Target.__extra_target_json_files:
189216
for k, v in json_file_to_dict(extra_target).items():
190217
if k in targets:
191-
print('WARNING: Custom target "%s" cannot replace existing '
192-
'target.' % k)
218+
print(
219+
'WARNING: Custom target "%s" cannot replace existing '
220+
'target.' % k
221+
)
193222
else:
194223
targets[k] = v
195224
targets[k]["_from_file"] = extra_target
@@ -206,8 +235,10 @@ def add_extra_targets(source_dir):
206235
@staticmethod
207236
def set_targets_json_location(location=None):
208237
"""Set the location of the targets.json file"""
209-
Target.__targets_json_location = (location or
210-
Target.__targets_json_location_default)
238+
Target.__targets_json_location = (
239+
location or
240+
Target.__targets_json_location_default
241+
)
211242
Target.__extra_target_json_files = []
212243
# Invalidate caches, since the location of the JSON file changed
213244
CACHES.clear()
@@ -229,8 +260,10 @@ def __add_paths_to_progen(data):
229260
if isinstance(val, dict):
230261
out[key] = Target.__add_paths_to_progen(val)
231262
elif key == "template":
232-
out[key] = [os.path.join(os.path.dirname(__file__), 'export', v)
233-
for v in val]
263+
out[key] = [
264+
os.path.join(os.path.dirname(__file__), 'export', v)
265+
for v in val
266+
]
234267
else:
235268
out[key] = val
236269
return out
@@ -301,14 +334,13 @@ def __getattr_helper(self, attrname):
301334
return self.__getattr_cumulative(attrname)
302335
else:
303336
tdata = self.json_data
304-
starting_value = None
305337
for tgt in self.resolution_order:
306338
data = tdata[tgt[0]]
307339
try:
308340
return data[attrname]
309341
except KeyError:
310342
pass
311-
else: # Attribute not found
343+
else: # Attribute not found
312344
raise AttributeError(
313345
"Attribute '%s' not found in target '%s'"
314346
% (attrname, self.name))
@@ -328,7 +360,6 @@ def get_target(target_name):
328360
""" Return the target instance starting from the target name """
329361
return target(target_name, Target.get_json_target_data())
330362

331-
332363
@property
333364
def program_cycle_s(self):
334365
"""Special override for program_cycle_s as it's default value depends
@@ -398,19 +429,20 @@ def get_post_build_hook(self, toolchain_labels):
398429
("Static function '%s' " % function_name) +
399430
("required by '%s' " % hook_data["function"]) +
400431
("in target '%s' " % self.name) +
401-
("not found in class '%s'" % class_name))
432+
("not found in class '%s'" % class_name))
402433
# Check if the hook specification also has toolchain restrictions
403434
toolchain_restrictions = set(hook_data.get("toolchains", []))
404435
if toolchain_restrictions and \
405436
not set(toolchain_labels).intersection(toolchain_restrictions):
406437
return None
407438
return getattr(cls, function_name)
408439

409-
################################################################################
440+
###############################################################################
410441
# Target specific code goes in this section
411442
# This code can be invoked from the target description using the
412443
# "post_binary_hook" key
413444

445+
414446
class LPCTargetCode(object):
415447
"""General LPC Target patching code"""
416448
@staticmethod
@@ -419,6 +451,7 @@ def lpc_patch(t_self, resources, elf, binf):
419451
t_self.notify.debug("LPC Patch: %s" % os.path.split(binf)[1])
420452
patch(binf)
421453

454+
422455
class LPC4088Code(object):
423456
"""Code specific to the LPC4088"""
424457
@staticmethod
@@ -450,18 +483,22 @@ def binary_hook(t_self, resources, elf, binf):
450483
# file to 'binf'
451484
shutil.rmtree(binf, True)
452485
os.rename(binf + '.temp', binf)
453-
t_self.notify.debug("Generated custom binary file (internal flash + SPIFI)")
486+
t_self.notify.debug(
487+
"Generated custom binary file (internal flash + SPIFI)"
488+
)
454489
LPCTargetCode.lpc_patch(t_self, resources, elf, binf)
455490

491+
456492
class TEENSY3_1Code(object):
457493
"""Hooks for the TEENSY3.1"""
458494
@staticmethod
459495
def binary_hook(t_self, resources, elf, binf):
460496
"""Hook that is run after elf is generated"""
461-
# This function is referenced by old versions of targets.json and should
462-
# be kept for backwards compatibility.
497+
# This function is referenced by old versions of targets.json and
498+
# should be kept for backwards compatibility.
463499
pass
464500

501+
465502
class MTSCode(object):
466503
"""Generic MTS code"""
467504
@staticmethod
@@ -506,15 +543,16 @@ def combine_bins_mtb_mts_dragonfly(t_self, resources, elf, binf):
506543
"""A hook for the MTB MTS Dragonfly"""
507544
MTSCode._combine_bins_helper("MTB_MTS_DRAGONFLY", binf)
508545

546+
509547
class MCU_NRF51Code(object):
510548
"""NRF51 Hooks"""
511549
@staticmethod
512550
def binary_hook(t_self, resources, _, binf):
513551
"""Hook that merges the soft device with the bin file"""
514552
# Scan to find the actual paths of soft device
515553
sdf = None
516-
for softdevice_and_offset_entry\
517-
in t_self.target.EXPECTED_SOFTDEVICES_WITH_OFFSETS:
554+
sd_with_offsets = t_self.target.EXPECTED_SOFTDEVICES_WITH_OFFSETS
555+
for softdevice_and_offset_entry in sd_with_offsets:
518556
for hexf in resources.get_file_paths(FileType.HEX):
519557
if hexf.find(softdevice_and_offset_entry['name']) != -1:
520558
t_self.notify.debug("SoftDevice file found %s."
@@ -536,8 +574,10 @@ def binary_hook(t_self, resources, _, binf):
536574
if t_self.target.MERGE_BOOTLOADER is True:
537575
for hexf in resources.get_file_paths(FileType.HEX):
538576
if hexf.find(t_self.target.OVERRIDE_BOOTLOADER_FILENAME) != -1:
539-
t_self.notify.debug("Bootloader file found %s."
540-
% t_self.target.OVERRIDE_BOOTLOADER_FILENAME)
577+
t_self.notify.debug(
578+
"Bootloader file found %s."
579+
% t_self.target.OVERRIDE_BOOTLOADER_FILENAME
580+
)
541581
blf = hexf
542582
break
543583
elif hexf.find(softdevice_and_offset_entry['boot']) != -1:
@@ -571,20 +611,23 @@ def binary_hook(t_self, resources, _, binf):
571611
with open(binf.replace(".bin", ".hex"), "w") as fileout:
572612
binh.write_hex_file(fileout, write_start_addr=False)
573613

614+
574615
class NCS36510TargetCode:
575616
@staticmethod
576617
def ncs36510_addfib(t_self, resources, elf, binf):
577618
from tools.targets.NCS import add_fib_at_start
578619
print("binf ", binf)
579620
add_fib_at_start(binf[:-4])
580621

622+
581623
class RTL8195ACode:
582624
"""RTL8195A Hooks"""
583625
@staticmethod
584626
def binary_hook(t_self, resources, elf, binf):
585627
from tools.targets.REALTEK_RTL8195AM import rtl8195a_elf2bin
586628
rtl8195a_elf2bin(t_self, elf, binf)
587629

630+
588631
class PSOC6Code:
589632
@staticmethod
590633
def complete(t_self, resources, elf, binf):
@@ -598,19 +641,27 @@ def complete(t_self, resources, elf, binf):
598641
else:
599642
psoc6_complete(t_self, elf, binf)
600643

644+
601645
class LPC55S69Code:
602646
"""LPC55S69 Hooks"""
603647
@staticmethod
604648
def binary_hook(t_self, resources, elf, binf):
605649
from tools.targets.LPC55S69 import lpc55s69_complete
606650
configured_secure_image_filename = t_self.target.secure_image_filename
607-
secure_bin = find_secure_image(t_self.notify, resources, binf, configured_secure_image_filename, FileType.BIN)
651+
secure_bin = find_secure_image(
652+
t_self.notify,
653+
resources,
654+
binf,
655+
configured_secure_image_filename,
656+
FileType.BIN
657+
)
608658
lpc55s69_complete(t_self, binf, secure_bin)
609659

610-
################################################################################
611660

612-
# Instantiate all public targets
661+
# End Target specific section
662+
###############################################################################
613663
def update_target_data():
664+
"""Instantiate all public targets"""
614665
TARGETS[:] = [Target.get_target(tgt) for tgt, obj
615666
in Target.get_json_target_data().items()
616667
if obj.get("public", True)]
@@ -619,6 +670,7 @@ def update_target_data():
619670
TARGET_MAP.update(dict([(tgt.name, tgt) for tgt in TARGETS]))
620671
TARGET_NAMES[:] = TARGET_MAP.keys()
621672

673+
622674
TARGETS = []
623675
TARGET_MAP = dict()
624676
TARGET_NAMES = []
@@ -628,6 +680,7 @@ def update_target_data():
628680
# Some targets with different name have the same exporters
629681
EXPORT_MAP = {}
630682

683+
631684
# Detection APIs
632685
def get_target_detect_codes():
633686
""" Returns dictionary mapping detect_code -> platform_name
@@ -638,6 +691,7 @@ def get_target_detect_codes():
638691
result[detect_code] = tgt.name
639692
return result
640693

694+
641695
def set_targets_json_location(location=None):
642696
"""Sets the location of the JSON file that contains the targets"""
643697
# First instruct Target about the new location
@@ -647,4 +701,3 @@ def set_targets_json_location(location=None):
647701
# instead. This ensures compatibility with code that does
648702
# "from tools.targets import TARGET_NAMES"
649703
update_target_data()
650-

0 commit comments

Comments
 (0)