diff options
author | Pootle daemon <pootle@pootle.sugarlabs.org> | 2012-02-17 05:31:37 (GMT) |
---|---|---|
committer | Pootle daemon <pootle@pootle.sugarlabs.org> | 2012-02-17 05:31:37 (GMT) |
commit | 1d34ea7dd59dacca85e9e3c63b8c582253b9eb1c (patch) | |
tree | 833b3b3208df8b791fb6a01d0fb634225b974828 | |
parent | 1c42ccbef2c58e007a56c2046033767f7713f9c6 (diff) | |
parent | 08044b689c3cbed3b18e58bb5267951024a778ee (diff) |
Merge branch 'master' of git.sugarlabs.org:simplegraph/simplegraph
-rw-r--r-- | activity.py | 81 | ||||
-rw-r--r-- | readers.py | 89 |
2 files changed, 89 insertions, 81 deletions
diff --git a/activity.py b/activity.py index ee445ce..c5c55a9 100644 --- a/activity.py +++ b/activity.py @@ -47,8 +47,8 @@ from sugar.graphics.alert import Alert from sugar.datastore import datastore from charts import Chart -from readers import StopWatch -from readers import Measure +from readers import StopWatchReader +from readers import MeasureReader def rgb_to_html(color): @@ -456,7 +456,7 @@ class SimpleGraph(activity.Activity): def _object_chooser(self, mime_type, type_name): chooser = ObjectChooser() - boolean = False + matches_mime_type = False response = chooser.run() if response == gtk.RESPONSE_ACCEPT: @@ -465,7 +465,7 @@ class SimpleGraph(activity.Activity): file_path = jobject.file_path if metadata['mime_type'] == mime_type: - boolean = True + matches_mime_type = True else: alert = Alert() @@ -484,68 +484,47 @@ class SimpleGraph(activity.Activity): alert.show() - return boolean, file_path, metadata['title'] + return matches_mime_type, file_path, metadata['title'] - def __import_stopwatch_cb(self, widget): - boolean, file_path, title = self._object_chooser( - STOPWATCH_MIME_TYPE, - _('StopWatch')) - - if boolean: - f = open(file_path) - reader = StopWatch(f) - - stopwatchs_list, count = reader.get_stopwatchs_with_marks() - - self.labels_and_values.model.clear() - self.chart_data = [] + def _graph_from_reader(self, reader): + self.labels_and_values.model.clear() + self.chart_data = [] - self.v_label.entry.set_text(_('Time')) + chart_data = reader.get_chart_data() - if count == 1: - num, name = stopwatchs_list[0] - self.h_label.entry.set_text(_('Mark')) + h, v = reader.get_labels_name() - self.set_title(name) - chart_data = reader.marks_to_chart_data(num - 1) + self.v_label.entry.set_text(h) + self.h_label.entry.set_text(v) - elif count == 0 or count > 1: - self.set_title(title) - self.h_label.entry.set_text(_('StopWatch')) + # Load the data + for row in chart_data: + self._add_value(None, + label=row[0], value=float(row[1])) - chart_data = reader.times_to_chart_data() + self.update_chart() - # Load the data - for row in chart_data: - self._add_value(None, - label=row[0], value=float(row[1])) + def __import_stopwatch_cb(self, widget): + matches_mime_type, file_path, title = self._object_chooser( + STOPWATCH_MIME_TYPE, + _('StopWatch')) - self.update_chart() + if matches_mime_type: + f = open(file_path) + reader = StopWatchReader(f) + self._graph_from_reader(reader) f.close() def __import_measure_cb(self, widget): - boolean, file_path, title = self._object_chooser(CSV_MIME_TYPE, + matches_mime_type, file_path, title = self._object_chooser( + CSV_MIME_TYPE, _('Measure')) - if boolean: + if matches_mime_type: f = open(file_path) - reader = Measure(f) - - self.v_label.entry.set_text(_('Values')) - self.h_label.entry.set_text(_('Samples')) - - self.chart_data = [] - self.labels_and_values.model.clear() - - chart_data = reader.get_chart_data() - - # Load the data - for row in chart_data: - self._add_value(None, - label=row[0], value=float(row[1])) - - self.update_chart() + reader = MeasureReader(f) + self._graph_from_reader(reader) f.close() @@ -21,57 +21,78 @@ import cPickle import csv +from gettext import gettext as _ -class StopWatch(): + +class StopWatchReader(): def __init__(self, data): + """Import chart data from file.""" + self._data = cPickle.load(data) - def get_stopwatchs_with_marks(self): - count = 0 - stopwatchs_list = [] - for i in self._data[-1]: - if i: - count += 1 - stopwatchs_list.append([count, self._data[1][count - 1]]) + self._v_label = _('Time') + self._h_label = '' - return stopwatchs_list, count + def get_chart_data(self): + """Return data suitable for pyCHA.""" - def get_stopwatch_name(self, num=0): - return self._data[1][num] + count = self._get_stopwatchs_with_marks() + chart_data = [] - def marks_to_chart_data(self, num=0, chart_data=[]): - marks_count = 0 + if count == 1: + self._h_label = _('Mark') - marks = self._data[-1][num] - marks.sort() + marks_count = 0 - for i in marks: - marks_count += 1 - chart_data.append((str(marks_count), round(i, 2))) + for x in self._data[-1]: + x.sort() + for y in x: + marks_count += 1 + chart_data.append((str(marks_count), round(y, 2))) - return chart_data + elif count == 0 or count > 1: + self._h_label = _('StopWatch') - def times_to_chart_data(self): - times = [i[0][0] for i in self._data[2]] + times = [i[0][0] for i in self._data[2]] - times_count = 0 - chart_data = [] + times_count = 0 + chart_data = [] - for i in times: - times_count += 1 - chart_data.append((self.get_stopwatch_name(times_count - 1), - round(i, 2))) + for i in times: + times_count += 1 + chart_data.append((self._get_stopwatch_name(times_count - 1), + round(i, 2))) return chart_data + def get_labels_name(self): + """Return the h_label and y_label names.""" -class Measure(): + return self._v_label, self._h_label - def __init__(self, data): - self._reader = csv.reader(data) + def _get_stopwatchs_with_marks(self): + count = 0 + for i in self._data[-1]: + if i: + count += 1 + + return count + + def _get_stopwatch_name(self, num=0): + return self._data[1][num] + + +class MeasureReader(): + + def __init__(self, file): + """Import chart data from file.""" + + self._reader = csv.reader(file) def get_chart_data(self): + """Return data suitable for pyCHA.""" + count = 0 chart_data = [] @@ -83,3 +104,11 @@ class Measure(): chart_data.append((label, float(value))) return chart_data + + def get_labels_name(self): + """Return the h_label and y_label names.""" + + v_label = _('Values') + h_label = _('Samples') + + return v_label, h_label |