Determine linecol from the CommentedMap for the `paths` location. CommentedMap from `ruamel.yaml` has `.lc` property from which we can read `.line` and `.col`. This is available in the collections type, i.e. list and dictionaries. But this may fail on non-collection types. For exam
(
data, paths, max_steps=5
)
| 152 | |
| 153 | |
| 154 | def determine_linecol( |
| 155 | data, paths, max_steps=5 |
| 156 | ) -> tuple[Optional[int], Optional[int], int]: |
| 157 | """Determine linecol from the CommentedMap for the `paths` location. |
| 158 | |
| 159 | CommentedMap from `ruamel.yaml` has `.lc` property from which we can read |
| 160 | `.line` and `.col`. This is available in the collections type, |
| 161 | i.e. list and dictionaries. |
| 162 | |
| 163 | But this may fail on non-collection types. For example, if the `paths` is |
| 164 | ['stages', 'metrics'], metrics being a boolean type does not have `lc` |
| 165 | prop. |
| 166 | ``` |
| 167 | stages: |
| 168 | metrics: true |
| 169 | ``` |
| 170 | |
| 171 | To provide some context to the user, we step up to the |
| 172 | path ['stages'], which being a collection type, will have `lc` prop |
| 173 | with which we can find line and col. |
| 174 | |
| 175 | This may end up being not accurate, so we try to show the same amount of |
| 176 | lines of code for `n` number of steps taken upwards. In a worst case, |
| 177 | it may be just 1 step (as non-collection item cannot have child items), |
| 178 | but `schema validator` may provide us arbitrary path. So, this caps the |
| 179 | number of steps upward to just 5. If it does not find any linecols, it'll |
| 180 | abort. |
| 181 | """ |
| 182 | from dpath import get |
| 183 | |
| 184 | step = 1 |
| 185 | line, col = None, None |
| 186 | while paths and step < max_steps: |
| 187 | value = get(data, paths, default=None) |
| 188 | if value is not None: |
| 189 | with suppress(AttributeError, TypeError): |
| 190 | line = value.lc.line + 1 # type: ignore[attr-defined] |
| 191 | col = value.lc.col + 1 # type: ignore[attr-defined] |
| 192 | break |
| 193 | step += 1 |
| 194 | *paths, _ = paths |
| 195 | |
| 196 | return line, col, step |
| 197 | |
| 198 | |
| 199 | class YAMLValidationError(PrettyDvcException): |