~dpm/+junk/langpacks-by-inst

« back to all changes in this revision

Viewing changes to generate_report.py

  • Committer: David Planella
  • Date: 2010-06-06 00:59:13 UTC
  • Revision ID: dpm@lillypilly-20100606005913-145mwdgcpg38yz6n
Ported all code to Python, moved modules to a local library location

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#!/usr/bin/python
2
 
#
3
 
 
4
 
"""Example of static use of Google Visualization Python API."""
5
 
 
6
 
import gviz_api
 
2
# -*- coding: utf-8 -*-
 
3
### BEGIN LICENSE
 
4
# Copyright (C) 2009 <David Planella> <david.planella@ubuntu.com>
 
5
#This program is free software: you can redistribute it and/or modify it
 
6
#under the terms of the GNU General Public License version 3, as published
 
7
#by the Free Software Foundation.
 
8
#
 
9
#This program is distributed in the hope that it will be useful, but
 
10
#WITHOUT ANY WARRANTY; without even the implied warranties of
 
11
#MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
 
12
#PURPOSE.  See the GNU General Public License for more details.
 
13
#
 
14
#You should have received a copy of the GNU General Public License along
 
15
#with this program.  If not, see <http://www.gnu.org/licenses/>.
 
16
### END LICENSE
 
17
 
 
18
from gviz_api import gviz_api
7
19
import csv
 
20
import sys
 
21
from LocaleInfo import LocaleInfo
 
22
from datetime import datetime
8
23
 
9
24
page_template = """
10
 
<html>
 
25
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
 
26
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
 
27
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
11
28
  <head>
 
29
  <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
12
30
  <title>Language packs</title>
13
31
    <script src="http://www.google.com/jsapi" type="text/javascript"></script>
14
 
    <script>
 
32
    <script type="text/javascript">
15
33
 
16
34
      google.load("visualization", "1", {packages:["table"]});
17
35
      google.setOnLoadCallback(drawTable);
18
36
 
19
 
      google.load("visualization", "1", {packages:["barchart"]});
 
37
      google.load("visualization", "1", {packages:["columnchart"]});
20
38
      google.setOnLoadCallback(draw_chart);
21
39
 
22
40
      function drawTable() {
23
 
        var langpack_data = new google.visualization.DataTable(%(langpacks_json)s, 0.5);
24
 
        var langpack_table = new google.visualization.Table(document.getElementById('table_div_langpack'));
25
 
        langpack_table.draw(langpack_data, {showRowNumber: true});
 
41
          var langpack_table_data = new google.visualization.DataTable(%(langpacks_json)s, 0.6);
 
42
          var langpack_table = new google.visualization.Table(
 
43
            document.getElementById('table_div_langpack'));
 
44
 
 
45
          langpack_table.draw(langpack_table_data, {
 
46
              showRowNumber: true,
 
47
              page: 'enable',
 
48
              pageSize: 20});
26
49
      }
27
50
 
28
51
      function draw_chart() {
29
 
          var json_data = new google.visualization.DataTable(%(langpacks_json)s, 0.5);
30
 
          var chart = new google.visualization.BarChart(
31
 
              document.getElementById('bar_chart_div_langpack'));
 
52
          var langpack_chart_data = new google.visualization.DataTable(%(langpacks_top_json)s, 0.6);
 
53
          var langpack_chart = new google.visualization.ColumnChart(
 
54
              document.getElementById('column_chart_div_langpack'));
32
55
 
33
 
          chart.draw(json_data, {
34
 
              width: 1200,
35
 
              height: 800,
36
 
              title: 'Language packs by installation'
 
56
          langpack_chart.draw(langpack_chart_data, {
 
57
              width: 650,
 
58
              height: 400,
 
59
              title: 'Language packs by installation',
 
60
              legend: 'none',
37
61
          });
38
62
      }
39
63
    </script>
 
64
    <style type="text/css">
 
65
        #report-meta {
 
66
          text-align: right;
 
67
          font-size: small;
 
68
          color: #999;
 
69
          float: right;
 
70
        }
 
71
 
 
72
       #report-meta p {
 
73
         margin: 0;
 
74
       }
 
75
 
 
76
    </style>
40
77
  </head>
41
78
  <body>
42
 
    <H1>Language packs chart</H1>
43
 
    <div id="bar_chart_div_langpack"></div>
44
 
 
45
 
    <H1>Language packs table</H1>
46
 
    <div id="table_div_langpack"></div>
 
79
    <div id="report-meta">
 
80
      <p>Generated on %(gen_date)s</p>
 
81
    </div>
 
82
 
 
83
    <h1>Top 20 language packs</h1>
 
84
    <div id="column_chart_div_langpack" align="center"></div>
 
85
 
 
86
    <h1>Language pack installations</h1>
 
87
    <div id="table_div_langpack" align="center"></div>
47
88
  </body>
48
89
</html>
49
90
"""
50
91
 
51
 
def read_csv(description, entries=-1):
52
 
    f = open('langpacks_by_inst', 'r')
 
92
def csv2json(reader, keys):
53
93
 
54
 
    reader = csv.reader(f)
55
 
    keys = ("langpack", "installs")
56
 
    #keys = tuple(description.keys())
57
94
    data = []
58
95
 
59
96
    for row in reader:
65
102
            else:
66
103
                data_json[key] = row.next()
67
104
        data += [data_json]
68
 
    #print data
 
105
 
69
106
    return data
70
107
 
71
108
def main():
72
109
    # Creating the data
 
110
    keys = ("langpack", "installs")
73
111
    description = {"langpack": ("string", "Language pack"),
74
112
                    "installs": ("number", "Installations")}
75
113
 
76
 
    data = read_csv(description, 15)
77
 
 
78
 
    # Loading it into gviz_api.DataTable
 
114
    # Read the data from the CSV file
 
115
    f = open('/home/dpm/langpacks-by-inst/data/langpacks_by_inst.csv', 'r')
 
116
    reader = csv.reader(f)
 
117
 
 
118
    # Convert the rows read to a list and translate the ISO codes to language
 
119
    # names
 
120
    reader_list = list(reader)
 
121
    li = LocaleInfo.LocaleInfo()
 
122
    i = 0
 
123
    for row in reader_list:
 
124
        try:
 
125
            lang_name = li.translate(reader_list[i][0], True)
 
126
            reader_list[i][0] = lang_name.split(';')[0]
 
127
        except:
 
128
            pass
 
129
        i += 1
 
130
 
 
131
    # For the graph we only want to visualise the top 50 language packs, excluding English
 
132
    reader_top_list = reader_list[1:21]
 
133
 
 
134
    data = csv2json(reader_list, keys)
 
135
    data_top = csv2json(reader_top_list, keys)
 
136
    print data
 
137
 
 
138
    # Load the data it into gviz_api.DataTable
79
139
    data_table = gviz_api.DataTable(description)
80
140
    data_table.LoadData(data)
81
141
 
82
 
    # Creating a JSon string
 
142
    data_top_table = gviz_api.DataTable(description)
 
143
    data_top_table.LoadData(data_top)
 
144
 
 
145
    # Create a JSon string
83
146
    langpacks_json = data_table.ToJSon(columns_order=("langpack", "installs"),
84
147
                                    order_by=("installs", "desc"))
85
148
 
86
 
    # Putting the JS code and JSon string into the template
 
149
    langpacks_top_json = data_top_table.ToJSon(columns_order=("langpack", "installs"),
 
150
                                    order_by=("installs", "desc"))
 
151
 
 
152
    # Log the date of creation for the report
 
153
    gen_date = datetime.utcnow()
 
154
 
 
155
    # Put the JSon string into the template
87
156
    print page_template % vars()
88
157
 
89
158
if __name__ == "__main__":