def is_valid_value(self, name, value): """Determine whether a value is valid for the named variable. The `value` argument must be a list of strings formatted as they would appear in the namelist (even for scalar variables, in which case the length of the list is always 1). """ name = name.lower() # Separate into a type, optional length, and optional size. type_, max_len, size = self.split_type_string(name) invalid = [] # Check value against type. for scalar in value: if not is_valid_fortran_namelist_literal(type_, scalar): invalid.append(scalar) if len(invalid) > 0: logger.warn("Invalid values %s" % invalid) return False # Now that we know that the strings as input are valid Fortran, do some # canonicalization for further checks. canonical_value = self._canonicalize_value(type_, value) # Check maximum length (if applicable). if max_len is not None: for scalar in canonical_value: if len(scalar) > max_len: return False # Check valid value constraints (if applicable). valid_values = self._valid_values[name] if valid_values is not None: expect( type_ in ('integer', 'character'), "Found valid_values attribute for variable %s with " "type %s, but valid_values only allowed for character " "and integer variables." % (name, type_)) if type_ == 'integer': compare_list = [int(vv) for vv in valid_values] else: compare_list = valid_values for scalar in canonical_value: if scalar not in compare_list: invalid.append(scalar) if len(invalid) > 0: logger.warn("Invalid values %s" % invalid) return False # Check size of input array. if len(expand_literal_list(value)) > size: expect( False, "Value index exceeds variable size for variable %s, allowed array length is %s value array size is %s" % (name, size, len(expand_literal_list(value)))) return True
def is_valid_value(self, name, value): """Determine whether a value is valid for the named variable. The `value` argument must be a list of strings formatted as they would appear in the namelist (even for scalar variables, in which case the length of the list is always 1). """ name = name.lower() # Separate into a type, optional length, and optional size. type_, max_len, size = self.split_type_string(name, self.get_type_info(name)) invalid = [] # Check value against type. for scalar in value: if not is_valid_fortran_namelist_literal(type_, scalar): invalid.append(scalar) if len(invalid) > 0: logger.warn("Invalid values %s"%invalid) return False # Now that we know that the strings as input are valid Fortran, do some # canonicalization for further checks. canonical_value = self._canonicalize_value(type_, value) # Check maximum length (if applicable). if max_len is not None: for scalar in canonical_value: if len(scalar) > max_len: return False # Check valid value constraints (if applicable). valid_values = self.get_valid_values(name) if valid_values is not None: expect(type_ in ('integer', 'character'), "Found valid_values attribute for variable %s with " "type %s, but valid_values only allowed for character " "and integer variables." % (name, type_)) if type_ == 'integer': compare_list = [int(vv) for vv in valid_values] else: compare_list = valid_values for scalar in canonical_value: if scalar not in compare_list: invalid.append(scalar) if len(invalid) > 0: logger.warn("Invalid values %s"%invalid) return False # Check size of input array. if len(expand_literal_list(value)) > size: return False return True