Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
Bart-Steensma committed Apr 16, 2024
2 parents 74ff42f + 2b82acf commit d1404eb
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 57 deletions.
3 changes: 0 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,8 @@ select = [
"W", # pycodestyle warnings
"I", # isort
"N", # PEP8 naming
"D", # pydocstyle
]

ignore = ["D212", "D205", "D210", "D203"]

[tool.autopep8]
max_line_length = 79
aggressive = 3
Expand Down
106 changes: 52 additions & 54 deletions src/khalorg/khal/calendar.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import logging
from os.path import dirname, join
import sys
from datetime import date, datetime
from typing import Callable, Iterable, TypedDict, Union

Expand All @@ -15,6 +13,7 @@
)

from khalorg.khal.helpers import (
find_khal_bin,
is_future,
remove_tzinfo,
subprocess_callback,
Expand All @@ -24,7 +23,7 @@


class CalendarProperties(TypedDict):
""" Properties of a khal Event. """
"""Properties of a khal Event."""

attendees: list[str]
categories: list[str]
Expand Down Expand Up @@ -67,14 +66,17 @@ class Calendar:
new_item: new command (khal new)
"""

MESSAGE_EDIT: str = ('When trying to edit an event, the number of events '
'found in the khal calendar was not 1 but: {}. The '
'command was aborted.')
MESSAGE_EDIT: str = (
"When trying to edit an event, the number of events "
"found in the khal calendar was not 1 but: {}. The "
"command was aborted."
)

MESSAGE_DELETE: str = (
'When trying to delete an event, the number of events'
' found in the khal calendar was not 1 but: {}. The '
'command was aborted.')
"When trying to delete an event, the number of events"
" found in the khal calendar was not 1 but: {}. The "
"command was aborted."
)

def __init__(self, name: str):
"""
Expand All @@ -86,7 +88,7 @@ def __init__(self, name: str):
"""
path_config: Union[str, None] = find_configuration_file()

khal_bin: str = join(dirname(sys.executable), 'khal')
khal_bin: str = find_khal_bin()
new_item_args: list = [khal_bin, "new"]
list_args: list = [khal_bin, "list", "-df", ""]

Expand Down Expand Up @@ -126,7 +128,7 @@ def list_command(self, khal_args: list) -> str:
-------
stdout of the `khal list`
"""
logging.info(f'khalorg list args are: {khal_args}')
logging.info(f"khalorg list args are: {khal_args}")
return self._list_command(khal_args)

@property
Expand All @@ -138,7 +140,7 @@ def date_format(self) -> str:
-------
"""
return self.config['locale']['longdateformat']
return self.config["locale"]["longdateformat"]

@property
def datetime_format(self) -> str:
Expand All @@ -149,7 +151,7 @@ def datetime_format(self) -> str:
-------
"""
return self.config['locale']['longdatetimeformat']
return self.config["locale"]["longdatetimeformat"]

@property
def collection(self) -> CalendarCollection:
Expand All @@ -164,13 +166,14 @@ def collection(self) -> CalendarCollection:
khal calendar collection.
"""
if not hasattr(self, '_collection'):
if not hasattr(self, "_collection"):
self._collection = get_calendar_collection(self.name)

return self._collection

def edit(self, props: CalendarProperties,
edit_dates: bool = False) -> list[Event]:
def edit(
self, props: CalendarProperties, edit_dates: bool = False
) -> list[Event]:
"""
Edit an existing event.
Expand All @@ -197,14 +200,14 @@ def edit(self, props: CalendarProperties,
"""
events: list[Event]

if props['uid']:
events = self.get_events(props['uid'])
if props["uid"]:
events = self.get_events(props["uid"])
else:
events = self.get_events_no_uid(props['summary'],
props['start'],
props['end'])
events = self.get_events_no_uid(
props["summary"], props["start"], props["end"]
)

logging.info(f'number of events is {len(events)}')
logging.info(f"number of events is {len(events)}")
events = [x for x in events if is_future(x.end)]

if len(events) == 0:
Expand All @@ -228,14 +231,13 @@ def now(self) -> datetime:
now in the local timezone
"""
return self.config['locale']['default_timezone'].localize(
datetime.now())
return self.config["locale"]["default_timezone"].localize(
datetime.now()
)

def update_event(
self,
event: Event,
props: CalendarProperties,
edit_dates: bool = False) -> Event:
self, event: Event, props: CalendarProperties, edit_dates: bool = False
) -> Event:
"""
Update an event with `props`.
Expand All @@ -249,16 +251,16 @@ def update_event(
the update version of `event`
"""
event.update_url(props['url'])
event.update_summary(props['summary'])
event.update_location(props['location'])
event.update_attendees(props['attendees'])
event.update_categories(props['categories'])
event.update_description(props['description'])
event.update_url(props["url"])
event.update_summary(props["summary"])
event.update_location(props["location"])
event.update_attendees(props["attendees"])
event.update_categories(props["categories"])
event.update_description(props["description"])

if edit_dates:
event.update_start_end(props['start'], props['end'])
event.update_rrule(props['rrule'])
event.update_start_end(props["start"], props["end"])
event.update_rrule(props["rrule"])

event.increment_sequence()
self.collection.update(event)
Expand All @@ -285,10 +287,8 @@ def get_events(self, uid: str) -> list[Event]:
return [x for x in events if x.uid == uid or not uid]

def get_events_no_uid(
self,
summary_wanted: str,
start_wanted: Time,
end_wanted: Time) -> list[Event]:
self, summary_wanted: str, start_wanted: Time, end_wanted: Time
) -> list[Event]:
"""
Return events that share the same summary, start time and stop time.
Expand All @@ -305,33 +305,31 @@ def get_events_no_uid(
-------
list of events
"""

def exists(summary: str, end: Time) -> bool:
equal_end: bool = remove_tzinfo(end) == remove_tzinfo(end_wanted)
equal_summary: bool = summary == summary_wanted
return equal_end and equal_summary

logging.info(f'Get events on date: {start_wanted}')
logging.info(f"Get events on date: {start_wanted}")
return [
event for event in self.collection.get_events_on(start_wanted)
event
for event in self.collection.get_events_on(start_wanted)
if exists(event.summary, event.end)
]

def update(self, event: Event) -> None:
self.collection.update(event)

def exists(
self,
summary_wanted: str,
start_wanted: Time,
end_wanted: Time) -> bool:
self, summary_wanted: str, start_wanted: Time, end_wanted: Time
) -> bool:
"""
Returns True if an item exists at this specific time, with the same
title.
"""
events: list = self.get_events_no_uid(
summary_wanted,
start_wanted,
end_wanted
summary_wanted, start_wanted, end_wanted
)
return len(events) > 0

Expand All @@ -343,7 +341,7 @@ def delete(self, props: CalendarProperties) -> str:
----
props: dictionary representing the event.
"""
events: list = self.get_events(props['uid'])
events: list = self.get_events(props["uid"])

if len(events) == 0:
logging.error(self.MESSAGE_DELETE.format(len(events)))
Expand All @@ -352,10 +350,10 @@ def delete(self, props: CalendarProperties) -> str:
event = events[0]
assert event.href is not None
try:
self.collection.delete(event.href,
event.etag,
calendar=event.calendar)
self.collection.delete(
event.href, event.etag, calendar=event.calendar
)
except NotFoundError as error:
logging.error(error)
finally:
return ''
return ""
19 changes: 19 additions & 0 deletions src/khalorg/khal/helpers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import logging
import sys
from datetime import date, datetime
from os.path import dirname, exists, join
from subprocess import STDOUT, CalledProcessError, check_output
from typing import Callable

Expand All @@ -8,6 +10,23 @@
Time = date | datetime


def find_khal_bin() -> str:
"""Returns the khal executable.
When pipx is used, the khal executable is located in the same directory
as the python executable. When using a virtual environment, or the
global python installation, the khal executable is located in the PATH.
Returns:
--------
path to the khal executable
"""
bin: str = join(dirname(sys.executable), "khal")
bin = bin if exists(bin) else "khal"
logging.debug("khal executable is: %s", bin)
return bin


def get_khal_format():
"""
Returns the format that is used for the `khal list --format` command
Expand Down

0 comments on commit d1404eb

Please sign in to comment.