@@ -332,6 +332,12 @@ def _process_macros(mlist, macros, unit_name, unit_kind):
332
332
macros [macro .macro_name ] = macro
333
333
334
334
335
+ def check_dict_types (dict , type_dict , dict_loc ):
336
+ for key , value in dict .iteritems ():
337
+ if not isinstance (value , type_dict [key ]):
338
+ raise ConfigException ("The value of %s.%s is not of type %s" %
339
+ (dict_loc , key , type_dict [key ].__name__ ))
340
+
335
341
Region = namedtuple ("Region" , "name start size active filename" )
336
342
337
343
class Config (object ):
@@ -342,14 +348,14 @@ class Config(object):
342
348
__mbed_app_config_name = "mbed_app.json"
343
349
__mbed_lib_config_name = "mbed_lib.json"
344
350
345
- # Allowed keys in configuration dictionaries
351
+ # Allowed keys in configuration dictionaries, and their types
346
352
# (targets can have any kind of keys, so this validation is not applicable
347
353
# to them)
348
354
__allowed_keys = {
349
- "library" : set ([ "name" , "config" , "target_overrides" , "macros" ,
350
- "__config_path" ]) ,
351
- "application" : set ([ "config" , "target_overrides" ,
352
- "macros" , "__config_path" ])
355
+ "library" : { "name" : str , "config" : dict , "target_overrides" : dict ,
356
+ "macros" : list , "__config_path" : str } ,
357
+ "application" : { "config" : dict , "target_overrides" : dict ,
358
+ "macros" : list , "__config_path" : str }
353
359
}
354
360
355
361
__unused_overrides = set (["target.bootloader_img" , "target.restrict_size" ])
@@ -398,11 +404,13 @@ def __init__(self, tgt, top_level_dirs=None, app_config=None):
398
404
399
405
# Check the keys in the application configuration data
400
406
unknown_keys = set (self .app_config_data .keys ()) - \
401
- self .__allowed_keys ["application" ]
407
+ set ( self .__allowed_keys ["application" ]. keys ())
402
408
if unknown_keys :
403
409
raise ConfigException ("Unknown key(s) '%s' in %s" %
404
410
("," .join (unknown_keys ),
405
411
self .__mbed_app_config_name ))
412
+ check_dict_types (self .app_config_data , self .__allowed_keys ["application" ],
413
+ "app-config" )
406
414
# Update the list of targets with the ones defined in the application
407
415
# config, if applicable
408
416
self .lib_config_data = {}
@@ -542,19 +550,34 @@ def _process_config_and_overrides(self, data, params, unit_name, unit_kind):
542
550
# Parse out cumulative overrides
543
551
for attr , cumulatives in self .cumulative_overrides .iteritems ():
544
552
if 'target.' + attr in overrides :
545
- cumulatives .strict_cumulative_overrides (
546
- overrides ['target.' + attr ])
547
- del overrides ['target.' + attr ]
553
+ key = 'target.' + attr
554
+ if not isinstance (overrides [key ], list ):
555
+ raise ConfigException (
556
+ "The value of %s.%s is not of type %s" %
557
+ (unit_name , "target_overrides." + key ,
558
+ "list" ))
559
+ cumulatives .strict_cumulative_overrides (overrides [key ])
560
+ del overrides [key ]
548
561
549
562
if 'target.' + attr + '_add' in overrides :
550
- cumulatives .add_cumulative_overrides (
551
- overrides ['target.' + attr + '_add' ])
552
- del overrides ['target.' + attr + '_add' ]
563
+ key = 'target.' + attr + "_add"
564
+ if not isinstance (overrides [key ], list ):
565
+ raise ConfigException (
566
+ "The value of %s.%s is not of type %s" %
567
+ (unit_name , "target_overrides." + key ,
568
+ "list" ))
569
+ cumulatives .add_cumulative_overrides (overrides [key ])
570
+ del overrides [key ]
553
571
554
572
if 'target.' + attr + '_remove' in overrides :
555
- cumulatives .remove_cumulative_overrides (
556
- overrides ['target.' + attr + '_remove' ])
557
- del overrides ['target.' + attr + '_remove' ]
573
+ key = 'target.' + attr + "_remove"
574
+ if not isinstance (overrides [key ], list ):
575
+ raise ConfigException (
576
+ "The value of %s.%s is not of type %s" %
577
+ (unit_name , "target_overrides." + key ,
578
+ "list" ))
579
+ cumulatives .remove_cumulative_overrides (overrides [key ])
580
+ del overrides [key ]
558
581
559
582
# Consider the others as overrides
560
583
for name , val in overrides .items ():
@@ -639,10 +662,12 @@ def get_lib_config_data(self):
639
662
"""
640
663
all_params , macros = {}, {}
641
664
for lib_name , lib_data in self .lib_config_data .items ():
642
- unknown_keys = set (lib_data .keys ()) - self .__allowed_keys ["library" ]
665
+ unknown_keys = (set (lib_data .keys ()) -
666
+ set (self .__allowed_keys ["library" ].keys ()))
643
667
if unknown_keys :
644
668
raise ConfigException ("Unknown key(s) '%s' in %s" %
645
669
("," .join (unknown_keys ), lib_name ))
670
+ check_dict_types (lib_data , self .__allowed_keys ["library" ], lib_name )
646
671
all_params .update (self ._process_config_and_overrides (lib_data , {},
647
672
lib_name ,
648
673
"library" ))
0 commit comments