Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

storage/http: add support for filter_hook #1136

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Version 0.19.3
- Add an option to request vCard v4.0. :gh:`1066`
- Require matching ``BEGIN`` and ``END`` lines in vobjects. :gh:`1103`
- A Docker environment for Vdirsyncer has been added `Vdirsyncer DOCKERIZED <https://github.com/Bleala/Vdirsyncer-DOCKERIZED>`_.
- Add ``filter_hook`` parameter to :storage:`http`. :gh:`1136`

Version 0.19.2
==============
Expand Down
6 changes: 6 additions & 0 deletions docs/config.rst
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,7 @@ leads to an error.
[storage holidays_remote]
type = "http"
url = https://example.com/holidays_from_hicksville.ics
#filter_hook = null

Too many WebCAL providers generate UIDs of all ``VEVENT``-components
on-the-fly, i.e. all UIDs change every time the calendar is downloaded.
Expand All @@ -508,3 +509,8 @@ leads to an error.
:param auth_cert: Optional. Either a path to a certificate with a client
certificate and the key or a list of paths to the files with them.
:param useragent: Default ``vdirsyncer``.
:param filter_hook: Optional. A filter command to call for each fetched
item, passed in raw form to stdin and returned via stdout.
If nothing is returned by the filter command, the item is skipped.
This can be used to alter fields as needed when dealing with providers
generating malformed events.
28 changes: 26 additions & 2 deletions vdirsyncer/storage/http.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from __future__ import annotations

import logging
import subprocess
import urllib.parse as urlparse

import aiohttp
Expand All @@ -14,6 +16,8 @@
from ..vobject import split_collection
from .base import Storage

logger = logging.getLogger(__name__)


class HttpStorage(Storage):
storage_name = "http"
Expand All @@ -34,6 +38,7 @@ def __init__(
useragent=USERAGENT,
verify_fingerprint=None,
auth_cert=None,
filter_hook=None,
*,
connector,
**kwargs,
Expand All @@ -56,6 +61,7 @@ def __init__(
self.useragent = useragent
assert connector is not None
self.connector = connector
self._filter_hook = filter_hook

collection = kwargs.get("collection")
if collection is not None:
Expand All @@ -66,6 +72,19 @@ def __init__(
def _default_headers(self):
return {"User-Agent": self.useragent}

def _run_filter_hook(self, raw_item):
try:
result = subprocess.run(
[self._filter_hook],
input=raw_item,
capture_output=True,
encoding="utf-8",
)
return result.stdout
except OSError as e:
logger.warning(f"Error executing external command: {str(e)}")
return raw_item

async def list(self):
async with aiohttp.ClientSession(
connector=self.connector,
Expand All @@ -82,8 +101,13 @@ async def list(self):
)
self._items = {}

for item in split_collection((await r.read()).decode("utf-8")):
item = Item(item)
for raw_item in split_collection((await r.read()).decode("utf-8")):
if self._filter_hook:
raw_item = self._run_filter_hook(raw_item)
if not raw_item:
continue

item = Item(raw_item)
if self._ignore_uids:
item = item.with_uid(item.hash)

Expand Down