-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoggl_importer.py
95 lines (85 loc) · 3.29 KB
/
toggl_importer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
from datetime import date
from enum import Enum
from typing import Optional
import typer
from toggl.main import DmcTimerImporter, TextDumpImporter, generate_toggl_invoice_model, pull_and_import_single_day
from toggl.toggl_secrets import get_toggl_secrets
from toggl.paths import toggl_secrets_file
app = typer.Typer(help="A tool for moving toggl data around")
@app.command()
def pull_and_import_one_day(
date: str = typer.Argument(...,
help="A string with the format YYYY-MM-DD"),
import_type: str = typer.
Argument(
"text-dump",
help=
"The type of importer to use. Possible values are 'text-dump' and 'dmc-timer'"
),
slowdown_factor: float = typer.
Argument(
1,
help=
"A multiplier applied to import waits. Increase the value of this parameter to run the import process more slowly."
),
api_key: str = typer.
Argument(
None,
help=
"Toggl API key. If none is provided, this command attempts to load one from a file called toggl_secrets.json."
)):
"""
Pulls one day of data from the Toggl API, then moves it somewhere else. Where and how it's moved depends on `import-type`
"""
print(f"Loading secrets from {toggl_secrets_file}")
toggl_secrets = get_toggl_secrets()
if api_key is None:
print(f"No API key provided, attempting to load key from secrets file")
api_key = toggl_secrets.api_key
if not api_key:
print("Invalid / blank API key found in secrets file")
raise typer.Exit(code=1)
if import_type == 'text-dump':
importer = TextDumpImporter()
elif import_type == 'dmc-timer':
importer = DmcTimerImporter()
else:
raise ValueError(f"Unsupported importer ('{import_type}') specified")
pull_and_import_single_day(date,
api_key,
importer=importer,
toggl_project_to_client_name_map=toggl_secrets.
toggl_project_to_client_name_map,
slowdown_factor=slowdown_factor)
@app.command()
def generate_invoice_model(
start_date: str = typer.Argument(
...,
help="Start date for the invoice model with the format YYYY-MM-DD"
),
end_date: str = typer.Argument(
...,
help="End date for the invoice model with the format YYYY-MM-DD"),
api_key: str = typer.
Argument(
None,
help=
"Toggl API key. If none is provided, this command attempts to load one from a file called toggl_secrets.json."
)):
"""
Generate an invoice model
"""
print(f"Loading secrets from {toggl_secrets_file}")
toggl_secrets = get_toggl_secrets()
if api_key is None:
print(f"No API key provided, attempting to load key from secrets file")
api_key = toggl_secrets.api_key
if not api_key:
print("Invalid / blank API key found in secrets file")
raise typer.Exit(code=1)
model = generate_toggl_invoice_model(start_date, end_date, api_key)
json_str = model.json().replace('\n', ' ').replace('\r', '')
print("INVOICE MODEL BELOW")
print(json_str)
if __name__ == "__main__":
app()