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, \ from tools.targets import CUMULATIVE_ATTRIBUTES, TARGET_MAP, \
generate_py_target, get_resolution_order, Target generate_py_target, get_resolution_order, Target
try:
unicode
except NameError:
unicode = str
PATH_OVERRIDES = set(["target.bootloader_img"]) PATH_OVERRIDES = set(["target.bootloader_img"])
BOOTLOADER_OVERRIDES = set(["target.bootloader_img", "target.restrict_size", BOOTLOADER_OVERRIDES = set(["target.bootloader_img", "target.restrict_size",
"target.mbed_app_start", "target.mbed_app_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) self.config_file = join(self.build_dir, self.MBED_CONFIG_FILE_NAME)
# If the file exists, read its current content in prev_data # If the file exists, read its current content in prev_data
if exists(self.config_file): 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() prev_data = f.read()
else: else:
prev_data = None prev_data = None
@ -1289,12 +1289,13 @@ class mbedToolchain:
self.config_file = None # this means "config file not present" self.config_file = None # this means "config file not present"
changed = True changed = True
elif crt_data != prev_data: # different content of config file 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) f.write(crt_data)
changed = True changed = True
else: # a previous mbed_config.h does not exist else: # a previous mbed_config.h does not exist
if crt_data is not None: # there's configuration data available 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) f.write(crt_data)
changed = True changed = True
else: else:
@ -1606,11 +1607,11 @@ from tools.toolchains.gcc import GCC_ARM
from tools.toolchains.iar import IAR from tools.toolchains.iar import IAR
TOOLCHAIN_CLASSES = { TOOLCHAIN_CLASSES = {
'ARM': ARM_STD, u'ARM': ARM_STD,
'uARM': ARM_MICRO, u'uARM': ARM_MICRO,
'ARMC6': ARMC6, u'ARMC6': ARMC6,
'GCC_ARM': GCC_ARM, u'GCC_ARM': GCC_ARM,
'IAR': IAR u'IAR': IAR
} }
TOOLCHAINS = set(TOOLCHAIN_CLASSES.keys()) 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 the string, or the hyphens/underscores do not match the expected
style of the argument. style of the argument.
""" """
if not isinstance(string, unicode):
string = string.decode()
if prefer_hyphen: if prefer_hyphen:
newstring = casedness(string).replace("_", "-") newstring = casedness(string).replace("_", "-")
else: else:
@ -399,10 +401,10 @@ def argparse_type(casedness, prefer_hyphen=False):
return middle return middle
# short cuts for the argparse_type versions # short cuts for the argparse_type versions
argparse_uppercase_type = argparse_type(str.upper, False) argparse_uppercase_type = argparse_type(unicode.upper, False)
argparse_lowercase_type = argparse_type(str.lower, False) argparse_lowercase_type = argparse_type(unicode.lower, False)
argparse_uppercase_hyphen_type = argparse_type(str.upper, True) argparse_uppercase_hyphen_type = argparse_type(unicode.upper, True)
argparse_lowercase_hyphen_type = argparse_type(str.lower, True) argparse_lowercase_hyphen_type = argparse_type(unicode.lower, True)
def argparse_force_type(case): def argparse_force_type(case):
""" validate that an argument passed in (as string) is a member of the list """ 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""" """ The parser type generator"""
def parse_type(string): def parse_type(string):
""" The parser type""" """ The parser type"""
if not isinstance(string, unicode):
string = string.decode()
for option in lst: for option in lst:
if case(string) == case(option): try:
return option if case(string) == case(option):
return option
except Exception as e:
print(e)
raise argparse.ArgumentTypeError( raise argparse.ArgumentTypeError(
"{0} is not a supported {1}. Supported {1}s are:\n{2}". "{0} is not a supported {1}. Supported {1}s are:\n{2}".
format(string, type_name, columnate(lst))) format(string, type_name, columnate(lst)))
@ -422,8 +429,8 @@ def argparse_force_type(case):
return middle return middle
# these two types convert the case of their arguments _before_ validation # these two types convert the case of their arguments _before_ validation
argparse_force_uppercase_type = argparse_force_type(str.upper) argparse_force_uppercase_type = argparse_force_type(unicode.upper)
argparse_force_lowercase_type = argparse_force_type(str.lower) argparse_force_lowercase_type = argparse_force_type(unicode.lower)
def argparse_many(func): def argparse_many(func):
""" An argument parser combinator that takes in an argument parser and """ An argument parser combinator that takes in an argument parser and