diff --git a/src/spi/binary/__init__.py b/src/spi/binary/__init__.py index 29674f1..5e6c29d 100644 --- a/src/spi/binary/__init__.py +++ b/src/spi/binary/__init__.py @@ -30,7 +30,6 @@ logger = logging.getLogger("spi.binary") - class Ensemble: """ Describes a DAB ensemble @@ -714,7 +713,7 @@ def frombits(bits): return CData(data.tostring()) def marshall(obj, **kwargs): - """Marshalls an :class:Epg or :class:ServiceInfo to its binary document""" + """Marshalls an :class:Epg or :class:ServiceInfo to its binary document""" if isinstance(obj, ServiceInfo): return marshall_serviceinfo(obj, kwargs.get('ensemble', None)) elif isinstance(obj, ProgrammeInfo): return marshall_programmeinfo(obj) @@ -775,7 +774,7 @@ def build_schedule(schedule): for name in programme.names: child = build_name(name) programme_element.children.append(child) - # descriptions + # descriptions for description in programme.descriptions: child = build_description(description) programme_element.children.append(child) @@ -784,9 +783,7 @@ def build_schedule(schedule): child = build_location(location) programme_element.children.append(child) # media - for media in programme.media: - child = build_mediagroup(media) - programme_element.children.append(child) + if programme.media: programme_element.children.append(build_mediagroup(programme.media)) # genre for genre in programme.genres: child = build_genre(genre) @@ -940,8 +937,7 @@ def build_programme_event(event): for location in event.locations: event_element.children.append(build_location(location)) # media - for media in event.media: - event_element.children.append(build_mediagroup(media)) + if event.media: event_element.children.append(build_mediagroup(event.media)) # genre for genre in event.genres: event_element.children.append(build_genre(genre)) @@ -979,9 +975,7 @@ def build_service(service): service_element.children.append(build_description(description)) # media - #for media in service.media: - # service_element.children.append(build_mediagroup(media)) - service_element.children.append(build_mediagroup(service.media)) + if service.media: service_element.children.append(build_mediagroup(service.media)) # genre for genre in service.genres: @@ -1027,8 +1021,7 @@ def build_ensemble(ensemble, services): event_element.children.append(build_description(description)) # media - for media in ensemble.media: - ensemble_element.children.append(build_mediagroup(media)) + if ensemble.media: ensemble_element.children.append(build_mediagroup(ensemble.media)) # keywords diff --git a/src/spi/xml/__init__.py b/src/spi/xml/__init__.py index 603c5b4..785d33c 100644 --- a/src/spi/xml/__init__.py +++ b/src/spi/xml/__init__.py @@ -537,14 +537,18 @@ def parse_time(timeElement): def parse_bearer(bearer_element): uri = bearer_element.attrib['id'] - if uri.startswith('dab'): - bearer = DabBearer.fromstring(uri) - elif uri.startswith('fm'): - bearer = FmBearer.fromstring(uri) - elif uri.startswith('http'): - bearer = IpBearer(uri) - else: - raise ValueError('bearer %s not recognised' % uri) + try: + if uri.startswith('dab'): + bearer = DabBearer.fromstring(uri) + elif uri.startswith('fm'): + bearer = FmBearer.fromstring(uri) + elif uri.startswith('http'): + bearer = IpBearer(uri) + else: + raise ValueError('bearer %s not recognised' % uri) + except: + bearer = IpBearer("http://null/") + logger.debug('bearer %s is malformed', uri) if bearer_element.attrib.has_key('cost'): bearer.cost = int(bearer_element.attrib['cost']) @@ -652,9 +656,9 @@ def parse_description(descriptionElement): if descriptionElement.tag == '{%s}shortDescription' % SCHEMA_NS: return ShortDescription(descriptionElement.text) elif descriptionElement.tag == '{%s}longDescription' % SCHEMA_NS: - return LongDescription(descriptionElement.text) + return LongDescription(descriptionElement.text) else: - raise ValueError('unknown description element: %s' % descriptionElement) + raise ValueError('unknown or malformed description element: %s' % descriptionElement) def parse_multimedia(mediaElement): type = None @@ -702,40 +706,40 @@ def parse_service(service_element): # names for child in service_element.findall("spi:shortName", namespaces): - service.names.append(parse_name(child)) + if child.text is not None: service.names.append(parse_name(child)) for child in service_element.findall("spi:mediumName", namespaces): - service.names.append(parse_name(child)) + if child.text is not None: service.names.append(parse_name(child)) for child in service_element.findall("spi:longName", namespaces): - service.names.append(parse_name(child)) + if child.text is not None: service.names.append(parse_name(child)) # bearers for child in service_element.findall("spi:bearer", namespaces): - service.bearers.append(parse_bearer(child)) + if child.attrib.has_key("id"): service.bearers.append(parse_bearer(child)) # media for media_element in service_element.findall("spi:mediaDescription", namespaces): for child in media_element.findall("spi:multimedia", namespaces): - service.media.append(parse_multimedia(child)) + if child.attrib.has_key("url"): service.media.append(parse_multimedia(child)) for child in media_element.findall("spi:shortDescription", namespaces): - service.descriptions.append(parse_description(child)) + if child.text is not None: service.descriptions.append(parse_description(child)) for child in media_element.findall("spi:longDescription", namespaces): - service.descriptions.append(parse_description(child)) + if child.text is not None: service.descriptions.append(parse_description(child)) # genres for child in service_element.findall("spi:genre", namespaces): - service.genres.append(parse_genre(child)) + if child.text is not None: service.genres.append(parse_genre(child)) # links - for child in service_element.findall("spi:link", namespaces): - service.links.append(parse_link(child)) + for child in service_element.findall("spi:link", namespaces): + if child.attrib.has_key("uri"): service.links.append(parse_link(child)) # keywords for child in service_element.findall("spi:keywords", namespaces): - service.keywords.extend(parse_keywords(child)) + if child.text is not None: service.keywords.extend(parse_keywords(child)) # lookup for child in service_element.findall("spi:radiodns", namespaces): - service.lookup = 'http://%s/%s' % (child.attrib['fqdn'], child.attrib['serviceIdentifier']) + if child.attrib.has_key("fqdn") and child.attrib.has_key("serviceIdentifier"): service.lookup = 'http://%s/%s' % (child.attrib['fqdn'], child.attrib['serviceIdentifier']) return service