diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ef4ae6b --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +__pycache__/* +**/__pycache__/* \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..9b38853 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,7 @@ +{ + "python.testing.pytestArgs": [ + "tests" + ], + "python.testing.unittestEnabled": false, + "python.testing.pytestEnabled": true +} \ No newline at end of file diff --git a/dateHandling.py b/dateHandling.py new file mode 100644 index 0000000..194d307 --- /dev/null +++ b/dateHandling.py @@ -0,0 +1,33 @@ +import datetime + +def isNowInTimePeriod(startTime, endTime, nowTime): + if startTime < endTime: + return nowTime >= startTime and nowTime <= endTime + else: + #Over midnight: + return nowTime >= startTime or nowTime <= endTime + +def dayText(event, today=datetime.datetime.today()): + + weekday = ['Mandag', 'Tirsdag', 'Onsdag', 'Torsdag', 'Fredag','Lørdag','Søndag'] + text = '' + dt = datetime.datetime.fromisoformat(event['start'].get('dateTime', event['start'].get('date'))) + # today = datetime.datetime.today() + tomorrow = today + datetime.timedelta(1) + delta = dt - today + + if dt.date() == today.date() : + text = text + 'I dag: ' + elif dt.date() == tomorrow.date() : + text = text + 'I morgen: ' + elif delta.days > 6: + text = text + weekday[dt.weekday()] + ' ' + dt.strftime('%d/%m') + ': ' + else : + text = text + weekday[dt.weekday()] + ': ' + + text = text + event['summary'] + + if dt.hour > 0: + text = text + dt.strftime(' kl. %H:%M') + + return text diff --git a/middag.py b/middag.py index feb6d04..2608e45 100755 --- a/middag.py +++ b/middag.py @@ -17,39 +17,12 @@ from luma.core.legacy.font import proportional, CP437_FONT, TINY_FONT, SINCLAIR_FONT, LCD_FONT from backports.datetime_fromisoformat import MonkeyPatch from pathlib import Path +from dateHandling import isNowInTimePeriod, dayText MonkeyPatch.patch_fromisoformat() # If modifying these scopes, delete the file token.pickle. SCOPES = ['https://www.googleapis.com/auth/calendar.readonly'] -def isNowInTimePeriod(startTime, endTime, nowTime): - if startTime < endTime: - return nowTime >= startTime and nowTime <= endTime - else: - #Over midnight: - return nowTime >= startTime or nowTime <= endTime - -def dayText(event): - - weekday = ['Mandag', 'Tirsdag', 'Onsdag', 'Torsdag', 'Fredag','Lørdag','Søndag'] - text = '' - dt = datetime.datetime.fromisoformat(event['start'].get('dateTime', event['start'].get('date'))) - today = datetime.datetime.today() - tomorrow = today + datetime.timedelta(1) - - if dt.date() == today.date() : - text = text + 'I dag: ' - elif dt.date() == tomorrow.date() : - text = text + 'I morgen: ' - else : - text = text + weekday[dt.weekday()] + ': ' - - text = text + event['summary'] - - if dt.hour > 0: - text = text + dt.strftime(' kl. %H:%M') - - return text def main(argv): loop = None @@ -64,7 +37,7 @@ def main(argv): sys.exit() elif opt in ("-m", "--mode"): if arg in ('loop'): - loop = True + loop = True print (loop) """Shows basic usage of the Google Calendar API. @@ -138,4 +111,4 @@ def main(argv): time.sleep(2) if __name__ == '__main__': - main(sys.argv[1:]) + main(sys.argv[1:]) diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/dateHandling_test.py b/tests/dateHandling_test.py new file mode 100644 index 0000000..97709ce --- /dev/null +++ b/tests/dateHandling_test.py @@ -0,0 +1,69 @@ +import datetime + +from dateHandling import dayText, isNowInTimePeriod + +def test_isNowInTimePeriod(): + startTime = datetime.datetime.strptime("2023-10-22T10:12", "%Y-%m-%dT%H:%M") + endTime = datetime.datetime.strptime("2023-10-25T10:12", "%Y-%m-%dT%H:%M") + today = datetime.datetime.strptime("2023-10-23T10:12", "%Y-%m-%dT%H:%M") + before = datetime.datetime.strptime("2023-10-22T10:11", "%Y-%m-%dT%H:%M") + after = datetime.datetime.strptime("2023-10-25T10:13", "%Y-%m-%dT%H:%M") + + assert isNowInTimePeriod(startTime, endTime, today) == True + assert isNowInTimePeriod(startTime, endTime, before) == False + assert isNowInTimePeriod(startTime, endTime, after) == False + + +today = datetime.datetime.strptime("2023-10-23T10:12", "%Y-%m-%dT%H:%M") + +def test_dayText_tomorrow(): + event = { + 'start': { + 'date': '2023-10-24' + }, + 'summary': 'Event summary' + } + + assert dayText(event, today) == "I morgen: Event summary" + +def test_dayText_today(): + event = { + 'start': { + 'date': '2023-10-23' + }, + 'summary': 'Event summary' + } + + assert dayText(event, today) == "I dag: Event summary" + +def test_dayText_today_with_time(): + event = { + 'start': { + 'date': '2023-10-23', + 'dateTime': '2023-10-23T10:22:18' + }, + 'summary': 'Event summary' + } + + assert dayText(event, today) == "I dag: Event summary kl. 10:22" + +def test_dayText_friday(): + event = { + 'start': { + 'date': '2023-10-27' + }, + 'summary': 'Event summary' + } + + assert dayText(event, today) == "Fredag: Event summary" + +def test_dayText_friday_plus_one_week(): + event = { + 'start': { + 'date': '2023-11-03' + }, + 'summary': 'Event summary' + } + + assert dayText(event, today) == "Fredag 03/11: Event summary" +