示例#1
0
    def dict_to_namelist(self, dict_, filename=None):
        """Converts a dictionary of name-value pairs to a `Namelist`.

        The input is assumed to be similar to the output of `parse` when
        `groupless=True` is set. This function uses the namelist definition file
        to look up the namelist group associated with each variable, and uses
        this information to create a true `Namelist` object.

        The optional `filename` argument can be used to assist in error
        reporting when the namelist comes from a specific, known file.
        """
        # Improve error reporting when a file name is provided.
        if filename is None:
            variable_template = "Variable %rs"
        else:
            variable_template = "Variable %r from file " + repr(str(filename))
        groups = {}
        for variable_name in dict_:
            variable_lc = variable_name.lower()
            qualified_varname = get_fortran_name_only(variable_lc)
            self._expect_variable_in_definition(qualified_varname, variable_template)
            group_name = self.get_group(qualified_varname)
            expect (group_name is not None, "No group found for var %s"%variable_lc)
            if group_name not in groups:
                groups[group_name] = collections.OrderedDict()
            groups[group_name][variable_lc] = dict_[variable_name]
        return Namelist(groups)
示例#2
0
    def dict_to_namelist(self, dict_, filename=None):
        """Converts a dictionary of name-value pairs to a `Namelist`.

        The input is assumed to be similar to the output of `parse` when
        `groupless=True` is set. This function uses the namelist definition file
        to look up the namelist group associated with each variable, and uses
        this information to create a true `Namelist` object.

        The optional `filename` argument can be used to assist in error
        reporting when the namelist comes from a specific, known file.
        """
        # Improve error reporting when a file name is provided.
        if filename is None:
            variable_template = "Variable {!s}"
        else:
            variable_template = "Variable {!r} from file " + repr(str(filename))
        groups = {}
        for variable_name in dict_:
            variable_lc = variable_name.lower()
            qualified_varname = get_fortran_name_only(variable_lc)
            self._expect_variable_in_definition(qualified_varname, variable_template)
            group_name = self.get_group(qualified_varname)
            expect (group_name is not None, "No group found for var {}".format(variable_lc))
            if group_name not in groups:
                groups[group_name] = collections.OrderedDict()
            groups[group_name][variable_lc] = dict_[variable_name]
        return Namelist(groups)
示例#3
0
    def validate(self, namelist,filename=None):
        """Validate a namelist object against this definition.

        The optional `filename` argument can be used to assist in error
        reporting when the namelist comes from a specific, known file.
        """
        # Improve error reporting when a file name is provided.
        if filename is None:
            variable_template = "Variable %r"
        else:
            variable_template = "Variable %r from file " + repr(str(filename))

        # Iterate through variables.
        for group_name in namelist.get_group_names():
            for variable_name in namelist.get_variable_names(group_name):
                # Check that the variable is defined...
                qualified_variable_name = get_fortran_name_only(variable_name)
                self._expect_variable_in_definition(qualified_variable_name, variable_template)

                # Check if can actually change this variable via filename change
                if filename is not None:
                    self._user_modifiable_in_variable_definition(qualified_variable_name)

                # and has the right group name...
                var_group = self.get_group(qualified_variable_name)
                expect(var_group == group_name,
                       (variable_template + " is in a group named %r, but should be in %r.") %
                       (str(variable_name), str(group_name), str(var_group)))

                # and has a valid value.
                value = namelist.get_variable_value(group_name, variable_name)
                expect(self.is_valid_value(qualified_variable_name, value),
                       (variable_template + " has invalid value %r.") %
                       (str(variable_name), [str(scalar) for scalar in value]))
示例#4
0
    def validate(self, namelist,filename=None):
        """Validate a namelist object against this definition.

        The optional `filename` argument can be used to assist in error
        reporting when the namelist comes from a specific, known file.
        """
        # Improve error reporting when a file name is provided.
        if filename is None:
            variable_template = "Variable {!r}"
        else:
            variable_template = "Variable {!r} from file " + repr(str(filename))

        # Iterate through variables.
        for group_name in namelist.get_group_names():
            for variable_name in namelist.get_variable_names(group_name):
                # Check that the variable is defined...
                qualified_variable_name = get_fortran_name_only(variable_name)
                self._expect_variable_in_definition(qualified_variable_name, variable_template)

                # Check if can actually change this variable via filename change
                if filename is not None:
                    self._user_modifiable_in_variable_definition(qualified_variable_name)

                # and has the right group name...
                var_group = self.get_group(qualified_variable_name)
                expect(var_group == group_name,
                       (variable_template + " is in a group named {!r}, but should be in {!r}.").format(str(variable_name), str(group_name), str(var_group)))

                # and has a valid value.
                value = namelist.get_variable_value(group_name, variable_name)
                expect(self.is_valid_value(qualified_variable_name, value),
                       (variable_template + " has invalid value {!r}.").format(str(variable_name), [str(scalar) for scalar in value]))