def toElement(self, **kwargs): root = Object.toElement(self, **kwargs) if self.childCount is not None: root.attrib['childCount'] = str(self.childCount) if self.createClass is not None: ET.SubElement(root, qname('createclass', UPNP_NS)).text = self.createClass if not isinstance(self.searchClass, (list, tuple)): self.searchClass = [self.searchClass] for i in self.searchClass: sc = ET.SubElement(root, qname('searchClass', UPNP_NS)) sc.attrib['includeDerived'] = '1' sc.text = i if self.searchable is not None: if self.searchable in (1, '1', True, 'true', 'True'): root.attrib['searchable'] = '1' else: root.attrib['searchable'] = '0' for res in self.res: root.append(res.toElement(**kwargs)) return root
def toElement(self, **kwargs): root = Object.toElement(self, **kwargs) if self.director is not None: ET.SubElement(root, qname('director', UPNP_NS)).text = self.director if self.refID is not None: ET.SubElement(root, 'refID').text = self.refID if self.actors is not None: for actor in self.actors: ET.SubElement(root, qname('actor', DC_NS)).text = actor #if self.language is not None: # ET.SubElement(root, qname('language',DC_NS)).text = self.language if kwargs.get('transcoding', False) == True: res = self.res.get_matching(['*:*:*:*'], protocol_type='http-get') if len(res) > 0 and is_audio(res[0].protocolInfo): old_res = res[0] if (kwargs.get('upnp_client', '') == 'XBox'): transcoded_res = old_res.transcoded('mp3') if transcoded_res != None: root.append(transcoded_res.toElement(**kwargs)) else: root.append(old_res.toElement(**kwargs)) else: for res in self.res: root.append(res.toElement(**kwargs)) transcoded_res = old_res.transcoded('lpcm') if transcoded_res != None: root.append(transcoded_res.toElement(**kwargs)) elif len(res) > 0 and is_video(res[0].protocolInfo): old_res = res[0] for res in self.res: root.append(res.toElement(**kwargs)) transcoded_res = old_res.transcoded('mpegts') if transcoded_res != None: root.append(transcoded_res.toElement(**kwargs)) else: for res in self.res: root.append(res.toElement(**kwargs)) else: for res in self.res: root.append(res.toElement(**kwargs)) return root
def toString(self): """ sigh - having that optional preamble here breaks some of the older ContentDirectoryClients """ #preamble = """<?xml version="1.0" encoding="utf-8"?>""" #return preamble + ET.tostring(self,encoding='utf-8') return ET.tostring(self, encoding='utf-8')
def build_soap_call(method, arguments, is_response=False, encoding=SOAP_ENCODING, envelope_attrib=None, typed=None): """ create a shell for a SOAP request or response element - set method to none to omitt the method element and add the arguments directly to the body (for an error msg) - arguments can be a dict or an ET.Element """ envelope = ET.Element("s:Envelope") if envelope_attrib: for n in envelope_attrib: envelope.attrib.update({n[0] : n[1]}) else: envelope.attrib.update({'s:encodingStyle' : "http://schemas.xmlsoap.org/soap/encoding/"}) envelope.attrib.update({'xmlns:s' :"http://schemas.xmlsoap.org/soap/envelope/"}) body = ET.SubElement(envelope, "s:Body") if method: # append the method call if is_response is True: method += "Response" re = ET.SubElement(body,method) if encoding: re.set(NS_SOAP_ENV + "encodingStyle", encoding) else: re = body # append the arguments if isinstance(arguments,dict): type_map = {str: 'xsd:string', unicode: 'xsd:string', int: 'xsd:int', long: 'xsd:int', float: 'xsd:float', bool: 'xsd:boolean'} for arg_name, arg_val in arguments.iteritems(): arg_type = type_map[type(arg_val)] if arg_type == 'xsd:string' and type(arg_val) == unicode: arg_val = arg_val.encode('utf-8') if arg_type == 'xsd:int' or arg_type == 'xsd:float': arg_val = str(arg_val) if arg_type == 'xsd:boolean': arg_val = arg_val.lower() e = ET.SubElement(re, arg_name) if typed and arg_type: if not isinstance(type, ET.QName): arg_type = ET.QName("http://www.w3.org/1999/XMLSchema", arg_type) e.set(NS_XSI + "type", arg_type) e.text = arg_val else: re.append(arguments) preamble = """<?xml version="1.0" encoding="utf-8"?>""" return preamble + ET.tostring(envelope,'utf-8')
def toElement(self, **kwargs): root = Item.toElement(self, **kwargs) for attr_name, ns in self.valid_attrs.iteritems(): value = getattr(self, attr_name, None) if value: ET.SubElement(root, qname(attr_name, ns)).text = value return root
def toElement(self, **kwargs): root = Item.toElement(self, **kwargs) if self.publisher is not None: ET.SubElement(root, qname('publisher', DC_NS)).text = self.publisher if self.language is not None: ET.SubElement(root, qname('language', DC_NS)).text = self.language if self.relation is not None: ET.SubElement(root, qname('relation', DC_NS)).text = self.relation if self.rights is not None: ET.SubElement(root, qname('rights', DC_NS)).text = self.rights return root
def toElement(self, **kwargs): root = Item.toElement(self, **kwargs) if self.rating is not None: ET.SubElement(root, qname('rating', UPNP_NS)).text = str(self.rating) if self.storageMedium is not None: ET.SubElement(root, qname('storageMedium', UPNP_NS)).text = self.storageMedium if self.publisher is not None: ET.SubElement(root, qname('publisher', DC_NS)).text = self.contributor if self.rights is not None: ET.SubElement(root, qname('rights', DC_NS)).text = self.rights return root
def toElement(self, **kwargs): root = AudioItem.toElement(self, **kwargs) if self.album is not None: ET.SubElement(root, qname('album', UPNP_NS)).text = self.album if self.playlist is not None: ET.SubElement(root, qname('playlist', UPNP_NS)).text = self.playlist if self.storageMedium is not None: ET.SubElement(root, qname('storageMedium', UPNP_NS)).text = self.storageMedium if self.contributor is not None: ET.SubElement(root, qname('contributor', DC_NS)).text = self.contributor return root
def fromString(cls, aString): instance = cls() elt = utils.parse_xml(aString, 'utf-8') elt = elt.getroot() for node in elt.getchildren(): upnp_class_name = node.findtext( '{%s}class' % 'urn:schemas-upnp-org:metadata-1-0/upnp/') upnp_class = instance.get_upnp_class(upnp_class_name.strip()) new_node = upnp_class.fromString(ET.tostring(node)) instance.addItem(new_node) return instance
def element_to_didl(item): """ a helper method to create a DIDLElement out of one ET element or XML fragment string """ if not isinstance(item, basestring): item = ET.tostring(item) didl = """<DIDL-Lite xmlns="urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dlna="urn:schemas-dlna-org:metadata-1-0" xmlns:pv="http://www.pv.com/pvns/" xmlns:upnp="urn:schemas-upnp-org:metadata-1-0/upnp/">""" \ + item + \ """</DIDL-Lite>""" return didl
def fromElement(self, elt): """ TODO: * creator * writeStatus """ self.elementName = elt.tag self.id = elt.attrib.get('id', None) self.parentID = elt.attrib.get('parentID', None) self.refID = elt.attrib.get('refID', None) if elt.attrib.get('restricted', None) in [1, 'true', 'True', '1', 'yes', 'Yes']: self.restricted = True else: self.restricted = False for child in elt.getchildren(): if child.tag.endswith('title'): self.title = child.text elif child.tag.endswith('albumArtURI'): self.albumArtURI = child.text elif child.tag.endswith('originalTrackNumber'): self.originalTrackNumber = int(child.text) elif child.tag.endswith('description'): self.description = child.text elif child.tag.endswith('longDescription'): self.longDescription = child.text elif child.tag.endswith('artist'): self.artist = child.text elif child.tag.endswith('genre'): if self.genre != None: if self.genres == None: self.genres = [ self.genre, ] self.genres.append(child.text) self.genre = child.text elif child.tag.endswith('album'): self.album = child.text elif child.tag.endswith('class'): self.upnp_class = child.text elif child.tag.endswith('server_uuid'): self.server_uuid = child.text elif child.tag.endswith('res'): res = Resource.fromString(ET.tostring(child)) self.res.append(res)
def toElement(self, **kwargs): root = ET.Element('res') if kwargs.get('upnp_client', '') in ('XBox', ): protocol, network, content_format, additional_info = self.protocolInfo.split( ':') if content_format in ['video/divx', 'video/x-msvideo']: content_format = 'video/avi' if content_format == 'audio/x-wav': content_format = 'audio/wav' additional_info = self.get_additional_info( upnp_client=kwargs.get('upnp_client', '')) root.attrib['protocolInfo'] = ':'.join( (protocol, network, content_format, additional_info)) else: protocol, network, content_format, additional_info = self.protocolInfo.split( ':') if content_format == 'video/x-msvideo': content_format = 'video/divx' additional_info = self.get_additional_info( upnp_client=kwargs.get('upnp_client', '')) root.attrib['protocolInfo'] = ':'.join( (protocol, network, content_format, additional_info)) root.text = self.data if self.bitrate is not None: root.attrib['bitrate'] = str(self.bitrate) if self.size is not None: root.attrib['size'] = str(self.size) if self.duration is not None: root.attrib['duration'] = self.duration if self.nrAudioChannels is not None: root.attrib['nrAudioChannels'] = self.nrAudioChannels if self.resolution is not None: root.attrib['resolution'] = self.resolution if self.importUri is not None: root.attrib['importUri'] = self.importUri return root
def toElement(self, **kwargs): root = ImageItem.toElement(self, **kwargs) if self.album is not None: ET.SubElement(root, qname('album', UPNP_NS)).text = self.album return root
def toString(self, **kwargs): return ET.tostring(self.toElement(**kwargs), encoding='utf-8')
def toElement(self, **kwargs): root = ET.Element(self.elementName) #if self.id == 1000: # root.attrib['id'] = '0' # ET.SubElement(root, 'dc:title').text = 'root' #else: # root.attrib['id'] = str(self.id) # ET.SubElement(root, 'dc:title').text = self.title root.attrib['id'] = str(self.id) ET.SubElement(root, qname('title', DC_NS)).text = self.title #if self.title != None: # ET.SubElement(root, 'dc:title').text = self.title #else: # ET.SubElement(root, 'dc:title').text = 'root' root.attrib['parentID'] = str(self.parentID) if (kwargs.get('upnp_client', '') != 'XBox'): if self.refID: root.attrib['refID'] = str(self.refID) if kwargs.get('requested_id', None): if kwargs.get('requested_id') == '0': t = root.find(qname('title', DC_NS)) t.text = 'root' #if kwargs.get('requested_id') != '0' and kwargs.get('requested_id') != root.attrib['id']: if kwargs.get('requested_id') != root.attrib['id']: if (kwargs.get('upnp_client', '') != 'XBox'): root.attrib['refID'] = root.attrib['id'] r_id = kwargs.get('requested_id') root.attrib['id'] = r_id r_id = r_id.split('@', 1) try: root.attrib['parentID'] = r_id[1] except IndexError: pass if (kwargs.get('upnp_client', '') != 'XBox'): self.info("Changing ID from %r to %r, with parentID %r", root.attrib['refID'], root.attrib['id'], root.attrib['parentID']) else: self.info("Changing ID from %r to %r, with parentID %r", self.id, root.attrib['id'], root.attrib['parentID']) elif kwargs.get('parent_container', None): if (kwargs.get('parent_container') != '0' and kwargs.get('parent_container') != root.attrib['parentID']): if (kwargs.get('upnp_client', '') != 'XBox'): root.attrib['refID'] = root.attrib['id'] root.attrib['id'] = '@'.join( (root.attrib['id'], kwargs.get('parent_container'))) root.attrib['parentID'] = kwargs.get('parent_container') if (kwargs.get('upnp_client', '') != 'XBox'): self.info( "Changing ID from %r to %r, with parentID from %r to %r", root.attrib['refID'], root.attrib['id'], self.parentID, root.attrib['parentID']) else: self.info( "Changing ID from %r to %r, with parentID from %r to %r", self.id, root.attrib['id'], self.parentID, root.attrib['parentID']) ET.SubElement(root, qname('class', UPNP_NS)).text = self.upnp_class if kwargs.get('upnp_client', '') == 'XBox': u = root.find(qname('class', UPNP_NS)) if (kwargs.get('parent_container', None) != None and u.text.startswith('object.container')): if kwargs.get('parent_container') in ('14', '15', '16'): u.text = 'object.container.storageFolder' if self.upnp_class == 'object.container': u.text = 'object.container.storageFolder' if self.restricted: root.attrib['restricted'] = '1' else: root.attrib['restricted'] = '0' if self.creator is not None: ET.SubElement(root, qname('creator', DC_NS)).text = self.creator if self.writeStatus is not None: ET.SubElement(root, qname('writeStatus', UPNP_NS)).text = self.writeStatus if self.date is not None: if isinstance(self.date, datetime): ET.SubElement(root, qname('date', DC_NS)).text = self.date.isoformat() else: ET.SubElement(root, qname('date', DC_NS)).text = self.date else: ET.SubElement(root, qname('date', DC_NS)).text = utils.datefaker().isoformat() if self.albumArtURI is not None: e = ET.SubElement(root, qname('albumArtURI', UPNP_NS)) e.text = self.albumArtURI e.attrib['xmlns:dlna'] = 'urn:schemas-dlna-org:metadata-1-0' e.attrib['dlna:profileID'] = 'JPEG_TN' if self.artist is not None: ET.SubElement(root, qname('artist', UPNP_NS)).text = self.artist if self.genre is not None: ET.SubElement(root, qname('genre', UPNP_NS)).text = self.genre if self.genres is not None: for genre in self.genres: ET.SubElement(root, qname('genre', UPNP_NS)).text = genre if self.originalTrackNumber is not None: ET.SubElement(root, qname('originalTrackNumber', UPNP_NS)).text = str(self.originalTrackNumber) if self.description is not None: ET.SubElement(root, qname('description', DC_NS)).text = self.description if self.longDescription is not None: ET.SubElement(root, qname('longDescription', UPNP_NS)).text = self.longDescription if self.server_uuid is not None: ET.SubElement(root, qname('server_uuid', UPNP_NS)).text = self.server_uuid return root