Пример #1
0
def timer_current(context: click.Context):
    downloader = retrieve_downloader_from_context(context.obj)

    workspaces = context.obj['data']['workspaces']
    projects = context.obj['data']['projects']

    try:
        entry = downloader.get_current_time_entry()
        if not entry:
            click.echo('No entry is currently running.')
        else:
            workspace = WorkspaceFilter.filter_on_identifier(
                workspaces, entry.workspace_identifier)[0]

            project = None
            if entry.project_identifier:
                project = ProjectFilter.filter_on_identifier(
                    projects, entry.project_identifier)[0]

            time_entry_view = TimeEntryView([entry], project, workspace)
            click.echo_via_pager(
                tabulate(time_entry_view.values_sorted(),
                         headers=TimeEntryView.headers(),
                         tablefmt="grid"))

    except HTTPError as e:
        logging.getLogger(__name__).error(e)
        click.echo(
            click.style('ERROR', fg='red') +
            f': failed to download the current running timer. An exception'
            f' has been logged; check the logs for more information.')
Пример #2
0
def sync_or_retrieve_time_entries(
        context_obj: dict,
        workspace: Workspace,
        start: datetime,
        stop: datetime,
        *,
        project: Optional[Project] = None) -> List[TimeEntry]:
    caching = retrieve_cache_from_context(context_obj)
    downloader = retrieve_downloader_from_context(context_obj)

    # Download any new time entries if remote sync is enabled.
    if context_obj['sync'] is True:
        current_time_entries = downloader.download_time_entries(start, stop)
        if current_time_entries:
            caching.update_time_entry_cache(current_time_entries)
    else:
        # Otherwise we just pull from the local cache.
        current_time_entries = caching.retrieve_time_entry_cache()

    # Neither downloading from remote nor pulling from local cache does any filtering, so let's do that.
    current_time_entries = TimeEntryFilter.filter_on_workspace(
        current_time_entries, workspace)

    # It also doesn't filter on start/stop, so let's do that too.
    current_time_entries = TimeEntryFilter.filter_on_date_range(
        current_time_entries, start, stop)

    # Time entries _can_ be filtered on project if it is provided.
    current_time_entries = TimeEntryFilter.filter_on_project(
        current_time_entries, project)

    return current_time_entries
Пример #3
0
def sync_or_retrieve_workspaces(context_obj: dict) -> List[Workspace]:
    caching = retrieve_cache_from_context(context_obj)
    downloader = retrieve_downloader_from_context(context_obj)

    # Download any new workspaces added remotely (this is only possible via
    # the web interface anyway), if remote sync is enabled.
    if context_obj['sync'] is True:
        current_workspaces = downloader.download_workspaces()
        if current_workspaces:
            caching.update_workspace_cache(current_workspaces)
    else:
        # Otherwise we just pull from the local cache.
        current_workspaces = caching.retrieve_workspace_cache()

    return current_workspaces
Пример #4
0
def sync_or_retrieve_tags(context_obj: dict, workspace: Workspace) -> List[Tag]:
    caching = retrieve_cache_from_context(context_obj)
    downloader = retrieve_downloader_from_context(context_obj)

    # Download any new tags if remote sync is enabled.
    if context_obj['sync'] is True:
        current_tags = downloader.download_tags(workspace)
        if current_tags:
            caching.update_tag_cache(current_tags)
    else:
        # Otherwise we just pull from the local cache.
        current_tags = caching.retrieve_tag_cache()

        if current_tags:
            # Retrieval from the DB doesn't filter on workspace, so we do that ourselves.
            current_tags = TagFilter.filter_on_workspace(current_tags, workspace)

    return current_tags
Пример #5
0
def timer_stop(context: click.Context):
    downloader = retrieve_downloader_from_context(context.obj)
    entry = downloader.get_current_time_entry()

    if not entry:
        click.echo('No entry is currently running.')
        return

    commands = retrieve_commands_from_context(context.obj)
    caching = retrieve_cache_from_context(context.obj)

    try:
        entry = commands.stop_time_entry(entry)
        caching.update_time_entry_cache([entry])
        click.echo(
            click.style('SUCCESS', fg='green') +
            f': stopped the current running timer({entry.description}).')

    except HTTPError as e:
        logging.getLogger(__name__).error(e)
        click.echo(
            click.style('ERROR', fg='red') +
            f': failed to stop the current running timer. An exception'
            f' has been logged; check the logs for more information.')