add ls, start and end command
This commit is contained in:
@@ -9,7 +9,7 @@ class DataStore:
|
|||||||
if create:
|
if create:
|
||||||
# Store emtpty list
|
# Store emtpty list
|
||||||
self._time_slots = []
|
self._time_slots = []
|
||||||
self._write_update()
|
self.write_update()
|
||||||
else:
|
else:
|
||||||
# Load dicts from file
|
# Load dicts from file
|
||||||
with open(self._filename, "r") as f:
|
with open(self._filename, "r") as f:
|
||||||
@@ -23,18 +23,18 @@ class DataStore:
|
|||||||
|
|
||||||
self._time_slots = [deserialize(d) for d in self._time_slots]
|
self._time_slots = [deserialize(d) for d in self._time_slots]
|
||||||
|
|
||||||
def _write_update(self):
|
def write_update(self):
|
||||||
"""Saves the updates made to the DataStore object to disk."""
|
"""Saves the updates made to the DataStore object to disk."""
|
||||||
with open(self._filename, "w+") as f:
|
with open(self._filename, "w+") as f:
|
||||||
json.dump(self._time_slots, f, default=vars)
|
json.dump(self._time_slots, f, default=vars)
|
||||||
|
|
||||||
def add_time_slot(self, time_slot: TimeSlot):
|
def add_time_slot(self, time_slot: TimeSlot):
|
||||||
self._time_slots.append(time_slot)
|
self._time_slots.append(time_slot)
|
||||||
self._write_update()
|
self.write_update()
|
||||||
|
|
||||||
def remove_time_slot(self, time_slot: TimeSlot):
|
def remove_time_slot(self, time_slot: TimeSlot):
|
||||||
self._time_slots.remove(time_slot)
|
self._time_slots.remove(time_slot)
|
||||||
self._write_update()
|
self.write_update()
|
||||||
|
|
||||||
def get_all_time_slots(self):
|
def get_all_time_slots(self):
|
||||||
return self._time_slots[:]
|
return self._time_slots[:]
|
||||||
|
|||||||
@@ -37,3 +37,6 @@ class TimeSlot():
|
|||||||
if d < self.start:
|
if d < self.start:
|
||||||
raise ValueError("End date must be after the start date.")
|
raise ValueError("End date must be after the start date.")
|
||||||
self._end = d.timestamp()
|
self._end = d.timestamp()
|
||||||
|
|
||||||
|
def end_now(self):
|
||||||
|
self.end = datetime.now()
|
||||||
|
|||||||
54
main.py
54
main.py
@@ -6,15 +6,59 @@ from lib.TimeSlot import TimeSlot
|
|||||||
from lib.ui import convert_to_table
|
from lib.ui import convert_to_table
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
def create_parser():
|
||||||
parser = argparse.ArgumentParser(description='Time tracker')
|
# Create the top-level parser
|
||||||
parser.add_argument('command', type=str, help='Manage time slots.', choices=['start', 'stop', 'add', 'ps', 'ls'])
|
parser = argparse.ArgumentParser(description="Time tracker.")
|
||||||
parser.add_argument('-b', '--begin', type=str, help="The start time of a time slot.")
|
subparsers = parser.add_subparsers(dest="command", help="Sub-command help")
|
||||||
parser.add_argument('-e', '--end', type=str, help="The end time of a time slot.")
|
|
||||||
|
|
||||||
|
# Create the parser for the "ls" command
|
||||||
|
parser_ls = subparsers.add_parser("ls", help="List all events.")
|
||||||
|
|
||||||
|
# Create the parser for the "ps" command
|
||||||
|
parser_ps = subparsers.add_parser("ps", help="Show the currently running event.")
|
||||||
|
|
||||||
|
# Create the parser for the "add" command
|
||||||
|
parser_add = subparsers.add_parser("add", help="Adding a new event.")
|
||||||
|
parser_add.add_argument("-b", "--begin", help="Begin datetime")
|
||||||
|
parser_add.add_argument("-e", "--end", help="End datetime")
|
||||||
|
|
||||||
|
# Create the parser for the "start" command
|
||||||
|
parser_start = subparsers.add_parser("start", help="Starting a new event.")
|
||||||
|
parser_start.add_argument("name", help="Name of the event")
|
||||||
|
|
||||||
|
# Create the parser for the "end" command
|
||||||
|
parser_end = subparsers.add_parser("end", help="Ending the current event.")
|
||||||
|
|
||||||
|
return parser
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
parser = create_parser()
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
ds = DataStore("data.json")
|
ds = DataStore("data.json")
|
||||||
|
|
||||||
if args.command == "ls":
|
if args.command == "ls":
|
||||||
print(convert_to_table(ds.get_all_time_slots()))
|
print(convert_to_table(ds.get_all_time_slots()))
|
||||||
|
|
||||||
|
elif args.command == "start":
|
||||||
|
time_slots = ds.get_all_time_slots()
|
||||||
|
running = list(filter(lambda x: x.end is None, time_slots))
|
||||||
|
if len(running):
|
||||||
|
print("An event is already running.")
|
||||||
|
else:
|
||||||
|
ds.add_time_slot(TimeSlot(args.name))
|
||||||
|
print(f"Started event {args.name}.")
|
||||||
|
|
||||||
|
elif args.command == "end":
|
||||||
|
time_slots = ds.get_all_time_slots()
|
||||||
|
running = list(filter(lambda x: x.end is None, time_slots))
|
||||||
|
if len(running) <= 0:
|
||||||
|
print("No running event.")
|
||||||
|
elif len(running) > 1:
|
||||||
|
raise Exception("Found multiple running event.")
|
||||||
|
else:
|
||||||
|
running[0].end_now()
|
||||||
|
ds.write_update()
|
||||||
|
print(f"Ended event {running[0].name}.")
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user