fix: always load data, regardless of the command

This commit is contained in:
Johann Dreo 2023-07-29 08:03:31 +02:00
commit fa37024a3d

View file

@ -165,6 +165,10 @@ def cli(context, **kwargs):
context.obj['debug'] = kwargs['debug'] context.obj['debug'] = kwargs['debug']
# At the end, always load data, whatever the command will be.
context.obj['data'] = load_data(context)
# If no command: defaults to `show`.
if not context.invoked_subcommand: if not context.invoked_subcommand:
context.invoke(show) context.invoke(show)
@ -177,7 +181,7 @@ def show(context, tid):
if tid is None: if tid is None:
# Show the kanban tables. # Show the kanban tables.
df = load_data(context) df = context.obj['data']
if df.empty: if df.empty:
print("No task.") print("No task.")
return return
@ -209,7 +213,7 @@ def show(context, tid):
else: # tid is not None. else: # tid is not None.
# Show a task card. # Show a task card.
df = load_data(context) df = context.obj['data']
row = df.loc[tid] row = df.loc[tid]
t_label = ["", "", ""] t_label = ["", "", ""]
@ -286,7 +290,7 @@ def show(context, tid):
@click.pass_context @click.pass_context
def add(context, title, status, details, tags, deadline): def add(context, title, status, details, tags, deadline):
"""Add a new task.""" """Add a new task."""
df = load_data(context) df = context.obj['data']
if df.index.empty: if df.index.empty:
next_id = 0 next_id = 0
else: else:
@ -309,7 +313,7 @@ def default_from_existing(key):
class OptionDefaultFromContext(click.Option): class OptionDefaultFromContext(click.Option):
def get_default(self, context): def get_default(self, context):
tid = context.params['tid'] tid = context.params['tid']
df = load_data(context) df = context.obj['data']
assert(tid in df.index) assert(tid in df.index)
row = df.loc[tid] row = df.loc[tid]
value = row[context.obj[key]] value = row[context.obj[key]]
@ -330,7 +334,7 @@ def default_from_existing(key):
@click.pass_context @click.pass_context
def edit(context, tid, title, status, details, tags, deadline): def edit(context, tid, title, status, details, tags, deadline):
"""Add a new task.""" """Add a new task."""
df = load_data(context) df = context.obj['data']
assert(tid in df.index) assert(tid in df.index)
df.loc[tid] = pd.Series({ df.loc[tid] = pd.Series({
context.obj['status_key']: status, context.obj['status_key']: status,
@ -357,7 +361,7 @@ def check_yes(context, param, value):
@click.pass_context @click.pass_context
def delete(context, tid): def delete(context, tid):
"""Delete a task.""" """Delete a task."""
df = load_data(context) df = context.obj['data']
df = df.drop(index=tid) df = df.drop(index=tid)
save_data(context, df) save_data(context, df)
@ -367,7 +371,7 @@ def delete(context, tid):
def change_status(context, tid, new_status): def change_status(context, tid, new_status):
"""Edit the status of a task.""" """Edit the status of a task."""
df = load_data(context) df = context.obj['data']
row = df.loc[tid] row = df.loc[tid]
if row.empty: if row.empty:
@ -404,7 +408,7 @@ def promote(context, tid):
Use status names configured with --status-list.""" Use status names configured with --status-list."""
df = load_data(context) df = context.obj['data']
row = df.loc[tid] row = df.loc[tid]
if row.empty: if row.empty:
@ -432,7 +436,7 @@ def demote(context, tid):
Use status names configured with --status-list.""" Use status names configured with --status-list."""
df = load_data(context) df = context.obj['data']
row = df.loc[tid] row = df.loc[tid]
if row.empty: if row.empty: