def move(self, toUrl): """ Moves this resource to the given path or renames it. @param toUrl: new (URI) path """ self.connection.logger.debug("Move to " + repr(toUrl)); _checkUrl(toUrl) response = self.connection.move(self.path, toUrl) if response.status == Constants.CODE_MULTISTATUS and response.msr.errorCount > 0: raise WebdavError("Request failed: " + response.msr.reason, response.msr.code)
def _filter_property_response(self, response): """ This is a workaround for servers which do not correctly handle the depth header. I.e., we simply try to identify the matching response for the resource. """ for key in [self.path, self.path[:-1], self.url]: # Server may return path or URL try: return response.msr[key] except KeyError: continue raise WebdavError("No property result for '%s'" % self.url)
def validate(self): """ Check whether URL contains a WebDAV resource Uses the WebDAV OPTIONS method. @raise WebdavError: L{WebdavError} if URL does not contain a WebDAV resource """ #davHeader = response.getheader(HTTP_HEADER_DAV) davHeader = self.getSpecificOption(Constants.HTTP_HEADER_DAV) self.connection.logger.debug("HEADER DAV: %s" % davHeader) if not(davHeader) or davHeader.find("2") < 0: # DAV class 2 supported ? raise WebdavError("URL does not support WebDAV", 0)
def lock(self, owner): """ Locks this resource for exclusive write access. This means that for succeeding write operations the returned lock token has to be passed. If the methode does not throw an exception the lock has been granted. @param owner: describes the lock holder @return: lock token string (automatically generated) @rtype: L{LockToken} """ response = self.connection.lock(self.path, owner) if response.status == Constants.CODE_MULTISTATUS and response.msr.errorCount > 0: raise WebdavError("Request failed: " + response.msr.reason, response.msr.code) return LockToken(self.url, response.locktoken)
def readProperty(self, nameSpace, name): """ Reads the given property. @param nameSpace: XML-namespace @type nameSpace: string @param name: A property name. @type name: string @return: a map from property names to DOM Element or String values. """ results = self.readProperties((nameSpace, name)) if not (nameSpace, name) in results: raise WebdavError("Property is missing: " + results.reason) return results[(nameSpace, name)]
def delete(self, lockToken=None): """ Deletes this resource. @param lockToken: String returned by last lock operation or null. @type lockToken: L{LockToken} """ assert lockToken == None or isinstance(lockToken, LockToken), \ "Invalid lockToken argument %s" % type(lockToken) header = {} if lockToken: header = lockToken.toHeader() response = self.connection.delete(self.path, header) if response.status == Constants.CODE_MULTISTATUS and response.msr.errorCount > 0: raise WebdavError("Request failed: " + response.msr.reason, response.msr.code)
def copy(self, other): """Copy Principal object. @param other: Another principal to copy. @type other: L{Principal} object @raise WebdavError: When an object that is not a L{Principal} is passed a L{WebdavError} is raised. """ if not isinstance(other, Principal): raise WebdavError('Non-Principal object passed to copy method: ' % other.__class__) self.displayname = other.displayname self.principalURL = other.principalURL self.property = other.property
def copy(self, toUrl, infinity=True): """ Copies this resource. @param toUrl: target URI path @param infinity: Flag that indicates that the complete content of collection is copied. (default) @type depth: C{boolean} """ self.connection.logger.debug("Copy to " + repr(toUrl)); _checkUrl(toUrl) if infinity: response = self.connection.copy(self.path, toUrl) else: response = self.connection.copy(self.path, toUrl, 0) if response.status == Constants.CODE_MULTISTATUS and response.msr.errorCount > 0: raise WebdavError("Request failed: " + response.msr.reason, response.msr.code)
def deleteProperties(self, lockToken=None, *names): """ Removes the given properties from this resource. @param lockToken: if the resource has been locked this is the lock token. @type lockToken: L{LockToken} @param names: a collection of property names. A property name is a (XmlNameSpace, propertyName) tuple. """ assert lockToken == None or isinstance(lockToken, LockToken), \ "Invalid lockToken argument %s" % type(lockToken) header = {} if lockToken: header = lockToken.toHeader() body = createDeleteBody(names, self.defaultNamespace) response = self.connection.proppatch(self.path, body, header) if response.msr.errorCount > 0: raise WebdavError("Request failed: " + response.msr.reason, response.msr.code)
def readProperties(self, *names, **kwargs): """ Reads the given properties. @param names: a list of property names. A property name is a (XmlNameSpace, propertyName) tuple. @param ignore404: a boolean flag. Indicates if an error should be raised for missing properties. @return: a map from property names to DOM Element or String values. """ assert names, "Property names are missing." ignore404 = kwargs.pop('ignore404', False) body = createFindBody(names, self.defaultNamespace) response = self.connection.propfind(self.path, body, depth=0) properties = self._filter_property_response(response) if not ignore404 and properties.errorCount > 0 : raise WebdavError("Property is missing on '%s': %s" % (self.path, properties.reason), properties.code) return properties
def writeProperties(self, properties, lockToken=None): """ Sets or updates the given properties. @param lockToken: if the resource has been locked this is the lock token. @type lockToken: L{LockToken} @param properties: a map from property names to a String or DOM element value for each property to add or update. """ assert isinstance(properties, types.DictType) assert lockToken == None or isinstance(lockToken, LockToken), \ "Invalid lockToken argument %s" % type(lockToken) header = {} if lockToken: header = lockToken.toHeader() body = createUpdateBody(properties, self.defaultNamespace) response = self.connection.proppatch(self.path, body, header) if response.msr.errorCount > 0: raise WebdavError("Request failed: " + response.msr.reason, response.msr.code)
def __init__(self, domroot=None, displayname=None, principalURL=None): """ Constructor should be called with either no parameters (create blank Principal), one parameter (a DOM tree), or two parameters (displayname and URL or property tag). @param domroot: A DOM tree (default: None). @type domroot: L{webdav.WebdavResponse.Element} object @param displayname: The display name of a principal (default: None). @type displayname: C{string} @param principalURL: The URL representing a principal (default: None). @type principalURL: C{string} @raise WebdavError: When non-valid parameters or sets of parameters are passed a L{WebdavError} is raised. """ self.displayname = None self.principalURL = None self.property = None if domroot: for child in domroot.children: if child.ns == Constants.NS_DAV and ( child.name in self._TAG_LIST_PRINCIPALS): if child.name == Constants.TAG_PROP: self.displayname = \ child.find(Constants.PROP_DISPLAY_NAME, Constants.NS_DAV) elif child.name == Constants.TAG_HREF: self.principalURL = child.textof() if self.principalURL and self.property in self._TAG_LIST_STATUS: raise WebdavError( 'Principal cannot contain a URL and "%s"' % (self.property)) elif child.name == Constants.TAG_PROPERTY: if child.count() == 1: if self.property: raise WebdavError('Property for principal has already been set: old "%s", new "%s"' \ % (self.property, child.pop().name)) elif self.principalURL: raise WebdavError( 'Principal cannot contain a URL and "%s"' % (self.property)) else: self.property = child.pop().name else: raise WebdavError("There should be only one value in the property for a principal, we have: %s" \ % child.name) else: if self.property: raise WebdavError('Property for principal has already been set: old "%s", new "%s"' \ % (self.property, child.name)) else: self.property = child.name if self.principalURL and self.property in self._TAG_LIST_STATUS: raise WebdavError( 'Principal cannot contain a URL and "%s"' % (self.property)) else: # This shouldn't happen, something's wrong with the DOM tree raise WebdavError( 'Non-valid tag in principal DOM tree for constructor: %s' % child.name) elif displayname == None or principalURL == None: if displayname: self.displayname = displayname if principalURL: self.principalURL = principalURL else: # This shouldn't happen, someone screwed up with the params ... raise WebdavError( 'Non-valid parameters handed to Principal constructor.')