def create_records(self, msg_text): ''' Given the text from a message, create a list of record objects and return that list. ''' # remove any whitespace msg_text = msg_text.strip() try: # XML format if msg_text.startswith('<'): # Get the primary XML namespace primary_ns = get_primary_ns(msg_text) if primary_ns == CarParser.NAMESPACE: created_records = self._create_cars(msg_text) # # Not available yet. elif primary_ns == AurParser.NAMESPACE: # created_records = self._create_aurs(msg_text) raise RecordFactoryException( 'Aggregated usage record not yet supported.') elif primary_ns == StarParser.NAMESPACE: created_records = self._create_stars(msg_text) else: raise RecordFactoryException('XML format not recognised.') # APEL format else: lines = msg_text.splitlines() header = lines.pop(0) # recreate message as string having removed header msg_text = '\n'.join(lines) created_records = [] # crop the string to before the first ':' index = header.index(':') if (header[0:index].strip() == RecordFactory.JR_HEADER): created_records = self._create_jrs(msg_text) elif (header.strip() == RecordFactory.SR_HEADER): created_records = self._create_srs(msg_text) elif (header.strip() == RecordFactory.NSR_HEADER): created_records = self._create_nsrs(msg_text) elif (header[0:index].strip() == RecordFactory.SYNC_HEADER): created_records = self._create_syncs(msg_text) elif (header[0:index].strip() == RecordFactory.CLOUD_HEADER): created_records = self._create_clouds(msg_text) elif (header[0:index].strip() == RecordFactory.CLOUD_SUMMARY_HEADER): created_records = self._create_cloud_summaries(msg_text) else: raise RecordFactoryException( 'Message type %s not recognised.' % header) return created_records except ValueError, e: raise RecordFactoryException('Message header is incorrect: %s' % e)
def create_records(self, msg_text): ''' Given the text from a message, create a list of record objects and return that list. ''' # remove any whitespace msg_text = msg_text.strip() try: # XML format if msg_text.startswith('<'): # Get the primary XML namespace primary_ns = get_primary_ns(msg_text) if primary_ns == CarParser.NAMESPACE: created_records = self._create_cars(msg_text) # # Not available yet. elif primary_ns == AurParser.NAMESPACE: # created_records = self._create_aurs(msg_text) raise RecordFactoryException('Aggregated usage record not yet supported.') elif primary_ns == StarParser.NAMESPACE: created_records = self._create_stars(msg_text) else: raise RecordFactoryException('XML format not recognised.') # APEL format else: lines = msg_text.splitlines() header = lines.pop(0) # recreate message as string having removed header msg_text = '\n'.join(lines) created_records = [] # crop the string to before the first ':' index = header.index(':') if (header[0:index].strip() == RecordFactory.JR_HEADER): created_records = self._create_jrs(msg_text) elif (header.strip() == RecordFactory.SR_HEADER): created_records = self._create_srs(msg_text) elif (header.strip() == RecordFactory.NSR_HEADER): created_records = self._create_nsrs(msg_text) elif (header[0:index].strip() == RecordFactory.SYNC_HEADER): created_records = self._create_syncs(msg_text) elif (header[0:index].strip() == RecordFactory.CLOUD_HEADER): created_records = self._create_clouds(msg_text) elif (header[0:index].strip() == RecordFactory.CLOUD_SUMMARY_HEADER): created_records = self._create_cloud_summaries(msg_text) else: raise RecordFactoryException('Message type %s not recognised.' % header) return created_records except ValueError, e: raise RecordFactoryException('Message header is incorrect: %s' % e)
def test_get_primary_ns(self): test_xml = '<?xml version="1.0" ?><ur:UsageRecord xmlns:ur="booboob"/>' ns = get_primary_ns(test_xml) self.assertEqual("booboob", ns)
def test_get_primary_ns(self): test_xml = '<?xml version="1.0" ?><ur:UsageRecord xmlns:ur="booboob"/>' ns = get_primary_ns(test_xml) self.assertEqual("booboob", ns)