def main(): u""" Aggregate work time a project. Command line arguments: <start day> : Day of start, format : YYYYMMDD or MMDD, MDD (required) <end day> : Day of end, format : YYYYMMDD or MMDD, MDD (required) """ parser = argparse.ArgumentParser(description = u'Aggregate work time a project.') parser.add_argument(u'start_day', help = u'Day of start. Format is YYYYMMDD or MMDD, MDD') parser.add_argument(u'end_day', help = u'Day of end. Format is YYYYMMDD or MMDD, MDD') args = parser.parse_args() try: start_day = work_recorder.convert_day(args.start_day) end_day = work_recorder.convert_day(args.end_day) except work_recorder.InvalidArgumentFormatException: print u'Your arguments discords with formats.' sys.exit(1) database = os.path.join(os.getcwdu(), work_recorder.DATABASE_FILE_NAME) with sqlite3.connect(database) as conn: times = aggregate_work_time(start_day, end_day, conn) print_result(times)
def convert_work_times(project, day_string, time_strings): u""" Convert the arguments to work times. If this method could not convert, raise InvalidArgumentFormatException. Parameters: project : Name of the project. day_string : String of the day. time_strings : List of string of the times. Return: A list of work time dictionary. """ # Time are pairs of start and end. # Therefore, the number of times should be an even number. if len(time_strings) % 2: raise work_recorder.InvalidArgumentFormatException() day = work_recorder.convert_day(day_string) times = [work_recorder.convert_time(a_time_string) for a_time_string in time_strings] # Input times should be sorted. if times != sorted(times): raise work_recorder.InvalidArgumentFormatException() start_times = [] end_times = [] time_count = 0 for a_time in times: if time_count % 2: end_times.append(a_time) else: start_times.append(a_time) time_count += 1 work_times = [] for index in range(len(start_times)): a_work_time = {} a_work_time[WORK_TIME_KEY_PROJECT] = project a_work_time[WORK_TIME_KEY_DAY] = day a_work_time[WORK_TIME_KEY_START] = start_times[index] a_work_time[WORK_TIME_KEY_END] = end_times[index] work_times.append(a_work_time) return work_times
def check_invalid(self, date_string): try: date = work_recorder.convert_day(date_string) self.fail(date) except work_recorder.InvalidArgumentFormatException: pass
def test_format_mdd(self): today = date.today() self.assertEquals(u'%s-01-28' % today.year, work_recorder.convert_day(u'128'))
def test_format_yyyymmdd(self): self.assertEquals(u'2010-11-28', work_recorder.convert_day(u'20101128'))