-
Notifications
You must be signed in to change notification settings - Fork 4
/
models.py
110 lines (79 loc) · 3.05 KB
/
models.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
"""models.py defines all classes and functions related to representing pol/pli(z) files.
"""
from typing import Callable, List, Optional, Sequence
from hydrolib.core.basemodel import BaseModel, ModelSaveSettings, ParsableFileModel
class Description(BaseModel):
"""Description of a single PolyObject.
The Description will be prepended to a block. Each line will
start with a '*'.
Attributes:
content (str): The content of this Description.
"""
content: str
class Metadata(BaseModel):
"""Metadata of a single PolyObject.
Attributes:
name (str): The name of the PolyObject
n_rows (int): The number of rows (i.e. Point instances) of the PolyObject
n_columns (int): The total number of values in a Point, including x, y, and z.
"""
name: str
n_rows: int
n_columns: int
class Point(BaseModel):
"""Point consisting of a x and y coordinate, an optional z coordinate and data.
Attributes:
x (float): The x-coordinate of this Point
y (float): The y-coordinate of this Point
z (Optional[float]): An optional z-coordinate of this Point.
data (Sequence[float]): The additional data variables of this Point.
"""
x: float
y: float
z: Optional[float]
data: Sequence[float]
def _get_identifier(self, data: dict) -> Optional[str]:
x = data.get("x")
y = data.get("y")
z = data.get("z")
return f"x:{x} y:{y} z:{z}"
class PolyObject(BaseModel):
"""PolyObject describing a single block in a poly file.
The metadata should be consistent with the points:
- The number of points should be equal to number of rows defined in the metadata
- The data of each point should be equal to the number of columns defined in the
metadata.
Attributes:
description (Optional[Description]):
An optional description of this PolyObject
metadata (Metadata):
The Metadata of this PolObject, describing the structure
points (List[Point]):
The points describing this PolyObject, structured according to the Metadata
"""
description: Optional[Description]
metadata: Metadata
points: List[Point]
class PolyFile(ParsableFileModel):
"""Poly-file (.pol/.pli/.pliz) representation."""
has_z_values: bool = False
objects: Sequence[PolyObject] = []
def _serialize(self, _: dict, save_settings: ModelSaveSettings) -> None:
from .serializer import write_polyfile
# We skip the passed dict for a better one.
write_polyfile(self._resolved_filepath, self.objects, self.serializer_config)
@classmethod
def _ext(cls) -> str:
return ".pli"
@classmethod
def _filename(cls) -> str:
return "objects"
@classmethod
def _get_serializer(cls) -> Callable:
# Unused, but requires abstract implementation
pass
@classmethod
def _get_parser(cls) -> Callable:
# TODO Prevent circular dependency in Parser
from .parser import read_polyfile
return read_polyfile