| 1 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 2 |
# Copyright (C) 2025-2026 Benjamin Abendroth <[email protected]> |
| 3 |
|
| 4 |
'''Value holder class with line and column information''' |
| 5 |
|
| 6 |
|
| 7 |
class ValueWithTrace: |
| 8 |
'''Represents a value from a configuration file along with its metadata. |
| 9 |
|
| 10 |
This class is used to associate a value from a YAML or JSON file with its |
| 11 |
source, line number, and column number. It is particularly useful for |
| 12 |
debugging and error reporting, as it provides precise location information |
| 13 |
for the value in the original configuration file. |
| 14 |
|
| 15 |
Attributes: |
| 16 |
value (Any): The value extracted from the configuration file. |
| 17 |
src (str): The source of the configuration file. |
| 18 |
line (int): The line number of the value in the source. |
| 19 |
column (int): The column number of the value in the source. |
| 20 |
''' |
| 21 |
|
| 22 |
def __init__(self, value, source, line, column): |
| 23 |
self.value = value |
| 24 |
self.source = source |
| 25 |
self.line = line |
| 26 |
self.column = column |
| 27 |
|
| 28 |
@staticmethod |
| 29 |
def from_yaml_event(value, source, event): |
| 30 |
'''Constructs a `ValueWithTrace` from a YAML event object.''' |
| 31 |
|
| 32 |
return ValueWithTrace( |
| 33 |
value, |
| 34 |
source, |
| 35 |
event.start_mark.line + 1, |
| 36 |
event.start_mark.column + 1) |
| 37 |
|
| 38 |
def get_position_string(self): |
| 39 |
'''Return a string describing the position of the value.''' |
| 40 |
|
| 41 |
return f'line {self.line}, column {self.column}' |
| 42 |
|
| 43 |
def __hash__(self): |
| 44 |
return hash(self.value) |
| 45 |
|
| 46 |
def __eq__(self, other): |
| 47 |
return self.value == other |
| 48 |
|
| 49 |
def __repr__(self): |
| 50 |
return f'{self.value!r}' |
| 51 |
|
| 52 |
|
| 53 |
class ValueWithOutTrace: |
| 54 |
'''Class holding a value without metadata. ''' |
| 55 |
|
| 56 |
def __init__(self, value): |
| 57 |
self.value = value |
| 58 |
|
| 59 |
def get_position_string(self): |
| 60 |
'''Return empty string.''' |
| 61 |
|
| 62 |
return '' |
| 63 |
|
| 64 |
def __hash__(self): |
| 65 |
return hash(self.value) |
| 66 |
|
| 67 |
def __eq__(self, other): |
| 68 |
return self.value == other |
| 69 |
|
| 70 |
def __repr__(self): |
| 71 |
return f'{self.value!r}' |