mirror of https://github.com/ARMmbed/mbed-os.git
Generalize parsing types
parent
ee8a02c6ab
commit
52a7f64d67
|
|
@ -10,6 +10,7 @@ import re
|
||||||
import csv
|
import csv
|
||||||
import json
|
import json
|
||||||
import argparse
|
import argparse
|
||||||
|
from utils import argparse_uppercase_type, argparse_lowercase_hyphen_type
|
||||||
from prettytable import PrettyTable
|
from prettytable import PrettyTable
|
||||||
|
|
||||||
debug = False
|
debug = False
|
||||||
|
|
@ -336,6 +337,8 @@ class MemapParser(object):
|
||||||
else:
|
else:
|
||||||
self.object_to_module.update({object_name:module_name})
|
self.object_to_module.update({object_name:module_name})
|
||||||
|
|
||||||
|
export_formats = ["json", "csv-ci", "table"]
|
||||||
|
|
||||||
def generate_output(self, export_format, file_output=None):
|
def generate_output(self, export_format, file_output=None):
|
||||||
"""
|
"""
|
||||||
Generates summary of memory map data
|
Generates summary of memory map data
|
||||||
|
|
@ -451,6 +454,8 @@ class MemapParser(object):
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
toolchains = ["ARM", "ARM_STD", "ARM_MICRO", "GCC_ARM", "IAR"]
|
||||||
|
|
||||||
def parse(self, mapfile, toolchain):
|
def parse(self, mapfile, toolchain):
|
||||||
"""
|
"""
|
||||||
Parse and decode map file depending on the toolchain
|
Parse and decode map file depending on the toolchain
|
||||||
|
|
@ -477,24 +482,6 @@ class MemapParser(object):
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
toolchains = ["ARM", "ARM_STD", "ARM_MICRO", "GCC_ARM", "IAR"]
|
|
||||||
@staticmethod
|
|
||||||
def parse_toolchain(string) :
|
|
||||||
newstring = string.upper().replace("-","_")
|
|
||||||
if newstring not in MemapParser.toolchains:
|
|
||||||
raise (argparse.ArgumentTypeError("{} is not a supported toolchain. Supported toolchains are {}".format(string, ", ".join(MemapParser.toolchains))))
|
|
||||||
else:
|
|
||||||
return newstring
|
|
||||||
|
|
||||||
export_formats = ["json", "csv-ci", "table"]
|
|
||||||
@staticmethod
|
|
||||||
def parse_export_format(string):
|
|
||||||
newstring = string.lower().replace("_","-")
|
|
||||||
if newstring not in MemapParser.export_formats:
|
|
||||||
raise (argparse.ArgumentTypeError("{} is not a supported export format. Supported export formats are {}".format(string, ", ".join(MemapParser.export_formats))))
|
|
||||||
else :
|
|
||||||
return newstring
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
|
||||||
version = '0.3.10'
|
version = '0.3.10'
|
||||||
|
|
@ -505,11 +492,11 @@ def main():
|
||||||
parser.add_argument('file', help='memory map file')
|
parser.add_argument('file', help='memory map file')
|
||||||
|
|
||||||
parser.add_argument('-t', '--toolchain', dest='toolchain', help='select a toolchain used to build the memory map file (%s)' % ", ".join(MemapParser.toolchains),\
|
parser.add_argument('-t', '--toolchain', dest='toolchain', help='select a toolchain used to build the memory map file (%s)' % ", ".join(MemapParser.toolchains),\
|
||||||
required=True, type=MemapParser.parse_toolchain)
|
required=True, type=argparse_uppercase_type(MemapParser.toolchains, "toolchain"))
|
||||||
|
|
||||||
parser.add_argument('-o', '--output', help='output file name', required=False)
|
parser.add_argument('-o', '--output', help='output file name', required=False)
|
||||||
|
|
||||||
parser.add_argument('-e', '--export', dest='export', required=False, default='table', type=MemapParser.parse_export_format,\
|
parser.add_argument('-e', '--export', dest='export', required=False, default='table', type=argparse_lowercase_hyphen_type(MemapParser.export_formats,'export format'),\
|
||||||
help="export format (examples: %s: default)" % ", ".join(MemapParser.export_formats))
|
help="export format (examples: %s: default)" % ", ".join(MemapParser.export_formats))
|
||||||
|
|
||||||
parser.add_argument('-v', '--version', action='version', version=version)
|
parser.add_argument('-v', '--version', action='version', version=version)
|
||||||
|
|
|
||||||
|
|
@ -17,6 +17,7 @@ limitations under the License.
|
||||||
import sys
|
import sys
|
||||||
import inspect
|
import inspect
|
||||||
import os
|
import os
|
||||||
|
import argparse
|
||||||
from os import listdir, remove, makedirs
|
from os import listdir, remove, makedirs
|
||||||
from shutil import copyfile
|
from shutil import copyfile
|
||||||
from os.path import isdir, join, exists, split, relpath, splitext
|
from os.path import isdir, join, exists, split, relpath, splitext
|
||||||
|
|
@ -196,3 +197,23 @@ def dict_to_ascii(input):
|
||||||
def json_file_to_dict(fname):
|
def json_file_to_dict(fname):
|
||||||
with open(fname, "rt") as f:
|
with open(fname, "rt") as f:
|
||||||
return dict_to_ascii(json.load(f, object_pairs_hook=OrderedDict))
|
return dict_to_ascii(json.load(f, object_pairs_hook=OrderedDict))
|
||||||
|
|
||||||
|
# Wowza, double closure
|
||||||
|
def argparse_list_type(casedness, prefer_hyphen=False) :
|
||||||
|
def middle(list, type_name):
|
||||||
|
def parse_type(string):
|
||||||
|
if prefer_hyphen: newstring = casedness(string).replace("_","-")
|
||||||
|
else: newstring = casedness(string).replace("-","_")
|
||||||
|
if string in list:
|
||||||
|
return string
|
||||||
|
elif string not in list and newstring in list:
|
||||||
|
raise argparse.ArgumentTypeError("{0} is not a supported {1}. Did you mean {2}?".format(string, type_name, newstring))
|
||||||
|
else:
|
||||||
|
raise argparse.ArgumentTypeError("{0} is not a supported {1}. Supported {1}s are {2}.".format(string, type_name, ", ".join(list)))
|
||||||
|
return parse_type
|
||||||
|
return middle
|
||||||
|
|
||||||
|
argparse_uppercase_type = argparse_list_type(str.upper, False)
|
||||||
|
argparse_lowercase_type = argparse_list_type(str.lower, False)
|
||||||
|
argparse_uppercase_hyphen_type = argparse_list_type(str.upper, True)
|
||||||
|
argparse_lowercase_hyphen_type = argparse_list_type(str.lower, True)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue