Skip to content

Commit 3ef2562

Browse files
theangryangelBlommaertsEdwin
authored andcommitted
WIP present
1 parent d37aaee commit 3ef2562

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

pydifact/mapping.py

+25
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,13 @@ def validate(self) -> bool:
100100
"""
101101
return True
102102

103+
@property
104+
def present(self) -> bool:
105+
"""
106+
Is the mapping component present?
107+
"""
108+
raise NotImplementedError()
109+
103110

104111
class Segment(AbstractMappingComponent):
105112
"""
@@ -113,6 +120,7 @@ def __init__(self, tag: str, *elements, **kwargs):
113120
self.__component__ = SegmentFactory.create_segment(
114121
tag, *elements, validate=False
115122
)
123+
self.__present__ = True
116124

117125
@property
118126
def tag(self) -> str:
@@ -126,6 +134,8 @@ def __getitem__(self, key):
126134

127135
def __setitem__(self, key, value):
128136
self.__component__[key] = value
137+
if not self.__present__:
138+
self.__present__ = True
129139

130140
def validate(self) -> bool:
131141
return self.__component__.validate()
@@ -135,16 +145,23 @@ def from_segments(self, iterator: BiDirectionalIterator):
135145

136146
if self.tag == segment.tag:
137147
self.__component__ = segment
148+
self.__present__ = True
138149
return
139150

140151
if self.mandatory:
141152
raise EDISyntaxError("Missing %s, found %s" % (self.tag, segment))
142153

154+
self.__present__ = False
155+
143156
iterator.prev()
144157

145158
def to_segments(self):
146159
return self.__component__
147160

161+
@property
162+
def present(self) -> bool:
163+
return self.__present__
164+
148165

149166
class SegmentGroupMetaClass(type):
150167
"""
@@ -220,6 +237,10 @@ def __str__(self) -> str:
220237
res.append(str(component))
221238
return "\n".join(res)
222239

240+
@property
241+
def present(self) -> bool:
242+
return any(getattr(self, component_name).present for component_name in self.__components__)
243+
223244

224245
class Loop(AbstractMappingComponent):
225246
"""
@@ -284,3 +305,7 @@ def append(self, value: AbstractMappingComponent):
284305
Append an item to the loop
285306
"""
286307
self.value.append(value)
308+
309+
@property
310+
def present(self) -> bool:
311+
return any(value.present for value in self.value)

0 commit comments

Comments
 (0)