| 15 | import tr |
| 16 | |
| 17 | def createRequestXML(service, action, arguments=None): |
| 18 | from xml.dom.minidom import Document |
| 19 | |
| 20 | doc = Document() |
| 21 | |
| 22 | # create the envelope element and set its attributes |
| 23 | envelope = doc.createElementNS('', 's:Envelope') |
| 24 | envelope.setAttribute('xmlns:s', 'http://schemas.xmlsoap.org/soap/envelope/') |
| 25 | envelope.setAttribute('s:encodingStyle', 'http://schemas.xmlsoap.org/soap/encoding/') |
| 26 | |
| 27 | # create the body element |
| 28 | body = doc.createElementNS('', 's:Body') |
| 29 | |
| 30 | # create the function element and set its attribute |
| 31 | fn = doc.createElementNS('', 'u:%s' % action) |
| 32 | fn.setAttribute('xmlns:u', 'urn:schemas-upnp-org:service:%s' % service) |
| 33 | |
| 34 | # setup the argument element names and values |
| 35 | # using a list of tuples to preserve order |
| 36 | |
| 37 | # container for created nodes |
| 38 | argument_list = [] |
| 39 | |
| 40 | # iterate over arguments, create nodes, create text nodes, |
| 41 | # append text nodes to nodes, and finally add the ready product |
| 42 | # to argument_list |
| 43 | if arguments is not None: |
| 44 | for k, v in arguments: |
| 45 | tmp_node = doc.createElement(k) |
| 46 | tmp_text_node = doc.createTextNode(v) |
| 47 | tmp_node.appendChild(tmp_text_node) |
| 48 | argument_list.append(tmp_node) |
| 49 | |
| 50 | # append the prepared argument nodes to the function element |
| 51 | for arg in argument_list: |
| 52 | fn.appendChild(arg) |
| 53 | |
| 54 | # append function element to the body element |
| 55 | body.appendChild(fn) |
| 56 | |
| 57 | # append body element to envelope element |
| 58 | envelope.appendChild(body) |
| 59 | |
| 60 | # append envelope element to document, making it the root element |
| 61 | doc.appendChild(envelope) |
| 62 | |
| 63 | # our tree is ready, conver it to a string |
| 64 | return doc.toxml() |
| 65 | |
| 66 | class UPnPError(Exception): |
| 67 | def __init__(self, message): |