ya3

ya3 -- yet another appointment application
Log | Files | Refs

commit c81b7200b03f9eff139ba2711ff75830aeac7aae
parent dc8c83534b91af7a6880a201ed35fc257468ef4f
Author: Matthias Balk <mbalk@mbalk.de>
Date:   Wed,  3 Feb 2021 17:17:09 +0100

refactoring: moved some functions to `utils.py`

Diffstat:
MREADME.rst | 2+-
Msrc/commands.py | 47++++-------------------------------------------
Asrc/utils.py | 71+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 76 insertions(+), 44 deletions(-)

diff --git a/README.rst b/README.rst @@ -10,7 +10,7 @@ Features ~~~~~~~~ - Reading all vCal files from a directory and spit out appointments of the next few days on stdout. -- Creation of new appointments +- Creation of new appointments. Features to be implemented ~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/src/commands.py b/src/commands.py @@ -1,4 +1,4 @@ -# Copyright 2018, 2019, 2020 Matthias Balk +# Copyright 2018, 2019, 2020, 2021 Matthias Balk # # This file is part of ya3 (yet another appointment application). # @@ -22,11 +22,10 @@ import uuid from collections import OrderedDict from datetime import datetime -import vobject from dateutil.parser import parse -from dateutil.tz import gettz import config +from utils import event2string, get_events def _localtime2utc(datetimestr): @@ -80,44 +79,6 @@ def create_event(): _event2file(event) -def _conv2local(dt): - if not isinstance(dt, datetime): - return datetime(dt.year, dt.month, dt.day, tzinfo=gettz()) - if dt.tzinfo is None: - return datetime(dt.year, dt.month, dt.day, - dt.hour, dt.minute, dt.second, - tzinfo=gettz()) - return dt.astimezone(gettz()) - - def print_events(icsfilenames, ignore_configured_range=False): - - def _is_in_range(event): - if ignore_configured_range: - return True - start = _conv2local(event.dtstart.value) - end = _conv2local(event.dtend.value) - return (end >= datetime.now(gettz()) - config.MAX_AGE and - start < datetime.now(gettz()) + config.MAX_AHEAD) - - def _print(event): - start = _conv2local(event.dtstart.value) - end = _conv2local(event.dtend.value) - - try: - location = " (" + event.location.value + ")" - except AttributeError: - location = "" - - print('%s - %s %s%s' % (start.strftime('%Y-%m-%d %H:%M'), - end.strftime('%H:%M'), - event.summary.value, - location)) - - events = [] - for filename in icsfilenames: - with open(filename) as f: - for cal in vobject.readComponents(f): - events.extend(filter(_is_in_range, cal.vevent_list)) - for event in sorted(events, key=lambda ev: _conv2local(ev.dtstart.value)): - _print(event) + for event in get_events(icsfilenames, ignore_configured_range): + print(event2string(event)) diff --git a/src/utils.py b/src/utils.py @@ -0,0 +1,71 @@ +# Copyright 2021 Matthias Balk +# +# This file is part of ya3 (yet another appointment application). +# +# ya3 is free software: you can redistribute it and/or modify it under the +# terms of the GNU Affero General Public License as published by the Free +# Software Foundation, either version 3 of the License, or (at your option) any +# later version. +# +# ya3 is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR +# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more +# details. +# +# You should have received a copy of the GNU Affero General Public License +# along with ya3. If not, see <http://www.gnu.org/licenses/>. + +from datetime import datetime + +import vobject +from dateutil.tz import gettz + +import config + + +def get_ya3_version(): + return '0.2-dev' + + +def _conv2local(dt): + if not isinstance(dt, datetime): + return datetime(dt.year, dt.month, dt.day, tzinfo=gettz()) + if dt.tzinfo is None: + return datetime(dt.year, dt.month, dt.day, + dt.hour, dt.minute, dt.second, + tzinfo=gettz()) + return dt.astimezone(gettz()) + + +def get_events(icsfilenames, ignore_configured_range=False): + + def _is_in_range(event): + if ignore_configured_range: + return True + start = _conv2local(event.dtstart.value) + end = _conv2local(event.dtend.value) + return (end >= datetime.now(gettz()) - config.MAX_AGE and + start < datetime.now(gettz()) + config.MAX_AHEAD) + + events = [] + for filename in icsfilenames: + with open(filename) as f: + for cal in vobject.readComponents(f): + events.extend(filter(_is_in_range, cal.vevent_list)) + + return sorted(events, key=lambda ev: _conv2local(ev.dtstart.value)) + + +def event2string(event): + start = _conv2local(event.dtstart.value) + end = _conv2local(event.dtend.value) + + try: + location = " (" + event.location.value + ")" + except AttributeError: + location = "" + + return ('%s - %s %s%s' % (start.strftime('%Y-%m-%d %H:%M'), + end.strftime('%H:%M'), + event.summary.value, + location))