2023-04-13 21:45:50 +00:00
|
|
|
from dataclasses import dataclass
|
|
|
|
from typing import Optional, List, Union
|
|
|
|
|
2023-04-14 11:03:48 +00:00
|
|
|
COMMENT_PREFIX = "# "
|
2023-04-14 09:22:47 +00:00
|
|
|
|
2023-04-13 21:45:50 +00:00
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class Attribute:
|
|
|
|
name: str
|
|
|
|
description: Optional[str]
|
|
|
|
value: Union[str, List[str]]
|
|
|
|
|
2023-04-14 09:22:47 +00:00
|
|
|
@property
|
|
|
|
def description_as_comment(self):
|
|
|
|
lines = self.description.split("\n")
|
|
|
|
|
|
|
|
return "\n".join(f"{COMMENT_PREFIX}{line}" for line in lines)
|
|
|
|
|
2023-04-13 21:45:50 +00:00
|
|
|
@property
|
|
|
|
def object_from_value(self):
|
|
|
|
return self.value
|
|
|
|
|
2023-04-14 09:22:47 +00:00
|
|
|
def __str__(self):
|
|
|
|
return f"{self.description_as_comment}\n{self.name}={self.value}"
|
|
|
|
|
2023-04-13 21:45:50 +00:00
|
|
|
|
|
|
|
class SingleAttribute(Attribute):
|
|
|
|
value: str
|
|
|
|
|
|
|
|
|
2023-04-14 09:22:47 +00:00
|
|
|
class StringAttribute(SingleAttribute):
|
2023-04-13 21:45:50 +00:00
|
|
|
@property
|
|
|
|
def object_from_value(self) -> str:
|
2023-04-14 11:03:48 +00:00
|
|
|
return self.value.strip()
|
2023-04-13 21:45:50 +00:00
|
|
|
|
|
|
|
|
2023-04-14 10:03:07 +00:00
|
|
|
class IntAttribute(SingleAttribute):
|
|
|
|
@property
|
|
|
|
def object_from_value(self) -> int:
|
|
|
|
if not self.value.isdigit():
|
|
|
|
raise ValueError(f"The value of {self.name} needs to be an integer, not {self.value}")
|
|
|
|
|
|
|
|
return int(self.value)
|
|
|
|
|
2023-04-14 11:03:48 +00:00
|
|
|
|
2023-04-14 10:40:52 +00:00
|
|
|
class FloatAttribute(SingleAttribute):
|
|
|
|
@property
|
|
|
|
def object_from_value(self) -> float:
|
|
|
|
if not self.value.isnumeric():
|
|
|
|
raise ValueError(f"The value of {self.name} needs to be a number, not {self.value}")
|
|
|
|
|
|
|
|
return float(self.value)
|
|
|
|
|
|
|
|
|
2023-04-13 21:45:50 +00:00
|
|
|
class ListAttribute(Attribute):
|
|
|
|
value: List[str]
|
|
|
|
|
2023-04-14 09:22:47 +00:00
|
|
|
def __str__(self):
|
|
|
|
return f"{self.description_as_comment}\n" + \
|
|
|
|
"\n".join(f"{self.name}={element}" for element in self.value)
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class Description:
|
|
|
|
description: str
|
|
|
|
|
|
|
|
def __str__(self):
|
2023-04-14 10:03:07 +00:00
|
|
|
lines = self.description.split("\n")
|
|
|
|
return "\n".join(f"{COMMENT_PREFIX}{line}" for line in lines)
|
2023-04-14 09:22:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
class EmptyLine(Description):
|
|
|
|
def __init__(self):
|
|
|
|
self.description = "\n"
|
|
|
|
|
2023-04-14 11:03:48 +00:00
|
|
|
def __str__(self):
|
|
|
|
return self.description
|
|
|
|
|
2023-04-13 21:45:50 +00:00
|
|
|
|
|
|
|
class Section:
|
|
|
|
"""
|
|
|
|
A placeholder class
|
|
|
|
"""
|
2023-04-14 09:22:47 +00:00
|
|
|
attribute_list: List[Union[
|
|
|
|
Attribute,
|
|
|
|
Description
|
|
|
|
]]
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return "\n".join(attribute.__str__() for attribute in self.attribute_list)
|