author | Lennard de Rijk <ljvderijk@gmail.com> |
Sat, 04 Jul 2009 14:19:39 +0200 | |
changeset 2528 | 9de8348890d8 |
parent 2373 | 05ab9393303d |
permissions | -rwxr-xr-x |
2373
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1 |
#!/usr/bin/python |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
2 |
# |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
3 |
# Copyright (C) 2009 Google Inc. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
4 |
# |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
5 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
6 |
# you may not use this file except in compliance with the License. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
7 |
# You may obtain a copy of the License at |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
8 |
# |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
9 |
# http://www.apache.org/licenses/LICENSE-2.0 |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
10 |
# |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
11 |
# Unless required by applicable law or agreed to in writing, software |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
12 |
# distributed under the License is distributed on an "AS IS" BASIS, |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
13 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
14 |
# See the License for the specific language governing permissions and |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
15 |
# limitations under the License. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
16 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
17 |
"""Converts Python data into data for Google Visualization API clients. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
18 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
19 |
This library can be used to create a google.visualization.DataTable usable by |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
20 |
visualizations built on the Google Visualization API. Output formats are raw |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
21 |
JSON, JSON response, and JavaScript. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
22 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
23 |
See http://code.google.com/apis/visualization/ for documentation on the |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
24 |
Google Visualization API. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
25 |
""" |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
26 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
27 |
__author__ = "Amit Weinstein, Misha Seltzer" |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
28 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
29 |
import cgi |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
30 |
import datetime |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
31 |
import types |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
32 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
33 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
34 |
class DataTableException(Exception): |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
35 |
"""The general exception object thrown by DataTable.""" |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
36 |
pass |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
37 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
38 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
39 |
class DataTable(object): |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
40 |
"""Wraps the data to convert to a Google Visualization API DataTable. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
41 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
42 |
Create this object, populate it with data, then call one of the ToJS... |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
43 |
methods to return a string representation of the data in the format described. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
44 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
45 |
You can clear all data from the object to reuse it, but you cannot clear |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
46 |
individual cells, rows, or columns. You also cannot modify the table schema |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
47 |
specified in the class constructor. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
48 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
49 |
You can add new data one or more rows at a time. All data added to an |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
50 |
instantiated DataTable must conform to the schema passed in to __init__(). |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
51 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
52 |
You can reorder the columns in the output table, and also specify row sorting |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
53 |
order by column. The default column order is according to the original |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
54 |
table_description parameter. Default row sort order is ascending, by column |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
55 |
1 values. For a dictionary, we sort the keys for order. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
56 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
57 |
The data and the table_description are closely tied, as described here: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
58 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
59 |
The table schema is defined in the class constructor's table_description |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
60 |
parameter. The user defines each column using a tuple of |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
61 |
(id[, type[, label[, custom_properties]]]). The default value for type is |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
62 |
string, label is the same as ID if not specified, and custom properties is |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
63 |
an empty dictionary if not specified. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
64 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
65 |
table_description is a dictionary or list, containing one or more column |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
66 |
descriptor tuples, nested dictionaries, and lists. Each dictionary key, list |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
67 |
element, or dictionary element must eventually be defined as |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
68 |
a column description tuple. Here's an example of a dictionary where the key |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
69 |
is a tuple, and the value is a list of two tuples: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
70 |
{('a', 'number'): [('b', 'number'), ('c', 'string')]} |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
71 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
72 |
This flexibility in data entry enables you to build and manipulate your data |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
73 |
in a Python structure that makes sense for your program. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
74 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
75 |
Add data to the table using the same nested design as the table's |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
76 |
table_description, replacing column descriptor tuples with cell data, and |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
77 |
each row is an element in the top level collection. This will be a bit |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
78 |
clearer after you look at the following examples showing the |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
79 |
table_description, matching data, and the resulting table: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
80 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
81 |
Columns as list of tuples [col1, col2, col3] |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
82 |
table_description: [('a', 'number'), ('b', 'string')] |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
83 |
AppendData( [[1, 'z'], [2, 'w'], [4, 'o'], [5, 'k']] ) |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
84 |
Table: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
85 |
a b <--- these are column ids/labels |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
86 |
1 z |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
87 |
2 w |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
88 |
4 o |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
89 |
5 k |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
90 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
91 |
Dictionary of columns, where key is a column, and value is a list of |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
92 |
columns {col1: [col2, col3]} |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
93 |
table_description: {('a', 'number'): [('b', 'number'), ('c', 'string')]} |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
94 |
AppendData( data: {1: [2, 'z'], 3: [4, 'w']} |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
95 |
Table: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
96 |
a b c |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
97 |
1 2 z |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
98 |
3 4 w |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
99 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
100 |
Dictionary where key is a column, and the value is itself a dictionary of |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
101 |
columns {col1: {col2, col3}} |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
102 |
table_description: {('a', 'number'): {'b': 'number', 'c': 'string'}} |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
103 |
AppendData( data: {1: {'b': 2, 'c': 'z'}, 3: {'b': 4, 'c': 'w'}} |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
104 |
Table: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
105 |
a b c |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
106 |
1 2 z |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
107 |
3 4 w |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
108 |
""" |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
109 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
110 |
def __init__(self, table_description, data=None, custom_properties=None): |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
111 |
"""Initialize the data table from a table schema and (optionally) data. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
112 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
113 |
See the class documentation for more information on table schema and data |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
114 |
values. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
115 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
116 |
Args: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
117 |
table_description: A table schema, following one of the formats described |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
118 |
in TableDescriptionParser(). Schemas describe the |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
119 |
column names, data types, and labels. See |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
120 |
TableDescriptionParser() for acceptable formats. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
121 |
data: Optional. If given, fills the table with the given data. The data |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
122 |
structure must be consistent with schema in table_description. See |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
123 |
the class documentation for more information on acceptable data. You |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
124 |
can add data later by calling AppendData(). |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
125 |
custom_properties: Optional. A dictionary from string to string that |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
126 |
goes into the table's custom properties. This can be |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
127 |
later changed by changing self.custom_properties. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
128 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
129 |
Raises: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
130 |
DataTableException: Raised if the data and the description did not match, |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
131 |
or did not use the supported formats. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
132 |
""" |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
133 |
self.__columns = self.TableDescriptionParser(table_description) |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
134 |
self.__data = [] |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
135 |
self.custom_properties = {} |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
136 |
if custom_properties is not None: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
137 |
self.custom_properties = custom_properties |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
138 |
if data: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
139 |
self.LoadData(data) |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
140 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
141 |
@staticmethod |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
142 |
def _EscapeValueForCsv(v): |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
143 |
"""Escapes the value for use in a CSV file. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
144 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
145 |
Puts the string in double-quotes, and escapes any inner double-quotes by |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
146 |
doubling them. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
147 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
148 |
Args: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
149 |
v: The value to escape. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
150 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
151 |
Returns: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
152 |
The escaped values. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
153 |
""" |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
154 |
return '"%s"' % v.replace('"', '""') |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
155 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
156 |
@staticmethod |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
157 |
def _EscapeValue(v): |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
158 |
"""Puts the string in quotes, and escapes any inner quotes and slashes.""" |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
159 |
if isinstance(v, unicode): |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
160 |
# Here we use repr as in the usual case, but on unicode strings, it |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
161 |
# also escapes the unicode characters (which we want to leave as is). |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
162 |
# So, after repr() we decode using raw-unicode-escape, which decodes |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
163 |
# only the unicode characters, and leaves all the rest (", ', \n and |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
164 |
# more) escaped. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
165 |
# We don't take the first character, because repr adds a u in the |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
166 |
# beginning of the string (usual repr output for unicode is u'...'). |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
167 |
return repr(v).decode("raw-unicode-escape")[1:] |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
168 |
# Here we use python built-in escaping mechanism for string using repr. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
169 |
return repr(str(v)) |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
170 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
171 |
@staticmethod |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
172 |
def _EscapeCustomProperties(custom_properties): |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
173 |
"""Escapes the custom properties dictionary.""" |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
174 |
l = [] |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
175 |
for key, value in custom_properties.iteritems(): |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
176 |
l.append("%s:%s" % (DataTable._EscapeValue(key), |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
177 |
DataTable._EscapeValue(value))) |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
178 |
return "{%s}" % ",".join(l) |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
179 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
180 |
@staticmethod |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
181 |
def SingleValueToJS(value, value_type, escape_func=None): |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
182 |
"""Translates a single value and type into a JS value. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
183 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
184 |
Internal helper method. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
185 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
186 |
Args: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
187 |
value: The value which should be converted |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
188 |
value_type: One of "string", "number", "boolean", "date", "datetime" or |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
189 |
"timeofday". |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
190 |
escape_func: The function to use for escaping strings. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
191 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
192 |
Returns: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
193 |
The proper JS format (as string) of the given value according to the |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
194 |
given value_type. For None, we simply return "null". |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
195 |
If a tuple is given, it should be in one of the following forms: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
196 |
- (value, formatted value) |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
197 |
- (value, formatted value, custom properties) |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
198 |
where the formatted value is a string, and custom properties is a |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
199 |
dictionary of the custom properties for this cell. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
200 |
To specify custom properties without specifying formatted value, one can |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
201 |
pass None as the formatted value. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
202 |
One can also have a null-valued cell with formatted value and/or custom |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
203 |
properties by specifying None for the value. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
204 |
This method ignores the custom properties except for checking that it is a |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
205 |
dictionary. The custom properties are handled in the ToJSon and ToJSCode |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
206 |
methods. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
207 |
The real type of the given value is not strictly checked. For example, |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
208 |
any type can be used for string - as we simply take its str( ) and for |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
209 |
boolean value we just check "if value". |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
210 |
Examples: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
211 |
SingleValueToJS(None, "boolean") returns "null" |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
212 |
SingleValueToJS(False, "boolean") returns "false" |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
213 |
SingleValueToJS((5, "5$"), "number") returns ("5", "'5$'") |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
214 |
SingleValueToJS((None, "5$"), "number") returns ("null", "'5$'") |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
215 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
216 |
Raises: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
217 |
DataTableException: The value and type did not match in a not-recoverable |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
218 |
way, for example given value 'abc' for type 'number'. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
219 |
""" |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
220 |
if escape_func is None: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
221 |
escape_func = DataTable._EscapeValue |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
222 |
if isinstance(value, tuple): |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
223 |
# In case of a tuple, we run the same function on the value itself and |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
224 |
# add the formatted value. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
225 |
if (len(value) not in [2, 3] or |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
226 |
(len(value) == 3 and not isinstance(value[2], dict))): |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
227 |
raise DataTableException("Wrong format for value and formatting - %s." % |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
228 |
str(value)) |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
229 |
if not isinstance(value[1], types.StringTypes + (types.NoneType,)): |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
230 |
raise DataTableException("Formatted value is not string, given %s." % |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
231 |
type(value[1])) |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
232 |
js_value = DataTable.SingleValueToJS(value[0], value_type) |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
233 |
if value[1] is None: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
234 |
return (js_value, None) |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
235 |
return (js_value, escape_func(value[1])) |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
236 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
237 |
# The standard case - no formatting. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
238 |
t_value = type(value) |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
239 |
if value is None: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
240 |
return "null" |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
241 |
if value_type == "boolean": |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
242 |
if value: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
243 |
return "true" |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
244 |
return "false" |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
245 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
246 |
elif value_type == "number": |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
247 |
if isinstance(value, (int, long, float)): |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
248 |
return str(value) |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
249 |
raise DataTableException("Wrong type %s when expected number" % t_value) |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
250 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
251 |
elif value_type == "string": |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
252 |
if isinstance(value, tuple): |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
253 |
raise DataTableException("Tuple is not allowed as string value.") |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
254 |
return escape_func(value) |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
255 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
256 |
elif value_type == "date": |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
257 |
if not isinstance(value, (datetime.date, datetime.datetime)): |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
258 |
raise DataTableException("Wrong type %s when expected date" % t_value) |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
259 |
# We need to shift the month by 1 to match JS Date format |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
260 |
return "new Date(%d,%d,%d)" % (value.year, value.month - 1, value.day) |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
261 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
262 |
elif value_type == "timeofday": |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
263 |
if not isinstance(value, (datetime.time, datetime.datetime)): |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
264 |
raise DataTableException("Wrong type %s when expected time" % t_value) |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
265 |
return "[%d,%d,%d]" % (value.hour, value.minute, value.second) |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
266 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
267 |
elif value_type == "datetime": |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
268 |
if not isinstance(value, datetime.datetime): |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
269 |
raise DataTableException("Wrong type %s when expected datetime" % |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
270 |
t_value) |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
271 |
return "new Date(%d,%d,%d,%d,%d,%d)" % (value.year, |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
272 |
value.month - 1, # To match JS |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
273 |
value.day, |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
274 |
value.hour, |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
275 |
value.minute, |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
276 |
value.second) |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
277 |
# If we got here, it means the given value_type was not one of the |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
278 |
# supported types. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
279 |
raise DataTableException("Unsupported type %s" % value_type) |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
280 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
281 |
@staticmethod |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
282 |
def ColumnTypeParser(description): |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
283 |
"""Parses a single column description. Internal helper method. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
284 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
285 |
Args: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
286 |
description: a column description in the possible formats: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
287 |
'id' |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
288 |
('id',) |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
289 |
('id', 'type') |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
290 |
('id', 'type', 'label') |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
291 |
('id', 'type', 'label', {'custom_prop1': 'custom_val1'}) |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
292 |
Returns: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
293 |
Dictionary with the following keys: id, label, type, and |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
294 |
custom_properties where: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
295 |
- If label not given, it equals the id. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
296 |
- If type not given, string is used by default. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
297 |
- If custom properties are not given, an empty dictionary is used by |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
298 |
default. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
299 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
300 |
Raises: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
301 |
DataTableException: The column description did not match the RE. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
302 |
""" |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
303 |
if not description: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
304 |
raise DataTableException("Description error: empty description given") |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
305 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
306 |
if not isinstance(description, (types.StringTypes, tuple)): |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
307 |
raise DataTableException("Description error: expected either string or " |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
308 |
"tuple, got %s." % type(description)) |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
309 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
310 |
if isinstance(description, types.StringTypes): |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
311 |
description = (description,) |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
312 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
313 |
# According to the tuple's length, we fill the keys |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
314 |
# We verify everything is of type string |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
315 |
for elem in description[:3]: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
316 |
if not isinstance(elem, types.StringTypes): |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
317 |
raise DataTableException("Description error: expected tuple of " |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
318 |
"strings, current element of type %s." % |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
319 |
type(elem)) |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
320 |
desc_dict = {"id": description[0], |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
321 |
"label": description[0], |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
322 |
"type": "string", |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
323 |
"custom_properties": {}} |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
324 |
if len(description) > 1: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
325 |
desc_dict["type"] = description[1].lower() |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
326 |
if len(description) > 2: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
327 |
desc_dict["label"] = description[2] |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
328 |
if len(description) > 3: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
329 |
if not isinstance(description[3], dict): |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
330 |
raise DataTableException("Description error: expected custom " |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
331 |
"properties of type dict, current element " |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
332 |
"of type %s." % type(description[3])) |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
333 |
desc_dict["custom_properties"] = description[3] |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
334 |
if len(description) > 4: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
335 |
raise DataTableException("Description error: tuple of length > 4") |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
336 |
return desc_dict |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
337 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
338 |
@staticmethod |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
339 |
def TableDescriptionParser(table_description, depth=0): |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
340 |
"""Parses the table_description object for internal use. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
341 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
342 |
Parses the user-submitted table description into an internal format used |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
343 |
by the Python DataTable class. Returns the flat list of parsed columns. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
344 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
345 |
Args: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
346 |
table_description: A description of the table which should comply |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
347 |
with one of the formats described below. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
348 |
depth: Optional. The depth of the first level in the current description. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
349 |
Used by recursive calls to this function. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
350 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
351 |
Returns: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
352 |
List of columns, where each column represented by a dictionary with the |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
353 |
keys: id, label, type, depth, container which means the following: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
354 |
- id: the id of the column |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
355 |
- name: The name of the column |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
356 |
- type: The datatype of the elements in this column. Allowed types are |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
357 |
described in ColumnTypeParser(). |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
358 |
- depth: The depth of this column in the table description |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
359 |
- container: 'dict', 'iter' or 'scalar' for parsing the format easily. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
360 |
- custom_properties: The custom properties for this column. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
361 |
The returned description is flattened regardless of how it was given. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
362 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
363 |
Raises: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
364 |
DataTableException: Error in a column description or in the description |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
365 |
structure. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
366 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
367 |
Examples: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
368 |
A column description can be of the following forms: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
369 |
'id' |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
370 |
('id',) |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
371 |
('id', 'type') |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
372 |
('id', 'type', 'label') |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
373 |
('id', 'type', 'label', {'custom_prop1': 'custom_val1'}) |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
374 |
or as a dictionary: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
375 |
'id': 'type' |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
376 |
'id': ('type',) |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
377 |
'id': ('type', 'label') |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
378 |
'id': ('type', 'label', {'custom_prop1': 'custom_val1'}) |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
379 |
If the type is not specified, we treat it as string. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
380 |
If no specific label is given, the label is simply the id. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
381 |
If no custom properties are given, we use an empty dictionary. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
382 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
383 |
input: [('a', 'date'), ('b', 'timeofday', 'b', {'foo': 'bar'})] |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
384 |
output: [{'id': 'a', 'label': 'a', 'type': 'date', |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
385 |
'depth': 0, 'container': 'iter', 'custom_properties': {}}, |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
386 |
{'id': 'b', 'label': 'b', 'type': 'timeofday', |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
387 |
'depth': 0, 'container': 'iter', |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
388 |
'custom_properties': {'foo': 'bar'}}] |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
389 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
390 |
input: {'a': [('b', 'number'), ('c', 'string', 'column c')]} |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
391 |
output: [{'id': 'a', 'label': 'a', 'type': 'string', |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
392 |
'depth': 0, 'container': 'dict', 'custom_properties': {}}, |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
393 |
{'id': 'b', 'label': 'b', 'type': 'number', |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
394 |
'depth': 1, 'container': 'iter', 'custom_properties': {}}, |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
395 |
{'id': 'c', 'label': 'column c', 'type': 'string', |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
396 |
'depth': 1, 'container': 'iter', 'custom_properties': {}}] |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
397 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
398 |
input: {('a', 'number', 'column a'): { 'b': 'number', 'c': 'string'}} |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
399 |
output: [{'id': 'a', 'label': 'column a', 'type': 'number', |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
400 |
'depth': 0, 'container': 'dict', 'custom_properties': {}}, |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
401 |
{'id': 'b', 'label': 'b', 'type': 'number', |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
402 |
'depth': 1, 'container': 'dict', 'custom_properties': {}}, |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
403 |
{'id': 'c', 'label': 'c', 'type': 'string', |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
404 |
'depth': 1, 'container': 'dict', 'custom_properties': {}}] |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
405 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
406 |
input: { ('w', 'string', 'word'): ('c', 'number', 'count') } |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
407 |
output: [{'id': 'w', 'label': 'word', 'type': 'string', |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
408 |
'depth': 0, 'container': 'dict', 'custom_properties': {}}, |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
409 |
{'id': 'c', 'label': 'count', 'type': 'number', |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
410 |
'depth': 1, 'container': 'scalar', 'custom_properties': {}}] |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
411 |
""" |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
412 |
# For the recursion step, we check for a scalar object (string or tuple) |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
413 |
if isinstance(table_description, (types.StringTypes, tuple)): |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
414 |
parsed_col = DataTable.ColumnTypeParser(table_description) |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
415 |
parsed_col["depth"] = depth |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
416 |
parsed_col["container"] = "scalar" |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
417 |
return [parsed_col] |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
418 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
419 |
# Since it is not scalar, table_description must be iterable. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
420 |
if not hasattr(table_description, "__iter__"): |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
421 |
raise DataTableException("Expected an iterable object, got %s" % |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
422 |
type(table_description)) |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
423 |
if not isinstance(table_description, dict): |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
424 |
# We expects a non-dictionary iterable item. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
425 |
columns = [] |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
426 |
for desc in table_description: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
427 |
parsed_col = DataTable.ColumnTypeParser(desc) |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
428 |
parsed_col["depth"] = depth |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
429 |
parsed_col["container"] = "iter" |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
430 |
columns.append(parsed_col) |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
431 |
if not columns: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
432 |
raise DataTableException("Description iterable objects should not" |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
433 |
" be empty.") |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
434 |
return columns |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
435 |
# The other case is a dictionary |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
436 |
if not table_description: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
437 |
raise DataTableException("Empty dictionaries are not allowed inside" |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
438 |
" description") |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
439 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
440 |
# The number of keys in the dictionary separates between the two cases of |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
441 |
# more levels below or this is the most inner dictionary. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
442 |
if len(table_description) != 1: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
443 |
# This is the most inner dictionary. Parsing types. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
444 |
columns = [] |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
445 |
# We sort the items, equivalent to sort the keys since they are unique |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
446 |
for key, value in sorted(table_description.items()): |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
447 |
# We parse the column type as (key, type) or (key, type, label) using |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
448 |
# ColumnTypeParser. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
449 |
if isinstance(value, tuple): |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
450 |
parsed_col = DataTable.ColumnTypeParser((key,) + value) |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
451 |
else: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
452 |
parsed_col = DataTable.ColumnTypeParser((key, value)) |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
453 |
parsed_col["depth"] = depth |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
454 |
parsed_col["container"] = "dict" |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
455 |
columns.append(parsed_col) |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
456 |
return columns |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
457 |
# This is an outer dictionary, must have at most one key. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
458 |
parsed_col = DataTable.ColumnTypeParser(table_description.keys()[0]) |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
459 |
parsed_col["depth"] = depth |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
460 |
parsed_col["container"] = "dict" |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
461 |
return ([parsed_col] + |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
462 |
DataTable.TableDescriptionParser(table_description.values()[0], |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
463 |
depth=depth + 1)) |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
464 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
465 |
@property |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
466 |
def columns(self): |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
467 |
"""Returns the parsed table description.""" |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
468 |
return self.__columns |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
469 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
470 |
def NumberOfRows(self): |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
471 |
"""Returns the number of rows in the current data stored in the table.""" |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
472 |
return len(self.__data) |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
473 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
474 |
def SetRowsCustomProperties(self, rows, custom_properties): |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
475 |
"""Sets the custom properties for given row(s). |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
476 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
477 |
Can accept a single row or an iterable of rows. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
478 |
Sets the given custom properties for all specified rows. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
479 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
480 |
Args: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
481 |
rows: The row, or rows, to set the custom properties for. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
482 |
custom_properties: A string to string dictionary of custom properties to |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
483 |
set for all rows. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
484 |
""" |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
485 |
if not hasattr(rows, "__iter__"): |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
486 |
rows = [rows] |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
487 |
for row in rows: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
488 |
self.__data[row] = (self.__data[row][0], custom_properties) |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
489 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
490 |
def LoadData(self, data, custom_properties=None): |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
491 |
"""Loads new rows to the data table, clearing existing rows. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
492 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
493 |
May also set the custom_properties for the added rows. The given custom |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
494 |
properties dictionary specifies the dictionary that will be used for *all* |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
495 |
given rows. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
496 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
497 |
Args: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
498 |
data: The rows that the table will contain. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
499 |
custom_properties: A dictionary of string to string to set as the custom |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
500 |
properties for all rows. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
501 |
""" |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
502 |
self.__data = [] |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
503 |
self.AppendData(data, custom_properties) |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
504 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
505 |
def AppendData(self, data, custom_properties=None): |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
506 |
"""Appends new data to the table. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
507 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
508 |
Data is appended in rows. Data must comply with |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
509 |
the table schema passed in to __init__(). See SingleValueToJS() for a list |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
510 |
of acceptable data types. See the class documentation for more information |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
511 |
and examples of schema and data values. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
512 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
513 |
Args: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
514 |
data: The row to add to the table. The data must conform to the table |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
515 |
description format. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
516 |
custom_properties: A dictionary of string to string, representing the |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
517 |
custom properties to add to all the rows. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
518 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
519 |
Raises: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
520 |
DataTableException: The data structure does not match the description. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
521 |
""" |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
522 |
# If the maximal depth is 0, we simply iterate over the data table |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
523 |
# lines and insert them using _InnerAppendData. Otherwise, we simply |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
524 |
# let the _InnerAppendData handle all the levels. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
525 |
if not self.__columns[-1]["depth"]: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
526 |
for row in data: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
527 |
self._InnerAppendData(({}, custom_properties), row, 0) |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
528 |
else: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
529 |
self._InnerAppendData(({}, custom_properties), data, 0) |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
530 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
531 |
def _InnerAppendData(self, prev_col_values, data, col_index): |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
532 |
"""Inner function to assist LoadData.""" |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
533 |
# We first check that col_index has not exceeded the columns size |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
534 |
if col_index >= len(self.__columns): |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
535 |
raise DataTableException("The data does not match description, too deep") |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
536 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
537 |
# Dealing with the scalar case, the data is the last value. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
538 |
if self.__columns[col_index]["container"] == "scalar": |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
539 |
prev_col_values[0][self.__columns[col_index]["id"]] = data |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
540 |
self.__data.append(prev_col_values) |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
541 |
return |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
542 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
543 |
if self.__columns[col_index]["container"] == "iter": |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
544 |
if not hasattr(data, "__iter__") or isinstance(data, dict): |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
545 |
raise DataTableException("Expected iterable object, got %s" % |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
546 |
type(data)) |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
547 |
# We only need to insert the rest of the columns |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
548 |
# If there are less items than expected, we only add what there is. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
549 |
for value in data: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
550 |
if col_index >= len(self.__columns): |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
551 |
raise DataTableException("Too many elements given in data") |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
552 |
prev_col_values[0][self.__columns[col_index]["id"]] = value |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
553 |
col_index += 1 |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
554 |
self.__data.append(prev_col_values) |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
555 |
return |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
556 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
557 |
# We know the current level is a dictionary, we verify the type. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
558 |
if not isinstance(data, dict): |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
559 |
raise DataTableException("Expected dictionary at current level, got %s" % |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
560 |
type(data)) |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
561 |
# We check if this is the last level |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
562 |
if self.__columns[col_index]["depth"] == self.__columns[-1]["depth"]: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
563 |
# We need to add the keys in the dictionary as they are |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
564 |
for col in self.__columns[col_index:]: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
565 |
if col["id"] in data: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
566 |
prev_col_values[0][col["id"]] = data[col["id"]] |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
567 |
self.__data.append(prev_col_values) |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
568 |
return |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
569 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
570 |
# We have a dictionary in an inner depth level. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
571 |
if not data.keys(): |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
572 |
# In case this is an empty dictionary, we add a record with the columns |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
573 |
# filled only until this point. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
574 |
self.__data.append(prev_col_values) |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
575 |
else: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
576 |
for key in sorted(data): |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
577 |
col_values = dict(prev_col_values[0]) |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
578 |
col_values[self.__columns[col_index]["id"]] = key |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
579 |
self._InnerAppendData((col_values, prev_col_values[1]), |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
580 |
data[key], col_index + 1) |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
581 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
582 |
def _PreparedData(self, order_by=()): |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
583 |
"""Prepares the data for enumeration - sorting it by order_by. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
584 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
585 |
Args: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
586 |
order_by: Optional. Specifies the name of the column(s) to sort by, and |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
587 |
(optionally) which direction to sort in. Default sort direction |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
588 |
is asc. Following formats are accepted: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
589 |
"string_col_name" -- For a single key in default (asc) order. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
590 |
("string_col_name", "asc|desc") -- For a single key. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
591 |
[("col_1","asc|desc"), ("col_2","asc|desc")] -- For more than |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
592 |
one column, an array of tuples of (col_name, "asc|desc"). |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
593 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
594 |
Returns: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
595 |
The data sorted by the keys given. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
596 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
597 |
Raises: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
598 |
DataTableException: Sort direction not in 'asc' or 'desc' |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
599 |
""" |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
600 |
if not order_by: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
601 |
return self.__data |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
602 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
603 |
proper_sort_keys = [] |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
604 |
if isinstance(order_by, types.StringTypes) or ( |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
605 |
isinstance(order_by, tuple) and len(order_by) == 2 and |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
606 |
order_by[1].lower() in ["asc", "desc"]): |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
607 |
order_by = (order_by,) |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
608 |
for key in order_by: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
609 |
if isinstance(key, types.StringTypes): |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
610 |
proper_sort_keys.append((key, 1)) |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
611 |
elif (isinstance(key, (list, tuple)) and len(key) == 2 and |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
612 |
key[1].lower() in ("asc", "desc")): |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
613 |
proper_sort_keys.append((key[0], key[1].lower() == "asc" and 1 or -1)) |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
614 |
else: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
615 |
raise DataTableException("Expected tuple with second value: " |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
616 |
"'asc' or 'desc'") |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
617 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
618 |
def SortCmpFunc(row1, row2): |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
619 |
"""cmp function for sorted. Compares by keys and 'asc'/'desc' keywords.""" |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
620 |
for key, asc_mult in proper_sort_keys: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
621 |
cmp_result = asc_mult * cmp(row1[0].get(key), row2[0].get(key)) |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
622 |
if cmp_result: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
623 |
return cmp_result |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
624 |
return 0 |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
625 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
626 |
return sorted(self.__data, cmp=SortCmpFunc) |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
627 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
628 |
def ToJSCode(self, name, columns_order=None, order_by=()): |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
629 |
"""Writes the data table as a JS code string. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
630 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
631 |
This method writes a string of JS code that can be run to |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
632 |
generate a DataTable with the specified data. Typically used for debugging |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
633 |
only. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
634 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
635 |
Args: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
636 |
name: The name of the table. The name would be used as the DataTable's |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
637 |
variable name in the created JS code. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
638 |
columns_order: Optional. Specifies the order of columns in the |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
639 |
output table. Specify a list of all column IDs in the order |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
640 |
in which you want the table created. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
641 |
Note that you must list all column IDs in this parameter, |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
642 |
if you use it. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
643 |
order_by: Optional. Specifies the name of the column(s) to sort by. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
644 |
Passed as is to _PreparedData. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
645 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
646 |
Returns: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
647 |
A string of JS code that, when run, generates a DataTable with the given |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
648 |
name and the data stored in the DataTable object. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
649 |
Example result: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
650 |
"var tab1 = new google.visualization.DataTable(); |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
651 |
tab1.addColumn('string', 'a', 'a'); |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
652 |
tab1.addColumn('number', 'b', 'b'); |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
653 |
tab1.addColumn('boolean', 'c', 'c'); |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
654 |
tab1.addRows(10); |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
655 |
tab1.setCell(0, 0, 'a'); |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
656 |
tab1.setCell(0, 1, 1, null, {'foo': 'bar'}); |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
657 |
tab1.setCell(0, 2, true); |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
658 |
... |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
659 |
tab1.setCell(9, 0, 'c'); |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
660 |
tab1.setCell(9, 1, 3, '3$'); |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
661 |
tab1.setCell(9, 2, false);" |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
662 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
663 |
Raises: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
664 |
DataTableException: The data does not match the type. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
665 |
""" |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
666 |
if columns_order is None: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
667 |
columns_order = [col["id"] for col in self.__columns] |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
668 |
col_dict = dict([(col["id"], col) for col in self.__columns]) |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
669 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
670 |
# We first create the table with the given name |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
671 |
jscode = "var %s = new google.visualization.DataTable();\n" % name |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
672 |
if self.custom_properties: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
673 |
jscode += "%s.setTableProperties(%s);\n" % ( |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
674 |
name, DataTable._EscapeCustomProperties(self.custom_properties)) |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
675 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
676 |
# We add the columns to the table |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
677 |
for i, col in enumerate(columns_order): |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
678 |
jscode += "%s.addColumn('%s', '%s', '%s');\n" % (name, |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
679 |
col_dict[col]["type"], |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
680 |
col_dict[col]["label"], |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
681 |
col_dict[col]["id"]) |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
682 |
if col_dict[col]["custom_properties"]: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
683 |
jscode += "%s.setColumnProperties(%d, %s);\n" % ( |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
684 |
name, i, DataTable._EscapeCustomProperties( |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
685 |
col_dict[col]["custom_properties"])) |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
686 |
jscode += "%s.addRows(%d);\n" % (name, len(self.__data)) |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
687 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
688 |
# We now go over the data and add each row |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
689 |
for (i, (row, cp)) in enumerate(self._PreparedData(order_by)): |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
690 |
# We add all the elements of this row by their order |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
691 |
for (j, col) in enumerate(columns_order): |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
692 |
if col not in row or row[col] is None: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
693 |
continue |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
694 |
cell_cp = "" |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
695 |
if isinstance(row[col], tuple) and len(row[col]) == 3: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
696 |
cell_cp = ", %s" % DataTable._EscapeCustomProperties(row[col][2]) |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
697 |
value = self.SingleValueToJS(row[col], col_dict[col]["type"]) |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
698 |
if isinstance(value, tuple): |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
699 |
# We have a formatted value or custom property as well |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
700 |
if value[1] is None: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
701 |
value = (value[0], "null") |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
702 |
jscode += ("%s.setCell(%d, %d, %s, %s%s);\n" % |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
703 |
(name, i, j, value[0], value[1], cell_cp)) |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
704 |
else: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
705 |
jscode += "%s.setCell(%d, %d, %s);\n" % (name, i, j, value) |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
706 |
if cp: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
707 |
jscode += "%s.setRowProperties(%d, %s);\n" % ( |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
708 |
name, i, DataTable._EscapeCustomProperties(cp)) |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
709 |
return jscode |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
710 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
711 |
def ToHtml(self, columns_order=None, order_by=()): |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
712 |
"""Writes the data table as an HTML table code string. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
713 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
714 |
Args: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
715 |
columns_order: Optional. Specifies the order of columns in the |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
716 |
output table. Specify a list of all column IDs in the order |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
717 |
in which you want the table created. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
718 |
Note that you must list all column IDs in this parameter, |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
719 |
if you use it. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
720 |
order_by: Optional. Specifies the name of the column(s) to sort by. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
721 |
Passed as is to _PreparedData. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
722 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
723 |
Returns: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
724 |
An HTML table code string. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
725 |
Example result (the result is without the newlines): |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
726 |
<html><body><table border='1'> |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
727 |
<thead><tr><th>a</th><th>b</th><th>c</th></tr></thead> |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
728 |
<tbody> |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
729 |
<tr><td>1</td><td>"z"</td><td>2</td></tr> |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
730 |
<tr><td>"3$"</td><td>"w"</td><td></td></tr> |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
731 |
</tbody> |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
732 |
</table></body></html> |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
733 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
734 |
Raises: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
735 |
DataTableException: The data does not match the type. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
736 |
""" |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
737 |
table_template = "<html><body><table border='1'>%s</table></body></html>" |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
738 |
columns_template = "<thead><tr>%s</tr></thead>" |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
739 |
rows_template = "<tbody>%s</tbody>" |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
740 |
row_template = "<tr>%s</tr>" |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
741 |
header_cell_template = "<th>%s</th>" |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
742 |
cell_template = "<td>%s</td>" |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
743 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
744 |
if columns_order is None: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
745 |
columns_order = [col["id"] for col in self.__columns] |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
746 |
col_dict = dict([(col["id"], col) for col in self.__columns]) |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
747 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
748 |
columns_list = [] |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
749 |
for col in columns_order: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
750 |
columns_list.append(header_cell_template % col_dict[col]["label"]) |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
751 |
columns_html = columns_template % "".join(columns_list) |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
752 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
753 |
rows_list = [] |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
754 |
# We now go over the data and add each row |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
755 |
for row, unused_cp in self._PreparedData(order_by): |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
756 |
cells_list = [] |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
757 |
# We add all the elements of this row by their order |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
758 |
for col in columns_order: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
759 |
# For empty string we want empty quotes (""). |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
760 |
value = "" |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
761 |
if col in row and row[col] is not None: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
762 |
value = self.SingleValueToJS(row[col], col_dict[col]["type"]) |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
763 |
if isinstance(value, tuple): |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
764 |
# We have a formatted value and we're going to use it |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
765 |
cells_list.append(cell_template % cgi.escape(value[1])) |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
766 |
else: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
767 |
cells_list.append(cell_template % cgi.escape(value)) |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
768 |
rows_list.append(row_template % "".join(cells_list)) |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
769 |
rows_html = rows_template % "".join(rows_list) |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
770 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
771 |
return table_template % (columns_html + rows_html) |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
772 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
773 |
def ToCsv(self, columns_order=None, order_by=(), separator=", "): |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
774 |
"""Writes the data table as a CSV string. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
775 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
776 |
Args: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
777 |
columns_order: Optional. Specifies the order of columns in the |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
778 |
output table. Specify a list of all column IDs in the order |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
779 |
in which you want the table created. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
780 |
Note that you must list all column IDs in this parameter, |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
781 |
if you use it. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
782 |
order_by: Optional. Specifies the name of the column(s) to sort by. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
783 |
Passed as is to _PreparedData. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
784 |
separator: Optional. The separator to use between the values. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
785 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
786 |
Returns: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
787 |
A CSV string representing the table. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
788 |
Example result: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
789 |
'a', 'b', 'c' |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
790 |
1, 'z', 2 |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
791 |
3, 'w', '' |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
792 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
793 |
Raises: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
794 |
DataTableException: The data does not match the type. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
795 |
""" |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
796 |
if columns_order is None: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
797 |
columns_order = [col["id"] for col in self.__columns] |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
798 |
col_dict = dict([(col["id"], col) for col in self.__columns]) |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
799 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
800 |
columns_list = [] |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
801 |
for col in columns_order: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
802 |
columns_list.append(DataTable._EscapeValueForCsv(col_dict[col]["label"])) |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
803 |
columns_line = separator.join(columns_list) |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
804 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
805 |
rows_list = [] |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
806 |
# We now go over the data and add each row |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
807 |
for row, unused_cp in self._PreparedData(order_by): |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
808 |
cells_list = [] |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
809 |
# We add all the elements of this row by their order |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
810 |
for col in columns_order: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
811 |
value = '""' |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
812 |
if col in row and row[col] is not None: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
813 |
value = self.SingleValueToJS(row[col], col_dict[col]["type"], |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
814 |
DataTable._EscapeValueForCsv) |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
815 |
if isinstance(value, tuple): |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
816 |
# We have a formatted value. Using it only for date/time types. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
817 |
if col_dict[col]["type"] in ["date", "datetime", "timeofday"]: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
818 |
cells_list.append(value[1]) |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
819 |
else: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
820 |
cells_list.append(value[0]) |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
821 |
else: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
822 |
# We need to quote date types, because they contain commas. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
823 |
if (col_dict[col]["type"] in ["date", "datetime", "timeofday"] and |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
824 |
value != '""'): |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
825 |
value = '"%s"' % value |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
826 |
cells_list.append(value) |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
827 |
rows_list.append(separator.join(cells_list)) |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
828 |
rows = "\n".join(rows_list) |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
829 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
830 |
return "%s\n%s" % (columns_line, rows) |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
831 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
832 |
def ToTsvExcel(self, columns_order=None, order_by=()): |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
833 |
"""Returns a file in tab-separated-format readable by MS Excel. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
834 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
835 |
Returns a file in UTF-16 little endian encoding, with tabs separating the |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
836 |
values. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
837 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
838 |
Args: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
839 |
columns_order: Delegated to ToCsv. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
840 |
order_by: Delegated to ToCsv. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
841 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
842 |
Returns: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
843 |
A tab-separated little endian UTF16 file representing the table. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
844 |
""" |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
845 |
return self.ToCsv( |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
846 |
columns_order, order_by, separator="\t").encode("UTF-16LE") |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
847 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
848 |
def ToJSon(self, columns_order=None, order_by=()): |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
849 |
"""Writes a JSON string that can be used in a JS DataTable constructor. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
850 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
851 |
This method writes a JSON string that can be passed directly into a Google |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
852 |
Visualization API DataTable constructor. Use this output if you are |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
853 |
hosting the visualization HTML on your site, and want to code the data |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
854 |
table in Python. Pass this string into the |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
855 |
google.visualization.DataTable constructor, e.g,: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
856 |
... on my page that hosts my visualization ... |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
857 |
google.setOnLoadCallback(drawTable); |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
858 |
function drawTable() { |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
859 |
var data = new google.visualization.DataTable(_my_JSon_string, 0.6); |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
860 |
myTable.draw(data); |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
861 |
} |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
862 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
863 |
Args: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
864 |
columns_order: Optional. Specifies the order of columns in the |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
865 |
output table. Specify a list of all column IDs in the order |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
866 |
in which you want the table created. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
867 |
Note that you must list all column IDs in this parameter, |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
868 |
if you use it. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
869 |
order_by: Optional. Specifies the name of the column(s) to sort by. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
870 |
Passed as is to _PreparedData(). |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
871 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
872 |
Returns: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
873 |
A JSon constructor string to generate a JS DataTable with the data |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
874 |
stored in the DataTable object. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
875 |
Example result (the result is without the newlines): |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
876 |
{cols: [{id:'a',label:'a',type:'number'}, |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
877 |
{id:'b',label:'b',type:'string'}, |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
878 |
{id:'c',label:'c',type:'number'}], |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
879 |
rows: [{c:[{v:1},{v:'z'},{v:2}]}, c:{[{v:3,f:'3$'},{v:'w'},{v:null}]}], |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
880 |
p: {'foo': 'bar'}} |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
881 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
882 |
Raises: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
883 |
DataTableException: The data does not match the type. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
884 |
""" |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
885 |
if columns_order is None: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
886 |
columns_order = [col["id"] for col in self.__columns] |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
887 |
col_dict = dict([(col["id"], col) for col in self.__columns]) |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
888 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
889 |
# Creating the columns jsons |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
890 |
cols_jsons = [] |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
891 |
for col_id in columns_order: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
892 |
d = dict(col_dict[col_id]) |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
893 |
d["cp"] = "" |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
894 |
if col_dict[col_id]["custom_properties"]: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
895 |
d["cp"] = ",p:%s" % DataTable._EscapeCustomProperties( |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
896 |
col_dict[col_id]["custom_properties"]) |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
897 |
cols_jsons.append( |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
898 |
"{id:'%(id)s',label:'%(label)s',type:'%(type)s'%(cp)s}" % d) |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
899 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
900 |
# Creating the rows jsons |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
901 |
rows_jsons = [] |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
902 |
for row, cp in self._PreparedData(order_by): |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
903 |
cells_jsons = [] |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
904 |
for col in columns_order: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
905 |
# We omit the {v:null} for a None value of the not last column |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
906 |
value = row.get(col, None) |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
907 |
if value is None and col != columns_order[-1]: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
908 |
cells_jsons.append("") |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
909 |
else: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
910 |
value = self.SingleValueToJS(value, col_dict[col]["type"]) |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
911 |
if isinstance(value, tuple): |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
912 |
# We have a formatted value or custom property as well |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
913 |
if len(row.get(col)) == 3: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
914 |
if value[1] is None: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
915 |
cells_jsons.append("{v:%s,p:%s}" % ( |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
916 |
value[0], |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
917 |
DataTable._EscapeCustomProperties(row.get(col)[2]))) |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
918 |
else: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
919 |
cells_jsons.append("{v:%s,f:%s,p:%s}" % (value + ( |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
920 |
DataTable._EscapeCustomProperties(row.get(col)[2]),))) |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
921 |
else: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
922 |
cells_jsons.append("{v:%s,f:%s}" % value) |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
923 |
else: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
924 |
cells_jsons.append("{v:%s}" % value) |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
925 |
if cp: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
926 |
rows_jsons.append("{c:[%s],p:%s}" % ( |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
927 |
",".join(cells_jsons), DataTable._EscapeCustomProperties(cp))) |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
928 |
else: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
929 |
rows_jsons.append("{c:[%s]}" % ",".join(cells_jsons)) |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
930 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
931 |
general_custom_properties = "" |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
932 |
if self.custom_properties: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
933 |
general_custom_properties = ( |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
934 |
",p:%s" % DataTable._EscapeCustomProperties(self.custom_properties)) |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
935 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
936 |
# We now join the columns jsons and the rows jsons |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
937 |
json = "{cols:[%s],rows:[%s]%s}" % (",".join(cols_jsons), |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
938 |
",".join(rows_jsons), |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
939 |
general_custom_properties) |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
940 |
return json |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
941 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
942 |
def ToJSonResponse(self, columns_order=None, order_by=(), req_id=0, |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
943 |
response_handler="google.visualization.Query.setResponse"): |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
944 |
"""Writes a table as a JSON response that can be returned as-is to a client. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
945 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
946 |
This method writes a JSON response to return to a client in response to a |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
947 |
Google Visualization API query. This string can be processed by the calling |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
948 |
page, and is used to deliver a data table to a visualization hosted on |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
949 |
a different page. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
950 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
951 |
Args: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
952 |
columns_order: Optional. Passed straight to self.ToJSon(). |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
953 |
order_by: Optional. Passed straight to self.ToJSon(). |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
954 |
req_id: Optional. The response id, as retrieved by the request. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
955 |
response_handler: Optional. The response handler, as retrieved by the |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
956 |
request. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
957 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
958 |
Returns: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
959 |
A JSON response string to be received by JS the visualization Query |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
960 |
object. This response would be translated into a DataTable on the |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
961 |
client side. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
962 |
Example result (newlines added for readability): |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
963 |
google.visualization.Query.setResponse({ |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
964 |
'version':'0.6', 'reqId':'0', 'status':'OK', |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
965 |
'table': {cols: [...], rows: [...]}}); |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
966 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
967 |
Note: The URL returning this string can be used as a data source by Google |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
968 |
Visualization Gadgets or from JS code. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
969 |
""" |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
970 |
table = self.ToJSon(columns_order, order_by) |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
971 |
return ("%s({'version':'0.6', 'reqId':'%s', 'status':'OK', " |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
972 |
"'table': %s});") % (response_handler, req_id, table) |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
973 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
974 |
def ToResponse(self, columns_order=None, order_by=(), tqx=""): |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
975 |
"""Writes the right response according to the request string passed in tqx. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
976 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
977 |
This method parses the tqx request string (format of which is defined in |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
978 |
the documentation for implementing a data source of Google Visualization), |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
979 |
and returns the right response according to the request. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
980 |
It parses out the "out" parameter of tqx, calls the relevant response |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
981 |
(ToJSonResponse() for "json", ToCsv() for "csv", ToHtml() for "html", |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
982 |
ToTsvExcel() for "tsv-excel") and passes the response function the rest of |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
983 |
the relevant request keys. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
984 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
985 |
Args: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
986 |
columns_order: Optional. Passed as is to the relevant response function. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
987 |
order_by: Optional. Passed as is to the relevant response function. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
988 |
tqx: Optional. The request string as received by HTTP GET. Should be in |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
989 |
the format "key1:value1;key2:value2...". All keys have a default |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
990 |
value, so an empty string will just do the default (which is calling |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
991 |
ToJSonResponse() with no extra parameters). |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
992 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
993 |
Returns: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
994 |
A response string, as returned by the relevant response function. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
995 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
996 |
Raises: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
997 |
DataTableException: One of the parameters passed in tqx is not supported. |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
998 |
""" |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
999 |
tqx_dict = {} |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1000 |
if tqx: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1001 |
tqx_dict = dict(opt.split(":") for opt in tqx.split(";")) |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1002 |
if tqx_dict.get("version", "0.6") != "0.6": |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1003 |
raise DataTableException( |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1004 |
"Version (%s) passed by request is not supported." |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1005 |
% tqx_dict["version"]) |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1006 |
|
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1007 |
if tqx_dict.get("out", "json") == "json": |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1008 |
response_handler = tqx_dict.get("responseHandler", |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1009 |
"google.visualization.Query.setResponse") |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1010 |
return self.ToJSonResponse(columns_order, order_by, |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1011 |
req_id=tqx_dict.get("reqId", 0), |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1012 |
response_handler=response_handler) |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1013 |
elif tqx_dict["out"] == "html": |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1014 |
return self.ToHtml(columns_order, order_by) |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1015 |
elif tqx_dict["out"] == "csv": |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1016 |
return self.ToCsv(columns_order, order_by) |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1017 |
elif tqx_dict["out"] == "tsv-excel": |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1018 |
return self.ToTsvExcel(columns_order, order_by) |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1019 |
else: |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1020 |
raise DataTableException( |
05ab9393303d
Add google-visualization-python project to Melange repository.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1021 |
"'out' parameter: '%s' is not supported" % tqx_dict["out"]) |