From 8f5ff158f85d470644c0b3f04e66230b4a3ed994 Mon Sep 17 00:00:00 2001 From: Agustin Zubiaga Date: Thu, 16 Feb 2012 20:25:54 +0000 Subject: Code fixes Signed-off-by: Agustin Zubiaga --- diff --git a/activity.py b/activity.py index ee445ce..87d0624 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,36 +484,26 @@ 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( + matches_mime_type, file_path, title = self._object_chooser( STOPWATCH_MIME_TYPE, _('StopWatch')) - if boolean: + if matches_mime_type: f = open(file_path) - reader = StopWatch(f) - - stopwatchs_list, count = reader.get_stopwatchs_with_marks() + reader = StopWatchReader(f) self.labels_and_values.model.clear() self.chart_data = [] - self.v_label.entry.set_text(_('Time')) - - if count == 1: - num, name = stopwatchs_list[0] - self.h_label.entry.set_text(_('Mark')) - - self.set_title(name) - chart_data = reader.marks_to_chart_data(num - 1) + chart_data = reader.get_chart_data() - elif count == 0 or count > 1: - self.set_title(title) - self.h_label.entry.set_text(_('StopWatch')) + h, v = reader.get_labels_name() - chart_data = reader.times_to_chart_data() + self.v_label.entry.set_text(h) + self.h_label.entry.set_text(v) # Load the data for row in chart_data: @@ -525,15 +515,18 @@ class SimpleGraph(activity.Activity): 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) + reader = MeasureReader(f) + + h, v = reader.get_labels_name() - self.v_label.entry.set_text(_('Values')) - self.h_label.entry.set_text(_('Samples')) + self.v_label.entry.set_text(h) + self.h_label.entry.set_text(v) self.chart_data = [] self.labels_and_values.model.clear() diff --git a/readers.py b/readers.py index f14e686..ce7af80 100644 --- a/readers.py +++ b/readers.py @@ -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 -- cgit v0.9.1