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

Source Code for Module brisa.upnp.base_device

  1  # Licensed under the MIT license 
  2  # http://opensource.org/licenses/mit-license.php or see LICENSE file. 
  3  # Copyright 2007-2009 Brisa Team <brisa-develop@garage.maemo.org> 
  4   
  5  """ Basic device classes. 
  6  """ 
  7   
  8  __all__ = ('BaseDevice', 'BaseDeviceIcon') 
  9   
 10  import uuid 
 11   
 12   
 13  from brisa.core import log 
 14   
 15   
16 -class BaseDevice(object):
17 """ Represents an UPnP device. 18 19 Consult http://upnp.org/standardizeddcps/basic.asp as a basic reference. 20 """ 21
22 - def __init__(self, device_type='', friendly_name='', location='', udn=None, 23 parent=None, manufacturer='', manufacturer_url='', 24 model_description='', model_name='', model_number='', 25 model_url='', serial_number='', upc='', presentation_url=''):
26 """ Constructor for the Device class. 27 28 @param device_type: device type as described on the device reference 29 @param friendly_name: a friendly name 30 @param location: network location 31 @param udn: uuid 32 @param parent: parent device 33 @param manufacturer: manufacturer 34 @param manufacturer_url: manufacturer url 35 @param model_description: model description 36 @param model_name: model name 37 @param model_number: model number 38 @param model_url: model url 39 @param serial_number: serial number 40 @param upc: upc 41 @param presentation_url: presentation url 42 43 @type device_type: string 44 @type friendly_name: string 45 @type location: string 46 @type udn: string 47 @type parent: Device 48 @type manufacturer: string 49 @type manufacturer_url: string 50 @type model_description: string 51 @type model_name: string 52 @type model_number: string 53 @type model_url: string 54 @type serial_number: string 55 @type upc: string 56 @type presentation_url: string 57 58 @note: basic device reference: 59 http://upnp.org/standardizeddcps/basic.asp 60 """ 61 self.device_type = device_type 62 self.friendly_name = friendly_name 63 self.location = location 64 self.udn = udn 65 self.parent = parent 66 self.manufacturer = manufacturer 67 self.manufacturer_url = manufacturer_url 68 self.model_description = model_description 69 self.model_name = model_name 70 self.model_number = model_number 71 self.model_url = model_url 72 self.serial_number = serial_number 73 self.upc = upc 74 self.presentation_url = presentation_url 75 self.services = {} 76 self.devices = {} 77 self.icons = [] 78 self.soap_service = None 79 self.is_root = True 80 81 if not udn: 82 self.udn = 'uuid:%s' % uuid.uuid4()
83
84 - def del_service_by_id(self, id):
85 """ Removes service that matches the given id. 86 87 @param id: service id 88 @type id: string 89 """ 90 for k, service in self.services.items(): 91 if service.id == id: 92 del self.services[k] 93 break
94
95 - def add_device(self, device):
96 """ Adds a device embedded inside this device. 97 98 @param device: device to be added 99 @type device: Device 100 """ 101 if device in self.devices.values(): 102 log.debug('Device %s already contained by %s' % (device, self)) 103 return False 104 if device.friendly_name not in self.devices: 105 self.devices[device.friendly_name] = device 106 else: 107 d = 0 108 name = None 109 while not name: 110 name = '%s%d' % (device.friendly_name, d) 111 if name not in [d.friendly_name for d in self.devices if \ 112 device.friendly_name in d.friendly_name]: 113 break 114 else: 115 d += 1 116 name = None 117 continue 118 self.devices[name] = device 119 return True
120
121 - def del_device(self, device):
122 if device in self.devices.values(): 123 for k, v in self.devices.items(): 124 if v == device: 125 del self.devices[k] 126 break
127
128 - def add_service(self, service):
129 """ Adds a service to the device. 130 """ 131 if service.service_type not in self.services: 132 self.services[service.service_type] = service
133
134 - def get_service_by_type(self, service_type):
135 """ Returns a service given its type. 136 """ 137 return self.services.get(service_type, None)
138
139 - def del_service(self, service):
140 """ Removes a service, if present on the device. 141 """ 142 if service.service_type in self.services: 143 del self.services[service.service_type]
144
145 - def is_root_device(self):
146 """ Returns True if this device is a root device (it contains embedded 147 devices). 148 """ 149 return True if self.devices or self.is_root else False
150 151
152 -class BaseDeviceIcon(object):
153 """ Represents an icon of a device. 154 """ 155
156 - def __init__(self, mimetype, width, height, depth, url):
157 """ Constructor for the DeviceIcon class. 158 159 @param mimetype: mimetype for the icon 160 @param width: icon width 161 @param height: icon height 162 @param depth: icon depth 163 @param url: icon url 164 165 @type mimetype: string 166 @type width: string 167 @type height: string 168 @type depth: string 169 @type url: string 170 """ 171 self.mimetype = mimetype 172 self.width = width 173 self.height = height 174 self.depth = depth 175 self.url = url
176
177 - def get_mimetype(self):
178 """ Returns icon's mimetype. 179 180 @rtype: string 181 """ 182 return self.mimetype
183
184 - def get_width(self):
185 """ Returns icon's width. 186 187 @rtype: string 188 """ 189 return self.width
190
191 - def get_height(self):
192 """ Returns icon's height. 193 194 @rtype: string 195 """ 196 return self.height
197
198 - def get_depth(self):
199 """ Returns icon's depth. 200 201 @rtype: string 202 """ 203 return self.depth
204
205 - def get_url(self):
206 """ Returns icon's url. 207 208 @rtype: string 209 """ 210 return self.url
211