Skip to content
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

add interrupt command support #482

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
add interrupt command support
  • Loading branch information
EdgyEdgemond committed Jul 15, 2022
commit 609ad9a0e3c163ef3fccaa3aa2a908ff7c04adc3
36 changes: 36 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,42 @@ def test_start_existing_frame_stopped(runner, watson, mocker):
assert watson.current["project"] == "b-project"


# watson interrupt existing task with new task, restart existing

def test_interrupt_new_frame_added(runner, watson, mocker):
# Simulate a start date so that 'at_dt' is older than now().
watson.config.set('options', 'stop_on_start', "true")
mocker.patch('arrow.arrow.dt_datetime', wraps=datetime)
start_dt = datetime(2019, 4, 10, 15, 0, 0, tzinfo=local_tz_info())
arrow.arrow.dt_datetime.now.return_value = start_dt
result = runner.invoke(
cli.start,
['a-project', '--at', "14:10"],
obj=watson,
)

result = runner.invoke(
cli.interrupt,
['b-project', '--at', "14:15"],
obj=watson,
)
assert result.exit_code == 0, result.stdout

assert watson._frames._rows[0].project == "a-project"
assert watson._frames._rows[0].stop == arrow.get(
"2019-04-10T14:15:00+01:00")

assert watson._frames._rows[1].project == "b-project"
assert watson._frames._rows[1].start == arrow.get(
"2019-04-10T14:15:00+01:00")
assert watson._frames._rows[1].stop == arrow.get(
"2019-04-10T15:00:00+01:00")

assert watson.current["project"] == "a-project"
assert watson.current["start"] == arrow.get(
"2019-04-10T15:00:00+01:00")


# watson restart

@pytest.mark.parametrize('at_dt', VALID_TIMES_DATA)
Expand Down
74 changes: 74 additions & 0 deletions watson/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,80 @@ def start(ctx, watson, confirm_new_project, confirm_new_tag, args, at_,
_start(watson, project, tags, start_at=at_, gap=gap_)


@cli.command()
@click.option('--at', 'at_', required=True, type=DateTime,
help=('Start frame at this time. Must be in '
'(YYYY-MM-DDT)?HH:MM(:SS)? format.'))
@click.argument('args', nargs=-1,
shell_complete=get_project_or_task_completion)
@click.option('-c', '--confirm-new-project', is_flag=True, default=False,
help="Confirm addition of new project.")
@click.option('-b', '--confirm-new-tag', is_flag=True, default=False,
help="Confirm creation of new tag.")
@click.pass_obj
@click.pass_context
@catch_watson_error
def interrupt(ctx, watson, confirm_new_project, confirm_new_tag, args, at_):
"""
Insert a project starting at `--at` time and ending `now`. You can add
tags indicating more specifically what you are working on with `+tag`. The
currently running task will be started again.

If there is already a running project and the configuration option
`options.stop_on_start` is not set to a true value (`1`, `on`, `true`, or
`yes`), this command will abort.

Example:

\b
$ watson start apollo11 --at 13:37
Starting project apollo11 at 13:37
$ watson interrupt apollo13 --at 13:39
Starting project apollo13 at 13:39
Stopping project apollo11, started an hour ago and stopped just now. (id: e9ccd52) # noqa: E501
Starting project apollo11 just now.
"""
if not watson.is_started:
raise click.ClickException(
'No project running, please use watson start'
)

project = ' '.join(
itertools.takewhile(lambda s: not s.startswith('+'), args)
)
if not project:
raise click.ClickException("No project given.")

# Confirm creation of new project if that option is set
if (watson.config.getboolean('options', 'confirm_new_project') or
confirm_new_project):
confirm_project(project, watson.projects)

# Parse all the tags
tags = parse_tags(args)

# Confirm creation of new tag(s) if that option is set
if (watson.config.getboolean('options', 'confirm_new_tag') or
confirm_new_tag):
confirm_tags(tags, watson.tags)

if not watson.config.getboolean('options', 'stop_on_start'):
raise click.ClickException(
style('error', 'config.stop_on_start is set to false,'
' can not interrupt running project.'),
)

current = watson.current
ctx.invoke(stop, at_=at_)
watson.frames.add(project, at_, arrow.utcnow(), tags)
_start(
watson,
current["project"],
current["tags"],
start_at=arrow.utcnow(),
)


@cli.command(context_settings={'ignore_unknown_options': True})
@click.option('--at', 'at_', type=DateTime, default=None,
help=('Stop frame at this time. Must be in '
Expand Down