~jedfrechette-gmail/gtimelog/gtimelog.jf

« back to all changes in this revision

Viewing changes to src/gtimelog/main.py

  • Committer: Jed Frechette
  • Date: 2011-04-16 19:26:17 UTC
  • Revision ID: jedfrechette@gmail.com-20110416192617-75vtjj1he5gm5w1z
Add itemized daily CSV report.

Show diffs side-by-side

added added

removed removed

Lines of Context:
483
483
                  for day, (start, slacking, work) in days.items()]
484
484
        items.sort()
485
485
        writer.writerows(items)
486
 
 
 
486
        
 
487
    def to_csv_itemized_daily(self, output, title_row=True):
 
488
        """Export daily task working times to a CSV file.
 
489
 
 
490
        The file has three columns: start date, task, and work (in decimal hours).
 
491
        """
 
492
        writer = csv.writer(output)
 
493
        if title_row:
 
494
            writer.writerow(["start date", "task", "work (hours)"])
 
495
 
 
496
        days = {} # {start_date: {entry: duration}}
 
497
        
 
498
        for start, stop, duration, entry in self.all_entries():
 
499
            if duration:
 
500
                if start.date() in days.keys():
 
501
                    day = days[start.date()]
 
502
                    if entry in day.keys():
 
503
                        day[entry] += duration
 
504
                    else:
 
505
                        day[entry] = duration
 
506
                else:
 
507
                    days[start.date()] = {entry: duration}
 
508
 
 
509
        items = []
 
510
        for day in sorted(days.keys()):
 
511
            for entry in sorted(days[day].keys()):
 
512
                items.append([day, entry, as_hours(days[day][entry])])
 
513
        writer.writerows(items)
487
514
 
488
515
class Reports(object):
489
516
    """Generation of reports."""
1758
1785
        self.timelog.whole_history().to_csv_daily(f)
1759
1786
        f.close()
1760
1787
        self.spawn(self.settings.spreadsheet, tempfn)
 
1788
    
 
1789
    def on_open_itemized_spreadsheet_activate(self, widget):
 
1790
        """Report -> Daily task stats in Spreadsheet"""
 
1791
        tempfn = tempfile.mktemp(suffix='gtimelog.csv') # XXX unsafe!
 
1792
        f = open(tempfn, 'w')
 
1793
        self.timelog.whole_history().to_csv_itemized_daily(f)
 
1794
        f.close()
 
1795
        self.spawn(self.settings.spreadsheet, tempfn)
1761
1796
 
1762
1797
    def on_edit_timelog_activate(self, widget):
1763
1798
        """File -> Edit timelog.txt"""