Пример #1
0
    def add_default(self, name, value=None):
        """Add a value for the specified variable to the namelist.

        If the specified variable is already defined in the object, the existing
        value is preserved. Otherwise, the `value` argument, if provided, will
        be used to set the value. If no such value is found, the defaults file
        will be consulted. If null values are present in any of the above, the
        result will be a merged array of values.

        If no value for the variable is found via any of the above, this method
        will raise an exception.
        """
        group = self._definition.get_node_element_info(name, "group")

        # Use this to see if we need to raise an error when nothing is found.
        have_value = False

        # Check for existing value.
        current_literals = self._namelist.get_variable_value(group, name)
        if current_literals != [""]:
            have_value = True

        # Check for input argument.
        if value is not None:
            have_value = True
            literals = self._to_namelist_literals(
                name, value
            )  #FIXME - this is where an array is compressed into a 3*value
            current_literals = merge_literal_lists(literals, current_literals)

        # Check for default value.
        default = self.get_default(name, allow_none=True)
        if default is not None:
            have_value = True
            default_literals = self._to_namelist_literals(name, default)
            current_literals = merge_literal_lists(default_literals,
                                                   current_literals)
        expect(have_value, "No default value found for %s." % name)

        # Go through file names and prepend input data root directory for
        # absolute pathnames.
        var_input_pathname = self._definition.get_input_pathname(name)
        if var_input_pathname == 'abs':
            current_literals = expand_literal_list(current_literals)
            for i, literal in enumerate(current_literals):
                if literal == '':
                    continue
                file_path = character_literal_to_string(literal)
                if file_path == 'UNSET' or file_path == 'idmap':
                    continue
                if file_path == 'null':
                    continue
                file_path = self.set_abs_file_path(file_path)
                expect(os.path.exists(file_path),
                       "File not found: %s = %s" % (name, literal))
                current_literals[i] = string_to_character_literal(file_path)
            current_literals = compress_literal_list(current_literals)

        # Set the new value.
        self._namelist.set_variable_value(group, name, current_literals)
Пример #2
0
    def add_default(self, name, value=None, ignore_abs_path=None):
        """Add a value for the specified variable to the namelist.

        If the specified variable is already defined in the object, the existing
        value is preserved. Otherwise, the `value` argument, if provided, will
        be used to set the value. If no such value is found, the defaults file
        will be consulted. If null values are present in any of the above, the
        result will be a merged array of values.

        If no value for the variable is found via any of the above, this method
        will raise an exception.
        """
        # pylint: disable=protected-access
        group = self._definition.get_group(name)

        # Use this to see if we need to raise an error when nothing is found.
        have_value = False
        # Check for existing value.
        current_literals = self._namelist.get_variable_value(group, name)

        # Check for input argument.
        if value is not None:
            have_value = True
            # if compression were to occur, this is where it does
            literals = self._to_namelist_literals(name, value)
            current_literals = merge_literal_lists(literals, current_literals)

        # Check for default value.
        default = self.get_default(name, allow_none=True)
        if default is not None:
            have_value = True
            default_literals = self._to_namelist_literals(name, default)
            current_literals = merge_literal_lists(default_literals, current_literals)
        expect(have_value, "No default value found for {}.".format(name))

        # Go through file names and prepend input data root directory for
        # absolute pathnames.
        var_type, _, var_size = self._definition.split_type_string(name)
        if var_type == "character" and ignore_abs_path is None:
            var_input_pathname = self._definition.get_input_pathname(name)
            if var_input_pathname == 'abs':
                current_literals = expand_literal_list(current_literals)
                for i, literal in enumerate(current_literals):
                    if literal == '':
                        continue
                    file_path = character_literal_to_string(literal)
                    # NOTE - these are hard-coded here and a better way is to make these extensible
                    if file_path == 'UNSET' or file_path == 'unset' or file_path == 'idmap':
                        continue
                    if file_path == 'null':
                        continue
                    file_path = self.set_abs_file_path(file_path)
                    if not os.path.exists(file_path):
                        logger.warning("File not found: {} = {}, will attempt to download in check_input_data phase".format(name, literal))
                    current_literals[i] = string_to_character_literal(file_path)
                current_literals = compress_literal_list(current_literals)

        # Set the new value.
        self._namelist.set_variable_value(group, name, current_literals, var_size)
Пример #3
0
    def add_default(self, name, value=None):
        """Add a value for the specified variable to the namelist.

        If the specified variable is already defined in the object, the existing
        value is preserved. Otherwise, the `value` argument, if provided, will
        be used to set the value. If no such value is found, the defaults file
        will be consulted. If null values are present in any of the above, the
        result will be a merged array of values.

        If no value for the variable is found via any of the above, this method
        will raise an exception.
        """
        group = self._definition.get_node_element_info(name, "group")

        # Use this to see if we need to raise an error when nothing is found.
        have_value = False

        # Check for existing value.
        current_literals = self._namelist.get_variable_value(group, name)
        if current_literals != [""]:
            have_value = True

        # Check for input argument.
        if value is not None:
            have_value = True
            literals = self._to_namelist_literals(name, value) #FIXME - this is where an array is compressed into a 3*value
            current_literals = merge_literal_lists(literals, current_literals)

        # Check for default value.
        default = self.get_default(name, allow_none=True)
        if default is not None:
            have_value = True
            default_literals = self._to_namelist_literals(name, default)
            current_literals = merge_literal_lists(default_literals,
                                                   current_literals)
        expect(have_value, "No default value found for %s." % name)

        # Go through file names and prepend input data root directory for
        # absolute pathnames.
        var_input_pathname = self._definition.get_input_pathname(name)
        if var_input_pathname == 'abs':
            current_literals = expand_literal_list(current_literals)
            for i, literal in enumerate(current_literals):
                if literal == '':
                    continue
                file_path = character_literal_to_string(literal)
                if file_path == 'UNSET' or file_path == 'idmap':
                    continue
                if file_path == 'null':
                    continue
                file_path = self.set_abs_file_path(file_path)
                expect(os.path.exists(file_path),
                       "File not found: %s = %s" % (name, literal))
                current_literals[i] = string_to_character_literal(file_path)
            current_literals = compress_literal_list(current_literals)

        # Set the new value.
        self._namelist.set_variable_value(group, name, current_literals)
Пример #4
0
    def _to_namelist_literals(self, name, value):
        """Transform a literal list as needed for `set_value`.

        This is the inverse of `_to_python_value`, except that many of the
        changes have potentially already been performed.
        """
        var_type, _, var_size, =  self._definition.split_type_string(name, self._definition.get_type_info(name))
        if var_size == 1 and not isinstance(value, list):
            value = [value]
        for i, scalar in enumerate(value):
            if scalar is None:
                value[i] = ""
            elif var_type == 'character':
                expect(not isinstance(scalar, list), name)
                value[i] = self.quote_string(scalar)
        return compress_literal_list(value)
Пример #5
0
    def _to_namelist_literals(self, name, value):
        """Transform a literal list as needed for `set_value`.

        This is the inverse of `_to_python_value`, except that many of the
        changes have potentially already been performed.
        """
        var_type, _, var_size, = self._definition.split_type_string(name)
        if var_size == 1 and not isinstance(value, list):
            value = [value]
        for i, scalar in enumerate(value):
            if scalar is None:
                value[i] = ""
            elif var_type == 'character':
                expect(not isinstance(scalar, list), name)
                value[i] = self.quote_string(scalar)
        return compress_literal_list(value)
Пример #6
0
    def add_default(self, name, value=None, ignore_abs_path=None):
        """Add a value for the specified variable to the namelist.

        If the specified variable is already defined in the object, the existing
        value is preserved. Otherwise, the `value` argument, if provided, will
        be used to set the value. If no such value is found, the defaults file
        will be consulted. If null values are present in any of the above, the
        result will be a merged array of values.

        If no value for the variable is found via any of the above, this method
        will raise an exception.
        """
        # pylint: disable=protected-access
        group = self._definition.get_group(name)

        # Use this to see if we need to raise an error when nothing is found.
        have_value = False

        # Check for existing value.
        current_literals = self._namelist.get_variable_value(group, name)
        if current_literals != [""]:
            have_value = True
            # Do not proceed further since this has been obtained the -infile contents
            return

        # Check for input argument.
        if value is not None:
            have_value = True
            # if compression were to occur, this is where it does
            literals = self._to_namelist_literals(name, value)
            current_literals = merge_literal_lists(literals, current_literals)

        # Check for default value.
        default = self.get_default(name, allow_none=True)
        if default is not None:
            have_value = True
            default_literals = self._to_namelist_literals(name, default)
            current_literals = merge_literal_lists(default_literals,
                                                   current_literals)
        expect(have_value, "No default value found for %s." % name)

        # Go through file names and prepend input data root directory for
        # absolute pathnames.
        if ignore_abs_path is None:
            var_input_pathname = self._definition.get_input_pathname(name)
            if var_input_pathname == 'abs':
                current_literals = expand_literal_list(current_literals)
                for i, literal in enumerate(current_literals):
                    if literal == '':
                        continue
                    file_path = character_literal_to_string(literal)
                    # NOTE - these are hard-coded here and a better way is to make these extensible
                    if file_path == 'UNSET' or file_path == 'idmap':
                        continue
                    if file_path == 'null':
                        continue
                    file_path = self.set_abs_file_path(file_path)
                    if not os.path.exists(file_path):
                        logger.warn(
                            "File not found: %s = %s, will attempt to download in check_input_data phase"
                            % (name, literal))
                    current_literals[i] = string_to_character_literal(
                        file_path)
                current_literals = compress_literal_list(current_literals)

        # Set the new value.
        self._namelist.set_variable_value(group, name, current_literals)