Skip to content

Commit

Permalink
BROKEN: enabling -a/-d for agenda and calendar
Browse files Browse the repository at this point in the history
All other commands are currently BROKEN.

While even strange combinations like `khal -a work agenda -a home` are
working and `khal -a work agenda -d home` raises an error (which it
should), this makes argument handling so ugly we could just as well use
argparse.
  • Loading branch information
geier committed Aug 26, 2014
1 parent 5df4a39 commit 7e1fb34
Showing 1 changed file with 49 additions and 20 deletions.
69 changes: 49 additions & 20 deletions khal/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,25 @@
from io import StringIO


def build_collection(conf, include_calendar, exclude_calendar):
collection = khalendar.CalendarCollection()
for name, cal in conf['calendars'].items():
if (not exclude_calendar and name in include_calendar) or \
(not include_calendar and name not in exclude_calendar):
collection.append(khalendar.Calendar(
name=name,
dbpath=conf['sqlite']['path'],
path=cal['path'],
readonly=cal['readonly'],
color=cal['color'],
unicode_symbols=conf['locale']['unicode_symbols'],
local_tz=conf['locale']['local_timezone'],
default_tz=conf['locale']['default_timezone']
))
collection._default_calendar_name = conf['default']['default_calendar']
return collection


def _get_cli():
@click.group(invoke_without_command=True)
@click.option('--config', '-c', default=None, metavar='PATH',
Expand Down Expand Up @@ -85,22 +104,8 @@ def cli(ctx, config, verbose, include_calendar, exclude_calendar):

if include_calendar and exclude_calendar:
raise click.UsageError('Can\'t use both -a and -d.')

ctx.obj['collection'] = collection = khalendar.CalendarCollection()
for name, cal in conf['calendars'].items():
if (not exclude_calendar and name in include_calendar) or \
(not include_calendar and name not in exclude_calendar):
collection.append(khalendar.Calendar(
name=name,
dbpath=conf['sqlite']['path'],
path=cal['path'],
readonly=cal['readonly'],
color=cal['color'],
unicode_symbols=conf['locale']['unicode_symbols'],
local_tz=conf['locale']['local_timezone'],
default_tz=conf['locale']['default_timezone']
))
collection._default_calendar_name = conf['default']['default_calendar']
ctx.obj['include_calendar'] = include_calendar
ctx.obj['exclude_calendar'] = exclude_calendar

if not ctx.invoked_subcommand:
command = conf['default']['default_command']
Expand All @@ -115,12 +120,29 @@ def cli(ctx, config, verbose, include_calendar, exclude_calendar):
dates_arg = click.argument('dates', nargs=-1)
time_args = lambda f: dates_arg(events_option(days_option(f)))

include_option = click.option(
'--include-calendar', '-a', multiple=True, metavar='CAL',
help=('Include the given calendar. Can be specified multiple times.'))
exclude_option = click.option(
'--exclude-calendar', '-d', multiple=True, metavar='CAL',
help=('Exclude the given calendar. Can be specified multiple times.'))

calendar_options = lambda f: include_option(exclude_option(f))

@cli.command()
@time_args
@calendar_options
@click.pass_context
def calendar(ctx, days, events, dates):
def calendar(ctx, days, events, dates, include_calendar, exclude_calendar):
include_calendar = list(include_calendar) + list(ctx.obj['include_calendar'])
exclude_calendar = list(exclude_calendar) + list(ctx.obj['exclude_calendar'])
if include_calendar and exclude_calendar:
raise click.UsageError('Can\'t use both -a and -d.')

collection = build_collection(
ctx.obj['conf'], include_calendar, exclude_calendar)
controllers.Calendar(
ctx.obj['collection'],
collection,
date=dates,
firstweekday=ctx.obj['conf']['locale']['firstweekday'],
encoding=ctx.obj['conf']['locale']['encoding'],
Expand All @@ -132,10 +154,17 @@ def calendar(ctx, days, events, dates):

@cli.command()
@time_args
@calendar_options
@click.pass_context
def agenda(ctx, days, events, dates):
def agenda(ctx, days, events, dates, include_calendar, exclude_calendar):
include_calendar = list(include_calendar) + list(ctx.obj['include_calendar'])
exclude_calendar = list(exclude_calendar) + list(ctx.obj['exclude_calendar'])
if include_calendar and exclude_calendar:
raise click.UsageError('Can\'t use both -a and -d.')
collection = build_collection(
ctx.obj['conf'], include_calendar, exclude_calendar)
controllers.Agenda(
ctx.obj['collection'],
collection,
date=dates,
firstweekday=ctx.obj['conf']['locale']['firstweekday'],
encoding=ctx.obj['conf']['locale']['encoding'],
Expand Down

0 comments on commit 7e1fb34

Please sign in to comment.