Package brisa :: Package upnp :: Package control_point :: Module device_builder
[hide private]
[frames] | no frames]

Source Code for Module brisa.upnp.control_point.device_builder

  1  # Licensed under the MIT license 
  2  # http://opensource.org/licenses/mit-license.php 
  3  # Copyright 2007-2008 Brisa Team <brisa-develop@garage.maemo.org> 
  4   
  5  """ Builder module for devices. 
  6  """ 
  7   
  8  from xml.etree.ElementTree import ElementTree 
  9   
 10  from brisa.core import log 
 11  from brisa.core.network import url_fetch 
 12  from brisa.core.threaded_call import run_async_call 
 13   
 14  from brisa.upnp.control_point.service import Service 
 15  from brisa.upnp.upnp_defaults import UPnPDefaults 
 16   
 17  import brisa 
 18   
 19   
20 -class DeviceBuilder(object):
21
22 - def __init__(self, device, location, tree):
23 self.device = device 24 self.tree = tree 25 self.location = location 26 self.ns = UPnPDefaults.NAME_SPACE_XML_SCHEMA 27 self._build()
28
29 - def _build(self):
35
36 - def _parse_device(self):
37 self.device.device_type = self.tree.\ 38 findtext('.//{%s}deviceType' % self.ns) 39 self.device.friendly_name = self.tree.\ 40 findtext('.//{%s}friendlyName' % self.ns) 41 self.device.manufacturer = self.tree.\ 42 findtext('.//{%s}manufacturer' % self.ns) 43 self.device.manufacturer_url = self.tree.\ 44 findtext('.//{%s}manufacturerURL' % self.ns) 45 self.device.model_description = self.tree.\ 46 findtext('.//{%s}modelDescription' % self.ns) 47 self.device.model_name = self.tree.\ 48 findtext('.//{%s}modelName' % self.ns) 49 self.device.model_number = self.tree.\ 50 findtext('.//{%s}modelNumber' % self.ns) 51 self.device.model_url = self.tree.\ 52 findtext('.//{%s}modelURL' % self.ns) 53 self.device.serial_number = self.tree.\ 54 findtext('.//{%s}serialNumber' % self.ns) 55 self.device.udn = self.tree.findtext('.//{%s}UDN' % self.ns) 56 self.device.upc = self.tree.findtext('.//{%s}UPC' % self.ns) 57 self.device.presentation_url = self.tree.\ 58 findtext('.//{%s}presentationURL' % self.ns)
59
60 - def _parse_services(self):
61 for xml_service_element in self.tree.\ 62 findall('.//{%s}service' % self.ns): 63 service_type = xml_service_element.\ 64 findtext('{%s}serviceType' % self.ns) 65 service_id = xml_service_element.\ 66 findtext('{%s}serviceId' % self.ns) 67 control_url = xml_service_element.\ 68 findtext('{%s}controlURL' % self.ns) 69 event_sub_url = xml_service_element.\ 70 findtext('{%s}eventSubURL' % self.ns) 71 presentation_url = xml_service_element.\ 72 findtext('{%s}presentationURL' % self.ns) 73 scpd_url = xml_service_element.\ 74 findtext('{%s}SCPDURL' % self.ns) 75 service = Service(service_id, service_type, self.location, 76 scpd_url, control_url, event_sub_url, 77 presentation_url) 78 self.device.add_service(service)
79
80 - def _parse_icons(self):
81 if self.tree.findtext('.//{%s}IconList' % self.ns) != None: 82 for xml_icon_element in self.tree.findall('.//{%s}icon' % self.ns): 83 mimetype = xml_icon_element.findtext('{%s}mimetype' % self.ns) 84 width = xml_icon_element.findtext('{%s}width' % self.ns) 85 height = xml_icon_element.findtext('{%s}height' % self.ns) 86 depth = xml_icon_element.findtext('{%s}depth' % self.ns) 87 url = xml_icon_element.findtext('{%s}url' % self.ns) 88 89 icon = DeviceIcon(mimetype, width, height, depth, url) 90 self.device.icons.append(icon)
91
92 - def _parse_embedded_devices(self):
93 device_list = self.tree.find('.//{%s}deviceList' % self.ns) 94 if device_list != None: 95 embedded_device_tag = device_list.findall('.//{%s}device' % 96 self.ns) 97 98 for xml_device_element in embedded_device_tag: 99 d = self.device.__class__() 100 DeviceBuilder(d, self.location, 101 xml_device_element).cleanup() 102 self.device.add_device(d)
103
104 - def cleanup(self):
105 self.device = None 106 self.tree = None 107 self.location = None
108 109
110 -class DeviceAssembler(object):
111
112 - def __init__(self, device, location, filename=None):
113 self.device = device 114 self.location = location 115 self.filename = filename
116
117 - def mount_device(self):
118 if self.filename is None: 119 filecontent = url_fetch(self.location) 120 if not filecontent: 121 return 122 data = filecontent.read() 123 data = data[data.find("<"):data.rfind(">")+1] 124 tree = ElementTree(data).getroot() 125 else: 126 from xml.etree.ElementTree import parse 127 tree = parse(self.filename) 128 129 DeviceBuilder(self.device, self.location, tree).cleanup() 130 return self.device
131
132 - def mount_device_async(self, callback, cargo):
133 self.callback = callback 134 self.cargo = cargo 135 log.debug('self.location is %s' % self.location) 136 137 if self.filename is None: 138 run_async_call(url_fetch, 139 success_callback=self.mount_device_async_gotdata, 140 error_callback=self.mount_device_async_error, 141 delay=0, url=self.location) 142 else: 143 self.mount_device_async_gotdata(self, open(self.filename))
144
145 - def mount_device_async_error(self, cargo, error):
146 log.debug("Error fetching %s - Error: %s" % (self.location, 147 str(error))) 148 self.callback(self.cargo, None) 149 return True
150
151 - def mount_device_async_gotdata(self, fd, cargo=None):
152 try: 153 log.debug('to object async got data getting tree') 154 tree = ElementTree(file=fd).getroot() 155 except Exception, e: 156 log.debug("Bad device XML %s" % e.message) 157 self.callback(self.cargo, None) 158 return 159 160 DeviceBuilder(self.device, self.location, tree).cleanup() 161 if brisa.__skip_service_xml__: 162 self.callback(self.cargo, self.device) 163 else: 164 log.debug("Fetching device services") 165 self.pending_services = len(self.device.services) 166 for service_name, service_object in \ 167 self.device.services.items(): 168 service_object._build_async(self.service_has_been_built)
169
170 - def service_has_been_built(self, built_ok):
171 log.debug("Service fetched, %d left, result %s", \ 172 self.pending_services - 1, built_ok) 173 if not built_ok and not brisa.__tolerate_service_parse_failure__: 174 log.debug("Device killed") 175 self.device = None 176 self.pending_services -= 1 177 178 if self.pending_services <= 0: 179 log.debug("All services fetched, sending device forward") 180 self.callback(self.cargo, self.device)
181