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

Source Code for Module caldavclientlibrary.protocol.webdav.propfindparser

  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.multiresponseparser import MultiResponseParser 
 18  from caldavclientlibrary.protocol.webdav.definitions import davxml 
 19  from caldavclientlibrary.protocol.http.util import parseStatusLine 
 20  from xml.etree.ElementTree import QName 
 21  from caldavclientlibrary.protocol.url import URL 
 22   
23 -class PropFindParser(MultiResponseParser):
24 25 textProperties = set() 26 hrefProperties = set() 27 hrefListProperties = set() 28
29 - class PropFindResult(object):
30
31 - def __init__(self):
32 self.status = None 33 self.textProperties = {} 34 self.hrefProperties = {} 35 self.nodeProperties = {} 36 self.badProperties = {}
37
38 - def setResource(self, resource):
40 - def getResource(self):
41 return self.resource
42
43 - def setStatus(self, status):
45 - def getStatus(self):
46 return self.status
47
48 - def addTextProperty(self, name, value):
49 self.textProperties[name] = value
50 - def getTextProperties(self):
51 return self.textProperties
52
53 - def addHrefProperty(self, name, value):
54 self.hrefProperties[name] = value
55 - def getHrefProperties(self):
56 return self.hrefProperties
57
58 - def addNodeProperty(self, name, node):
59 self.nodeProperties[name] = node
60 - def getNodeProperties(self):
61 return self.nodeProperties
62
63 - def addBadProperty(self, name, status):
64 self.badProperties[name] = status
65 - def getBadProperties(self):
66 return self.badProperties
67
68 - def __init__(self):
69 self.results = {}
70
71 - def getResults(self):
72 return self.results
73 74 # Parse the response element down to the properties
75 - def parseResponse(self, response):
76 # Verify that the node is the correct element <DAV:response> 77 if response.tag != davxml.response: 78 return 79 80 # Node is the right type, so iterate over all child response nodes and process each one 81 result = PropFindParser.PropFindResult() 82 for child in response.getchildren(): 83 84 # Is it the href 85 if child.tag == davxml.href: 86 result.setResource(child.text) 87 88 # Is it propstat 89 elif child.tag == davxml.propstat: 90 self.parsePropStat(child, result) 91 92 elif child.tag == davxml.status: 93 result.setStatus(child.text) 94 95 # Add the resource only if we got one 96 if result.getResource(): 97 self.results[result.getResource()] = result
98
99 - def parsePropStat(self, propstat, result):
100 # Scan the propstat node the status - we only process OK status 101 102 # Now look for a <DAV:status> element in its children 103 status = propstat.find(str(davxml.status)) 104 105 # Now parse the response and dispatch accordingly 106 if status is not None: 107 108 status_result = parseStatusLine(status.text) 109 badstatus = status_result if status_result / 100 != 2 else None 110 111 # Now look for a <DAV:prop> element in its children 112 for item in propstat.findall(str(davxml.prop)): 113 self.parseProp(item, result, badstatus)
114
115 - def parseProp(self, property, result, badstatus):
116 # Scan the prop node - each child is processed 117 for item in property.getchildren(): 118 self.parsePropElement(item, result, badstatus)
119 120 # Parsing of property elements
121 - def parsePropElement(self, prop, result, badstatus):
122 # Here we need to detect the type of element and dispatch accordingly 123 if badstatus: 124 result.addBadProperty(QName(prop.tag), badstatus) 125 126 # Determine what type of content we have 127 else: 128 children = prop.getchildren() 129 children_length = len(children) 130 if children_length == 0: 131 self.parsePropElementText(prop, result) 132 elif prop.text is None or not prop.text.strip(): 133 if children_length == 1: 134 if children[0].tag == davxml.href: 135 self.parsePropElementHref(prop, result, False) 136 else: 137 self.parsePropElementUnknown(prop, result) 138 else: 139 allHref = True 140 for child in children: 141 if child.tag != davxml.href: 142 allHref = False 143 if allHref: 144 self.parsePropElementHref(prop, result, True) 145 else: 146 self.parsePropElementUnknown(prop, result) 147 else: 148 self.parsePropElementUnknown(prop, result)
149
150 - def parsePropElementText(self, prop, result):
151 # Grab the element data 152 result.addTextProperty(QName(prop.tag), prop.text if prop.text else "") 153 result.addNodeProperty(QName(prop.tag), prop)
154
155 - def parsePropElementHref(self, prop, result, is_list):
156 # Grab the element data 157 hrefs = tuple([URL(url=href.text, decode=True) for href in prop.findall(str(davxml.href))]) 158 if not is_list: 159 if len(hrefs) == 1: 160 hrefs = hrefs[0] 161 else: 162 hrefs = "" 163 result.addHrefProperty(QName(prop.tag), hrefs) 164 result.addNodeProperty(QName(prop.tag), prop)
165
166 - def parsePropElementUnknown(self, prop, result):
167 # Just add the node 168 result.addNodeProperty(QName(prop.tag), prop)
169