def read_proto(filename): dates=[] hours=[] with open(filename,'r') as f: for line in f: line=line.strip() if line.startswith('#'): continue if line=='': continue date,all_hours = line.split(None, 1) dates.append(from_iso(date)) hours.append(all_hours) return (dates,hours)
def read_hours_file(fn, days=dict()): with open(fn, 'r') as f: for line in f: line = line.strip() if line.startswith('$'): continue if line.startswith('#'): continue if len(line) < 1: continue if debug: print line try: date, rest = line.split(None, 1) except ValueError: date = line rest = "" date = tif.from_iso(date) if date <= tif.max_date(): raise Exception("Date %s falls into previous record range." % date) for record in rest.split(): sign = 1.0 if '-' in record: sign = -1.0 record = record.replace('-', '') key, hour, factor, flags = record_re.match(record).groups() if "" == hour: hour = 1 if "" == factor: factor = 1 minutes = int(60.0 * sign * float(hour) / float(factor)) try: key = tif.from_initials(key) except: raise Exception("Keyholder %s not found." % key) if key.active_on(date): record = dict(key=key, minutes=timedelta(minutes=minutes), flags=flags) if date in days: days[date].append(record) else: days[date] = [record] else: raise Exception("%s contains invalid keyholder %s." % (fn, key)) return days
def read_hours_file(fn,days=dict()): with open(fn,'r') as f: for line in f: line = line.strip() if line.startswith('$'): continue if line.startswith('#'): continue if len(line) < 1: continue if debug: print line try: date, rest = line.split(None,1) except ValueError: date = line rest = "" date = tif.from_iso(date) if date <= tif.max_date(): raise Exception("Date %s falls into previous record range." % date) for record in rest.split(): sign = 1.0 if '-' in record: sign = -1.0 record = record.replace('-','') key, hour, factor, flags = record_re.match(record).groups() if ""==hour: hour=1 if ""==factor: factor=1 minutes = int(60.0*sign*float(hour)/float(factor)) try: key=tif.from_initials(key) except: raise Exception("Keyholder %s not found." % key) if key.active_on(date): record = dict(key=key, minutes=timedelta(minutes=minutes), flags=flags) if date in days: days[date].append(record) else: days[date]=[record] else: raise Exception("%s contains invalid keyholder %s." % (fn,key)) return days
dates.append(from_iso(date)) hours.append(all_hours) return (dates,hours) def write_files(dates,hours,enddate): while dates[0] <= enddate: filename = 'week_of_%s.txt' % dates[0].isoformat() with open(filename,'w') as f: print filename for i in range(0,7): f.write("%s %s\n" % (dates[i], hours[i])) dates = next_week(dates) def next_week(dates): return [d + timedelta(days=7) for d in dates] if __name__=='__main__': if len(sys.argv) != 3: print "%s [first week schedule.txt] [end date]" % sys.argv[0] print " Outputs files with the names 'week_of_YYYY-MM-DD.txt'." else: our_name, proto_fn, enddate = sys.argv dates,hours = read_proto(proto_fn) if len(dates) != 7: print "This is not a weekly file, aborting" exit(-1) write_files(dates,hours,from_iso(enddate))