def parse(self, message, characters=None): u"""Parse the message into a list of segments. :param characters: the control characters to use, if there is no UNA segment present :param message: The EDI message :rtype: """ # FIXME: DRY: use get_control_characters here? tokens = [] # If there is a UNA token, take the following 6 characters # unconditionally, save them as token and use it as control characters # for further parsing if message[0:3] == u'UNA': control_chars = message[3:9] tokens.append(Token(Token.Type.CONTENT, u'UNA')) tokens.append(Token(Token.Type.CTRL_CHARS, control_chars)) # remove the UNA segment from the string message = message[9:].lstrip(u"\r\n") self.characters = Characters.from_str(u'UNA' + control_chars) else: # if no UNA header present, use default control characters if characters is not None: self.characters = characters tokenizer = Tokenizer() tokens += tokenizer.get_tokens(message, self.characters) segments = self.convert_tokens_to_segments(tokens, self.characters) return segments
def parse(self, message: str, characters: Characters = None) -> Generator[Segment, Any, None]: """Parse the message into a list of segments. :param characters: the control characters to use, if there is no UNA segment present :param message: The EDI message :rtype: """ # If there is a UNA, take the following 6 characters # unconditionally, save them, strip them, and make control Characters() # for further parsing if message[0:3] == "UNA": self.characters = Characters.from_str("UNA" + message[3:9]) # remove the UNA segment from the string message = message[9:].lstrip("\r\n") else: # if no UNA header present, use default control characters if characters is not None: self.characters = characters tokenizer = Tokenizer() return self.convert_tokens_to_segments( tokenizer.get_tokens(message, self.characters), self.characters)
def from_segments(cls, segments: list or collections.Iterable) -> "Interchange": segments = iter(segments) first_segment = next(segments) if first_segment.tag == 'UNA': unb = next(segments) elif first_segment.tag == 'UNB': unb = first_segment else: raise EDISyntaxError( 'An interchange must start with UNB or UNA and UNB') # Loosy syntax check : if len(unb.elements) < 4: raise EDISyntaxError('Missing elements in UNB header') datetime_str = '-'.join(unb.elements[3]) timestamp = datetime.datetime.strptime(datetime_str, '%y%m%d-%H%M') interchange = Interchange( syntax_identifier=unb.elements[0], sender=unb.elements[1], recipient=unb.elements[2], timestamp=timestamp, control_reference=unb.elements[4], ) if first_segment.tag == 'UNA': interchange.has_una_segment = True interchange.characters = Characters.from_str( first_segment.elements[0]) return interchange.add_segments(segment for segment in segments if segment.tag != 'UNZ')
def from_segments(cls, segments: Union[list, Iterable]) -> "Interchange": segments = iter(segments) first_segment = next(segments) if first_segment.tag == "UNA": unb = next(segments) elif first_segment.tag == "UNB": unb = first_segment else: raise EDISyntaxError( "An interchange must start with UNB or UNA and UNB") # Loosy syntax check : if len(unb.elements) < 4: raise EDISyntaxError("Missing elements in UNB header") datetime_str = "-".join(unb.elements[3]) timestamp = datetime.datetime.strptime(datetime_str, "%y%m%d-%H%M") interchange = Interchange( syntax_identifier=unb.elements[0], sender=unb.elements[1], recipient=unb.elements[2], timestamp=timestamp, control_reference=unb.elements[4], ) if first_segment.tag == "UNA": interchange.has_una_segment = True interchange.characters = Characters.from_str( first_segment.elements[0]) return interchange.add_segments(segments)
def add_segment(self, segment: Segment) -> "Message": """Append a segment to the message. :param segment: The segment to add """ if segment.tag == "UNA": self.has_una_segment = True self.characters = Characters.from_str(segment.elements[0]) self.segments.append(segment) return self
def add_segment(self, segment: Segment) -> "UNAHandlingMixin": """Append a segment to the collection. Passing a UNA segment means setting/overriding the control characters and setting the serializer to output the Service String Advice. If you wish to change the control characters from the default and not output the Service String Advice, change self.characters instead, without passing a UNA Segment. :param segment: The segment to add """ if segment.tag == "UNA": self.has_una_segment = True self.characters = Characters.from_str(segment.elements[0]) return self return super().add_segment(segment)
def setup(): setup = Setup() una_segment = "UNA:+.? '" setup.cc = Characters.from_str(una_segment) return setup