Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def apply_filter(el, resource):
"""Compile a filter element into a Python function.
"""
if el is None or not list(el):
# Empty filter, let's not bother parsing
return lambda x: True
ab = addressbook_from_resource(resource)
if ab is None:
return False
test_name = el.get('test', 'anyof')
test = {'allof': all, 'anyof': any}[test_name]
return test(apply_prop_filter(subel, ab) for subel in el)
class AddressbookQueryReporter(webdav.Reporter):
name = '{%s}addressbook-query' % NAMESPACE
resource_type = ADDRESSBOOK_RESOURCE_TYPE
data_property = AddressDataProperty()
@webdav.multistatus
async def report(self, environ, body, resources_by_hrefs, properties,
base_href, base_resource, depth):
requested = None
filter_el = None
limit = None
for el in body:
if el.tag in ('{DAV:}prop', '{DAV:}allprop', '{DAV:}propname'):
requested = el
elif el.tag == ('{%s}filter' % NAMESPACE):
filter_el = el
if base_href is not None:
href = urllib.parse.urljoin(ensure_trailing_slash(base_href), href)
et.text = urllib.parse.quote(href)
return et
def read_href_element(et: ET.Element) -> Optional[str]:
if et.text is None:
return None
el = urllib.parse.unquote(et.text)
parsed_url = urllib.parse.urlsplit(el)
# TODO(jelmer): Check that the hostname matches the local hostname?
return parsed_url.path
class ExpandPropertyReporter(Reporter):
"""A expand-property reporter.
See https://tools.ietf.org/html/rfc3253, section 3.8
"""
name = '{DAV:}expand-property'
async def _populate(
self, prop_list: ET.Element,
resources_by_hrefs: Callable[
[Iterable[str]], List[Tuple[str, Resource]]],
properties: Dict[str, Property], href: str,
resource: Resource, environ) -> AsyncIterable[Status]:
"""Expand properties for a resource.
:param prop_list: DAV:property elements to retrieve and expand
def get_pytz_from_text(tztext):
tzid = extract_tzid(ICalendar.from_ical(tztext))
return pytz.timezone(tzid)
def get_calendar_timezone(resource):
try:
tztext = resource.get_calendar_timezone()
except KeyError:
return LocalTimezone()
else:
return get_pytz_from_text(tztext)
class CalendarQueryReporter(webdav.Reporter):
name = '{%s}calendar-query' % NAMESPACE
resource_type = (CALENDAR_RESOURCE_TYPE, SCHEDULE_INBOX_RESOURCE_TYPE)
data_property = CalendarDataProperty()
@webdav.multistatus
async def report(
self, environ, body, resources_by_hrefs, properties, base_href,
base_resource, depth):
# TODO(jelmer): Verify that resource is a calendar
requested = None
filter_el = None
tztext = None
for el in body:
if el.tag in ('{DAV:}prop', '{DAV:}propname', '{DAV:}allprop'):
requested = el
FEATURE = 'sync-collection'
class SyncToken(object):
"""A sync token wrapper."""
def __init__(self, token):
self.token = token
def aselement(self):
ret = ET.Element('{DAV:}sync-token')
ret.text = self.token
return ret
class SyncCollectionReporter(webdav.Reporter):
"""sync-collection reporter implementation.
See https://tools.ietf.org/html/rfc6578, section 3.2.
"""
name = '{DAV:}sync-collection'
@webdav.multistatus
async def report(self, environ, request_body, resources_by_hrefs,
properties, href, resource, depth):
old_token = None
sync_level = None
limit = None
requested = None
for el in request_body:
if el.tag == '{DAV:}sync-token':
async def iter_freebusy(resources, start, end, tzify):
async for (href, resource) in resources:
c = calendar_from_resource(resource)
if c is None:
continue
if c.name != 'VCALENDAR':
continue
for comp in c.subcomponents:
if comp.name == 'VEVENT':
if apply_time_range_vevent(start, end, comp, tzify):
vp = extract_freebusy(comp, tzify)
if vp is not None:
yield vp
class FreeBusyQueryReporter(webdav.Reporter):
"""free-busy-query reporter.
See https://tools.ietf.org/html/rfc4791, section 7.10
"""
name = '{urn:ietf:params:xml:ns:caldav}free-busy-query'
resource_type = CALENDAR_RESOURCE_TYPE
async def report(self, environ, body, resources_by_hrefs,
properties, base_href, base_resource, depth):
requested = None
for el in body:
if el.tag == '{urn:ietf:params:xml:ns:caldav}time-range':
requested = el
else:
raise AssertionError("unexpected XML element")
:param environ: WSGI environ dict
:param requested: Requested property (including subelements)
"""
raise NotImplementedError(self.get_value_ext)
async def get_properties_with_data(data_property, href, resource, properties,
environ, requested):
properties = dict(properties)
properties[data_property.name] = data_property
async for ps in webdav.get_properties(
href, resource, properties, environ, requested):
yield ps
class MultiGetReporter(webdav.Reporter):
"""Abstract base class for multi-get reporters."""
name = None
# A SubbedProperty subclass
data_property = None
@webdav.multistatus
async def report(self, environ, body, resources_by_hrefs, properties,
base_href, resource, depth):
# TODO(jelmer): Verify that depth == "0"
# TODO(jelmer): Verify that resource is an the right resource type
requested = None
hrefs = []
for el in body:
if el.tag in ('{DAV:}prop', '{DAV:}allprop', '{DAV:}propname'):