Python2+3: clean argparse

pull/5848/head
Jimmy Brisson 2018-01-12 13:36:32 -06:00
parent 10a91216f5
commit 7abeec9744
3 changed files with 28 additions and 16 deletions

View File

@ -34,6 +34,10 @@ from tools.arm_pack_manager import Cache
from tools.targets import CUMULATIVE_ATTRIBUTES, TARGET_MAP, \
generate_py_target, get_resolution_order, Target
try:
unicode
except NameError:
unicode = str
PATH_OVERRIDES = set(["target.bootloader_img"])
BOOTLOADER_OVERRIDES = set(["target.bootloader_img", "target.restrict_size",
"target.mbed_app_start", "target.mbed_app_size"])

View File

@ -1275,7 +1275,7 @@ class mbedToolchain:
self.config_file = join(self.build_dir, self.MBED_CONFIG_FILE_NAME)
# If the file exists, read its current content in prev_data
if exists(self.config_file):
with open(self.config_file, "rt") as f:
with open(self.config_file, "r") as f:
prev_data = f.read()
else:
prev_data = None
@ -1289,12 +1289,13 @@ class mbedToolchain:
self.config_file = None # this means "config file not present"
changed = True
elif crt_data != prev_data: # different content of config file
with open(self.config_file, "wt") as f:
print("changed!")
with open(self.config_file, "w") as f:
f.write(crt_data)
changed = True
else: # a previous mbed_config.h does not exist
if crt_data is not None: # there's configuration data available
with open(self.config_file, "wt") as f:
with open(self.config_file, "w") as f:
f.write(crt_data)
changed = True
else:
@ -1606,11 +1607,11 @@ from tools.toolchains.gcc import GCC_ARM
from tools.toolchains.iar import IAR
TOOLCHAIN_CLASSES = {
'ARM': ARM_STD,
'uARM': ARM_MICRO,
'ARMC6': ARMC6,
'GCC_ARM': GCC_ARM,
'IAR': IAR
u'ARM': ARM_STD,
u'uARM': ARM_MICRO,
u'ARMC6': ARMC6,
u'GCC_ARM': GCC_ARM,
u'IAR': IAR
}
TOOLCHAINS = set(TOOLCHAIN_CLASSES.keys())

View File

@ -381,6 +381,8 @@ def argparse_type(casedness, prefer_hyphen=False):
the string, or the hyphens/underscores do not match the expected
style of the argument.
"""
if not isinstance(string, unicode):
string = string.decode()
if prefer_hyphen:
newstring = casedness(string).replace("_", "-")
else:
@ -399,10 +401,10 @@ def argparse_type(casedness, prefer_hyphen=False):
return middle
# short cuts for the argparse_type versions
argparse_uppercase_type = argparse_type(str.upper, False)
argparse_lowercase_type = argparse_type(str.lower, False)
argparse_uppercase_hyphen_type = argparse_type(str.upper, True)
argparse_lowercase_hyphen_type = argparse_type(str.lower, True)
argparse_uppercase_type = argparse_type(unicode.upper, False)
argparse_lowercase_type = argparse_type(unicode.lower, False)
argparse_uppercase_hyphen_type = argparse_type(unicode.upper, True)
argparse_lowercase_hyphen_type = argparse_type(unicode.lower, True)
def argparse_force_type(case):
""" validate that an argument passed in (as string) is a member of the list
@ -412,9 +414,14 @@ def argparse_force_type(case):
""" The parser type generator"""
def parse_type(string):
""" The parser type"""
if not isinstance(string, unicode):
string = string.decode()
for option in lst:
if case(string) == case(option):
return option
try:
if case(string) == case(option):
return option
except Exception as e:
print(e)
raise argparse.ArgumentTypeError(
"{0} is not a supported {1}. Supported {1}s are:\n{2}".
format(string, type_name, columnate(lst)))
@ -422,8 +429,8 @@ def argparse_force_type(case):
return middle
# these two types convert the case of their arguments _before_ validation
argparse_force_uppercase_type = argparse_force_type(str.upper)
argparse_force_lowercase_type = argparse_force_type(str.lower)
argparse_force_uppercase_type = argparse_force_type(unicode.upper)
argparse_force_lowercase_type = argparse_force_type(unicode.lower)
def argparse_many(func):
""" An argument parser combinator that takes in an argument parser and