Package caldavclientlibrary :: Package protocol :: Package carddav :: Module makeaddressbook
[hide private]
[frames] | no frames]

Source Code for Module caldavclientlibrary.protocol.carddav.makeaddressbook

 1  ## 
 2  # Copyright (c) 2007-2016 Apple Inc. All rights reserved. 
 3  # 
 4  # Licensed under the Apache License, Version 2.0 (the "License"); 
 5  # you may not use this file except in compliance with the License. 
 6  # You may obtain a copy of the License at 
 7  # 
 8  # http://www.apache.org/licenses/LICENSE-2.0 
 9  # 
10  # Unless required by applicable law or agreed to in writing, software 
11  # distributed under the License is distributed on an "AS IS" BASIS, 
12  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
13  # See the License for the specific language governing permissions and 
14  # limitations under the License. 
15  ## 
16   
17  from StringIO import StringIO 
18  from caldavclientlibrary.protocol.carddav.definitions import carddavxml 
19  from caldavclientlibrary.protocol.http.data.string import RequestDataString 
20  from caldavclientlibrary.protocol.utils.xmlhelpers import BetterElementTree 
21  from caldavclientlibrary.protocol.webdav.definitions import davxml, methods 
22  from caldavclientlibrary.protocol.webdav.requestresponse import RequestResponse 
23  from xml.etree.ElementTree import Element 
24  from xml.etree.ElementTree import SubElement 
25   
26 -class MakeAddressBook(RequestResponse):
27
28 - def __init__(self, session, url, displayname=None, description=None):
34
35 - def initRequestData(self):
36 # Write XML info to a string 37 os = StringIO() 38 self.generateXML(os) 39 self.request_data = RequestDataString(os.getvalue(), "text/xml charset=utf-8")
40
41 - def generateXML(self, os):
42 # Structure of document is: 43 # 44 # <WEBDAV:mkcol> 45 # <DAV:set> 46 # <DAV:prop> 47 # <DAV:resourcetype><DAV:collection/><CARDDAV:addressbook/></DAV:resourcetype> 48 # <<each property as elements>> 49 # </DAV:prop> 50 # </DAV:set> 51 # </WEBDAV:mkcol> 52 53 # <CALDAV:mkcalendar> element 54 mkcol = Element(davxml.mkcol) 55 56 # <DAV:set> element 57 set = SubElement(mkcol, davxml.set) 58 59 # <DAV:prop> element 60 prop = SubElement(set, davxml.prop) 61 62 # <WebDAV:resourcetype> element 63 resourcetype = SubElement(prop, davxml.resourcetype) 64 SubElement(resourcetype, davxml.collection) 65 SubElement(resourcetype, carddavxml.addressbook) 66 67 # <DAV:displayname> element 68 if self.displayname: 69 displayname = SubElement(prop, davxml.displayname) 70 displayname.text = self.displayname 71 72 # <CardDAV:addressbook-description> element 73 if self.description: 74 description = SubElement(prop, carddavxml.addressbook_description) 75 description.text = self.description 76 77 # Now we have the complete document, so write it out (no indentation) 78 xmldoc = BetterElementTree(mkcol) 79 xmldoc.writeUTF8(os)
80