Ejemplo n.º 1
0
class NamelistGenerator(object):
    """Utility class for generating namelists for a given component."""

    _streams_variables = []

    #pylint:disable=too-many-arguments
    def __init__(self, case, definition_files):
        """Construct a namelist generator.

        Arguments:
        `case`             - `Case` object corresponding to the current case.
        `infiles`          - List of files with user namelist options.
        `definition_files` - List of XML files containing namelist definitions.
        `config`           - A dictionary of attributes for matching defaults.
        """
        # Save off important information from inputs.
        self._case = case
        self._din_loc_root = case.get_value('DIN_LOC_ROOT')

        # Create definition object - this will validate the xml schema in the definition file
        self._definition = NamelistDefinition(definition_files[0])

        # Determine array of _stream_variables from definition object
        # This is only applicable to data models
        self._streams_namelists = {"streams": []}
        self._streams_variables = self._definition.get_per_stream_entries()
        for variable in self._streams_variables:
            self._streams_namelists[variable] = []

        # Create namelist object.
        self._namelist = Namelist()
Ejemplo n.º 2
0
    def test_set_nodes(self):
        test_data = """<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="http://www.cgd.ucar.edu/~cam/namelist/namelist_definition.xsl"?>

<entry_id version="2.0">
    <entry id="test1">
        <type>char</type>
        <category>test</category>
    </entry>
    <entry id="test2">
        <type>char</type>
        <category>test</category>
    </entry>
</entry_id>"""

        with tempfile.NamedTemporaryFile() as temp:
            temp.write(test_data.encode())
            temp.flush()

            nmldef = NamelistDefinition(temp.name)

            nmldef.set_nodes()

            assert len(nmldef._entry_nodes) == 2
            assert nmldef._entry_ids == ["test1", "test2"]
            assert len(nmldef._nodes) == 2
            assert nmldef._entry_types == {"test1": "char", "test2": "char"}
            assert nmldef._valid_values == {"test1": None, "test2": None}
            assert nmldef._group_names == {"test1": None, "test2": None}
Ejemplo n.º 3
0
class NamelistGenerator(object):
    """Utility class for generating namelists for a given component."""

    _streams_variables = []

    def __init__(
            self,
            case,
            infiles,  #pylint:disable=too-many-arguments
            definition_files,
            config):
        """Construct a namelist generator.

        Arguments:
        `case`             - `Case` object corresponding to the current case.
        `infiles`          - List of files with user namelist options.
        `definition_files` - List of XML files containing namelist definitions.
        `config`           - A dictionary of attributes for matching defaults.
        """
        # Save off important information from inputs.
        self._case = case
        self._din_loc_root = case.get_value('DIN_LOC_ROOT')

        # Create definition object.
        self._definition = NamelistDefinition(definition_files[0],
                                              attributes=config)

        # Determine array of _stream_variables from definition object
        # This is only applicable to data models
        self._streams_namelists = {"streams": []}
        self._streams_variables = self._definition.get_per_stream_entries()
        for variable in self._streams_variables:
            self._streams_namelists[variable] = []

        # Create namelist object.
        self._namelist = Namelist()
        for file_ in infiles:
            # Parse settings in "groupless" mode.
            nml_dict = parse(in_file=file_, groupless=True)

            # Add groups using the namelist definition.
            new_namelist = self._definition.dict_to_namelist(nml_dict,
                                                             filename=file_)

            # Make sure that the input is actually valid.
            self._definition.validate(new_namelist, filename=file_)

            # Merge into existing settings (earlier settings have precedence
            # over later settings).
            self._namelist.merge_nl(new_namelist)