Package caldavclientlibrary :: Package ui :: Module resource
[hide private]
[frames] | no frames]

Source Code for Module caldavclientlibrary.ui.resource

  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.url import URL 
 18  from caldavclientlibrary.protocol.webdav.definitions import davxml 
 19  import os 
 20   
21 -class Resource(object):
22 """ 23 Maintains data for a WebDAV resource, including list of properties, 24 child resources and actual data. 25 """ 26
27 - def __init__(self, session, path, lastmod="", size="", type=""):
28 29 self.session = session 30 self.path = path.rstrip("/") 31 self.iscollection = path.endswith("/") 32 self.lastmod = lastmod 33 self.size = size 34 self.type = type 35 self.children = None 36 self.details = None 37 self.data = None
38
39 - def getPath(self):
40 return self.path
41
42 - def getName(self):
43 return os.path.basename(self.path)
44
45 - def getLastMod(self):
46 return self.lastmod
47
48 - def getSize(self):
49 return self.size
50
51 - def getType(self):
52 return self.type
53
54 - def isCollection(self):
55 return self.iscollection
56
57 - def findPath(self, path=None, elements=None):
58 if path: 59 elements = path.lstrip("/").rstrip("/").split("/") 60 if self.listChildren(): 61 for child in self.children: 62 if child.getName() == elements[0]: 63 elements = elements[1:] 64 if elements: 65 return child.findPath(elements=elements) 66 else: 67 return child 68 return None
69
70 - def findChild(self, name):
71 if self.children: 72 for child in self.children: 73 if child.getName() == name: 74 return child 75 return None
76
77 - def listChildren(self):
78 if self.children is None: 79 resource = URL(url=self.path+"/") 80 props = ( 81 davxml.resourcetype, 82 davxml.getlastmodified, 83 davxml.getcontentlength, 84 davxml.getcontenttype, 85 ) 86 results = self.session.account.session.getPropertiesOnHierarchy(resource, props) 87 items = results.keys() 88 items.sort() 89 self.children = [Resource( 90 self.session, 91 rurl, 92 lastmod=results[rurl].get(davxml.getlastmodified, ""), 93 size=results[rurl].get(davxml.getcontentlength, ""), 94 type=results[rurl].get(davxml.getcontenttype, ""), 95 ) for rurl in items if rurl != self.path + "/"] 96 return self.children
97
98 - def getDetails(self):
99 resource = URL(url=self.path+"/") 100 props = (davxml.resourcetype,) 101 props += (davxml.getcontentlength, davxml.getlastmodified,) 102 props, _ignore_bad = self.session.account.session.getProperties(resource, props) 103 size = props.get(davxml.getcontentlength, "-") 104 if not size: 105 size = "0" 106 modtime = props.get(davxml.getlastmodified, "-") 107 return ["Size: %s" % (size,), "Modtime: %s" % (modtime,)]
108
109 - def getAllDetails(self):
110 if self.details is None: 111 resource = URL(url=self.path+"/") 112 props = self.session.account.session.getPropertyNames(resource) 113 results, _ignore_bad = self.session.account.session.getProperties(resource, props) 114 sorted = results.keys() 115 sorted.sort() 116 self.details = [(key, results[key],) for key in sorted] 117 return self.details
118
119 - def getData(self):
120 if self.data is None: 121 resource = URL(url=self.path+"/") 122 self.data, _ignore_etag = self.session.account.session.readData(resource) 123 return self.data
124
125 - def getDataAsHTML(self):
126 data = self.getData() 127 if not self.type.startswith("text/html"): 128 data = "<HTML><BODY>%s</BODY></HTML>" % (data.replace("\r\n", "\n").replace("\r", "\n").replace("\n", "<br>\n"),) 129 return data
130
131 - def clear(self):
132 self.children = None 133 self.details = None 134 self.data = None
135