Compare commits

...

3 Commits

6 changed files with 63 additions and 11 deletions

35
main.py
View File

@@ -1,31 +1,52 @@
#!/usr/bin/python3 #!/usr/bin/python3
from datetime import datetime, timedelta
from util.str_to_datetime import 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,4 +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":
d = timeslotlist.accumulate_duration(timeslotlist.filter(slots, args.query))
print(f"{d.seconds // 3600}:{d.seconds % 3600 // 60:02}")

View File

@@ -42,3 +42,8 @@ class TimeSlot():
def end_now(self): def end_now(self):
self.end = datetime.now() self.end = datetime.now()
def duration(self) -> datetime.timedelta:
end = self.end
if not end: end = datetime.now()
return end - self.start

View File

@@ -1,4 +1,4 @@
from datetime import date, datetime 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
@@ -14,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: datetime, end: datetime = datetime.now()):
# 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:
@@ -44,4 +45,7 @@ class TimeSlotContainer:
raise NotImplementedError raise NotImplementedError
time_slot_container = TimeSlotContainer(DataStore("data.json")) try:
time_slot_container = TimeSlotContainer(DataStore("data.json"))
except FileNotFoundError:
time_slot_container = TimeSlotContainer(DataStore("data.json", create=True))

View File

@@ -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.")
@@ -27,4 +28,9 @@ def create_parser():
parser_end = subparsers.add_parser("end", help="Ending the current event.") parser_end = subparsers.add_parser("end", help="Ending the current event.")
parser_end.add_argument("-e", "--end", help="End datetime") parser_end.add_argument("-e", "--end", help="End datetime")
# Create the parser for the "acc" command
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("-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

View File

@@ -1,7 +1,7 @@
from datetime import datetime as dt from datetime import datetime as dt
def datetime(s: str) -> dt: def str_to_datetime(s: str) -> dt:
s = s.strip() s = s.strip()
try: try:

12
util/timeslotlist.py Normal file
View 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)]