Configuration option with metadata.
| 22 | |
| 23 | @dataclass |
| 24 | class ConfigOption: |
| 25 | """Configuration option with metadata.""" |
| 26 | name: str |
| 27 | value: Any |
| 28 | type: ConfigType |
| 29 | line_number: int = 0 |
| 30 | comment: str = "" |
| 31 | |
| 32 | def as_bool(self) -> bool: |
| 33 | """Get value as boolean.""" |
| 34 | if self.type == ConfigType.BOOLEAN: |
| 35 | return bool(self.value) |
| 36 | elif self.type == ConfigType.INTEGER: |
| 37 | return self.value != 0 |
| 38 | elif self.type == ConfigType.STRING: |
| 39 | return bool(self.value) |
| 40 | return False |
| 41 | |
| 42 | def as_int(self) -> int: |
| 43 | """Get value as integer.""" |
| 44 | if self.type == ConfigType.INTEGER: |
| 45 | return self.value |
| 46 | elif self.type == ConfigType.BOOLEAN: |
| 47 | return 1 if self.value else 0 |
| 48 | elif self.type == ConfigType.STRING: |
| 49 | try: |
| 50 | return int(self.value) |
| 51 | except ValueError: |
| 52 | return 0 |
| 53 | return 0 |
| 54 | |
| 55 | def as_str(self) -> str: |
| 56 | """Get value as string.""" |
| 57 | if self.type == ConfigType.STRING: |
| 58 | return self.value |
| 59 | return str(self.value) |
| 60 | |
| 61 | |
| 62 | class ConfigParser: |