36 '''Module containing the abstract CommonMetaArgMetadata class which
37 captures the metadata associated with an LFRic meta_arg
38 argument. Supports the creation, modification and Fortran output of
42 from abc
import ABC, abstractmethod
49 '''Abstract class to capture common aspects of LFRic kernel metadata.
51 :param Optional[str] datatype: the datatype of this argument.
52 :param Optional[str] access: the way the kernel accesses this \
60 vector_length_arg_index = 0
61 datatype_arg_index = 1
63 function_space_arg_index = 3
68 def __init__(self, datatype, access):
75 def _get_metadata(cls, fparser2_tree):
76 '''Extract the required metadata from the fparser2 tree and return it
77 as strings. Also check that the metadata is in the expected
78 form (but do not check the metadata values as that is done
83 '''Create an instance of the class from an fparser2 tree.
85 :param fparser2_tree: fparser2 tree containing the metadata \
87 :type fparser2_tree: :py:class:`fparser.two.Fortran2003.Part_Ref`
89 :returns: an instance of the class.
91 :py:class:`psyclone.domain.lfric.kernel.CommonMetaArgMetadata`
100 '''Check that the first metadata argument has the expected value.
102 :param fparser2_tree: the metadata encoded in an fparser2_tree
103 :type fparser2_tree: :py:class:`fparser.two.Fortran2003.Part_Ref` | \
104 :py:class:`fparser.two.Fortran2003.Structure_Constructor`
106 :raises ValueError: if the first metadata argument has an \
111 form = cls.
get_argget_arg(fparser2_tree, idx)
114 form = form.split(
"*")[0].strip()
116 if not form.lower() == cls.
formform.lower():
118 f
"Metadata for '{cls.check_name}' kernel arguments should "
119 f
"have '{cls.form}' {word} the first metadata "
120 f
"property, but found '{form}'.")
124 '''Check that the remaining untested metadata arguments have the
125 expected value. If they do not then re-raise the exception
126 from the class constructor, adding in positional information
127 and the metadata arguments to make it clearer where the
130 :param fparser2_tree: the metadata encoded in an fparser2_tree.
131 :type fparser2_tree: :py:class:`fparser.two.Fortran2003.Part_Ref` or \
132 :py:class:`fparser.two.Fortran2003.Structure_Constructor`
133 :param metadata_args: the metadata arguments required to \
134 create an instance of the class provided by the 'cls' \
136 :type metadata_args: unwrapped dict
138 :raises ValueError: if the metadata has an incorrect value.
139 :raises InternalError: if an unrecognised exception message is found.
143 _ = cls(*metadata_args)
144 except ValueError
as info:
146 if "datatype descriptor" in message:
148 elif "access descriptor" in message:
150 elif "function space" in message:
152 elif "mesh_arg" in message:
153 index = cls.mesh_arg_index
154 elif "function_space_to" in message:
155 index = cls.function_space_to_arg_index
156 elif "function_space_from" in message:
157 index = cls.function_space_from_arg_index
160 f
"Unexpected error message found '{message}'")
from info
161 raise ValueError(f
"At argument index '{index}' for metadata "
162 f
"'{str(fparser2_tree)}'. {message}")
from info
167 '''Check that the metadata has the expected number of arguments,
168 otherwise an exception is raised.
170 :param fparser2_tree: fparser2 tree capturing a metadata argument.
171 :type fparser2_tree: :py:class:`fparser.two.Fortran2003.Part_Ref` | \
172 :py:class:`fparser.two.Fortran2003.Structure_Constructor`
180 '''Retrieves the vector length metadata value found within the
181 supplied fparser2 tree and checks that it is valid.
183 :param fparser2_tree: fparser2 tree capturing the required metadata.
184 :type fparser2_tree: :py:class:`fparser.two.Fortran2003.Part_Ref`
186 :returns: the vector length value extracted from the fparser2 tree.
189 :raises TypeError: if the vector length metadata is not in the \
193 vector_datatype = CommonArgMetadata.get_arg(
195 components = vector_datatype.split(
"*")
196 if len(components) != 2:
198 f
"The vector length metadata should be in the form "
199 f
"'form*vector_length' but found '{vector_datatype}'.")
200 vector_length = components[1].strip()
206 :returns: the datatype for this metadata argument.
214 '''Check that value is a valid datatype.'''
217 def datatype(self, value):
219 :param str value: set the datatype to the \
228 '''Check that value is a valid value for the access descriptor.'''
233 :returns: the access descriptor for this argument.
239 def access(self, value):
241 :param str value: set the access descriptor to the \
246 self.
_access_access = value.lower()
249 __all__ = [
"CommonMetaArgMetadata"]
def get_arg(fparser2_tree, index)
def check_remaining_args(cls, fparser2_tree, *metadata_args)
def get_vector_length(cls, fparser2_tree)
def check_first_arg(cls, fparser2_tree)
def datatype(self, value)
def check_nargs(cls, fparser2_tree)
def _get_metadata(cls, fparser2_tree)
int vector_length_arg_index
int function_space_arg_index
def check_datatype(value)
def create_from_fparser2(cls, fparser2_tree)