diff options
author | Mattori Birnbaum <mattori.birnbaum@gmail.com> | 2021-05-08 16:54:36 +0900 |
---|---|---|
committer | Christian Geier <geier@lostpackets.de> | 2023-10-28 16:40:56 +0200 |
commit | 27f5a6f02093e816754ba802cac4076a29f129c4 (patch) | |
tree | e56601a0a3d897f9d797c7c62176d6cafda9e2c6 | |
parent | 15fcb7692c23c49ae5fd3571ce1babc10b3dfa07 (diff) |
add json to new
-rw-r--r-- | khal/cli.py | 4 | ||||
-rw-r--r-- | khal/controllers.py | 22 | ||||
-rw-r--r-- | tests/cli_test.py | 24 |
3 files changed, 39 insertions, 11 deletions
diff --git a/khal/cli.py b/khal/cli.py index 72adfb8c..cc7f02a6 100644 --- a/khal/cli.py +++ b/khal/cli.py @@ -363,6 +363,7 @@ def _get_cli(): help=('Stop an event repeating on this date.')) @click.option('--format', '-f', help=('The format to print the event.')) + @click.option('--json', help=("Fields to output in json"), multiple=True) @click.option('--alarms', '-m', help=('Alarm times for the new event as DELTAs comma separated')) @click.option('--url', help=("URI for the event.")) @@ -370,7 +371,7 @@ def _get_cli(): nargs=-1) @click.pass_context def new(ctx, calendar, info, location, categories, repeat, until, alarms, url, format, - interactive): + json, interactive): '''Create a new event from arguments. START and END can be either dates, times or datetimes, please have a @@ -418,6 +419,7 @@ def _get_cli(): alarms=alarms, url=url, format=format, + json=json ) except FatalError as error: logger.debug(error, exc_info=True) diff --git a/khal/controllers.py b/khal/controllers.py index aeac9632..41401a99 100644 --- a/khal/controllers.py +++ b/khal/controllers.py @@ -175,9 +175,9 @@ def get_events_between( formatter: Callable, notstarted: bool, env: dict, - width: Optional[int], - seen=None, original_start: dt.datetime, + seen=None, + colors: bool = True, ) -> List[str]: """returns a list of events scheduled between start and end. Start and end are strings or datetimes (of some kind). @@ -323,7 +323,7 @@ def khal_list( def new_interactive(collection, calendar_name, conf, info, location=None, categories=None, repeat=None, until=None, alarms=None, - format=None, env=None, url=None): + format=None, json=[], env=None, url=None): info: EventCreationTypes try: info = parse_datetime.eventinfofstr( @@ -390,6 +390,7 @@ def new_interactive(collection, calendar_name, conf, info, location=None, format=format, env=env, calendar_name=calendar_name, + json=json, ) echo("event saved") @@ -399,7 +400,7 @@ def new_interactive(collection, calendar_name, conf, info, location=None, def new_from_string(collection, calendar_name, conf, info, location=None, categories=None, repeat=None, until=None, alarms=None, - url=None, format=None, env=None): + url=None, format=None, json=[], env=None): """construct a new event from a string and add it""" info = parse_datetime.eventinfofstr( info, conf['locale'], @@ -415,7 +416,7 @@ def new_from_string(collection, calendar_name, conf, info, location=None, 'alarms': alarms, 'url': url, }) - new_from_dict(info, collection, conf=conf, format=format, env=env, calendar_name=calendar_name) + new_from_dict(info, collection, conf=conf, format=format, env=env, calendar_name=calendar_name, json=json) def new_from_dict( @@ -425,6 +426,7 @@ def new_from_dict( calendar_name: Optional[str]=None, format=None, env=None, + json=[], ) -> Event: """Create a new event from arguments and save in vdirs @@ -446,9 +448,13 @@ def new_from_dict( ) if conf['default']['print_new'] == 'event': - if format is None: - format = conf['view']['event_format'] - echo(human_formatter(format)(event.format(dt.datetime.now(), env=env))) + if len(json) == 0: + if format is None: + format = conf['view']['event_format'] + formatter = human_formatter(format) + else: + formatter = json_formatter(json) + echo(formatter(event.format(dt.datetime.now(), env=env))) elif conf['default']['print_new'] == 'path': assert event.href path = os.path.join(collection._calendars[event.calendar]['path'], event.href) diff --git a/tests/cli_test.py b/tests/cli_test.py index a82aa108..121dda0b 100644 --- a/tests/cli_test.py +++ b/tests/cli_test.py @@ -875,7 +875,27 @@ def test_new(runner): assert result.output.startswith(str(runner.tmpdir)) -@freeze_time('2015-6-1 8:00') +def test_new_format(runner): + runner = runner(print_new='event') + + format = '{start-end-time-style}: {title}' + result = runner.invoke(main_khal, ['new', '13.03.2016 12:00', '3d', + '--format', format, 'Visit']) + assert not result.exception + assert result.output.startswith('→12:00: Visit') + + +def test_new_json(runner): + runner = runner(print_new='event') + + result = runner.invoke(main_khal, ['new', '13.03.2016 12:00', '3d', + '--json', 'start-end-time-style', '--json', 'title', 'Visit']) + assert not result.exception + assert result.output.startswith( + '[{"start-end-time-style": "→12:00", "title": "Visit"}]') + + +@ freeze_time('2015-6-1 8:00') def test_new_interactive(runner): runner = runner(print_new='path') @@ -896,7 +916,7 @@ def test_debug(runner): assert not result.exception -@freeze_time('2015-6-1 8:00') +@ freeze_time('2015-6-1 8:00') def test_new_interactive_extensive(runner): runner = runner(print_new='path', default_calendar=False) |