Compare commits
5 Commits
20a66b8f48
...
5cf69dcee5
Author | SHA1 | Date | |
---|---|---|---|
5cf69dcee5 | |||
ebe9548fb3 | |||
93655127db | |||
6915ea0368 | |||
f4a2735da0 |
89
tx
89
tx
@ -1,34 +1,83 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
from datetime import datetime, timedelta
|
from datetime import date, datetime, timedelta
|
||||||
import argparse
|
import argparse
|
||||||
|
import os
|
||||||
|
|
||||||
|
def get_current_date():
|
||||||
|
return date.today().strftime("%Y-%m-%d")
|
||||||
|
|
||||||
|
def get_timefile_line(date, time, desc):
|
||||||
|
return date + " " + time + " " + desc + "\n"
|
||||||
|
|
||||||
|
def get_total(filename):
|
||||||
|
with open(filename, 'r') as time_file:
|
||||||
|
total_time = timedelta()
|
||||||
|
|
||||||
|
for line in time_file:
|
||||||
|
date_time_description = line.split(" ", 2)
|
||||||
|
|
||||||
|
hours, minutes = date_time_description[1].split(':', 1)
|
||||||
|
|
||||||
|
# Parse the time to integers
|
||||||
|
hours_int = int(hours)
|
||||||
|
minutes_int = int(minutes)
|
||||||
|
|
||||||
|
# Make sure that both the hours and the minutes are negative if there's a "-" before the time
|
||||||
|
if hours_int < 0: minutes_int *= -1
|
||||||
|
|
||||||
|
delta = timedelta(hours=hours_int, minutes=minutes_int)
|
||||||
|
|
||||||
|
total_time += delta
|
||||||
|
|
||||||
|
return total_time
|
||||||
|
|
||||||
# Parse command line arguments
|
# Parse command line arguments
|
||||||
parser = argparse.ArgumentParser(description="Description")
|
parser = argparse.ArgumentParser(prog='time.txt', description="simple plain-text time tracking.")
|
||||||
|
subparsers = parser.add_subparsers(help='Commands', dest='mode')
|
||||||
|
|
||||||
# add arguments to the parser
|
parser.add_argument("-f", "--file", help="a file in the time.txt format. default: ./time.txt")
|
||||||
parser.add_argument("-f", "--file")
|
|
||||||
parser.add_argument("mode",
|
# create the parser for the "a" command
|
||||||
help="display the square of a given number",
|
parser_total = subparsers.add_parser('total', help='print the total unbilled time. (default)')
|
||||||
nargs='?', default="total",
|
|
||||||
choices=["total"])
|
# create the parser for the "b" command
|
||||||
|
parser_add = subparsers.add_parser('add', help='add a time entry.')
|
||||||
|
parser_add.add_argument("date", nargs='?', default=get_current_date(), help="date in the format 2020-12-30. defaults to the current date.")
|
||||||
|
parser_add.add_argument("time", help="time in the format 19:20.")
|
||||||
|
parser_add.add_argument("description", help="short description of how the time was spent.")
|
||||||
|
|
||||||
|
parser_bill = subparsers.add_parser('bill', help='add a bill entry (a negative time entry).')
|
||||||
|
parser_bill.add_argument("date", nargs='?', default=get_current_date(), help="date in the format 2020-12-30. defaults to the current date.")
|
||||||
|
parser_bill.add_argument("time", help="time in the format 19:20.")
|
||||||
|
parser_bill.add_argument("description", help="short description of how the time was spent.")
|
||||||
|
|
||||||
|
parser_create = subparsers.add_parser('create', help='create a new time.txt file.')
|
||||||
|
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
if args.file:
|
filename = args.file if args.file else 'time.txt'
|
||||||
time_file = open(args.file,'r')
|
|
||||||
else:
|
|
||||||
time_file = open('time.txt','r')
|
|
||||||
|
|
||||||
if args.mode == "total":
|
if not args.mode or args.mode == "total":
|
||||||
total_time = timedelta()
|
print("Total time:", get_total(filename))
|
||||||
|
|
||||||
for line in time_file:
|
elif args.mode == "add":
|
||||||
date_time_description = line.split(" ", 2)
|
with open(filename, 'a') as time_file:
|
||||||
|
# TODO: Validate date and time
|
||||||
|
time_file.write(get_timefile_line(args.date, args.time, args.description))
|
||||||
|
|
||||||
hours, minutes = date_time_description[1].split(':', 1)
|
print("New total:", get_total(filename))
|
||||||
delta = timedelta(hours=int(hours), minutes=int(minutes))
|
|
||||||
|
|
||||||
total_time += delta
|
elif args.mode == "bill":
|
||||||
|
with open(filename, 'a') as time_file:
|
||||||
|
# TODO: Validate date and time
|
||||||
|
time_file.write(get_timefile_line(args.date, "-" + args.time, args.description))
|
||||||
|
|
||||||
print('Total time: ', total_time)
|
print("New total:", get_total(filename))
|
||||||
|
|
||||||
|
elif args.mode == "create":
|
||||||
|
if (os.path.exists(filename)):
|
||||||
|
print("Error: File", filename, "already exists, can't create a new one!")
|
||||||
|
else:
|
||||||
|
with open(filename, 'w') as time_file:
|
||||||
|
time_file.write(get_timefile_line(get_current_date(), "00:01", "Setup time.txt"))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user