Skip to content

Commit

Permalink
Merge pull request #15 from cun3yt/master
Browse files Browse the repository at this point in the history
Reading Ability of Events from String Content
  • Loading branch information
irgangla authored Apr 10, 2018
2 parents 788365c + 5831b1e commit a9b4792
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 6 deletions.
6 changes: 6 additions & 0 deletions icalevents/icaldownload.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
16 changes: 11 additions & 5 deletions icalevents/icalevents.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -33,43 +34,48 @@ 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
"""
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:
Expand Down
18 changes: 17 additions & 1 deletion test/test_icalevents.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

0 comments on commit a9b4792

Please sign in to comment.