1#!/usr/bin/env python3
2
3"""Implementation of Report group reader class."""
4
5import logging
6import collections
7
8from chart.common.decorators import memoized
9from chart.backend.activity import Activity
10from chart.common.xml import parsechildstr
11from chart.common.xml import parsechildbool
12from chart.common.xml import parsechildstrs
13from chart.common.xml import parsechildtimedelta
14from chart.common.xml import load_xml
15from chart.project import settings
16
17
18ELEM_GROUP = 'group'
19ELEM_NAME = 'name'
20ELEM_REPORT = 'report'
21ELEM_DESCRIPTION = 'description'
22ELEM_DURATION = 'duration'
23ELEM_NAVIGATION = 'navigation'
24
25logger = logging.getLogger()
26
27
28class ReportGroup:
29 """Read and write the report group file."""
30
31 def __init__(self):
32 """Open report group file."""
33 logger.info('Parsing report groups')
34 # we should trap case of a report in multiple groups
35
36 self.elem = load_xml(settings.REPORT_GROUP_FILE)
37 # group name gainst group definnition
38 self.groups = collections.OrderedDict()
39
40 for group_elem in self.elem.findall(ELEM_GROUP):
41 name = parsechildstr(group_elem, ELEM_NAME)
42 activities = []
43 for activity_name in parsechildstrs(group_elem, ELEM_REPORT):
44 activity = Activity(activity_name)
45 activities.append(activity)
46
47 self.groups[name] = {
48 'name': name,
49 'description': parsechildstr(group_elem, ELEM_DESCRIPTION, None),
50 'duration': parsechildtimedelta(group_elem, ELEM_DURATION, None),
51 'navigation': parsechildbool(group_elem, ELEM_NAVIGATION, False),
52 'activities': activities}
53
54 # activity object against group
55 self.activities = {}
56 for group in self.groups.values():
57 for a in group['activities']:
58 self.activities[a] = group
59
60
61@memoized
62def get_groups():
63 """Memoized constructor."""
64 if settings.REPORT_GROUP_FILE is not None:
65 return ReportGroup()
66
67 else:
68 return None