From 8fcc6a279c4776f6b9c955596cca8195de1c0917 Mon Sep 17 00:00:00 2001 From: Raef Coles Date: Tue, 18 Sep 2018 17:03:06 +0100 Subject: [PATCH 01/13] Add priority system for coloring days Adds a priority config option to each calendar, with default 10. Uses this value to determine which color to use of each day. --- khal/calendar_display.py | 22 +++++++++++++++++++--- khal/cli.py | 1 + khal/khalendar/khalendar.py | 2 ++ khal/settings/khal.spec | 6 ++++++ 4 files changed, 28 insertions(+), 3 deletions(-) diff --git a/khal/calendar_display.py b/khal/calendar_display.py index 9aa0a4884..e6763d1eb 100644 --- a/khal/calendar_display.py +++ b/khal/calendar_display.py @@ -58,6 +58,24 @@ def get_calendar_color(calendar, default_color, collection): return default_color return collection._calendars[calendar]['color'] +def get_color_list(calendars, default_color, collection): + dcolors = list( + map(lambda x: (get_calendar_color(x, default_color, collection), + collection._calendars[x]['priority']), calendars) + ) + + dcolors.sort(key=lambda x:x[1], reverse=True) + + maxPriority = dcolors[0][1] + dcolors = list( + filter(lambda x:x[1] == maxPriority, dcolors) + ) + + dcolors = list( + map(lambda x: x[0], dcolors) + ) + + return dcolors def str_highlight_day( day, calendars, hmethod, default_color, multiple, color, bold_for_light_color, collection): @@ -65,9 +83,7 @@ def str_highlight_day( """ dstr = str(day.day).rjust(2) if color == '': - dcolors = list(set( - map(lambda x: get_calendar_color(x, default_color, collection), calendars) - )) + dcolors = get_color_list(calendars, default_color, collection) if len(dcolors) > 1: if multiple == '': if hmethod == "foreground" or hmethod == "fg": diff --git a/khal/cli.py b/khal/cli.py index faf1e3cc6..81894584b 100644 --- a/khal/cli.py +++ b/khal/cli.py @@ -162,6 +162,7 @@ def build_collection(conf, selection): 'path': cal['path'], 'readonly': cal['readonly'], 'color': cal['color'], + 'priority' : cal['priority'], 'ctype': cal['type'], } collection = khalendar.CalendarCollection( diff --git a/khal/khalendar/khalendar.py b/khal/khalendar/khalendar.py index 5cfdd6e79..dfb2d1507 100644 --- a/khal/khalendar/khalendar.py +++ b/khal/khalendar/khalendar.py @@ -65,6 +65,7 @@ def __init__(self, default_color: str='', multiple: str='', color: str='', + priority: int=10, highlight_event_days: bool=False, locale: Dict[str, Any]=dict(), dbpath: Optional[str]=None, @@ -93,6 +94,7 @@ def __init__(self, self.default_color = default_color self.multiple = multiple self.color = color + self.priority = priority self.highlight_event_days = highlight_event_days self._locale = locale self._backend = backend.SQLiteDb(self.names, dbpath, self._locale) diff --git a/khal/settings/khal.spec b/khal/settings/khal.spec index 9b39e80c9..056fba7f2 100644 --- a/khal/settings/khal.spec +++ b/khal/settings/khal.spec @@ -38,6 +38,12 @@ path = expand_path(default=None) color = color(default='auto') +# When coloring days, the color will be determined based on the calendar with +# the highest priority. If the priorities are equal, then the "multiple" color +# will be used. + +priority = integer(default=10) + # setting this to *True*, will keep khal from making any changes to this # calendar readonly = boolean(default=False) From 8a6c0b10f914c100433449eaf39ba7265cfe11f1 Mon Sep 17 00:00:00 2001 From: Raef Coles Date: Tue, 18 Sep 2018 17:12:23 +0100 Subject: [PATCH 02/13] Add priority to settings_test No additional testing added, just prevents previous tests failing --- tests/settings_test.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/settings_test.py b/tests/settings_test.py index abffdbbbf..fd9ddf02e 100644 --- a/tests/settings_test.py +++ b/tests/settings_test.py @@ -26,9 +26,9 @@ def test_simple_config(self): comp_config = { 'calendars': { 'home': {'path': os.path.expanduser('~/.calendars/home/'), - 'readonly': False, 'color': None, 'type': 'calendar'}, + 'readonly': False, 'color': None, 'priority': 10, 'type': 'calendar'}, 'work': {'path': os.path.expanduser('~/.calendars/work/'), - 'readonly': False, 'color': None, 'type': 'calendar'}, + 'readonly': False, 'color': None, 'priority': 10, 'type': 'calendar'}, }, 'sqlite': {'path': os.path.expanduser('~/.local/share/khal/khal.db')}, 'locale': LOCALE_BERLIN, @@ -60,10 +60,10 @@ def test_small(self): comp_config = { 'calendars': { 'home': {'path': os.path.expanduser('~/.calendars/home/'), - 'color': 'dark green', 'readonly': False, + 'color': 'dark green', 'readonly': False, 'priority': 10, 'type': 'calendar'}, 'work': {'path': os.path.expanduser('~/.calendars/work/'), - 'readonly': True, 'color': None, + 'readonly': True, 'color': None, 'priority': 10, 'type': 'calendar'}}, 'sqlite': {'path': os.path.expanduser('~/.local/share/khal/khal.db')}, 'locale': { From 1234a731ed9469a1ef197006b3f0b10854cefea2 Mon Sep 17 00:00:00 2001 From: Raef Coles Date: Tue, 18 Sep 2018 17:21:33 +0100 Subject: [PATCH 03/13] Add test for priority config loading --- tests/configs/small.conf | 1 + tests/settings_test.py | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/configs/small.conf b/tests/configs/small.conf index e9c59a191..9981df2fe 100644 --- a/tests/configs/small.conf +++ b/tests/configs/small.conf @@ -3,6 +3,7 @@ [[home]] path = ~/.calendars/home/ color = dark green + priority = 20 [[work]] path = ~/.calendars/work/ diff --git a/tests/settings_test.py b/tests/settings_test.py index fd9ddf02e..037abac8d 100644 --- a/tests/settings_test.py +++ b/tests/settings_test.py @@ -60,7 +60,7 @@ def test_small(self): comp_config = { 'calendars': { 'home': {'path': os.path.expanduser('~/.calendars/home/'), - 'color': 'dark green', 'readonly': False, 'priority': 10, + 'color': 'dark green', 'readonly': False, 'priority': 20, 'type': 'calendar'}, 'work': {'path': os.path.expanduser('~/.calendars/work/'), 'readonly': True, 'color': None, 'priority': 10, From c9bf7c167a6effe3c9aaa780da1d7f71a302a545 Mon Sep 17 00:00:00 2001 From: Raef Coles Date: Tue, 18 Sep 2018 17:47:30 +0100 Subject: [PATCH 04/13] Update AUTHORS and CHANGELOG as per instructions --- AUTHORS.txt | 1 + CHANGELOG.rst | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/AUTHORS.txt b/AUTHORS.txt index 04c93b866..42d95d3b5 100644 --- a/AUTHORS.txt +++ b/AUTHORS.txt @@ -34,3 +34,4 @@ August Lindberg Thomas Kluyver - thomas [at] kluyver [dot] me [dot] uk Tobias Brummer - hallo [at] t0bybr.de - https://t0bybr.de Amanda Hickman - amanda [at] velociraptor [dot] info +Raef Coles - raefcoles [at] gmail [dot] com diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 4f2f3b866..0f0d3b73b 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -52,6 +52,11 @@ not released yet * NEW python 3.7 is now officially supported. +* NEW configuration option [[per_calendar]]priority = int (default 10). If + multiple calendars events are on the same day, the day will be colored with + the color of the calendar with highest priority. If multiple calendars have + the same highest priority, it falls back to the previous system. + 0.9.8 ===== released 2017-10-05 From 9cec2332135ee56fca30a14e4ca5553aef973d3e Mon Sep 17 00:00:00 2001 From: Raef Coles Date: Tue, 18 Sep 2018 18:39:08 +0100 Subject: [PATCH 05/13] Comment get_color_list --- khal/calendar_display.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/khal/calendar_display.py b/khal/calendar_display.py index e6763d1eb..f29be35ea 100644 --- a/khal/calendar_display.py +++ b/khal/calendar_display.py @@ -59,6 +59,8 @@ def get_calendar_color(calendar, default_color, collection): return collection._calendars[calendar]['color'] def get_color_list(calendars, default_color, collection): + """Get the list of possible colors for the day, taking into account priority + """ dcolors = list( map(lambda x: (get_calendar_color(x, default_color, collection), collection._calendars[x]['priority']), calendars) From 6c596b30ba46b9218766b8b1803e434bbe5862f0 Mon Sep 17 00:00:00 2001 From: Raef Coles Date: Tue, 18 Sep 2018 18:39:46 +0100 Subject: [PATCH 06/13] Make color list into a set to prevent duplicates This would be a good candidate for a test of the coloring system, as it is an edge case. --- khal/calendar_display.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/khal/calendar_display.py b/khal/calendar_display.py index f29be35ea..fe01f0ef0 100644 --- a/khal/calendar_display.py +++ b/khal/calendar_display.py @@ -77,6 +77,8 @@ def get_color_list(calendars, default_color, collection): map(lambda x: x[0], dcolors) ) + dcolors = set(dcolors) + return dcolors def str_highlight_day( From cc09ce37ac39e29351d8aa6ad54a6be03b8e78c5 Mon Sep 17 00:00:00 2001 From: Raef Coles Date: Tue, 18 Sep 2018 19:30:48 +0100 Subject: [PATCH 07/13] Update code to match style guidelines --- khal/calendar_display.py | 6 ++++-- khal/cli.py | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/khal/calendar_display.py b/khal/calendar_display.py index fe01f0ef0..cbb027c7b 100644 --- a/khal/calendar_display.py +++ b/khal/calendar_display.py @@ -58,6 +58,7 @@ def get_calendar_color(calendar, default_color, collection): return default_color return collection._calendars[calendar]['color'] + def get_color_list(calendars, default_color, collection): """Get the list of possible colors for the day, taking into account priority """ @@ -66,11 +67,11 @@ def get_color_list(calendars, default_color, collection): collection._calendars[x]['priority']), calendars) ) - dcolors.sort(key=lambda x:x[1], reverse=True) + dcolors.sort(key=lambda x: x[1], reverse=True) maxPriority = dcolors[0][1] dcolors = list( - filter(lambda x:x[1] == maxPriority, dcolors) + filter(lambda x: x[1] == maxPriority, dcolors) ) dcolors = list( @@ -81,6 +82,7 @@ def get_color_list(calendars, default_color, collection): return dcolors + def str_highlight_day( day, calendars, hmethod, default_color, multiple, color, bold_for_light_color, collection): """returns a string with day highlighted according to configuration diff --git a/khal/cli.py b/khal/cli.py index 81894584b..f8d3c4a6f 100644 --- a/khal/cli.py +++ b/khal/cli.py @@ -162,7 +162,7 @@ def build_collection(conf, selection): 'path': cal['path'], 'readonly': cal['readonly'], 'color': cal['color'], - 'priority' : cal['priority'], + 'priority': cal['priority'], 'ctype': cal['type'], } collection = khalendar.CalendarCollection( From bd0e9c09c1e1e59906afc8bae1c81a063825589b Mon Sep 17 00:00:00 2001 From: Raef Coles Date: Tue, 2 Oct 2018 12:27:35 +0100 Subject: [PATCH 08/13] Fix issues with color set duplicate elimination --- khal/calendar_display.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/khal/calendar_display.py b/khal/calendar_display.py index cbb027c7b..9c41741f1 100644 --- a/khal/calendar_display.py +++ b/khal/calendar_display.py @@ -78,7 +78,7 @@ def get_color_list(calendars, default_color, collection): map(lambda x: x[0], dcolors) ) - dcolors = set(dcolors) + dcolors = list(set(dcolors)) return dcolors From 58654ecee2c415d87b77263863ef6ee288328f32 Mon Sep 17 00:00:00 2001 From: Raef Coles Date: Tue, 2 Oct 2018 12:39:21 +0100 Subject: [PATCH 09/13] Add test for get_calendar_color --- tests/cal_display_test.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/tests/cal_display_test.py b/tests/cal_display_test.py index 9c76ddb14..499aa382e 100644 --- a/tests/cal_display_test.py +++ b/tests/cal_display_test.py @@ -4,7 +4,9 @@ import unicodedata import pytest -from khal.calendar_display import getweeknumber, str_week, vertical_month +from khal.calendar_display import (getweeknumber, str_week, vertical_month, + get_calendar_color, get_color_list, + str_highlight_day) today = dt.date.today() yesterday = today - dt.timedelta(days=1) @@ -32,6 +34,23 @@ def test_str_week(): assert str_week(week, aday) == ' 6 7 8 9 10 11 12 13 ' assert str_week(week, bday) == ' 6 7 \x1b[7m 8\x1b[0m 9 10 11 12 13 ' +class testCollection(): + def __init__(self): + self._calendars = {} + + def addCalendar(self, name, color, priority): + self._calendars[name] = {'color': color, 'priority': priority} + +exampleCollection = testCollection() +exampleCollection.addCalendar('testCalendar1', 'dark red', 20) +exampleCollection.addCalendar('testCalendar2', 'light green', 10) +exampleCollection.addCalendar('testCalendar3', '', 10) + +def test_get_calendar_color(): + assert get_calendar_color('testCalendar1', 'light blue', exampleCollection) == 'dark red' + assert get_calendar_color('testCalendar2', 'light blue', exampleCollection) == 'light green' + assert get_calendar_color('testCalendar3', 'light blue', exampleCollection) == 'light blue' + example1 = [ '\x1b[1m Mo Tu We Th Fr Sa Su \x1b[0m', From 26b70c92ced2c0ce932dafb232c4b6fc9bcd9feb Mon Sep 17 00:00:00 2001 From: Raef Coles Date: Tue, 2 Oct 2018 12:42:45 +0100 Subject: [PATCH 10/13] Move setup code into test --- tests/cal_display_test.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/tests/cal_display_test.py b/tests/cal_display_test.py index 499aa382e..6477fb3f6 100644 --- a/tests/cal_display_test.py +++ b/tests/cal_display_test.py @@ -41,12 +41,13 @@ def __init__(self): def addCalendar(self, name, color, priority): self._calendars[name] = {'color': color, 'priority': priority} -exampleCollection = testCollection() -exampleCollection.addCalendar('testCalendar1', 'dark red', 20) -exampleCollection.addCalendar('testCalendar2', 'light green', 10) -exampleCollection.addCalendar('testCalendar3', '', 10) - def test_get_calendar_color(): + + exampleCollection = testCollection() + exampleCollection.addCalendar('testCalendar1', 'dark red', 20) + exampleCollection.addCalendar('testCalendar2', 'light green', 10) + exampleCollection.addCalendar('testCalendar3', '', 10) + assert get_calendar_color('testCalendar1', 'light blue', exampleCollection) == 'dark red' assert get_calendar_color('testCalendar2', 'light blue', exampleCollection) == 'light green' assert get_calendar_color('testCalendar3', 'light blue', exampleCollection) == 'light blue' From ba86071686c484ffc7976fee6b91ab7e98da363f Mon Sep 17 00:00:00 2001 From: Raef Coles Date: Tue, 2 Oct 2018 12:46:23 +0100 Subject: [PATCH 11/13] Comment tests --- tests/cal_display_test.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/cal_display_test.py b/tests/cal_display_test.py index 6477fb3f6..43146f225 100644 --- a/tests/cal_display_test.py +++ b/tests/cal_display_test.py @@ -50,6 +50,8 @@ def test_get_calendar_color(): assert get_calendar_color('testCalendar1', 'light blue', exampleCollection) == 'dark red' assert get_calendar_color('testCalendar2', 'light blue', exampleCollection) == 'light green' + + #test default color assert get_calendar_color('testCalendar3', 'light blue', exampleCollection) == 'light blue' From 76d05b90578d664405d8636d54cf7438728cf7f7 Mon Sep 17 00:00:00 2001 From: Raef Coles Date: Tue, 2 Oct 2018 13:02:19 +0100 Subject: [PATCH 12/13] Add test for get_calendar_color --- tests/cal_display_test.py | 43 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/tests/cal_display_test.py b/tests/cal_display_test.py index 43146f225..7d10e8c0a 100644 --- a/tests/cal_display_test.py +++ b/tests/cal_display_test.py @@ -54,6 +54,49 @@ def test_get_calendar_color(): #test default color assert get_calendar_color('testCalendar3', 'light blue', exampleCollection) == 'light blue' +def test_get_color_list(): + + exampleCalendarList = ['testCalendar1', 'testCalendar2'] + + #test different priorities + exampleCollection1 = testCollection() + exampleCollection1.addCalendar('testCalendar1', 'dark red', 20) + exampleCollection1.addCalendar('testCalendar2', 'light green', 10) + + testList1 = get_color_list(exampleCalendarList, 'light_blue', exampleCollection1) + assert 'dark red' in testList1 + assert len(testList1) == 1 + + + #test same priorities + exampleCollection2 = testCollection() + exampleCollection2.addCalendar('testCalendar1', 'dark red', 20) + exampleCollection2.addCalendar('testCalendar2', 'light green', 20) + + testList2 = get_color_list(exampleCalendarList, 'light_blue', exampleCollection2) + assert 'dark red' in testList2 + assert 'light green' in testList2 + assert len(testList2) == 2 + + + #test duplicated colors + exampleCollection3 = testCollection() + exampleCollection3.addCalendar('testCalendar1', 'dark red', 20) + exampleCollection3.addCalendar('testCalendar2', 'dark red', 20) + + testList3 = get_color_list(exampleCalendarList, 'light_blue', exampleCollection3) + assert len(testList3) == 1 + + + #test indexing operator (required by str_highlight_day()) + exampleCollection4 = testCollection() + exampleCollection4.addCalendar('testCalendar1', 'dark red', 20) + exampleCollection4.addCalendar('testCalendar2', 'dark red', 20) + + testList3 = get_color_list(exampleCalendarList, 'light_blue', exampleCollection4) + assert testList3[0] == 'dark red' + + example1 = [ '\x1b[1m Mo Tu We Th Fr Sa Su \x1b[0m', From 3d75769881b33629191deca52f23fde272300945 Mon Sep 17 00:00:00 2001 From: Raef Coles Date: Tue, 2 Oct 2018 13:11:55 +0100 Subject: [PATCH 13/13] Update code formatting --- tests/cal_display_test.py | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/tests/cal_display_test.py b/tests/cal_display_test.py index 7d10e8c0a..e3b247d91 100644 --- a/tests/cal_display_test.py +++ b/tests/cal_display_test.py @@ -5,8 +5,7 @@ import pytest from khal.calendar_display import (getweeknumber, str_week, vertical_month, - get_calendar_color, get_color_list, - str_highlight_day) + get_calendar_color, get_color_list) today = dt.date.today() yesterday = today - dt.timedelta(days=1) @@ -34,6 +33,7 @@ def test_str_week(): assert str_week(week, aday) == ' 6 7 8 9 10 11 12 13 ' assert str_week(week, bday) == ' 6 7 \x1b[7m 8\x1b[0m 9 10 11 12 13 ' + class testCollection(): def __init__(self): self._calendars = {} @@ -41,6 +41,7 @@ def __init__(self): def addCalendar(self, name, color, priority): self._calendars[name] = {'color': color, 'priority': priority} + def test_get_calendar_color(): exampleCollection = testCollection() @@ -51,14 +52,15 @@ def test_get_calendar_color(): assert get_calendar_color('testCalendar1', 'light blue', exampleCollection) == 'dark red' assert get_calendar_color('testCalendar2', 'light blue', exampleCollection) == 'light green' - #test default color + # test default color assert get_calendar_color('testCalendar3', 'light blue', exampleCollection) == 'light blue' + def test_get_color_list(): exampleCalendarList = ['testCalendar1', 'testCalendar2'] - #test different priorities + # test different priorities exampleCollection1 = testCollection() exampleCollection1.addCalendar('testCalendar1', 'dark red', 20) exampleCollection1.addCalendar('testCalendar2', 'light green', 10) @@ -67,8 +69,7 @@ def test_get_color_list(): assert 'dark red' in testList1 assert len(testList1) == 1 - - #test same priorities + # test same priorities exampleCollection2 = testCollection() exampleCollection2.addCalendar('testCalendar1', 'dark red', 20) exampleCollection2.addCalendar('testCalendar2', 'light green', 20) @@ -78,8 +79,7 @@ def test_get_color_list(): assert 'light green' in testList2 assert len(testList2) == 2 - - #test duplicated colors + # test duplicated colors exampleCollection3 = testCollection() exampleCollection3.addCalendar('testCalendar1', 'dark red', 20) exampleCollection3.addCalendar('testCalendar2', 'dark red', 20) @@ -87,8 +87,7 @@ def test_get_color_list(): testList3 = get_color_list(exampleCalendarList, 'light_blue', exampleCollection3) assert len(testList3) == 1 - - #test indexing operator (required by str_highlight_day()) + # test indexing operator (required by str_highlight_day()) exampleCollection4 = testCollection() exampleCollection4.addCalendar('testCalendar1', 'dark red', 20) exampleCollection4.addCalendar('testCalendar2', 'dark red', 20) @@ -97,7 +96,6 @@ def test_get_color_list(): assert testList3[0] == 'dark red' - example1 = [ '\x1b[1m Mo Tu We Th Fr Sa Su \x1b[0m', '\x1b[1mDec \x1b[0m28 29 30 1 2 3 4 ',