Skip to content

Commit 52a7f64

Browse files
committed
Generalize parsing types
1 parent ee8a02c commit 52a7f64

File tree

2 files changed

+28
-20
lines changed

2 files changed

+28
-20
lines changed

tools/memap.py

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import csv
1111
import json
1212
import argparse
13+
from utils import argparse_uppercase_type, argparse_lowercase_hyphen_type
1314
from prettytable import PrettyTable
1415

1516
debug = False
@@ -336,6 +337,8 @@ def search_objects(self, path, toolchain):
336337
else:
337338
self.object_to_module.update({object_name:module_name})
338339

340+
export_formats = ["json", "csv-ci", "table"]
341+
339342
def generate_output(self, export_format, file_output=None):
340343
"""
341344
Generates summary of memory map data
@@ -451,6 +454,8 @@ def generate_output(self, export_format, file_output=None):
451454

452455
return True
453456

457+
toolchains = ["ARM", "ARM_STD", "ARM_MICRO", "GCC_ARM", "IAR"]
458+
454459
def parse(self, mapfile, toolchain):
455460
"""
456461
Parse and decode map file depending on the toolchain
@@ -477,24 +482,6 @@ def parse(self, mapfile, toolchain):
477482

478483
return True
479484

480-
toolchains = ["ARM", "ARM_STD", "ARM_MICRO", "GCC_ARM", "IAR"]
481-
@staticmethod
482-
def parse_toolchain(string) :
483-
newstring = string.upper().replace("-","_")
484-
if newstring not in MemapParser.toolchains:
485-
raise (argparse.ArgumentTypeError("{} is not a supported toolchain. Supported toolchains are {}".format(string, ", ".join(MemapParser.toolchains))))
486-
else:
487-
return newstring
488-
489-
export_formats = ["json", "csv-ci", "table"]
490-
@staticmethod
491-
def parse_export_format(string):
492-
newstring = string.lower().replace("_","-")
493-
if newstring not in MemapParser.export_formats:
494-
raise (argparse.ArgumentTypeError("{} is not a supported export format. Supported export formats are {}".format(string, ", ".join(MemapParser.export_formats))))
495-
else :
496-
return newstring
497-
498485
def main():
499486

500487
version = '0.3.10'
@@ -505,11 +492,11 @@ def main():
505492
parser.add_argument('file', help='memory map file')
506493

507494
parser.add_argument('-t', '--toolchain', dest='toolchain', help='select a toolchain used to build the memory map file (%s)' % ", ".join(MemapParser.toolchains),\
508-
required=True, type=MemapParser.parse_toolchain)
495+
required=True, type=argparse_uppercase_type(MemapParser.toolchains, "toolchain"))
509496

510497
parser.add_argument('-o', '--output', help='output file name', required=False)
511498

512-
parser.add_argument('-e', '--export', dest='export', required=False, default='table', type=MemapParser.parse_export_format,\
499+
parser.add_argument('-e', '--export', dest='export', required=False, default='table', type=argparse_lowercase_hyphen_type(MemapParser.export_formats,'export format'),\
513500
help="export format (examples: %s: default)" % ", ".join(MemapParser.export_formats))
514501

515502
parser.add_argument('-v', '--version', action='version', version=version)

tools/utils.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import sys
1818
import inspect
1919
import os
20+
import argparse
2021
from os import listdir, remove, makedirs
2122
from shutil import copyfile
2223
from os.path import isdir, join, exists, split, relpath, splitext
@@ -196,3 +197,23 @@ def dict_to_ascii(input):
196197
def json_file_to_dict(fname):
197198
with open(fname, "rt") as f:
198199
return dict_to_ascii(json.load(f, object_pairs_hook=OrderedDict))
200+
201+
# Wowza, double closure
202+
def argparse_list_type(casedness, prefer_hyphen=False) :
203+
def middle(list, type_name):
204+
def parse_type(string):
205+
if prefer_hyphen: newstring = casedness(string).replace("_","-")
206+
else: newstring = casedness(string).replace("-","_")
207+
if string in list:
208+
return string
209+
elif string not in list and newstring in list:
210+
raise argparse.ArgumentTypeError("{0} is not a supported {1}. Did you mean {2}?".format(string, type_name, newstring))
211+
else:
212+
raise argparse.ArgumentTypeError("{0} is not a supported {1}. Supported {1}s are {2}.".format(string, type_name, ", ".join(list)))
213+
return parse_type
214+
return middle
215+
216+
argparse_uppercase_type = argparse_list_type(str.upper, False)
217+
argparse_lowercase_type = argparse_list_type(str.lower, False)
218+
argparse_uppercase_hyphen_type = argparse_list_type(str.upper, True)
219+
argparse_lowercase_hyphen_type = argparse_list_type(str.lower, True)

0 commit comments

Comments
 (0)