• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

zopefoundation / Zope / 10365406565

13 Aug 2024 07:23AM UTC coverage: 82.876% (+0.6%) from 82.268%
10365406565

push

github

web-flow
Fix MANIFEST.in (#1222)

5525 of 8123 branches covered (68.02%)

Branch coverage included in aggregate %.

28030 of 32365 relevant lines covered (86.61%)

0.87 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

45.3
/src/webdav/common.py
1
##############################################################################
2
#
3
# Copyright (c) 2002 Zope Foundation and Contributors.
4
#
5
# This software is subject to the provisions of the Zope Public License,
6
# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
7
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
8
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
9
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
10
# FOR A PARTICULAR PURPOSE
11
#
12
##############################################################################
13
"""Commonly used functions for WebDAV support modules."""
14

15
import re
1✔
16
from urllib.parse import urlparse
1✔
17
from urllib.parse import urlunparse
1✔
18

19
from Acquisition import aq_base
1✔
20
from Acquisition import aq_parent
1✔
21
from zExceptions import HTTPConflict
1✔
22
from zExceptions import HTTPLocked
1✔
23
from zExceptions import HTTPPreconditionFailed
1✔
24
from zExceptions import HTTPUnsupportedMediaType
1✔
25

26

27
class WebDAVException(Exception):
1✔
28
    pass
1✔
29

30

31
class Locked(WebDAVException, HTTPLocked):
1✔
32
    pass
1✔
33

34

35
class PreconditionFailed(WebDAVException, HTTPPreconditionFailed):
1✔
36
    pass
1✔
37

38

39
class Conflict(WebDAVException, HTTPConflict):
1✔
40
    pass
1✔
41

42

43
class UnsupportedMediaType(WebDAVException, HTTPUnsupportedMediaType):
1✔
44
    pass
1✔
45

46

47
def absattr(attr):
1✔
48
    if callable(attr):
1✔
49
        return attr()
1✔
50
    return attr
1✔
51

52

53
def urljoin(url, s):
1✔
54
    url = url.rstrip('/')
1✔
55
    s = s.lstrip('/')
1✔
56
    return '/'.join((url, s))
1✔
57

58

59
def urlfix(url, s):
1✔
60
    n = len(s)
1✔
61
    if url[-n:] == s:
1✔
62
        url = url[:-n]
1✔
63
    if len(url) > 1 and url[-1] == '/':
1✔
64
        url = url[:-1]
1✔
65
    return url
1✔
66

67

68
def is_acquired(ob):
1✔
69
    # Return true if this object is not a direct
70
    # subobject of its __parent__ object.
71
    if not hasattr(ob, '__parent__'):
×
72
        return 0
×
73
    if hasattr(aq_base(aq_parent(ob)), absattr(ob.id)):
×
74
        return 0
×
75
    if hasattr(aq_base(ob), 'isTopLevelPrincipiaApplicationObject') and \
×
76
            ob.isTopLevelPrincipiaApplicationObject:
77
        return 0
×
78
    return 1
×
79

80

81
def urlbase(url, ftype=None, fhost=None):
1✔
82
    # Return a '/' based url such as '/foo/bar', removing
83
    # type, host and port information if necessary.
84
    parsed = urlparse(url)
1✔
85
    return urlunparse(('', '') + tuple(parsed)[2:]) or '/'
1✔
86

87

88
def isDavCollection(object):
1✔
89
    """Return true if object is a DAV collection."""
90
    return getattr(object, '__dav_collection__', 0)
1✔
91

92

93
def tokenFinder(token):
1✔
94
    # takes a string like '<opaquelocktoken:afsdfadfadf> and returns the token
95
    # part.
96
    if not token:
×
97
        return None  # An empty string was passed in
×
98
    if token[0] == '[':
×
99
        return None  # An Etag was passed in
×
100
    if token[0] == '<':
×
101
        token = token[1:-1]
×
102
    return token[token.find(':') + 1:]
×
103

104

105
# If: header handling support.  IfParser returns a sequence of
106
# TagList objects in the order they were parsed which can then
107
# be used in WebDAV methods to decide whether an operation can
108
# proceed or to raise HTTP Error 412 (Precondition failed)
109
IfHdr = re.compile(
1✔
110
    r"(?P<resource><.+?>)?\s*\((?P<listitem>[^)]+)\)"
111
)
112

113
ListItem = re.compile(
1✔
114
    r"(?P<not>not)?\s*(?P<listitem><[a-zA-Z]+:[^>]*>|\[.*?\])",
115
    re.I)
116

117

118
class TagList:
1✔
119
    def __init__(self):
1✔
120
        self.resource = None
×
121
        self.list = []
×
122
        self.NOTTED = 0
×
123

124

125
def IfParser(hdr):
1✔
126
    out = []
×
127
    i = 0
×
128
    while 1:
×
129
        m = IfHdr.search(hdr[i:])
×
130
        if not m:
×
131
            break
×
132

133
        i = i + m.end()
×
134
        tag = TagList()
×
135
        tag.resource = m.group('resource')
×
136
        if tag.resource:                # We need to delete < >
×
137
            tag.resource = tag.resource[1:-1]
×
138
        listitem = m.group('listitem')
×
139
        tag.NOTTED, tag.list = ListParser(listitem)
×
140
        out.append(tag)
×
141

142
    return out
×
143

144

145
def ListParser(listitem):
1✔
146
    out = []
×
147
    NOTTED = 0
×
148
    i = 0
×
149
    while 1:
×
150
        m = ListItem.search(listitem[i:])
×
151
        if not m:
×
152
            break
×
153

154
        i = i + m.end()
×
155
        out.append(m.group('listitem'))
×
156
        if m.group('not'):
×
157
            NOTTED = 1
×
158

159
    return NOTTED, out
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc