Merge pull request #61 from gangverk/default_http_client_fix

Initialize default http client for downloader in constructor
pull/63/head
Thomas Irgang 2019-09-25 15:57:23 +02:00 zatwierdzone przez GitHub
commit ba905c3085
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 4AEE18F83AFDEB23
2 zmienionych plików z 23 dodań i 14 usunięć

Wyświetl plik

@ -4,14 +4,6 @@ Downloads an iCal url or reads an iCal file.
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.
@ -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

Wyświetl plik

@ -12,7 +12,15 @@ event_store = {}
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.
@ -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)