commit-bot@chromium.org | 536b15f | 2014-02-13 17:17:05 +0000 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
| 3 | """ |
| 4 | Copyright 2014 Google Inc. |
| 5 | |
| 6 | Use of this source code is governed by a BSD-style license that can be |
| 7 | found in the LICENSE file. |
| 8 | |
| 9 | ColumnHeaderFactory class (see class docstring for details) |
| 10 | """ |
| 11 | |
| 12 | # Keys used within dictionary representation of each column header. |
| 13 | KEY__HEADER_TEXT = 'headerText' |
| 14 | KEY__HEADER_URL = 'headerUrl' |
| 15 | KEY__IS_FILTERABLE = 'isFilterable' |
| 16 | KEY__IS_SORTABLE = 'isSortable' |
| 17 | KEY__VALUES_AND_COUNTS = 'valuesAndCounts' |
| 18 | |
| 19 | |
| 20 | class ColumnHeaderFactory(object): |
| 21 | """Factory which assembles the header for a single column of data.""" |
| 22 | |
| 23 | def __init__(self, header_text, header_url=None, |
| 24 | is_filterable=True, is_sortable=True, |
| 25 | include_values_and_counts=True): |
| 26 | """ |
| 27 | Args: |
| 28 | header_text: string; text the client should display within column header. |
| 29 | header_url: string; target URL if user clicks on column header. |
| 30 | If None, nothing to click on. |
| 31 | is_filterable: boolean; whether client should allow filtering on this |
| 32 | column. |
| 33 | is_sortable: boolean; whether client should allow sorting on this column. |
| 34 | include_values_and_counts: boolean; whether the set of values found |
| 35 | within this column, and their counts, should be available for the |
| 36 | client to display. |
| 37 | """ |
| 38 | self._header_text = header_text |
| 39 | self._header_url = header_url |
| 40 | self._is_filterable = is_filterable |
| 41 | self._is_sortable = is_sortable |
| 42 | self._include_values_and_counts = include_values_and_counts |
| 43 | |
| 44 | def create_as_dict(self, values_and_counts_dict=None): |
| 45 | """Creates the header for this column, in dictionary form. |
| 46 | |
| 47 | Creates the header for this column in dictionary form, as needed when |
| 48 | constructing the JSON representation. Uses the KEY__* constants as keys. |
| 49 | |
| 50 | Args: |
| 51 | values_and_counts_dict: dictionary mapping each possible column value |
| 52 | to its count (how many entries in the column have this value), or |
| 53 | None if this information is not available. |
| 54 | """ |
| 55 | asdict = { |
| 56 | KEY__HEADER_TEXT: self._header_text, |
| 57 | KEY__IS_FILTERABLE: self._is_filterable, |
| 58 | KEY__IS_SORTABLE: self._is_sortable, |
| 59 | } |
| 60 | if self._header_url: |
| 61 | asdict[KEY__HEADER_URL] = self._header_url |
| 62 | if self._include_values_and_counts and values_and_counts_dict: |
| 63 | asdict[KEY__VALUES_AND_COUNTS] = values_and_counts_dict |
| 64 | return asdict |