-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathEnvelope.py
73 lines (59 loc) · 2.18 KB
/
Envelope.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
from Segment import Segment
class Envelope(object):
def __init__(self):
self.header = Segment()
self.trailer = Segment()
self.body = []
def format_as_edi(self, document_configuration):
"""
Format the envelope as an EDI string.
:param document_configuration: configuration for formatting.
:return: document as a string of EDI.
"""
document = self.header.format_as_edi(document_configuration)
document += self.__format_body_as_edi(document_configuration)
document += self.trailer.format_as_edi(document_configuration)
return document
def __format_body_as_edi(self, document_configuration):
"""
Format the body of the envelope as an EDI string.
This calls format_as_edi in all the children.
:param document_configuration: configuration for formatting.
:return: document as a string of EDI.
"""
document = ""
for item in self.body:
document += item.format_as_edi(document_configuration)
return document
def validate(self, report):
"""
Performs validation of the envelope and its components.
:param report: the validation report to append errors.
"""
self.header.validate(report)
self.__validate_body(report)
self.trailer.validate(report)
def __validate_body(self, report):
"""
Validates each of the children of the envelope.
:param report: the validation report to append errors.
"""
for item in self.body:
item.validate(report)
def number_of_segments(self):
return len(self.body)
class InterchangeEnvelope(Envelope):
def __init__(self):
Envelope.__init__(self)
self.groups = self.body
class GroupEnvelope(Envelope):
def __init__(self):
Envelope.__init__(self)
self.transaction_sets = self.body
class TransactionSetEnvelope(Envelope):
def __init__(self):
Envelope.__init__(self)
self.transaction_body = self.body
def number_of_segments(self):
header_trailer_count = 2
return len(self.transaction_body) + header_trailer_count