|
| 1 | +""" |
| 2 | +Runtime helper to support wrapping of Fortran derived types |
| 3 | +""" |
| 4 | + |
| 5 | +from __future__ import annotations |
| 6 | + |
| 7 | +from abc import ABC, abstractmethod |
| 8 | +from functools import wraps |
| 9 | +from typing import Any, Callable, Concatenate, ParamSpec, TypeVar |
| 10 | + |
| 11 | +import attrs |
| 12 | +from attrs import define, field |
| 13 | + |
| 14 | +from example_fgen_basic.pyfgen_runtime.exceptions import ( |
| 15 | + NotInitialisedError, |
| 16 | +) |
| 17 | +from example_fgen_basic.pyfgen_runtime.formatting import to_html, to_pretty, to_str |
| 18 | + |
| 19 | +# Might be needed for Python 3.9 |
| 20 | +# from typing_extensions import Concatenate, ParamSpec |
| 21 | + |
| 22 | + |
| 23 | +INVALID_INSTANCE_INDEX: int = -1 |
| 24 | +""" |
| 25 | +Value used to denote an invalid `instance_index`. |
| 26 | +
|
| 27 | +This can occur value when a wrapper class |
| 28 | +has not yet been initialised (connected to a Fortran instance). |
| 29 | +""" |
| 30 | + |
| 31 | + |
| 32 | +@define |
| 33 | +class FinalisableWrapperBase(ABC): |
| 34 | + """ |
| 35 | + Base class for Fortran derived type wrappers |
| 36 | + """ |
| 37 | + |
| 38 | + instance_index: int = field( |
| 39 | + validator=attrs.validators.instance_of(int), |
| 40 | + default=INVALID_INSTANCE_INDEX, |
| 41 | + ) |
| 42 | + """ |
| 43 | + Model index of wrapper Fortran instance |
| 44 | + """ |
| 45 | + |
| 46 | + def __str__(self) -> str: |
| 47 | + """ |
| 48 | + Get string representation of self |
| 49 | + """ |
| 50 | + return to_str( |
| 51 | + self, |
| 52 | + self.exposed_attributes, |
| 53 | + ) |
| 54 | + |
| 55 | + def _repr_pretty_(self, p: Any, cycle: bool) -> None: |
| 56 | + """ |
| 57 | + Get pretty representation of self |
| 58 | +
|
| 59 | + Used by IPython notebooks and other tools |
| 60 | + """ |
| 61 | + to_pretty( |
| 62 | + self, |
| 63 | + self.exposed_attributes, |
| 64 | + p=p, |
| 65 | + cycle=cycle, |
| 66 | + ) |
| 67 | + |
| 68 | + def _repr_html_(self) -> str: |
| 69 | + """ |
| 70 | + Get html representation of self |
| 71 | +
|
| 72 | + Used by IPython notebooks and other tools |
| 73 | + """ |
| 74 | + return to_html( |
| 75 | + self, |
| 76 | + self.exposed_attributes, |
| 77 | + ) |
| 78 | + |
| 79 | + @property |
| 80 | + def initialized(self) -> bool: |
| 81 | + """ |
| 82 | + Is the instance initialised, i.e. connected to a Fortran instance? |
| 83 | + """ |
| 84 | + return self.instance_index != INVALID_INSTANCE_INDEX |
| 85 | + |
| 86 | + @property |
| 87 | + @abstractmethod |
| 88 | + def exposed_attributes(self) -> tuple[str, ...]: |
| 89 | + """ |
| 90 | + Attributes exposed by this wrapper |
| 91 | + """ |
| 92 | + ... |
| 93 | + |
| 94 | + # TODO: consider whether we need these |
| 95 | + # @classmethod |
| 96 | + # @abstractmethod |
| 97 | + # def from_new_connection(cls) -> FinalisableWrapperBase: |
| 98 | + # """ |
| 99 | + # Initialise by establishing a new connection with the Fortran module |
| 100 | + # |
| 101 | + # This requests a new model index from the Fortran module and then |
| 102 | + # initialises a class instance |
| 103 | + # |
| 104 | + # Returns |
| 105 | + # ------- |
| 106 | + # New class instance |
| 107 | + # """ |
| 108 | + # ... |
| 109 | + # |
| 110 | + # @abstractmethod |
| 111 | + # def finalize(self) -> None: |
| 112 | + # """ |
| 113 | + # Finalise the Fortran instance and set self back to being uninitialised |
| 114 | + # |
| 115 | + # This method resets ``self.instance_index`` back to |
| 116 | + # ``_UNINITIALISED_instance_index`` |
| 117 | + # |
| 118 | + # Should be decorated with :func:`check_initialised` |
| 119 | + # """ |
| 120 | + # # call to Fortran module goes here when implementing |
| 121 | + # self._uninitialise_instance_index() |
| 122 | + |
| 123 | + def _uninitialise_instance_index(self) -> None: |
| 124 | + self.instance_index = INVALID_INSTANCE_INDEX |
| 125 | + |
| 126 | + |
| 127 | +P = ParamSpec("P") |
| 128 | +T = TypeVar("T") |
| 129 | +Wrapper = TypeVar("Wrapper", bound=FinalisableWrapperBase) |
| 130 | + |
| 131 | + |
| 132 | +def check_initialised( |
| 133 | + method: Callable[Concatenate[Wrapper, P], T], |
| 134 | +) -> Callable[Concatenate[Wrapper, P], T]: |
| 135 | + """ |
| 136 | + Check that the wrapper object has been initialised before executing the method |
| 137 | +
|
| 138 | + Parameters |
| 139 | + ---------- |
| 140 | + method |
| 141 | + Method to wrap |
| 142 | +
|
| 143 | + Returns |
| 144 | + ------- |
| 145 | + : |
| 146 | + Wrapped method |
| 147 | +
|
| 148 | + Raises |
| 149 | + ------ |
| 150 | + InitialisationError |
| 151 | + Wrapper is not initialised |
| 152 | + """ |
| 153 | + |
| 154 | + @wraps(method) |
| 155 | + def checked( |
| 156 | + ref: Wrapper, |
| 157 | + *args: P.args, |
| 158 | + **kwargs: P.kwargs, |
| 159 | + ) -> Any: |
| 160 | + if not ref.initialized: |
| 161 | + raise NotInitialisedError(ref, method) |
| 162 | + |
| 163 | + return method(ref, *args, **kwargs) |
| 164 | + |
| 165 | + return checked # type: ignore |
0 commit comments