36 '''Module containing the CommonMetadata base class which captures the
37 common functionality for LFRic kernel metadata.
40 from abc
import ABC, abstractmethod
42 from fparser.common.readfortran
import FortranStringReader
43 from fparser.two.parser
import ParserFactory
44 from fparser.two.utils
import NoMatchError, FortranSyntaxError
50 '''Abstract class to capture common LFRic kernel metadata.'''
57 '''Checks that the fparser2 tree is valid.
59 :param fparser2_tree: fparser2 tree capturing a metadata argument.
60 :type fparser2_tree: :py:class:`fparser.two.Fortran2003.Base`
61 :param encoding: class in which the fparser2 tree should \
63 :type encoding: :py:class:`fparser.two.Fortran2003.Base`
65 :raises TypeError: if the fparser2_tree argument is not of the \
66 type specified by the encoding argument.
69 if not isinstance(fparser2_tree, encoding):
71 f
"Expected kernel metadata to be encoded as an "
72 f
"fparser2 {encoding.__name__} object but found type "
73 f
"'{type(fparser2_tree).__name__}' with value "
74 f
"'{str(fparser2_tree)}'.")
78 '''Check that the value argument is one of the values supplied in the
81 :param str value: the value being checked.
82 :param List[str] valid_values: a list of valid values.
83 :param str name: the name of the metadata being checked
85 :raises TypeError: if the value is not a string.
86 :raises ValueError: if the supplied value is not one of the \
87 values in the valid_values list.
90 if not isinstance(value, str):
91 raise TypeError(f
"The '{name}' value should be of type str, but "
92 f
"found '{type(value).__name__}'.")
93 if value.lower()
not in valid_values:
95 f
"The '{name}' metadata should be a recognised "
96 f
"value (one of {valid_values}) "
97 f
"but found '{value}'.")
101 '''Creates an fparser2 tree from a Fortran string. The resultant
102 parent node of the tree will be the same type as the encoding
103 argument if the string conforms to the encoding, otherwise an
104 exception will be raised.
106 TODO: issue #1965: relocate this method as it is not specific
107 to metadata processing.
109 :param str fortran_string: a string containing the metadata in \
111 :param encoding: the parent class with which we will encode the \
113 :type encoding: subclass of :py:class:`fparser.two.Fortran2003.Base`
115 :returns: an fparser2 tree containing a metadata \
117 :rtype: subclass of :py:class:`fparser.two.Fortran2003.Base`
119 :raises ValueError: if the Fortran string is not in the \
123 _ = ParserFactory().create(std=
"f2003")
124 reader = FortranStringReader(fortran_string)
127 fparser2_tree = encoding(reader)
128 except (NoMatchError, FortranSyntaxError):
130 if not match
or not fparser2_tree:
132 f
"Expected kernel metadata to be a Fortran "
133 f
"{encoding.__name__}, but found '{fortran_string}'.")
138 '''Create an instance of this class from Fortran.
140 :param str fortran_string: a string containing the metadata in \
143 :returns: an instance of this class.
144 :rtype: subclass of \
145 :py:class:`python.domain.lfric.kernel.CommonMetadata`
154 '''Create an instance of this class from an fparser2 tree.
159 __all__ = [
"CommonMetadata"]
def create_from_fortran_string(cls, fortran_string)
def validate_scalar_value(value, valid_values, name)
def check_fparser2(fparser2_tree, encoding)
def create_from_fparser2(fparser2_tree)
def create_fparser2(fortran_string, encoding)