141 | | fnkey = fieldnumber[key] |
142 | | newentries = [ headers ] |
143 | | sortedentries = entries[1:] |
144 | | sortedentries.sort(lambda x, y, fnum=fnkey : cmp(x[fnum], y[fnum])) |
145 | | totals = {} |
146 | | for (k, t) in totalize : |
147 | | totals[k] = { "convert" : t, "value" : 0.0 } |
148 | | prevkey = sortedentries[0][fnkey] |
149 | | for entry in sortedentries : |
150 | | if entry[fnkey] != prevkey : |
151 | | summary = [None] * nbheaders |
152 | | summary[fnkey] = prevkey |
153 | | for ignore in ignored : |
154 | | summary[fieldnumber[ignore]] = '*' |
155 | | for k in totals.keys() : |
156 | | summary[fieldnumber[k]] = totals[k]["convert"](totals[k]["value"]) |
157 | | for i in range(nbheaders) : |
158 | | if summary[i] is None : |
159 | | summary[i] = entry[i] |
160 | | newentries.append(summary) |
161 | | for k in totals.keys() : |
162 | | totals[k]["value"] = totals[k]["convert"](entry[fieldnumber[k]]) |
163 | | else : |
164 | | for k in totals.keys() : |
165 | | totals[k]["value"] += totals[k]["convert"](entry[fieldnumber[k]]) |
166 | | prevkey = entry[fnkey] |
167 | | summary = [None] * nbheaders |
168 | | summary[fnkey] = prevkey |
169 | | for ignore in ignored : |
170 | | summary[fieldnumber[ignore]] = '*' |
171 | | for k in totals.keys() : |
172 | | summary[fieldnumber[k]] = totals[k]["convert"](totals[k]["value"]) |
173 | | for i in range(nbheaders) : |
174 | | if summary[i] is None : |
175 | | summary[i] = entry[i] |
176 | | newentries.append(summary) |
177 | | elif datatype == "history" : |
178 | | newentries = entries # Fake this for now |
179 | | else : |
180 | | raise PyKotaToolError, _("Summarizing is not implemented for the [%s] data type, sorry.") % datatype |
| 144 | newentries = [ headers ] |
| 145 | sortedentries = entries[1:] |
| 146 | if keys : |
| 147 | # If we have several keys, we can sort only on the first one, because they |
| 148 | # will vary the same way. |
| 149 | sortedentries.sort(lambda x, y, fnum=fieldnumber[keys[0]] : cmp(x[fnum], y[fnum])) |
| 150 | totals = {} |
| 151 | for (k, t) in totalize : |
| 152 | totals[k] = { "convert" : t, "value" : 0.0 } |
| 153 | prevkeys = {} |
| 154 | for k in keys : |
| 155 | prevkeys[k] = sortedentries[0][fieldnumber[k]] |
| 156 | for entry in sortedentries : |
| 157 | curval = '-'.join([str(entry[fieldnumber[k]]) for k in keys]) |
| 158 | prevval = '-'.join([str(prevkeys[k]) for k in keys]) |
| 159 | if curval != prevval : |
| 160 | summary = [ "*" ] * nbheaders |
| 161 | for k in keys : |
| 162 | summary[fieldnumber[k]] = prevkeys[k] |
| 163 | for k in totals.keys() : |
| 164 | summary[fieldnumber[k]] = totals[k]["convert"](totals[k]["value"]) |
| 165 | newentries.append(summary) |
| 166 | for k in totals.keys() : |
| 167 | totals[k]["value"] = totals[k]["convert"](entry[fieldnumber[k]]) |
| 168 | else : |
| 169 | for k in totals.keys() : |
| 170 | totals[k]["value"] += totals[k]["convert"](entry[fieldnumber[k]]) |
| 171 | for k in keys : |
| 172 | prevkeys[k] = entry[fieldnumber[k]] |
| 173 | summary = [ "*" ] * nbheaders |
| 174 | for k in keys : |
| 175 | summary[fieldnumber[k]] = prevkeys[k] |
| 176 | for k in totals.keys() : |
| 177 | summary[fieldnumber[k]] = totals[k]["convert"](totals[k]["value"]) |
| 178 | newentries.append(summary) |