Skip to content

Commit 7abeec9

Browse files
committed
Python2+3: clean argparse
1 parent 10a9121 commit 7abeec9

File tree

3 files changed

+28
-16
lines changed

3 files changed

+28
-16
lines changed

tools/config/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@
3434
from tools.targets import CUMULATIVE_ATTRIBUTES, TARGET_MAP, \
3535
generate_py_target, get_resolution_order, Target
3636

37+
try:
38+
unicode
39+
except NameError:
40+
unicode = str
3741
PATH_OVERRIDES = set(["target.bootloader_img"])
3842
BOOTLOADER_OVERRIDES = set(["target.bootloader_img", "target.restrict_size",
3943
"target.mbed_app_start", "target.mbed_app_size"])

tools/toolchains/__init__.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1275,7 +1275,7 @@ def get_config_header(self):
12751275
self.config_file = join(self.build_dir, self.MBED_CONFIG_FILE_NAME)
12761276
# If the file exists, read its current content in prev_data
12771277
if exists(self.config_file):
1278-
with open(self.config_file, "rt") as f:
1278+
with open(self.config_file, "r") as f:
12791279
prev_data = f.read()
12801280
else:
12811281
prev_data = None
@@ -1289,12 +1289,13 @@ def get_config_header(self):
12891289
self.config_file = None # this means "config file not present"
12901290
changed = True
12911291
elif crt_data != prev_data: # different content of config file
1292-
with open(self.config_file, "wt") as f:
1292+
print("changed!")
1293+
with open(self.config_file, "w") as f:
12931294
f.write(crt_data)
12941295
changed = True
12951296
else: # a previous mbed_config.h does not exist
12961297
if crt_data is not None: # there's configuration data available
1297-
with open(self.config_file, "wt") as f:
1298+
with open(self.config_file, "w") as f:
12981299
f.write(crt_data)
12991300
changed = True
13001301
else:
@@ -1606,11 +1607,11 @@ def report(self):
16061607
from tools.toolchains.iar import IAR
16071608

16081609
TOOLCHAIN_CLASSES = {
1609-
'ARM': ARM_STD,
1610-
'uARM': ARM_MICRO,
1611-
'ARMC6': ARMC6,
1612-
'GCC_ARM': GCC_ARM,
1613-
'IAR': IAR
1610+
u'ARM': ARM_STD,
1611+
u'uARM': ARM_MICRO,
1612+
u'ARMC6': ARMC6,
1613+
u'GCC_ARM': GCC_ARM,
1614+
u'IAR': IAR
16141615
}
16151616

16161617
TOOLCHAINS = set(TOOLCHAIN_CLASSES.keys())

tools/utils.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,8 @@ def parse_type(string):
381381
the string, or the hyphens/underscores do not match the expected
382382
style of the argument.
383383
"""
384+
if not isinstance(string, unicode):
385+
string = string.decode()
384386
if prefer_hyphen:
385387
newstring = casedness(string).replace("_", "-")
386388
else:
@@ -399,10 +401,10 @@ def parse_type(string):
399401
return middle
400402

401403
# short cuts for the argparse_type versions
402-
argparse_uppercase_type = argparse_type(str.upper, False)
403-
argparse_lowercase_type = argparse_type(str.lower, False)
404-
argparse_uppercase_hyphen_type = argparse_type(str.upper, True)
405-
argparse_lowercase_hyphen_type = argparse_type(str.lower, True)
404+
argparse_uppercase_type = argparse_type(unicode.upper, False)
405+
argparse_lowercase_type = argparse_type(unicode.lower, False)
406+
argparse_uppercase_hyphen_type = argparse_type(unicode.upper, True)
407+
argparse_lowercase_hyphen_type = argparse_type(unicode.lower, True)
406408

407409
def argparse_force_type(case):
408410
""" validate that an argument passed in (as string) is a member of the list
@@ -412,18 +414,23 @@ def middle(lst, type_name):
412414
""" The parser type generator"""
413415
def parse_type(string):
414416
""" The parser type"""
417+
if not isinstance(string, unicode):
418+
string = string.decode()
415419
for option in lst:
416-
if case(string) == case(option):
417-
return option
420+
try:
421+
if case(string) == case(option):
422+
return option
423+
except Exception as e:
424+
print(e)
418425
raise argparse.ArgumentTypeError(
419426
"{0} is not a supported {1}. Supported {1}s are:\n{2}".
420427
format(string, type_name, columnate(lst)))
421428
return parse_type
422429
return middle
423430

424431
# these two types convert the case of their arguments _before_ validation
425-
argparse_force_uppercase_type = argparse_force_type(str.upper)
426-
argparse_force_lowercase_type = argparse_force_type(str.lower)
432+
argparse_force_uppercase_type = argparse_force_type(unicode.upper)
433+
argparse_force_lowercase_type = argparse_force_type(unicode.lower)
427434

428435
def argparse_many(func):
429436
""" An argument parser combinator that takes in an argument parser and

0 commit comments

Comments
 (0)