~dricottone/fmg-timesheets

ref: b10830e901cd0280a155e931848201cb7febd194 fmg-timesheets/exporter/long_csv.py -rw-r--r-- 1001 bytes
b10830e9Dominic Ricottone README updates 2 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#!/usr/bin/env python3

import csv

def handle_date(date):
    return date.strftime("%m/%d/%Y")

def encode_list(timesheets):
    """Given a list of timesheets, which themselves are lists of time entries,
    create a long list of data.

    ```
    [ ['PROJECT', 'MM/DD/YYYY', decimal.Decimal(HOURS)],
      ...
    ]
    ```
    """
    projects = []

    for timesheet in timesheets:
        for entry in timesheet:
            # identify the project key to use
            key = entry.project
            if entry.time_code in ("HOL", "OTU", "VAC", "OPL", ):
                key = entry.time_code

            # set hours into the projects list
            for date, hours in entry.data.items():
                projects.append([key, handle_date(date), hours])

    return projects

def export(filename, timesheets):
    """Main routine."""
    with open(filename, "w", newline="") as f:
        writer = csv.writer(f)
        for row in encode_list(timesheets):
            writer.writerow(row)