def _parse_template_parameter(where: str, raw: object) -> TemplateParameter: rd = check_keys(raw, where, ['name', 'desc', 'type'], ['default']) name = check_str(rd['name'], 'name field of ' + where) r_desc = rd.get('desc') if r_desc is None: desc = None else: desc = check_str(r_desc, 'desc field of ' + where) r_type = rd.get('type') param_type = check_str(r_type, 'type field of ' + where) if param_type not in TemplateParameter.VALID_PARAM_TYPES: raise ValueError('At {}, the {} param has an invalid type field {!r}. ' 'Allowed values are: {}.'.format( where, name, param_type, ', '.join(TemplateParameter.VALID_PARAM_TYPES))) r_default = rd.get('default') if param_type == 'int': default = check_int( r_default, 'default field of {}, (an integer parameter)'.format(name)) elif param_type == 'string': default = check_str(r_default, 'default field of ' + where) elif param_type == 'object': default = IpConfig._check_object(r_default, 'default field of ' + where) else: assert False, f"Unknown parameter type found: {param_type!r}" return TemplateParameter(name, desc, param_type, default)
def _parse_template_parameter(where: str, raw: object) -> TemplateParameter: rd = check_keys(raw, where, ['name', 'desc', 'type'], ['default']) name = check_str(rd['name'], 'name field of ' + where) r_desc = rd.get('desc') if r_desc is None: desc = None else: desc = check_str(r_desc, 'desc field of ' + where) r_type = rd.get('type') param_type = check_str(r_type, 'type field of ' + where) if not param_type in ('string', 'int'): raise ValueError('At {}, the {} param has an invalid type field {!r}. ' 'Allowed values are: string, int.'.format( where, name, param_type)) r_default = rd.get('default') default = check_str(r_default, 'default field of ' + where) if param_type[:3] == 'int': check_int(default, 'default field of {}, (an integer parameter)'.format(name)) return TemplateParameter(name, desc, param_type, default)
def from_raw(cls, raw: object, where: str) -> 'IpConfig': """ Load an IpConfig from a raw object """ rd = check_keys(raw, 'configuration file ' + where, ['instance_name'], ['param_values']) instance_name = check_name(rd.get('instance_name'), "the key 'instance_name' of " + where) param_values = check_param_dict(rd.get('param_values', []), f"the key 'param_values' of {where}") return cls(instance_name, param_values)
def from_raw(cls, template_params: TemplateParams, raw: object, where: str) -> 'IpConfig': """ Load an IpConfig from a raw object """ rd = check_keys(raw, 'configuration file ' + where, ['instance_name'], ['param_values']) instance_name = check_name(rd.get('instance_name'), "the key 'instance_name' of " + where) if not isinstance(raw, dict): raise ValueError( "The IP configuration is expected to be a dict, but was " "actually a " + type(raw).__name__) param_values = IpConfig._check_param_values(template_params, rd['param_values']) return cls(template_params, instance_name, param_values)