Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_add_item_to_sonos_playlist(self, moco):
moco.contentDirectory.reset_mock()
playlist = mock.Mock()
playlist.item_id = 7
ressource = mock.Mock()
ressource.uri = 'fake_uri'
track = mock.Mock()
track.resources = [ressource]
track.uri = 'fake_uri'
track.to_element.return_value = XML.Element('a')
update_id = 100
moco.contentDirectory.Browse.return_value = {
'NumberReturned': '0',
'Result': '',
'TotalMatches': '0',
'UpdateID': update_id
}
moco.add_item_to_sonos_playlist(track, playlist)
moco.contentDirectory.Browse.assert_called_once_with([
('ObjectID', playlist.item_id),
('BrowseFlag', 'BrowseDirectChildren'),
('Filter', '*'),
('StartingIndex', 0),
def test_add_item_to_sonos_playlist(self, moco):
moco.contentDirectory.reset_mock()
playlist = mock.Mock()
playlist.item_id = 7
track = mock.Mock()
track.uri = 'fake_uri'
track.didl_metadata = XML.Element('a')
update_id = 100
moco.contentDirectory.Browse.return_value = {
'NumberReturned': '0',
'Result': '',
'TotalMatches': '0',
'UpdateID': update_id
}
moco.add_item_to_sonos_playlist(track, playlist)
moco.contentDirectory.Browse.assert_called_once_with([
('ObjectID', playlist.item_id),
('BrowseFlag', 'BrowseDirectChildren'),
('Filter', '*'),
('StartingIndex', 0),
def get_element(self):
if not self.include_derived:
raise Exception('Could not create Element from CreateClass: '
'includeDerived attribute missing (required).')
elt = ElementTree.Element('upnp:createClass')
elt.attrib['includeDerived'] = {True: 'true', False: 'false'}\
.get(self.include_derived, False)
if self.class_friendly_name:
elt.attrib['name'] = self.class_friendly_name
elt.text = self.class_name
return elt
def _base_body(self):
"""Return the base XML body, which has the following form:
.. code :: xml
self._session_id
self._serial_number
Sonos
"""
xml = XML.Element(ns_tag('s', 'Envelope'))
# Add the Header part
XML.SubElement(xml, ns_tag('s', 'Header'))
credentials = XML.SubElement(xml[0], ns_tag('', 'credentials'))
XML.SubElement(credentials, 'sessionID').text = self._session_id
XML.SubElement(credentials, 'deviceID').text = self._serial_number
XML.SubElement(credentials, 'deviceProvider').text = 'Sonos'
return xml
token = XML.SubElement(login_token, 'token')
token.text = music_service.account.oa_device_id
key = XML.SubElement(login_token, 'key')
key.text = music_service.account.key
household_id = XML.SubElement(login_token, 'householdId')
household_id.text = self._device.household_id
credentials_header.append(login_token)
# otherwise, perhaps use DeviceLink or UserId auth
elif music_service.auth_type in ['DeviceLink', 'UserId']:
# We need a session ID from Sonos
session_id = self._device.musicServices.GetSessionId([
('ServiceId', music_service.service_id),
('Username', music_service.account.username)
])['SessionId']
session_elt = XML.Element('sessionId')
session_elt.text = session_id
credentials_header.append(session_elt)
# Anonymous auth. No need for anything further.
self._cached_soap_header = XML.tostring(
credentials_header,
encoding='utf-8').decode(encoding='utf-8')
return self._cached_soap_header
def to_didl_element(self):
""" Returns an Element based on this Resource.
"""
if not self.protocol_info:
raise Exception('Could not create Element for this resource: '
'protocolInfo not set (required).')
root = ElementTree.Element('res')
# Required
root.attrib['protocolInfo'] = self.protocol_info
# Optional
if self.import_uri:
root.attrib['importUri'] = self.import_uri
if self.size:
root.attrib['size'] = self.size
if self.duration:
root.attrib['duration'] = self.duration
if self.bitrate:
root.attrib['bitrate'] = self.bitrate
if self.sample_frequency:
root.attrib['sampleFrequency'] = self.sample_frequency
if self.bits_per_sample:
Return:
~xml.etree.ElementTree.Element: an Element.
"""
elt_attrib = {}
if include_namespaces:
elt_attrib.update({
'xmlns': "urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/",
'xmlns:dc': "http://purl.org/dc/elements/1.1/",
'xmlns:upnp': "urn:schemas-upnp-org:metadata-1-0/upnp/",
})
elt_attrib.update({
'parentID': self.parent_id,
'restricted': 'true' if self.restricted else 'false',
'id': self.item_id
})
elt = XML.Element(self.tag, elt_attrib)
# Add the title, which should always come first, according to the spec
XML.SubElement(elt, 'dc:title').text = self.title
# Add in any resources
for resource in self.resources:
elt.append(resource.to_element())
# Add the rest of the metadata attributes (i.e all those listed in
# _translation) as sub-elements of the item element.
for key, value in self._translation.items():
if hasattr(self, key):
# Some attributes have a namespace of '', which means they
# are in the default namespace. We need to handle those
# carefully
tag = "%s:%s" % value if value[0] else "%s" % value[1]
# Check if we have the attributes to create the didl metadata:
for key in ['extended_id', 'title', 'item_class']:
if not hasattr(self, key):
message = 'The property \'{}\' is not present on this item. '\
'This indicates that this item was not meant to create '\
'didl_metadata'.format(key)
raise DIDLMetadataError(message)
if 'description' not in self.content:
message = 'The item for \'description\' is not present in '\
'self.content. This indicates that this item was not meant '\
'to create didl_metadata'
raise DIDLMetadataError(message)
# Main element, ugly? yes! but I have given up on using namespaces
# with xml.etree.ElementTree
xml = XML.Element(
'{urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/}DIDL-Lite'
)
# Item sub element
item_attrib = {
'parentID': '',
'restricted': 'true',
'id': self.extended_id
}
# Only add the parent_id if we have it
if self.parent_id:
item_attrib['parentID'] = self.parent_id
item = XML.SubElement(
xml,
'{urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/}item',
item_attrib,
)