Skip to content

Commit d3430ee

Browse files
committed
Filters on report, and other minor fixes
1 parent 58d092b commit d3430ee

File tree

6 files changed

+56
-14
lines changed

6 files changed

+56
-14
lines changed

HISTORY.rst

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
History
22
=======
33

4-
0.7.5 (unreleased)
4+
0.8.0 (unreleased)
55
------------------
66

7-
- Nothing changed yet.
8-
7+
- Improved ``read`` logging
8+
- A bit more verbose messages here and there
9+
- Added warning when event summary is empty
10+
- Added warning when ``TIMEZONE`` is not explicitly set
11+
- Added filters on report (``-f``)
912

1013
0.7.4 (2024-06-25)
1114
------------------

haunts/calendars.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,8 @@ def execute_creation():
115115
f'in date {formatDate(event["start"]["date"], "%d/%m")} '
116116
f'on calendar {event["organizer"]["displayName"]}'
117117
)
118+
if not summary:
119+
click.echo(Back.YELLOW + Fore.BLACK + "Summary is empty!" + Style.RESET_ALL)
118120

119121
event_data = {
120122
"id": event["id"],

haunts/cli.py

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
import sys
77
from pathlib import Path
88
from importlib.metadata import version
9+
from colorama import Fore, Style
910
import click
1011

11-
from .ini import create_default, init
12+
from .ini import create_default, init, get
1213
from .calendars import init as init_calendars
1314
from .spreadsheet import sync_report
1415
from .report import report
@@ -70,6 +71,11 @@
7071
show_default=True,
7172
default=False,
7273
)
74+
@click.option(
75+
"--filter",
76+
"-f",
77+
help='filter report glob search inside "Activity" columns. ',
78+
)
7379
@click.option(
7480
"--version",
7581
"-v",
@@ -85,6 +91,7 @@ def main(
8591
action=[],
8692
project=[],
8793
overtime=False,
94+
filter=None,
8895
show_version=False,
8996
):
9097
"""
@@ -134,6 +141,17 @@ def main(
134141
click.echo("All done. You can now start using haunts.")
135142
sys.exit(0)
136143

144+
try:
145+
get("TIMEZONE")
146+
except KeyError:
147+
click.echo(
148+
Fore.YELLOW
149+
+ "TIMEZONE not set in configuration. Default is on Etc/GMT but you should customize it"
150+
+ " (for example: TIMEZONE=Europe/Rome).\n"
151+
+ "To suppress this warning set TIMEZONE explicitly in haunts.ini."
152+
+ Style.RESET_ALL
153+
)
154+
137155
init_calendars(config_dir)
138156
if execute == "sync":
139157
sync_report(
@@ -144,7 +162,14 @@ def main(
144162
allowed_actions=action,
145163
)
146164
elif execute == "report":
147-
report(config_dir, sheet, days=day, projects=project, overtime=overtime)
165+
report(
166+
config_dir,
167+
sheet,
168+
days=day,
169+
projects=project,
170+
overtime=overtime,
171+
filter=filter,
172+
)
148173
elif execute == "read":
149174
extract_events(
150175
config_dir,

haunts/download.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ def extract_events(config_dir, sheet, day):
8585
events_service = calendar_service.events()
8686
sheet_service = spreadsheet_service.spreadsheets()
8787

88-
click.echo("Checking your calendars…")
88+
click.echo(f"Checking your calendars at {day}…")
8989

9090
configured_calendars = get_calendars(
9191
sheet_service, ignore_alias=True, use_read_col=True
@@ -149,7 +149,10 @@ def extract_events(config_dir, sheet, day):
149149
+ Style.RESET_ALL
150150
)
151151
continue
152-
click.echo(f"Adding new event {event_summary} ({project}) to selected sheet")
152+
click.echo(
153+
f"Adding new event {event_summary} ({project}) "
154+
f"{f'at {start_time}' if duration else 'full day'} to selected sheet"
155+
)
153156
append_line(
154157
sheet_service,
155158
sheet,

haunts/report.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ def print_report(report, days=[], projects=[], overtime=False):
4646
if proj_stats.get("have_full_day", False):
4747
adjust_full_day(proj_stats)
4848
for project, stat in proj_stats["projects"].items():
49+
if stat.get("ignore"):
50+
continue
4951
overtime_value = stat["overtime"]
5052
if overtime and overtime_value:
5153
# Just want to count the overtime amount
@@ -60,6 +62,8 @@ def print_report(report, days=[], projects=[], overtime=False):
6062
and (not days or date in days)
6163
# not filtering by overtime, or this is an overtime entry
6264
and (not overtime or overtime_value)
65+
# not marked has "ignore"
66+
and (not proj_stats.get("ignore", False))
6367
):
6468
rows.append([date, project, total])
6569
gran_total += total
@@ -72,7 +76,7 @@ def print_report(report, days=[], projects=[], overtime=False):
7276
click.echo(tabulate(rows, headers=headers, tablefmt="simple"))
7377

7478

75-
def create_report(sheet, sheet_name, data, overtime=False):
79+
def create_report(sheet, sheet_name, data, overtime=False, filter=None):
7680
"""Create a time consumption report from a sheet."""
7781
headers_id = get_headers(sheet, sheet_name, indexes=True)
7882
overtime_from = get("OVERTIME_FROM", default=False)
@@ -138,6 +142,7 @@ def create_report(sheet, sheet_name, data, overtime=False):
138142
"total": 0,
139143
"overtime": 0,
140144
"full_day": False,
145+
"ignore": False,
141146
},
142147
)
143148
spent = get_col(row, headers_id["Spent"])
@@ -160,13 +165,17 @@ def create_report(sheet, sheet_name, data, overtime=False):
160165
date_stats["have_full_day"] = True
161166
prog_stats["full_day"] = True
162167

168+
# Filter defined, but it doesn't match
169+
if filter and filter not in get_col(row, headers_id["Activity"], ""):
170+
prog_stats["ignore"] = True
171+
163172
date_stats["projects"][project] = prog_stats
164173
dates[str(date)] = date_stats
165174

166175
return dates
167176

168177

169-
def report(config_dir, sheet_name, days=[], projects=[], overtime=False):
178+
def report(config_dir, sheet_name, days=[], projects=[], overtime=False, filter=None):
170179
"""Open a sheet, analyze it and extract stats."""
171180
# The ID and range of the controller timesheet
172181
creds = get_credentials(config_dir, SCOPES, "sheets-token.json")
@@ -205,10 +214,10 @@ def report(config_dir, sheet_name, days=[], projects=[], overtime=False):
205214
click.echo(err.error_details)
206215
sys.exit(1)
207216

208-
report = create_report(
209-
sheet=sheet, sheet_name=sheet_name, data=data, overtime=overtime
217+
computed_report = create_report(
218+
sheet=sheet, sheet_name=sheet_name, data=data, overtime=overtime, filter=filter
210219
)
211220

212221
click.echo("")
213-
print_report(report, days=days, projects=projects, overtime=overtime)
222+
print_report(computed_report, days=days, projects=projects, overtime=overtime)
214223
click.echo("")

haunts/spreadsheet.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@
1717
SCOPES = ["https://www.googleapis.com/auth/spreadsheets"]
1818

1919

20-
def get_col(row, index):
20+
def get_col(row, index, default=None):
2121
try:
2222
return row[index]
2323
except IndexError:
24-
return None
24+
return default
2525

2626

2727
def get_headers(sheet, month, indexes=False):

0 commit comments

Comments
 (0)