| 1 | # Copyright (C) 2007 Insecure.Com LLC. |
|---|
| 2 | # |
|---|
| 3 | # Authors: Guilherme Polo <ggpolo@gmail.com> |
|---|
| 4 | # |
|---|
| 5 | # This program is free software; you can redistribute it and/or modify |
|---|
| 6 | # it under the terms of the GNU General Public License as published by |
|---|
| 7 | # the Free Software Foundation; either version 2 of the License, or |
|---|
| 8 | # (at your option) any later version. |
|---|
| 9 | # |
|---|
| 10 | # This program is distributed in the hope that it will be useful, |
|---|
| 11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 13 | # GNU General Public License for more details. |
|---|
| 14 | # |
|---|
| 15 | # You should have received a copy of the GNU General Public License |
|---|
| 16 | # along with this program; if not, write to the Free Software |
|---|
| 17 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
|---|
| 18 | # USA |
|---|
| 19 | |
|---|
| 20 | import datetime |
|---|
| 21 | |
|---|
| 22 | from umitCore.I18N import _ |
|---|
| 23 | from umitCore.Paths import Path |
|---|
| 24 | #from umitCore.UmitConfigParser import UmitConfigParser # stopped working here |
|---|
| 25 | from ConfigParser import ConfigParser |
|---|
| 26 | |
|---|
| 27 | from umitInventory.Calendar import CalendarManager |
|---|
| 28 | from umitInventory.Calendar import months |
|---|
| 29 | from umitInventory.Calendar import monthname |
|---|
| 30 | from umitInventory.Calendar import startup_calendar_opts |
|---|
| 31 | from umitInventory.DataGrabber import DataGrabber |
|---|
| 32 | from umitInventory.DataGrabber import DATA_GRAB_MODES |
|---|
| 33 | |
|---|
| 34 | settings_file = Path.tl_conf |
|---|
| 35 | #configparser = UmitConfigParser() |
|---|
| 36 | configparser = ConfigParser() |
|---|
| 37 | configparser.read(settings_file) |
|---|
| 38 | |
|---|
| 39 | # this dict may have keys exchanged with values, but before doing this |
|---|
| 40 | # I will need to rework how InventoryChanges selects "change category". |
|---|
| 41 | colors_in_file = { _("Changes Sum"): "changes_sum", |
|---|
| 42 | _("Nothing"): "nothing", |
|---|
| 43 | _("Inventory"): "inventory", |
|---|
| 44 | _("Availability"): "availability", |
|---|
| 45 | _("Several"): "several", |
|---|
| 46 | _("Fingerprint"): "fingerprint", |
|---|
| 47 | _("Ports"): "ports" |
|---|
| 48 | } |
|---|
| 49 | # |
|---|
| 50 | |
|---|
| 51 | changes_in_db = { _("Nothing"): "nothing", |
|---|
| 52 | _("Inventory"): "inventory", |
|---|
| 53 | _("Availability"): "availability", |
|---|
| 54 | _("Several"): "several", |
|---|
| 55 | _("Fingerprint"): "fingerprint", |
|---|
| 56 | _("Ports"): "ports" |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | changes_list = [ "nothing", "inventory", "availability", "several", |
|---|
| 60 | "fingerprint", "ports" ] |
|---|
| 61 | |
|---|
| 62 | view_mode = { "yearly": _("Yearly View"), |
|---|
| 63 | "monthly": _("Monthly View"), |
|---|
| 64 | "daily": _("Daily View"), |
|---|
| 65 | "hourly": _("Hourly View") |
|---|
| 66 | } |
|---|
| 67 | |
|---|
| 68 | view_mode_order = ["yearly", "monthly", "daily", "hourly"] |
|---|
| 69 | |
|---|
| 70 | view_mode_descr = { "yearly": _("Year"), |
|---|
| 71 | "monthly": _("Month"), |
|---|
| 72 | "daily": _("Day"), |
|---|
| 73 | "hourly": _("Hour") |
|---|
| 74 | } |
|---|
| 75 | |
|---|
| 76 | view_kind = { "sum": _("Changes Sum"), |
|---|
| 77 | "category": _("By Category") |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | view_kind_order = ["sum", "category"] |
|---|
| 81 | |
|---|
| 82 | xlabels = { "yearly": _("Months"), |
|---|
| 83 | "monthly": _("Days"), |
|---|
| 84 | "daily": _("Hours"), |
|---|
| 85 | "hourly": _("Minutes") |
|---|
| 86 | } |
|---|
| 87 | |
|---|
| 88 | def startup_options(): |
|---|
| 89 | """ |
|---|
| 90 | Returns startup options dict. |
|---|
| 91 | """ |
|---|
| 92 | section = "Startup" |
|---|
| 93 | |
|---|
| 94 | if not configparser.has_section(section): |
|---|
| 95 | return # would be better to raise some Exception |
|---|
| 96 | |
|---|
| 97 | startup = { } |
|---|
| 98 | for opt, value in configparser.items(section): |
|---|
| 99 | startup[opt] = value |
|---|
| 100 | |
|---|
| 101 | return startup |
|---|
| 102 | |
|---|
| 103 | |
|---|
| 104 | def gradient_colors_from_file(): |
|---|
| 105 | """ |
|---|
| 106 | Retrieve gradient from timeline settings file. |
|---|
| 107 | """ |
|---|
| 108 | section = "Gradient" |
|---|
| 109 | |
|---|
| 110 | if not configparser.has_section(section): |
|---|
| 111 | return |
|---|
| 112 | |
|---|
| 113 | gradient = { } |
|---|
| 114 | for category, color in configparser.items(section): |
|---|
| 115 | ccolor = color.split(',') |
|---|
| 116 | ccolor = [float(colr) for colr in ccolor] |
|---|
| 117 | gradient[category] = ccolor |
|---|
| 118 | |
|---|
| 119 | return gradient |
|---|
| 120 | |
|---|
| 121 | |
|---|
| 122 | def colors_from_file(): |
|---|
| 123 | """ |
|---|
| 124 | Retrieve colors from timeline settings file. |
|---|
| 125 | """ |
|---|
| 126 | section = "Colors" |
|---|
| 127 | |
|---|
| 128 | if not configparser.has_section(section): |
|---|
| 129 | return # would be better to raise some Exception |
|---|
| 130 | |
|---|
| 131 | colors = { } |
|---|
| 132 | for category, color in configparser.items(section): |
|---|
| 133 | ccolor = color.split(',') |
|---|
| 134 | ccolor = [float(colr) for colr in ccolor] |
|---|
| 135 | colors[category] = ccolor |
|---|
| 136 | |
|---|
| 137 | return colors |
|---|
| 138 | |
|---|
| 139 | |
|---|
| 140 | def colors_from_file_gdk(): |
|---|
| 141 | """ |
|---|
| 142 | Retrieve colors from timeline settings file and convert to gdk format. |
|---|
| 143 | """ |
|---|
| 144 | colors = colors_from_file() |
|---|
| 145 | |
|---|
| 146 | for key, value in colors.items(): |
|---|
| 147 | colors[key] = [int(65535 * v) for v in value] |
|---|
| 148 | |
|---|
| 149 | return colors |
|---|
| 150 | |
|---|
| 151 | |
|---|
| 152 | class TLBase(CalendarManager, DataGrabber): |
|---|
| 153 | """ |
|---|
| 154 | This class does the necessary Timeline management. |
|---|
| 155 | """ |
|---|
| 156 | |
|---|
| 157 | def __init__(self, connector, inventory, hostaddr): |
|---|
| 158 | # using current date at startup |
|---|
| 159 | CalendarManager.__init__(self, **startup_calendar_opts()) |
|---|
| 160 | DataGrabber.__init__(self, self, inventory, hostaddr) |
|---|
| 161 | |
|---|
| 162 | self.connector = connector |
|---|
| 163 | self.grabber_method = None |
|---|
| 164 | self.grabber_params = None |
|---|
| 165 | self.labels = None |
|---|
| 166 | self.xlabel = None |
|---|
| 167 | |
|---|
| 168 | self.selection = -1 |
|---|
| 169 | self.sel_range = (None, None) |
|---|
| 170 | self.graph_mode = startup_options()["mode"] |
|---|
| 171 | self.graph_kind = startup_options()["kind"] |
|---|
| 172 | self.update_grabber() |
|---|
| 173 | |
|---|
| 174 | self.connector.connect('selection-changed', self._handle_selection) |
|---|
| 175 | self.connector.connect('data-changed', self._update_graph_data) |
|---|
| 176 | self.connector.connect('date-update', self._update_date) |
|---|
| 177 | |
|---|
| 178 | |
|---|
| 179 | def grab_data(self): |
|---|
| 180 | """ |
|---|
| 181 | Grab data for graph using current settings. |
|---|
| 182 | """ |
|---|
| 183 | return getattr(self, self.grabber_method)(*self.grabber_params) |
|---|
| 184 | |
|---|
| 185 | |
|---|
| 186 | def update_grabber(self): |
|---|
| 187 | """ |
|---|
| 188 | Updates grabber method, params and graph vlabels. |
|---|
| 189 | """ |
|---|
| 190 | if self.graph_kind == "sum": |
|---|
| 191 | grab_mode = self.graph_mode + "_" + self.graph_kind |
|---|
| 192 | else: |
|---|
| 193 | grab_mode = "category" |
|---|
| 194 | |
|---|
| 195 | self.grabber_method = DATA_GRAB_MODES[grab_mode] |
|---|
| 196 | |
|---|
| 197 | labels = [ ] |
|---|
| 198 | if self.graph_mode == "yearly": |
|---|
| 199 | params = (self.year, ) |
|---|
| 200 | for m in months: |
|---|
| 201 | labels.append(m[:3]) |
|---|
| 202 | |
|---|
| 203 | elif self.graph_mode == "monthly": |
|---|
| 204 | params = (self.year, self.month) |
|---|
| 205 | for i in range(self.get_current_monthrange()[1]): |
|---|
| 206 | labels.append("%d" % (i + 1)) |
|---|
| 207 | |
|---|
| 208 | elif self.graph_mode == "daily": |
|---|
| 209 | params = (self.year, self.month, self.day) |
|---|
| 210 | for i in range(24): |
|---|
| 211 | labels.append("%d" % i) |
|---|
| 212 | |
|---|
| 213 | elif self.graph_mode == "hourly": |
|---|
| 214 | params = (self.year, self.month, self.day, self.hour) |
|---|
| 215 | for i in range(60): |
|---|
| 216 | labels.append("%d" % i) |
|---|
| 217 | |
|---|
| 218 | self.grabber_params = params |
|---|
| 219 | self.labels = labels |
|---|
| 220 | self.xlabel = xlabels[self.graph_mode] |
|---|
| 221 | |
|---|
| 222 | |
|---|
| 223 | def descr_by_graphmode(self): |
|---|
| 224 | """ |
|---|
| 225 | Returns a description with graph meaning. |
|---|
| 226 | """ |
|---|
| 227 | graph_descr = [ _("end of a week"), _("end of 12 hours period"), |
|---|
| 228 | _("end of half hour period"), _("end of a minute") ] |
|---|
| 229 | |
|---|
| 230 | return _("Each point break represents ") + \ |
|---|
| 231 | graph_descr[view_mode_order.index(self.graph_mode)] |
|---|
| 232 | |
|---|
| 233 | |
|---|
| 234 | def title_by_graphmode(self, useselection=False): |
|---|
| 235 | """ |
|---|
| 236 | Returns a formatted date based on current graph mode (Yearly, |
|---|
| 237 | Monthly, .. ). |
|---|
| 238 | """ |
|---|
| 239 | def adjust(date): |
|---|
| 240 | # prepends a 0 in cases where a date is minor than 10, |
|---|
| 241 | # so (example) hour 2 displays as 02. |
|---|
| 242 | if date < 10: |
|---|
| 243 | date = "0%s" % date |
|---|
| 244 | |
|---|
| 245 | return date |
|---|
| 246 | |
|---|
| 247 | |
|---|
| 248 | if useselection and self.selection != -1: |
|---|
| 249 | fmtddate = [ |
|---|
| 250 | "%s, %s" % (monthname((self.selection + 1) % 12), |
|---|
| 251 | # % 12 above is used so we don't have |
|---|
| 252 | # problems in case self.selection > 12, |
|---|
| 253 | # that is, when someone is viewing in |
|---|
| 254 | # other mode different than "Yearly". |
|---|
| 255 | self.year), |
|---|
| 256 | |
|---|
| 257 | "%s, %s %s, %s" % (self.get_weekday(self.year, |
|---|
| 258 | self.month, (self.selection+1)%\ |
|---|
| 259 | (self.get_current_monthrange()[1]+1))[1], |
|---|
| 260 | monthname(self.month), self.selection+1, |
|---|
| 261 | self.year), |
|---|
| 262 | |
|---|
| 263 | "%s, %s %s, %s (%s:00)" % (self.get_current_weekday_name(), |
|---|
| 264 | monthname(self.month), |
|---|
| 265 | self.day, self.year, |
|---|
| 266 | adjust(self.selection % 23)), |
|---|
| 267 | |
|---|
| 268 | "%s, %s %s, %s (%s:%s)" % (self.get_current_weekday_name(), |
|---|
| 269 | monthname(self.month), self.day, |
|---|
| 270 | self.year, adjust(self.hour), |
|---|
| 271 | adjust(self.selection)) |
|---|
| 272 | ] |
|---|
| 273 | else: |
|---|
| 274 | fmtddate = [ |
|---|
| 275 | |
|---|
| 276 | _("Year %s") % self.year, |
|---|
| 277 | |
|---|
| 278 | "%s, %s" % (monthname(self.month), self.year), |
|---|
| 279 | |
|---|
| 280 | "%s, %s %s, %s" % (self.get_current_weekday_name(), |
|---|
| 281 | monthname(self.month), self.day, |
|---|
| 282 | self.year), |
|---|
| 283 | |
|---|
| 284 | "%s, %s %s, %s (%s:00)" % (self.get_current_weekday_name(), |
|---|
| 285 | monthname(self.month), |
|---|
| 286 | self.day, self.year, self.hour) |
|---|
| 287 | ] |
|---|
| 288 | |
|---|
| 289 | return fmtddate[view_mode_order.index(self.graph_mode)] |
|---|
| 290 | |
|---|
| 291 | |
|---|
| 292 | def bounds_by_graphmode(self): |
|---|
| 293 | """ |
|---|
| 294 | Return min, max and current value for graph mode. |
|---|
| 295 | """ |
|---|
| 296 | |
|---|
| 297 | values = [ (self.year_range[0], self.year_range[1], self.year), |
|---|
| 298 | (1, 12, self.month), |
|---|
| 299 | (1, self.get_current_monthrange()[1], self.day), |
|---|
| 300 | (0, 23, self.hour) |
|---|
| 301 | ] |
|---|
| 302 | |
|---|
| 303 | return values[view_mode_order.index(self.graph_mode)] |
|---|
| 304 | |
|---|
| 305 | |
|---|
| 306 | def _handle_selection(self, obj, selection): |
|---|
| 307 | """ |
|---|
| 308 | Handles TLGraph selection, so we detect the selected timerange. |
|---|
| 309 | """ |
|---|
| 310 | self.selection = selection |
|---|
| 311 | |
|---|
| 312 | if selection == -1: # deselected |
|---|
| 313 | start = end = None |
|---|
| 314 | |
|---|
| 315 | elif self.graph_mode == "yearly": # months |
|---|
| 316 | selection += 1 # months starting at 1 |
|---|
| 317 | start = datetime.datetime(self.year, selection, 1) |
|---|
| 318 | end = start + datetime.timedelta(days=self.get_monthrange(self.year, |
|---|
| 319 | selection)[1]) |
|---|
| 320 | elif self.graph_mode == "monthly": # days |
|---|
| 321 | selection += 1 # days starting at 1 |
|---|
| 322 | start = datetime.datetime(self.year, self.month, selection) |
|---|
| 323 | end = start + datetime.timedelta(days=1) |
|---|
| 324 | |
|---|
| 325 | elif self.graph_mode == "daily": # hours |
|---|
| 326 | start = datetime.datetime(self.year, self.month, self.day, |
|---|
| 327 | selection) |
|---|
| 328 | end = start + datetime.timedelta(seconds=3600) |
|---|
| 329 | |
|---|
| 330 | elif self.graph_mode == "hourly": # minutes |
|---|
| 331 | start = datetime.datetime(self.year, self.month, self.day, |
|---|
| 332 | self.hour, selection) |
|---|
| 333 | end = start + datetime.timedelta(seconds=60) |
|---|
| 334 | |
|---|
| 335 | self.sel_range = (start, end) |
|---|
| 336 | |
|---|
| 337 | self.connector.emit('selection-update', start, end) |
|---|
| 338 | |
|---|
| 339 | |
|---|
| 340 | def _update_graph_data(self, obj, *args): |
|---|
| 341 | """ |
|---|
| 342 | Received a request to perform graph data update. |
|---|
| 343 | """ |
|---|
| 344 | if args[0] and args[1]: |
|---|
| 345 | self.graph_mode = args[0] |
|---|
| 346 | self.graph_kind = args[1] |
|---|
| 347 | |
|---|
| 348 | self.update_grabber() |
|---|
| 349 | |
|---|
| 350 | glabel = self.title_by_graphmode() # graph title |
|---|
| 351 | dlabel = self.descr_by_graphmode() # graph description |
|---|
| 352 | |
|---|
| 353 | line_filter, start, evts = self.grab_data() |
|---|
| 354 | |
|---|
| 355 | self.connector.emit('data-update', line_filter, start, evts, |
|---|
| 356 | self.labels, self.xlabel, glabel, dlabel) |
|---|
| 357 | |
|---|
| 358 | |
|---|
| 359 | self.connector.emit('date-changed') |
|---|
| 360 | |
|---|
| 361 | |
|---|
| 362 | def _update_date(self, obj, arg): |
|---|
| 363 | """ |
|---|
| 364 | Update date based on current mode. |
|---|
| 365 | """ |
|---|
| 366 | if self.graph_mode == "yearly": |
|---|
| 367 | self.year = arg |
|---|
| 368 | |
|---|
| 369 | elif self.graph_mode == "monthly": |
|---|
| 370 | self.month = arg |
|---|
| 371 | |
|---|
| 372 | elif self.graph_mode == "daily": |
|---|
| 373 | self.day = arg |
|---|
| 374 | |
|---|
| 375 | elif self.graph_mode == "hourly": |
|---|
| 376 | self.hour = arg |
|---|
| 377 | |
|---|
| 378 | self.connector.emit('data-changed', None, None) |
|---|
| 379 | |
|---|