~dricottone/fmg-timesheets

fmg-timesheets/exporter/json.py -rw-r--r-- 976 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
40
#!/usr/bin/env python3

#TODO: dumps dictionary into JSON file

def encode_dict(timesheets):
    """Given a list of timesheets, which themselves are lists of time entries,
    create a nested dictionary to represent the data.

    ```
    {
      'PROJECT': {
        datetime.datetime(DATE): 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 new dictionary for new keys
            if key not in projects.keys():
                projects[key] = {}

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

    return projects

def export(filename, timesheets):
    pass