def _gs1_get_components(self): self.ensure_one() if self.body: segments = [] for segment in separate_segments(self._gs1_decode_msg(base64.b64decode(self.body))): segments.append(separate_components(segment)) return segments
def _gs1_get_components(self): self.ensure_one() if self.body: segments = [] for segment in separate_segments( self._gs1_decode_msg(base64.b64decode(self.body))): segments.append(separate_components(segment)) return segments
def _init_from_string(self, src_string): """Initialize from string source.""" # Get components segments = separate_segments(src_string, segment_terminator=self.una.segment_terminator, release_character=self.una.release_character) unh_index = [i for i, x in enumerate(segments) if x.startswith('UNH')][0] components = separate_components(segments[unh_index], data_element_separator=self.una.data_element_separator, component_data_element_separator=self.una.component_data_element_separator, segment_terminator=self.una.segment_terminator, release_character=self.una.release_character) # Map comonents to standard self.data = process_entries(components[1:])
def _split(self): if self.route_type == 'esap20': message = '' #~ _logger.warn('body: %s' % base64.b64decode(self.body)) msg_count = 0 msgs = [] segment_check = {} msg_dict = {} for segment_string in separate_segments(base64.b64decode( self.body)): segment = separate_components(segment_string) if segment[0] == 'UNB': segment_check['UNB'] = True self.ref = segment[5] self.sender = self._get_partner(segment[2], 'sender') self.recipient = self._get_partner(segment[3], 'recipent') date = segment[4][0] time = segment[4][1] self.date = "20%s-%s-%s %s:%s:00" % ( date[:2], date[2:4], date[4:], time[:2], time[2:]) if len(segment) > 7: self.application = segment[7] elif segment[0] == 'UNH': edi_type = segment[2][0] msg_name = segment[1] message = segment_string segment_count = 1 elif segment[0] == 'UNT': #skapa message if segment_count + 1 != int(segment[1]): raise TypeError( 'Wrong number of segments! %s %s' % (segment_count, segment), segment) message += segment_string msgs.append({ 'name': msg_name, 'envelope_id': self.id, 'body': base64.b64encode(message), 'edi_type': self._get_edi_type_id(edi_type), 'sender': self.sender.id, 'recipient': self.recipient.id, 'route_type': self.route_id.route_type, 'route_id': self.route_id.id, }) message = '' msg_count += 1 elif segment[0] == 'UNZ': segment_check['UNZ'] = True if msg_count != int(segment[1]): raise TypeError('Wrong message count!') elif message: message += segment_string segment_count += 1 if not segment_check.get('UNB'): raise TypeError('UNB segment missing!') elif not segment_check.get('UNZ'): raise TypeError('UNZ segment missing!') #~ _logger.warn('msgs to create: %s' % msgs) for msg_dict in msgs: #Large potential for transaction lock when unpacking messages. #Commit for every message and rollback on error. #Every working message is unpacked. try: #self._cr.commit() msg = self.env['edi.message'].create(msg_dict) #~ _logger.warn('msg created: %s' % msg) msg.unpack() except Exception as e: #self._cr.rollback() self.route_id.log( "Error when reading message '%s' of envelope '%s'" % (msg_dict.get('name'), self.name), sys.exc_info()) self.state = 'canceled' super(edi_envelope, self)._split()
def _split(self): if self.route_type == 'esap20': message = '' #~ _logger.warn('body: %s' % base64.b64decode(self.body)) msg_count = 0 msgs = [] segment_check = {} msg_dict = {} for segment_string in separate_segments(base64.b64decode(self.body)): segment = separate_components(segment_string) if segment[0] == 'UNB': segment_check['UNB'] = True self.ref = segment[5] self.sender = self._get_partner(segment[2],'sender') self.recipient = self._get_partner(segment[3],'recipent') date = segment[4][0] time = segment[4][1] self.date = "20%s-%s-%s %s:%s:00" % (date[:2], date[2:4], date[4:], time[:2], time[2:]) if len(segment) > 7: self.application = segment[7] elif segment[0] == 'UNH': edi_type = segment[2][0] msg_name = segment[1] message = segment_string segment_count = 1 elif segment[0] == 'UNT': #skapa message if segment_count + 1 != int(segment[1]): raise TypeError('Wrong number of segments! %s %s' % (segment_count, segment), segment) message += segment_string msgs.append({ 'name': msg_name, 'envelope_id': self.id, 'body': base64.b64encode(message), 'edi_type': self._get_edi_type_id(edi_type), 'sender': self.sender.id, 'recipient': self.recipient.id, 'route_type': self.route_id.route_type, 'route_id': self.route_id.id, }) message = '' msg_count += 1 elif segment[0] == 'UNZ': segment_check['UNZ'] = True if msg_count != int(segment[1]): raise TypeError('Wrong message count!') elif message: message += segment_string segment_count += 1 if not segment_check.get('UNB'): raise TypeError('UNB segment missing!') elif not segment_check.get('UNZ'): raise TypeError('UNZ segment missing!') #~ _logger.warn('msgs to create: %s' % msgs) for msg_dict in msgs: #Large potential for transaction lock when unpacking messages. #Commit for every message and rollback on error. #Every working message is unpacked. try: #self._cr.commit() msg = self.env['edi.message'].create(msg_dict) #~ _logger.warn('msg created: %s' % msg) msg.unpack() except Exception as e: #self._cr.rollback() self.route_id.log("Error when reading message '%s' of envelope '%s'" % (msg_dict.get('name'), self.name), sys.exc_info()) self.state = 'canceled' super(edi_envelope, self)._split()
def initialize_from_src_string(self, src_string): segments = [separate_components(segment) for segment in separate_segments(src_string)] validate_anchor_segments(segments) self.process_segments(segments, 0, [0], 0)
def test_separates_segments_as_expected_with_release_character_and_newline(self): src_string="UNH+1+MSCONS:D:01B:UN:EAN004'\nBGM+94E::9+6078+9' NAD+SU+++Papa John?'s'" expected = ["UNH+1+MSCONS:D:01B:UN:EAN004'", "BGM+94E::9+6078+9'", "NAD+SU+++Papa John?'s'"] actual = separate_segments(src_string) self.assertEqual(actual, expected)