def __init__(self, tgt, top_level_dirs=None, app_config=None): """Construct a mbed configuration Positional arguments: target - the name of the mbed target used for this configuration instance Keyword argumets: top_level_dirs - a list of top level source directories (where mbed_app_config.json could be found) app_config - location of a chosen mbed_app.json file NOTE: Construction of a Config object will look for the application configuration file in top_level_dirs. If found once, it'll parse it. top_level_dirs may be None (in this case, the constructor will not search for a configuration file). """ app_config_location = app_config if app_config_location is None: for directory in top_level_dirs or []: full_path = os.path.join(directory, self.__mbed_app_config_name) if os.path.isfile(full_path): if app_config_location is not None: raise ConfigException("Duplicate '%s' file in '%s' and '%s'" % (self.__mbed_app_config_name, app_config_location, full_path)) else: app_config_location = full_path try: self.app_config_data = json_file_to_dict(app_config_location) \ if app_config_location else {} except ValueError as exc: sys.stderr.write(str(exc) + "\n") self.app_config_data = {} # Check the keys in the application configuration data unknown_keys = set(self.app_config_data.keys()) - \ self.__allowed_keys["application"] if unknown_keys: raise ConfigException("Unknown key(s) '%s' in %s" % (",".join(unknown_keys), self.__mbed_app_config_name)) # Update the list of targets with the ones defined in the application # config, if applicable self.lib_config_data = {} # Make sure that each config is processed only once self.processed_configs = {} if isinstance(tgt, basestring): if tgt in TARGET_MAP: self.target = TARGET_MAP[tgt] else: self.target = generate_py_target( self.app_config_data.get("custom_targets", {}), tgt) else: self.target = tgt self.target = deepcopy(self.target) self.target_labels = self.target.labels self.cumulative_overrides = {key: ConfigCumulativeOverride(key) for key in CUMULATIVE_ATTRIBUTES} self._process_config_and_overrides(self.app_config_data, {}, "app", "application") self.config_errors = None
def __init__(self, tgt, top_level_dirs=None, app_config=None): """Construct a mbed configuration Positional arguments: target - the name of the mbed target used for this configuration instance Keyword argumets: top_level_dirs - a list of top level source directories (where mbed_app_config.json could be found) app_config - location of a chosen mbed_app.json file NOTE: Construction of a Config object will look for the application configuration file in top_level_dirs. If found once, it'll parse it. top_level_dirs may be None (in this case, the constructor will not search for a configuration file). """ config_errors = [] self.app_config_location = app_config if self.app_config_location is None: for directory in top_level_dirs or []: full_path = os.path.join(directory, self.__mbed_app_config_name) if os.path.isfile(full_path): if self.app_config_location is not None: raise ConfigException("Duplicate '%s' file in '%s' and '%s'" % (self.__mbed_app_config_name, self.app_config_location, full_path)) else: self.app_config_location = full_path try: self.app_config_data = json_file_to_dict(self.app_config_location) \ if self.app_config_location else {} except ValueError as exc: self.app_config_data = {} config_errors.append( ConfigException("Could not parse mbed app configuration from %s" % self.app_config_location)) # Check the keys in the application configuration data unknown_keys = set(self.app_config_data.keys()) - \ set(self.__allowed_keys["application"].keys()) if unknown_keys: raise ConfigException("Unknown key(s) '%s' in %s" % (",".join(unknown_keys), self.__mbed_app_config_name)) check_dict_types(self.app_config_data, self.__allowed_keys["application"], "app-config") # Update the list of targets with the ones defined in the application # config, if applicable self.lib_config_data = {} # Make sure that each config is processed only once self.processed_configs = {} if isinstance(tgt, basestring): if tgt in TARGET_MAP: self.target = TARGET_MAP[tgt] else: self.target = generate_py_target( self.app_config_data.get("custom_targets", {}), tgt) else: self.target = tgt self.target = deepcopy(self.target) self.target_labels = self.target.labels self.cumulative_overrides = {key: ConfigCumulativeOverride(key) for key in CUMULATIVE_ATTRIBUTES} self._process_config_and_overrides(self.app_config_data, {}, "app", "application") self.config_errors = config_errors
def __init__(self, tgt, top_level_dirs=None, app_config=None): """Construct a mbed configuration Positional arguments: target - the name of the mbed target used for this configuration instance Keyword argumets: top_level_dirs - a list of top level source directories (where mbed_app_config.json could be found) app_config - location of a chosen mbed_app.json file NOTE: Construction of a Config object will look for the application configuration file in top_level_dirs. If found once, it'll parse it. top_level_dirs may be None (in this case, the constructor will not search for a configuration file). """ config_errors = [] self.app_config_location = app_config if self.app_config_location is None and top_level_dirs: self.app_config_location = self.find_app_config(top_level_dirs) try: self.app_config_data = json_file_to_dict(self.app_config_location) \ if self.app_config_location else {} except ValueError as exc: self.app_config_data = {} config_errors.append( ConfigException( "Could not parse mbed app configuration from %s" % self.app_config_location)) if self.app_config_location is not None: # Validate the format of the JSON file based on schema_app.json schema_root = os.path.dirname(os.path.abspath(__file__)) schema_path = os.path.join(schema_root, "schema_app.json") schema = json_file_to_dict(schema_path) url = moves.urllib.request.pathname2url(schema_path) uri = moves.urllib_parse.urljoin("file://", url) resolver = RefResolver(uri, schema) validator = Draft4Validator(schema, resolver=resolver) errors = sorted(validator.iter_errors(self.app_config_data)) if errors: raise ConfigException(",".join(x.message for x in errors)) # Update the list of targets with the ones defined in the application # config, if applicable self.lib_config_data = {} # Make sure that each config is processed only once self.processed_configs = {} if isinstance(tgt, basestring): if tgt in TARGET_MAP: self.target = TARGET_MAP[tgt] else: self.target = generate_py_target( self.app_config_data.get("custom_targets", {}), tgt) else: self.target = tgt self.target = deepcopy(self.target) self.target_labels = self.target.labels for override in BOOTLOADER_OVERRIDES: _, attr = override.split(".") setattr(self.target, attr, None) self.cumulative_overrides = { key: ConfigCumulativeOverride(key) for key in CUMULATIVE_ATTRIBUTES } self._process_config_and_overrides(self.app_config_data, {}, "app", "application") self.config_errors = config_errors