1
|
|
|
# vim: set expandtab sw=4 ts=4: |
2
|
|
|
""" |
3
|
|
|
Dictionary based collection class. |
4
|
|
|
|
5
|
|
|
Copyright (C) 2014-2016 Dieter Adriaenssens <[email protected]> |
6
|
|
|
|
7
|
|
|
This file is part of buildtimetrend/python-lib |
8
|
|
|
<https://github.com/buildtimetrend/python-lib/> |
9
|
|
|
|
10
|
|
|
This program is free software: you can redistribute it and/or modify |
11
|
|
|
it under the terms of the GNU Affero General Public License as published by |
12
|
|
|
the Free Software Foundation, either version 3 of the License, or |
13
|
|
|
any later version. |
14
|
|
|
|
15
|
|
|
This program is distributed in the hope that it will be useful, |
16
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
17
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
18
|
|
|
GNU Affero General Public License for more details. |
19
|
|
|
|
20
|
|
|
You should have received a copy of the GNU Affero General Public License |
21
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>. |
22
|
|
|
""" |
23
|
|
|
|
24
|
|
|
import copy |
25
|
|
|
from collections import OrderedDict |
26
|
|
|
from buildtimetrend.tools import check_dict |
27
|
|
|
from buildtimetrend import logger |
28
|
|
|
|
29
|
|
|
|
30
|
|
|
class Collection(object): |
31
|
|
|
|
32
|
|
|
"""Dictionary based collection object.""" |
33
|
|
|
|
34
|
|
|
def __init__(self): |
35
|
|
|
"""Initialize instance.""" |
36
|
|
|
self.items = {} |
37
|
|
|
|
38
|
|
|
def add_item(self, name, value): |
39
|
|
|
""" |
40
|
|
|
Add an item to the collection. |
41
|
|
|
|
42
|
|
|
Parameters : |
43
|
|
|
- name : Item name |
44
|
|
|
- value : Item value |
45
|
|
|
""" |
46
|
|
|
if check_dict(value) and name in self.items and \ |
47
|
|
|
check_dict(self.items[name]): |
48
|
|
|
self.items[name].update(value) |
49
|
|
|
else: |
50
|
|
|
self.items[name] = value |
51
|
|
|
|
52
|
|
|
def get_item(self, name): |
53
|
|
|
""" |
54
|
|
|
Get an item from a collection. |
55
|
|
|
|
56
|
|
|
Parameters : |
57
|
|
|
- name : Item name |
58
|
|
|
""" |
59
|
|
|
if name in self.items: |
60
|
|
|
return self.items[name] |
61
|
|
|
else: |
62
|
|
|
return None |
63
|
|
|
|
64
|
|
|
def get_size(self): |
65
|
|
|
"""Get collection size.""" |
66
|
|
|
return len(self.items) |
67
|
|
|
|
68
|
|
|
def add_items(self, items_dict): |
69
|
|
|
""" |
70
|
|
|
Add items as a dictionary to the collection. |
71
|
|
|
|
72
|
|
|
Parameters: |
73
|
|
|
- items_dict : dictionary with items |
74
|
|
|
""" |
75
|
|
|
if check_dict(items_dict, "items_dict"): |
76
|
|
|
# append dictionary with items to the existing collection |
77
|
|
|
self.items.update(items_dict) |
78
|
|
|
|
79
|
|
|
def get_items(self): |
80
|
|
|
"""Return items collection as dictionary.""" |
81
|
|
|
# copy values of items collection |
82
|
|
|
return copy.deepcopy(self.items) |
83
|
|
|
|
84
|
|
|
def get_items_with_summary(self): |
85
|
|
|
"""Return items collection as dictionary with an summary property.""" |
86
|
|
|
items = self.get_items() |
87
|
|
|
|
88
|
|
|
# concatenate all properties in a summary field |
89
|
|
|
matrix_params = self.get_key_sorted_items().values() |
90
|
|
|
try: |
91
|
|
|
items["summary"] = " ".join(matrix_params) |
92
|
|
|
except TypeError as msg: |
93
|
|
|
logger.error( |
94
|
|
|
"Error parsing build matrix properties : %s, message : %s", |
95
|
|
|
matrix_params, str(msg) |
96
|
|
|
) |
97
|
|
|
|
98
|
|
|
return items |
99
|
|
|
|
100
|
|
|
def get_key_sorted_items(self): |
101
|
|
|
"""Return items as an ordered dictionary, sorted on key.""" |
102
|
|
|
return OrderedDict(sorted(self.items.items())) |
103
|
|
|
|