|
| 1 | +import re |
| 2 | +import copy |
| 3 | +import dataclasses |
| 4 | + |
| 5 | + |
| 6 | +from . import cell |
| 7 | +from ._card import Card |
| 8 | +from ..utils import types |
| 9 | +from ..utils import errors |
| 10 | +from ..utils import _parser |
| 11 | + |
| 12 | + |
| 13 | +class Like(Card): |
| 14 | + """ |
| 15 | + Represents INP cell cards. |
| 16 | +
|
| 17 | + Attributes: |
| 18 | + number: cell number. |
| 19 | + original: cell similar. |
| 20 | + options: cell options. |
| 21 | + """ |
| 22 | + |
| 23 | + _ATTRS = { |
| 24 | + 'number': types.Integer, |
| 25 | + 'original': types.Integer, |
| 26 | + 'options': types.Tuple[cell.CellOption], |
| 27 | + } |
| 28 | + |
| 29 | + _REGEX = re.compile(rf'\A(\S+) like (\S+) but( ({cell.CellOption._REGEX.pattern[2:-2]}))*\Z') |
| 30 | + |
| 31 | + def __init__( |
| 32 | + self, |
| 33 | + number: types.Integer, |
| 34 | + original: types.Integer, |
| 35 | + options: types.Tuple[cell.CellOption] = None, |
| 36 | + ): |
| 37 | + """ |
| 38 | + Initializes ``Like``. |
| 39 | +
|
| 40 | + Parameters: |
| 41 | + number: cell number. |
| 42 | + original: cell similar. |
| 43 | + options: cell options. |
| 44 | +
|
| 45 | + Raises: |
| 46 | + InpError: SEMANTICS_CARD. |
| 47 | + """ |
| 48 | + |
| 49 | + if number is None or not (1 <= number <= 99_999_999): |
| 50 | + raise errors.InpError(errors.InpCode.SEMANTICS_CARD, number) |
| 51 | + if original is None or not (1 <= original <= 99_999_999): |
| 52 | + raise errors.InpError(errors.InpCode.SEMANTICS_CARD, original) |
| 53 | + if options is not None and None in options: |
| 54 | + raise errors.InpError(errors.InpCode.SEMANTICS_CARD, options) |
| 55 | + |
| 56 | + self.number: types.Integer = number |
| 57 | + self.original: types.Integer = original |
| 58 | + self.options: types.Tuple[cell.CellOption] = options |
| 59 | + |
| 60 | + def to_mcnp(self): |
| 61 | + """ |
| 62 | + Generates INP from ``Like``. |
| 63 | +
|
| 64 | + Returns: |
| 65 | + INP cell card. |
| 66 | + """ |
| 67 | + |
| 68 | + source = f'{self.number} like {self.original} but {self.options or ""}' |
| 69 | + source, comments = _parser.preprocess_inp(source) |
| 70 | + source = _parser.postprocess_inp(source) |
| 71 | + |
| 72 | + return source |
| 73 | + |
| 74 | + |
| 75 | +@dataclasses.dataclass |
| 76 | +class LikeBuilder: |
| 77 | + """ |
| 78 | + Builds ``Like``. |
| 79 | +
|
| 80 | + Attributes: |
| 81 | + number: cell number. |
| 82 | + original: cell similar. |
| 83 | + options: cell options. |
| 84 | + """ |
| 85 | + |
| 86 | + number: str | int | types.Integer |
| 87 | + original: str | int | types.Integer |
| 88 | + options: list[str] | list[cell.CellOption] | list[cell.CellOptionBuilder] = None |
| 89 | + |
| 90 | + def build(self): |
| 91 | + """ |
| 92 | + Builds ``LikeBuilder`` into ``Like``. |
| 93 | +
|
| 94 | + Returns: |
| 95 | + ``Like`` for ``LikeBuilder``. |
| 96 | + """ |
| 97 | + |
| 98 | + number = self.number |
| 99 | + if isinstance(self.number, types.Integer): |
| 100 | + number = self.number |
| 101 | + elif isinstance(self.number, int): |
| 102 | + number = types.Integer(self.number) |
| 103 | + elif isinstance(self.number, str): |
| 104 | + number = types.Integer.from_mcnp(self.number) |
| 105 | + |
| 106 | + original = self.original |
| 107 | + if isinstance(self.original, types.Integer): |
| 108 | + original = self.original |
| 109 | + elif isinstance(self.original, int): |
| 110 | + original = types.Integer(self.original) |
| 111 | + elif isinstance(self.original, str): |
| 112 | + original = types.Integer.from_mcnp(self.original) |
| 113 | + |
| 114 | + if self.options: |
| 115 | + options = [] |
| 116 | + for item in self.options: |
| 117 | + if isinstance(item, cell.CellOption): |
| 118 | + options.append(item) |
| 119 | + elif isinstance(item, str): |
| 120 | + options.append(cell.CellOption.from_mcnp(item)) |
| 121 | + elif isinstance(item, cell.CellOptionBuilder): |
| 122 | + options.append(item.build()) |
| 123 | + options = types.Tuple(options) |
| 124 | + else: |
| 125 | + options = None |
| 126 | + |
| 127 | + return Like( |
| 128 | + number=number, |
| 129 | + original=original, |
| 130 | + options=options, |
| 131 | + ) |
| 132 | + |
| 133 | + @staticmethod |
| 134 | + def unbuild(ast: Like): |
| 135 | + """ |
| 136 | + Unbuilds ``Like`` into ``LikeBuilder`` |
| 137 | +
|
| 138 | + Returns: |
| 139 | + ``LikeBuilder`` for ``Like``. |
| 140 | + """ |
| 141 | + |
| 142 | + return LikeBuilder( |
| 143 | + number=copy.deepcopy(ast.number), |
| 144 | + original=copy.deepcopy(ast.original), |
| 145 | + options=copy.deepcopy(ast.options), |
| 146 | + ) |
0 commit comments