~ubuntu-branches/ubuntu/lucid/twisted-web2/lucid

« back to all changes in this revision

Viewing changes to twisted/web2/dav/static.py

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2008-03-30 22:08:20 UTC
  • mfrom: (0.1.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20080330220820-m2cd4jon35wz2efx
Tags: 8.0.1-1
New upstream version.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
##
2
 
# Copyright (c) 2005 Apple Computer, Inc. All rights reserved.
3
 
#
4
 
# Permission is hereby granted, free of charge, to any person obtaining a copy
5
 
# of this software and associated documentation files (the "Software"), to deal
6
 
# in the Software without restriction, including without limitation the rights
7
 
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
 
# copies of the Software, and to permit persons to whom the Software is
9
 
# furnished to do so, subject to the following conditions:
10
 
11
 
# The above copyright notice and this permission notice shall be included in all
12
 
# copies or substantial portions of the Software.
13
 
14
 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
 
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
 
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
 
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
 
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
 
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
 
# SOFTWARE.
21
 
#
22
 
# DRI: Wilfredo Sanchez, wsanchez@apple.com
23
 
##
24
 
 
25
 
"""
26
 
WebDAV-aware static resources.
27
 
"""
28
 
 
29
 
__all__ = ["DAVFile"]
30
 
 
31
 
from twisted.python import log
32
 
from twisted.web2.static import File
33
 
from twisted.web2.dav import davxml
34
 
from twisted.web2.dav.idav import IDAVResource
35
 
from twisted.web2.dav.resource import DAVResource
36
 
from twisted.web2.dav.util import bindMethods
37
 
 
38
 
try:
39
 
    from twisted.web2.dav.xattrprops import xattrPropertyStore as DeadPropertyStore
40
 
except ImportError:
41
 
    log.msg("No dead property store available; using nonePropertyStore.")
42
 
    log.msg("Setting of dead properties will not be allowed.")
43
 
    from twisted.web2.dav.noneprops import NonePropertyStore as DeadPropertyStore
44
 
 
45
 
class DAVFile (DAVResource, File):
46
 
    """
47
 
    WebDAV-accessible File resource.
48
 
 
49
 
    Extends twisted.web2.static.File to handle WebDAV methods.
50
 
    """
51
 
    def __init__(self, path,
52
 
                 defaultType="text/plain",
53
 
                 indexNames=None):
54
 
        """
55
 
        @param path: the path of the file backing this resource.
56
 
        @param defaultType: the default mime type (as a string) for this
57
 
            resource and (eg. child) resources derived from it.
58
 
        @param indexNames: a sequence of index file names.
59
 
        @param acl: an L{IDAVAccessControlList} with the .
60
 
        """
61
 
        super(DAVFile, self).__init__(path,
62
 
                                      defaultType = defaultType,
63
 
                                      ignoredExts = (),
64
 
                                      processors  = None,
65
 
                                      indexNames  = indexNames)
66
 
 
67
 
    def __repr__(self):
68
 
        return "<%s: %s>" % (self.__class__.__name__, self.fp.path)
69
 
 
70
 
    ##
71
 
    # WebDAV
72
 
    ##
73
 
 
74
 
    def davComplianceClasses(self):
75
 
        return ("1",) # Add "2" when we have locking
76
 
 
77
 
    def deadProperties(self):
78
 
        if not hasattr(self, "_dead_properties"):
79
 
            self._dead_properties = DeadPropertyStore(self)
80
 
        return self._dead_properties
81
 
 
82
 
    def isCollection(self):
83
 
        """
84
 
        See L{IDAVResource.isCollection}.
85
 
        """
86
 
        for child in self.listChildren(): return True
87
 
        return self.fp.isdir()
88
 
 
89
 
    def findChildren(self, depth):
90
 
        """
91
 
        See L{IDAVResource.findChildren}.
92
 
        """
93
 
        assert depth in ("0", "1", "infinity"), "Invalid depth: %s" % (depth,)
94
 
        if depth != "0" and self.isCollection():
95
 
            for name in self.listChildren():
96
 
                try:
97
 
                    child = IDAVResource(self.getChild(name))
98
 
                except TypeError:
99
 
                    child = None
100
 
 
101
 
                if child is not None:
102
 
                    if child.isCollection():
103
 
                        yield (child, name + "/")
104
 
                        if depth == "infinity":
105
 
                            for grandchild in child.findChildren(depth):
106
 
                                yield (grandchild[0], name + "/" + grandchild[1])
107
 
                    else:
108
 
                        yield (child, name)
109
 
 
110
 
    ##
111
 
    # ACL
112
 
    ##
113
 
 
114
 
    def supportedPrivileges(self):
115
 
        if not hasattr(DAVFile, "_supportedPrivilegeSet"):
116
 
            DAVFile._supportedPrivilegeSet = davxml.SupportedPrivilegeSet(
117
 
                davxml.SupportedPrivilege(
118
 
                    davxml.Privilege(davxml.All()),
119
 
                    davxml.Description("all privileges", **{"xml:lang": "en"}),
120
 
                    davxml.SupportedPrivilege(
121
 
                        davxml.Privilege(davxml.Read()),
122
 
                        davxml.Description("read resource", **{"xml:lang": "en"}),
123
 
                    ),
124
 
                    davxml.SupportedPrivilege(
125
 
                        davxml.Privilege(davxml.Write()),
126
 
                        davxml.Description("write resource", **{"xml:lang": "en"}),
127
 
                        davxml.SupportedPrivilege(
128
 
                            davxml.Privilege(davxml.WriteProperties()),
129
 
                            davxml.Description("write resource properties", **{"xml:lang": "en"}),
130
 
                        ),
131
 
                        davxml.SupportedPrivilege(
132
 
                            davxml.Privilege(davxml.WriteContent()),
133
 
                            davxml.Description("write resource content", **{"xml:lang": "en"}),
134
 
                        ),
135
 
                        davxml.SupportedPrivilege(
136
 
                            davxml.Privilege(davxml.Bind()),
137
 
                            davxml.Description("add child resource", **{"xml:lang": "en"}),
138
 
                        ),
139
 
                        davxml.SupportedPrivilege(
140
 
                            davxml.Privilege(davxml.Unbind()),
141
 
                            davxml.Description("remove child resource", **{"xml:lang": "en"}),
142
 
                        ),
143
 
                    ),
144
 
                    davxml.SupportedPrivilege(
145
 
                        davxml.Privilege(davxml.Unlock()),
146
 
                        davxml.Description("unlock resource without ownership", **{"xml:lang": "en"}),
147
 
                    ),
148
 
                    davxml.SupportedPrivilege(
149
 
                        davxml.Privilege(davxml.ReadACL()),
150
 
                        davxml.Description("read resource access control list", **{"xml:lang": "en"}),
151
 
                    ),
152
 
                    davxml.SupportedPrivilege(
153
 
                        davxml.Privilege(davxml.WriteACL()),
154
 
                        davxml.Description("write resource access control list", **{"xml:lang": "en"}),
155
 
                    ),
156
 
                    davxml.SupportedPrivilege(
157
 
                        davxml.Privilege(davxml.ReadCurrentUserPrivilegeSet()),
158
 
                        davxml.Description("read privileges for current principal", **{"xml:lang": "en"}),
159
 
                    ),
160
 
                ),
161
 
            )
162
 
        return DAVFile._supportedPrivilegeSet
163
 
 
164
 
    ##
165
 
    # Workarounds for issues with File
166
 
    ##
167
 
 
168
 
    def ignoreExt(self, ext):
169
 
        """
170
 
        Does nothing; doesn't apply to this subclass.
171
 
        """
172
 
        pass
173
 
 
174
 
    def createSimilarFile(self, path):
175
 
        return self.__class__(path, defaultType=self.defaultType, indexNames=self.indexNames[:])
176
 
 
177
 
#
178
 
# Attach method handlers to DAVFile
179
 
#
180
 
 
181
 
import twisted.web2.dav.method
182
 
 
183
 
bindMethods(twisted.web2.dav.method, DAVFile)