Coverage for compiler_admin / commands / time / convert.py: 100%
25 statements
« prev ^ index » next coverage.py v7.13.5, created at 2026-04-28 05:48 +0000
« prev ^ index » next coverage.py v7.13.5, created at 2026-04-28 05:48 +0000
1import os
2import sys
3from typing import TextIO
5import click
7from compiler_admin.services.harvest import HarvestTime
8from compiler_admin.services.toggl import TogglTime
10TIME_SERVICES = {"harvest": HarvestTime(), "toggl": TogglTime()}
13def _get_source_converter(from_fmt: str, to_fmt: str):
14 from_fmt = from_fmt.lower().strip() if from_fmt else ""
15 to_fmt = to_fmt.lower().strip() if to_fmt else ""
16 time_service = TIME_SERVICES.get(from_fmt, None)
17 converter = time_service.converters.get(to_fmt) if time_service else None
19 if converter:
20 return converter
21 else:
22 raise NotImplementedError(
23 f"A converter for the given source and target formats does not exist: {from_fmt} to {to_fmt}"
24 )
27@click.command()
28@click.option(
29 "--input",
30 default=os.environ.get("TOGGL_DATA", sys.stdin),
31 help="The path to the source data for conversion. Defaults to $TOGGL_DATA or stdin.",
32)
33@click.option(
34 "--output",
35 default=os.environ.get("HARVEST_DATA", sys.stdout),
36 help="The path to the file where converted data should be written. Defaults to $HARVEST_DATA or stdout.",
37)
38@click.option(
39 "--from",
40 "from_fmt",
41 default="toggl",
42 help="The format of the source data.",
43 show_default=True,
44 type=click.Choice(sorted(TIME_SERVICES.keys()), case_sensitive=False),
45)
46@click.option(
47 "--to",
48 "to_fmt",
49 default="harvest",
50 help="The format of the converted data.",
51 show_default=True,
52 type=click.Choice(
53 sorted([to_fmt for sub in TIME_SERVICES.values() for to_fmt in sub.converters.keys()]), case_sensitive=False
54 ),
55)
56@click.option("--client", help="The name of the client to use in converted data.")
57def convert(
58 input: str | TextIO = os.environ.get("TOGGL_DATA", sys.stdin),
59 output: str | TextIO = os.environ.get("HARVEST_DATA", sys.stdout),
60 from_fmt="toggl",
61 to_fmt="harvest",
62 client="",
63):
64 """Convert a time report from one format into another."""
65 converter = _get_source_converter(from_fmt, to_fmt)
67 click.echo(f"Converting data from format: {from_fmt} to format: {to_fmt}")
69 converter(source_path=input, output_path=output, client_name=client)