Skip to content

Commit

Permalink
Initialize default http client for downloader in constructor
Browse files Browse the repository at this point in the history
Allowing users to pass a manually created `Http` object to the download
class increases flexibility for various circumstances.

The current implementation does not work when used inside an AWS Lambda
as the filesystem is read-only and raises an unhandled IOError.

The changes should be fully backwards compatible.
  • Loading branch information
pall-valmundsson committed Sep 23, 2019
1 parent a9d614f commit 85d8c46
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
18 changes: 9 additions & 9 deletions icalevents/icaldownload.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,6 @@
from httplib2 import Http


# default http connection to use
try:
default_http = Http('.cache')
except PermissionError:
# Cache disabled if no write permission in working directory
default_http = Http()


def apple_data_fix(content):
"""
Fix Apple tzdata bug.
Expand All @@ -38,7 +30,15 @@ class ICalDownload:
"""
Downloads or reads and decodes iCal sources.
"""
def __init__(self, http=default_http, encoding='utf-8'):
def __init__(self, http=None, encoding='utf-8'):
# default http connection to use
if http is None:
try:
http = Http('.cache')
except PermissionError:
# Cache disabled if no write permission in working directory
http = Http()

self.http = http
self.encoding = encoding

Expand Down
19 changes: 14 additions & 5 deletions icalevents/icalevents.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,15 @@
threads = {}


def events(url=None, file=None, string_content=None, start=None, end=None, fix_apple=False):
def events(
url=None,
file=None,
string_content=None,
start=None,
end=None,
fix_apple=False,
http=None,
):
"""
Get all events form the given iCal URL occurring in the given time range.
Expand All @@ -27,16 +35,17 @@ def events(url=None, file=None, string_content=None, start=None, end=None, fix_a
found_events = []

content = None
ical_download = ICalDownload(http=http)

if url:
content = ICalDownload().data_from_url(url, apple_fix=fix_apple)
content = ical_download.data_from_url(url, apple_fix=fix_apple)

if not content and file:
content = ICalDownload().data_from_file(file, apple_fix=fix_apple)
content = ical_download.data_from_file(file, apple_fix=fix_apple)

if not content and string_content:
content = ICalDownload().data_from_string(string_content,
apple_fix=fix_apple)
content = ical_download.data_from_string(
string_content, apple_fix=fix_apple)

found_events += parse_events(content, start=start, end=end)

Expand Down

0 comments on commit 85d8c46

Please sign in to comment.