-
Notifications
You must be signed in to change notification settings - Fork 24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix update_events
for nonexistent IDs and support creation of new events
#134
Draft
dhomeier
wants to merge
3
commits into
Olen:main
Choose a base branch
from
dhomeier:update_and_create_events
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
#!/usr/bin/env python | ||
|
||
import argparse | ||
import asyncio | ||
|
||
import ics | ||
|
||
from config import password, username | ||
from spond import spond | ||
|
||
DESCRIPTION = """ | ||
Read in iCal events from .ics file[s] and post them to Spond. | ||
""".strip() | ||
|
||
|
||
def ics2spond(event): | ||
"""Create Spond event dictionary from ics.Event""" | ||
|
||
return { | ||
"heading": event.name, | ||
"description": event.description, | ||
"startTimestamp": event.begin.isoformat(), | ||
"endTimestamp": event.end.isoformat(), | ||
"location": {"feature": event.location}, | ||
} | ||
|
||
|
||
async def post_events(args, gid=None, owners=[]): | ||
""" | ||
Read Calendar from .ics file[s] and post all events to Spond. | ||
|
||
Parameters | ||
---------- | ||
args : argparse.Namespace | ||
Command line arguments and options returned by ArgumentParser.parse_args(), | ||
containing options and file name[s] (wildcards supported). | ||
gid : str | ||
'id' of Spond group to post to (default: first group from `get_groups()` for user). | ||
owners : list | ||
list of user's {'id': uid} (default: [user] from `config.username`). | ||
""" | ||
|
||
s = spond.Spond(username=username, password=password) | ||
|
||
if len(owners) == 0: | ||
user = await s.get_person(username) | ||
owners = [user["profile"]] | ||
|
||
if gid is None: | ||
groups = await s.get_groups() | ||
for mygroup in groups: | ||
if mygroup["contactPerson"]["id"] == owners[0]["id"]: | ||
break | ||
else: | ||
raise ValueError(f"No group with contact person {owners[0]['id']} found") | ||
recipients = {"group": mygroup} | ||
else: | ||
recipients = {"group": {"id": gid}} | ||
|
||
if not args.quiet: | ||
print(f"Posting as {username} ({owners[0]['id']}): {recipients['group']['id']}") | ||
|
||
for filename in args.filename: # Support wildcards | ||
if not args.quiet: | ||
print(f"Reading {filename}:") | ||
calendar = ics.Calendar(open(filename).read()) | ||
for event in calendar.events: | ||
updates = {"owners": owners, "recipients": recipients} | ||
updates.update(ics2spond(event)) | ||
uid = getattr(event, "uid", "") | ||
if args.verbose: | ||
print(event.serialize()) | ||
elif not args.quiet: | ||
print(event.name) | ||
events = await s.update_event(uid, updates) | ||
await s.clientsession.close() | ||
|
||
|
||
def main(args=None): | ||
"""The main function called by the `postics` script.""" | ||
parser = argparse.ArgumentParser( | ||
description=DESCRIPTION, formatter_class=argparse.RawDescriptionHelpFormatter | ||
) | ||
# parser.add_argument("--version", action="version", version=f"%(prog)s {__version__}") | ||
parser.add_argument( | ||
"-v", | ||
"--verbose", | ||
default=False, | ||
action="store_true", | ||
help="verbose mode; echo full iCal events parsed", | ||
) | ||
parser.add_argument( | ||
"-q", | ||
"--quiet", | ||
default=False, | ||
action="store_true", | ||
help="quiet mode; do not echo iCal event names", | ||
) | ||
parser.add_argument( | ||
"-g", "--gid", default=None, help="specify Spond group ID of recipients group" | ||
) | ||
parser.add_argument( | ||
"filename", | ||
nargs="+", | ||
help="Path to one or more ics files; wildcards are supported", | ||
) | ||
|
||
args = parser.parse_args(args) | ||
|
||
loop = asyncio.new_event_loop() | ||
asyncio.set_event_loop(loop) | ||
asyncio.run(post_events(args, gid=args.gid)) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suppose the second part should become a
LookupError
in the spirit of #135; for the first part, which covers an empty or altogether missing list of owners, it is not as clear – could of course handle both exceptions separately.More or less the same for
recipients
below.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually my error has confused things - as per discussion at #113, PR #135 was to change failed lookups to return
KeyError
, and I've now amended so it does.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, I forgot I already argued for that myself! ;-)