Package caldavclientlibrary :: Package protocol :: Package webdav :: Module lock
[hide private]
[frames] | no frames]

Source Code for Module caldavclientlibrary.protocol.webdav.lock

  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 caldavclientlibrary.protocol.webdav.requestresponse import RequestResponse 
 18  from caldavclientlibrary.protocol.webdav.definitions import methods 
 19  from caldavclientlibrary.protocol.webdav.definitions import headers 
 20  from StringIO import StringIO 
 21  from caldavclientlibrary.protocol.http.data.string import RequestDataString 
 22  from caldavclientlibrary.protocol.webdav.definitions import davxml 
 23  from xml.etree.ElementTree import Element 
 24  from caldavclientlibrary.protocol.utils.xmlhelpers import BetterElementTree 
 25   
26 -class Lock(RequestResponse):
27 28 eExclusive = 0 29 eShared = 1 30 31 eResourceMustExist = 0 32 eResourceMustNotExist = 1 33 eResourceMayExist = 2 34
35 - def __init__(self, session, url, depth, scope, owner, timeout, exists=eResourceMustExist):
36 37 assert(depth in (headers.Depth0, headers.Depth1, headers.DepthInfinity)) 38 assert(scope in (Lock.eExclusive, Lock.eShared,)) 39 assert(exists in (Lock.eResourceMustExist, Lock.eResourceMustNotExist, Lock.eResourceMayExist)) 40 41 super(Lock, self).__init__(session, methods.LOCK, url) 42 43 self.depth = depth 44 self.scope = scope 45 self.owner = owner 46 self.timeout = timeout 47 48 # Do appropriate etag based on exists 49 if exists == Lock.eResourceMustExist: 50 self.etag = "*" 51 self.etag_match = True 52 elif exists == Lock.eResourceMustNotExist: 53 self.etag = "*" 54 self.etag_match = False 55 elif exists == Lock.eResourceMayExist: 56 pass 57 58 self.initRequestData()
59
60 - def initRequestData(self):
61 # Write XML info to a string 62 os = StringIO() 63 self.generateXML(os) 64 self.request_data = RequestDataString(os.getvalue(), "text/xml charset=utf-8")
65
66 - def addHeaders(self, hdrs):
67 # Do default 68 super(Lock, self).addHeaders(hdrs) 69 70 # Add depth header 71 hdrs.append((headers.Depth, self.depth)) 72 73 # Add timeout header 74 if self.timeout == -1: 75 hdrs.append((headers.Timeout, headers.TimeoutInfinite)) 76 elif self.timeout > 0: 77 hdrs.append((headers.Timeout, "%s%d" % (headers.TimeoutSeconds, self.timeout)))
78
79 - def generateXML(self, os):
80 # Structure of document is: 81 # 82 # <DAV:lockinfo> 83 # <DAV:lockscope> 84 # <DAV:exclusive/> | <DAV:shared/> 85 # </DAV:lockscope> 86 # <DAV:locktype> 87 # <DAV:write/> 88 # </DAV:locktype> 89 # <DAV:owner> 90 # <<owner>> 91 # </DAV:owner> 92 # </DAV:lockinfo> 93 94 # <DAV:lockinfo> element 95 lockinfo = Element(davxml.lockinfo) 96 97 # <DAV:lockscope> element 98 lockscope = Element(davxml.lockscope) 99 lockinfo.append(lockscope) 100 101 # <DAV:exclusive/> | <DAV:shared/> element 102 lockscope.append(Element(davxml.exclusive if self.scope == Lock.eExclusive else davxml.shared)) 103 104 # <DAV:locktype> element 105 locktype = Element(davxml.locktype) 106 lockinfo.append(locktype) 107 108 # <DAV:write/> element 109 locktype.append(Element(davxml.write)) 110 111 # <DAV:owner> element is optional 112 if self.owner: 113 # <DAV:owner> element 114 owner = Element(davxml.owner) 115 owner.text = self.owner 116 lockinfo.append(owner) 117 118 # Now we have the complete document, so write it out (no indentation) 119 BetterElementTree(lockinfo).writeUTF8(os)
120
121 - def getLockToken(self):
122 123 # Get the Lock-Token header from response headers 124 result = "" 125 if self.hasResponseHeader(headers.LockToken): 126 127 # Get Coded-URL 128 codedurl = self.getResponseHeader(headers.LockToken) 129 130 # Strip leading/trailing <> 131 codeurl = codedurl.strip() 132 if codeurl.startswith("<") and codeurl.endswith(">"): 133 result = codeurl[1:-1] 134 else: 135 result = codeurl 136 137 return result
138