From 5831b1ee1f969bf7c74a1c8a5b287ecf9b7f49ce Mon Sep 17 00:00:00 2001 From: Cuneyt Mertayak Date: Tue, 3 Apr 2018 22:32:25 -0700 Subject: [PATCH] Reading Ability of Events from String Content Reading events from pure string content is added. It becomes a need when the calendar content is not served from a URL or a file. --- icalevents/icaldownload.py | 6 ++++++ icalevents/icalevents.py | 16 +++++++++++----- test/test_icalevents.py | 18 +++++++++++++++++- 3 files changed, 34 insertions(+), 6 deletions(-) diff --git a/icalevents/icaldownload.py b/icalevents/icaldownload.py index 8931119..d7435cf 100644 --- a/icalevents/icaldownload.py +++ b/icalevents/icaldownload.py @@ -73,6 +73,12 @@ def data_from_file(self, file, apple_fix=False): return self.decode(content, apple_fix=apple_fix) + def data_from_string(self, string_content, apple_fix=False): + if not string_content or len(string_content) == 0: + raise IOError("String content is not readable or is empty!") + + return self.decode(string_content, apple_fix=apple_fix) + def decode(self, content, apple_fix=False): """ Decode content using the set charset. diff --git a/icalevents/icalevents.py b/icalevents/icalevents.py index 8740aba..83ae159 100644 --- a/icalevents/icalevents.py +++ b/icalevents/icalevents.py @@ -12,12 +12,13 @@ threads = {} -def events(url=None, file=None, start=None, end=None, fix_apple=False): +def events(url=None, file=None, string_content=None, start=None, end=None, fix_apple=False): """ Get all events form the given iCal URL occurring in the given time range. :param url: iCal URL :param file: iCal file path + :param string_content: iCal content as string :param start: start date (see dateutils.date) :param end: end date (see dateutils.date) :param fix_apple: fix known Apple iCal issues @@ -33,18 +34,22 @@ def events(url=None, file=None, start=None, end=None, fix_apple=False): if not content and file: content = ICalDownload().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) + found_events += parse_events(content, start=start, end=end) return found_events -def request_data(key, url, file, start, end, fix_apple): +def request_data(key, url, file, string_content, start, end, fix_apple): """ Request data, update local data cache and remove this Thread form queue. :param key: key for data source to get result later :param url: iCal URL :param file: iCal file path + :param string_content: iCal content as string :param start: start date :param end: end date :param fix_apple: fix known Apple iCal issues @@ -52,24 +57,25 @@ def request_data(key, url, file, start, end, fix_apple): data = [] try: - data += events(url=url, file=file, start=start, end=end, fix_apple=fix_apple) + data += events(url=url, file=file, string_content=string_content, start=start, end=end, fix_apple=fix_apple) finally: update_events(key, data) request_finished(key) -def events_async(key, url=None, file=None, start=None, end=None, fix_apple=False): +def events_async(key, url=None, file=None, start=None, string_content=None, end=None, fix_apple=False): """ Trigger an asynchronous data request. :param key: key for data source to get result later :param url: iCal URL :param file: iCal file path + :param string_content: iCal content as string :param start: start date :param end: end date :param fix_apple: fix known Apple iCal issues """ - t = Thread(target=request_data, args=(key, url, file, start, end, fix_apple)) + t = Thread(target=request_data, args=(key, url, file, string_content, start, end, fix_apple)) with event_lock: if key not in threads: diff --git a/test/test_icalevents.py b/test/test_icalevents.py index 1cd7a96..7aae018 100644 --- a/test/test_icalevents.py +++ b/test/test_icalevents.py @@ -74,7 +74,23 @@ def test_request_data(self): end = date(2017, 5, 19) key = "basic" - icalevents.request_data(key, url=None, file=ical, start=start, end=end, fix_apple=False) + icalevents.request_data(key, url=None, file=ical, string_content=None, start=start, end=end, fix_apple=False) + + self.assertTrue(icalevents.all_done(key), "request is finished") + self.assertEqual(len(icalevents.latest_events(key)), 2, "two events are found") + + def test_string_data(self): + ical = "test/test_data/basic.ics" + + with open(ical, mode='rb') as f: + string_content = f.read() + + start = date(2017, 5, 18) + end = date(2017, 5, 19) + key = "basic" + + icalevents.request_data(key, url=None, file=None, string_content=string_content, start=start, end=end, + fix_apple=False) self.assertTrue(icalevents.all_done(key), "request is finished") self.assertEqual(len(icalevents.latest_events(key)), 2, "two events are found")