Skip to content

Commit

Permalink
Fix import and agendaupdate to handle encoding errors more gracefully
Browse files Browse the repository at this point in the history
Configures file arguments with errors='replace' so that if they
encounter characters they can't encode/decode they'll just replace them
with a replacement marker such as '?' instead of raising an exception.

Fixes #387 (probably)
  • Loading branch information
dbarnett committed Aug 25, 2024
1 parent 6dbb9ac commit 8f81542
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
4 changes: 3 additions & 1 deletion ChangeLog
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
v4.4.0
* Fix lots of bugs by switching from deprecaetd oauth2client to
* Fix lots of bugs by switching from deprecated oauth2client to
google_auth_oauthlib
* Handle encoding/decoding errors more gracefully by replacing with
placeholder chars instead of blowing up
* Fix `--lineart` option failing with unicode errors
* `quick` command now prompts for which calendar to use when ambiguous
* Fix --nodeclined option failing on events with aliased email
Expand Down
10 changes: 8 additions & 2 deletions gcalcli/argparsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,10 @@ def get_argument_parser():
help='update calendar from agenda TSV file',
description='Update calendar from agenda TSV file.')
agendaupdate.add_argument(
'file', type=argparse.FileType('r'), nargs='?', default=sys.stdin)
'file',
type=argparse.FileType('r', errors='replace'),
nargs='?',
default=sys.stdin)

sub.add_parser(
'updates',
Expand Down Expand Up @@ -395,7 +398,10 @@ def get_argument_parser():
'must be specified. Reads from stdin when no file argument is '
'provided.')
_import.add_argument(
'file', type=argparse.FileType('r'), nargs='?', default=None)
'file',
type=argparse.FileType('r', errors='replace'),
nargs='?',
default=None)
_import.add_argument(
'--verbose', '-v', action='count', help='Be verbose on imports')
_import.add_argument(
Expand Down
6 changes: 1 addition & 5 deletions tests/test_gcalcli.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from json import load
import re

import pytest
from dateutil.tz import tzutc

from gcalcli.argparsers import (
Expand Down Expand Up @@ -274,14 +273,11 @@ def test_modify_event(PatchedGCalI):
gcal._edit_event, opts.text, opts.start, opts.end) == 0


@pytest.mark.skipif(
os.name == 'nt',
reason='Known unicode encode/decode issue, see insanum/gcalcli#387.')
def test_import(PatchedGCalI):
cal_names = parse_cal_names(['[email protected]'])
gcal = PatchedGCalI(cal_names=cal_names, default_reminders=True)
vcal_path = TEST_DATA_DIR + '/vv.txt'
assert gcal.ImportICS(icsFile=open(vcal_path))
assert gcal.ImportICS(icsFile=open(vcal_path, errors='replace'))


def test_parse_reminder():
Expand Down

0 comments on commit 8f81542

Please sign in to comment.