adding definition for timespans for ls and acc command
This commit is contained in:
35
main.py
35
main.py
@@ -1,31 +1,52 @@
|
|||||||
#!/usr/bin/python3
|
#!/usr/bin/python3
|
||||||
|
from datetime import datetime, timedelta
|
||||||
|
|
||||||
from util.str_to_datetime import str_to_datetime as datetime
|
from util.str_to_datetime import str_to_datetime
|
||||||
|
|
||||||
from model.TimeSlotContainer import time_slot_container as tsc
|
from model.TimeSlotContainer import time_slot_container as tsc
|
||||||
from model.TimeSlot import TimeSlot
|
from model.TimeSlot import TimeSlot
|
||||||
|
|
||||||
from util.views import convert_to_table
|
from util.views import convert_to_table
|
||||||
from util.parser import create_parser
|
from util.parser import create_parser
|
||||||
|
from util import timeslotlist
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
parser = create_parser()
|
parser = create_parser()
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
if args.command in ["ls", "acc"]:
|
||||||
|
match args.span:
|
||||||
|
case "w":
|
||||||
|
start_of_week = datetime.now() - timedelta(days=datetime.now().weekday())
|
||||||
|
start_of_week_midnight = start_of_week.replace(hour=0, minute=0, second=0, microsecond=0)
|
||||||
|
slots = tsc.get_time_slots_by_date(start=start_of_week_midnight)
|
||||||
|
case "lw":
|
||||||
|
start_of_week = datetime.now() - timedelta(days=datetime.now().weekday())
|
||||||
|
start_of_week_midnight = start_of_week.replace(hour=0, minute=0, second=0, microsecond=0)
|
||||||
|
slots = tsc.get_time_slots_by_date(
|
||||||
|
start=start_of_week_midnight - timedelta(days=7),
|
||||||
|
end=start_of_week_midnight,
|
||||||
|
)
|
||||||
|
case "d":
|
||||||
|
last_midnight = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
|
||||||
|
slots = tsc.get_time_slots_by_date(start=last_midnight)
|
||||||
|
case _:
|
||||||
|
slots = tsc.get_all_time_slots()
|
||||||
|
|
||||||
if args.command == "ls":
|
if args.command == "ls":
|
||||||
print(convert_to_table(tsc.get_time_slots_by_date()))
|
print(convert_to_table(slots))
|
||||||
|
|
||||||
elif args.command == "start":
|
elif args.command == "start":
|
||||||
if args.start is not None:
|
if args.start is not None:
|
||||||
tsc.open_time_slot(args.name, datetime(args.start))
|
tsc.open_time_slot(args.name, str_to_datetime(args.start))
|
||||||
else:
|
else:
|
||||||
tsc.open_time_slot(args.name)
|
tsc.open_time_slot(args.name)
|
||||||
print(f"Started event {args.name}.")
|
print(f"Started event {args.name}.")
|
||||||
|
|
||||||
elif args.command == "end":
|
elif args.command == "end":
|
||||||
if args.end is not None:
|
if args.end is not None:
|
||||||
tsc.close_time_slot(datetime(args.end))
|
tsc.close_time_slot(str_to_datetime(args.end))
|
||||||
else:
|
else:
|
||||||
tsc.close_time_slot()
|
tsc.close_time_slot()
|
||||||
print("Ended event.")
|
print("Ended event.")
|
||||||
@@ -34,8 +55,8 @@ if __name__ == "__main__":
|
|||||||
print(convert_to_table(tsc.get_open_time_slots()))
|
print(convert_to_table(tsc.get_open_time_slots()))
|
||||||
|
|
||||||
elif args.command == "add":
|
elif args.command == "add":
|
||||||
tsc.add_time_slot(args.name, datetime(args.start), datetime(args.end))
|
tsc.add_time_slot(args.name, str_to_datetime(args.start), str_to_datetime(args.end))
|
||||||
|
|
||||||
elif args.command == "acc":
|
elif args.command == "acc":
|
||||||
d = tsc.accumulate_duration(args.query)
|
d = timeslotlist.accumulate_duration(timeslotlist.filter(slots, args.query))
|
||||||
print(f"{d.seconds // 3600}:{d.seconds % 3600 // 60}")
|
print(f"{d.seconds // 3600}:{d.seconds % 3600 // 60:02}")
|
||||||
|
|||||||
@@ -1,5 +1,3 @@
|
|||||||
import re
|
|
||||||
|
|
||||||
from datetime import date, datetime, timedelta
|
from datetime import date, datetime, timedelta
|
||||||
from model.DataStore import DataStore
|
from model.DataStore import DataStore
|
||||||
from model.TimeSlot import TimeSlot
|
from model.TimeSlot import TimeSlot
|
||||||
@@ -16,9 +14,10 @@ class TimeSlotContainer:
|
|||||||
def get_all_time_slots(self) -> list[TimeSlot]:
|
def get_all_time_slots(self) -> list[TimeSlot]:
|
||||||
return self._ds.get_all_time_slots()
|
return self._ds.get_all_time_slots()
|
||||||
|
|
||||||
def get_time_slots_by_date(self, d: date = datetime.now().date()):
|
def get_time_slots_by_date(self, start: date, end: date = datetime.now().date()):
|
||||||
|
# The selection will be oriented at the start date
|
||||||
return [ts for ts in self._ds.get_all_time_slots()
|
return [ts for ts in self._ds.get_all_time_slots()
|
||||||
if ts.start.date() == datetime.now().date()]
|
if ts.start >= start and ts.start <= end]
|
||||||
|
|
||||||
def open_time_slot(self, name: str, start_dt: datetime = datetime.now()):
|
def open_time_slot(self, name: str, start_dt: datetime = datetime.now()):
|
||||||
if len(self.get_open_time_slots()) > 0:
|
if len(self.get_open_time_slots()) > 0:
|
||||||
@@ -45,10 +44,6 @@ class TimeSlotContainer:
|
|||||||
def delete_time_slot(self, ts: TimeSlot):
|
def delete_time_slot(self, ts: TimeSlot):
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
||||||
def accumulate_duration(self, query: str) -> timedelta:
|
|
||||||
pattern = re.compile(query)
|
|
||||||
return sum([ts.duration() for ts in self._ds.get_all_time_slots()
|
|
||||||
if pattern.search(ts.name) ], start=timedelta(0))
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
time_slot_container = TimeSlotContainer(DataStore("data.json"))
|
time_slot_container = TimeSlotContainer(DataStore("data.json"))
|
||||||
|
|||||||
@@ -7,7 +7,8 @@ def create_parser():
|
|||||||
subparsers = parser.add_subparsers(dest="command", help="Sub-command help")
|
subparsers = parser.add_subparsers(dest="command", help="Sub-command help")
|
||||||
|
|
||||||
# Create the parser for the "ls" command
|
# Create the parser for the "ls" command
|
||||||
_ = subparsers.add_parser("ls", help="List all events.")
|
parser_ls = subparsers.add_parser("ls", help="List events.")
|
||||||
|
parser_ls.add_argument("-s", "--span", choices=['d', 'w', 'lw'], help="Display only envent in a certain time span (d = current day; w = current week; lw = last week)")
|
||||||
|
|
||||||
# Create the parser for the "ps" command
|
# Create the parser for the "ps" command
|
||||||
_ = subparsers.add_parser("ps", help="Show the currently running event.")
|
_ = subparsers.add_parser("ps", help="Show the currently running event.")
|
||||||
@@ -30,5 +31,6 @@ def create_parser():
|
|||||||
# Create the parser for the "acc" command
|
# Create the parser for the "acc" command
|
||||||
parser_end = subparsers.add_parser("acc", help="Accumulate the duration of events.")
|
parser_end = subparsers.add_parser("acc", help="Accumulate the duration of events.")
|
||||||
parser_end.add_argument("query", help="The regex matching for the name of the event.")
|
parser_end.add_argument("query", help="The regex matching for the name of the event.")
|
||||||
|
parser_end.add_argument("-s", "--span", choices=['d', 'w', 'lw'], help="Display only envent in a certain time span (d = current day; w = current week; lw = last week)")
|
||||||
|
|
||||||
return parser
|
return parser
|
||||||
|
|||||||
12
util/timeslotlist.py
Normal file
12
util/timeslotlist.py
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
import re
|
||||||
|
from datetime import timedelta
|
||||||
|
|
||||||
|
from model.TimeSlot import TimeSlot
|
||||||
|
|
||||||
|
|
||||||
|
def accumulate_duration(timeslots: list[TimeSlot]) -> timedelta:
|
||||||
|
return sum([ts.duration() for ts in timeslots], start=timedelta(0))
|
||||||
|
|
||||||
|
def filter(timeslots: list[TimeSlot], query: str) -> list[TimeSlot]:
|
||||||
|
pattern = re.compile(query)
|
||||||
|
return [ts for ts in timeslots if pattern.search(ts.name)]
|
||||||
Reference in New Issue
Block a user