Skip to content

Commit

Permalink
WIP present
Browse files Browse the repository at this point in the history
  • Loading branch information
theangryangel authored and BlommaertsEdwin committed Oct 19, 2022
1 parent d37aaee commit 3ef2562
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions pydifact/mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,13 @@ def validate(self) -> bool:
"""
return True

@property
def present(self) -> bool:
"""
Is the mapping component present?
"""
raise NotImplementedError()


class Segment(AbstractMappingComponent):
"""
Expand All @@ -113,6 +120,7 @@ def __init__(self, tag: str, *elements, **kwargs):
self.__component__ = SegmentFactory.create_segment(
tag, *elements, validate=False
)
self.__present__ = True

@property
def tag(self) -> str:
Expand All @@ -126,6 +134,8 @@ def __getitem__(self, key):

def __setitem__(self, key, value):
self.__component__[key] = value
if not self.__present__:
self.__present__ = True

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

if self.tag == segment.tag:
self.__component__ = segment
self.__present__ = True
return

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

self.__present__ = False

iterator.prev()

def to_segments(self):
return self.__component__

@property
def present(self) -> bool:
return self.__present__


class SegmentGroupMetaClass(type):
"""
Expand Down Expand Up @@ -220,6 +237,10 @@ def __str__(self) -> str:
res.append(str(component))
return "\n".join(res)

@property
def present(self) -> bool:
return any(getattr(self, component_name).present for component_name in self.__components__)


class Loop(AbstractMappingComponent):
"""
Expand Down Expand Up @@ -284,3 +305,7 @@ def append(self, value: AbstractMappingComponent):
Append an item to the loop
"""
self.value.append(value)

@property
def present(self) -> bool:
return any(value.present for value in self.value)

0 comments on commit 3ef2562

Please sign in to comment.