diff options
author | Walter Bender <walter@sugarlabs.org> | 2010-07-26 23:12:39 (GMT) |
---|---|---|
committer | Walter Bender <walter@sugarlabs.org> | 2010-07-26 23:12:39 (GMT) |
commit | e77abbd88d1e8b9c880fb347110c71b07a45527a (patch) | |
tree | e7b6f08af35497f4e3bf6d79b8e6e5f5e92a2c8b | |
parent | eec1f703684244d37c8c6cb881a830978ef11dbf (diff) |
refactoring of Journal code
-rw-r--r-- | journal.py | 256 | ||||
-rw-r--r-- | measure.py | 25 |
2 files changed, 51 insertions, 230 deletions
@@ -2,7 +2,7 @@ # # Author: Arjun Sarwal arjun@laptop.org # Copyright (C) 2007, Arjun Sarwal -# Copyright (C) 2009, Walter Bender +# Copyright (C) 2009,10 Walter Bender # # # This program is free software; you can redistribute it and/or modify @@ -19,28 +19,9 @@ # along with this program; if not, write to the Free Software # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. -""" -journal.py - -Start_new_session(user="", Xscale=0, yscale=0) or -continue_existing_session(session_id_to_continue=-1): must be called -before every logging session for logging value by value, use -write_value(value=0),for logging a whole set of values into one -session at one time, use write_record(value_set=[]) Note: session_id's -are unique within one instance of an Activity and are not re-assigned -even if one or more logging sessions are deleted -""" - -# -# TODO: Clean up this mess -# - - -import csv import gtk -from tempfile import mkstemp -from os import environ, chmod, close, remove +from os import environ, remove from os.path import join from numpy import array from gettext import gettext as _ @@ -53,124 +34,47 @@ log = logging.getLogger('Measure') log.setLevel(logging.DEBUG) logging.basicConfig() + class JournalInteraction(): """ Handles all of the data I/O with the Journal """ - def __init__(self, activity): - """_jobject is the Journal object exiting denotes if a file exists with - that journal object (resumed from journal) or not (started afresh)""" + def __init__(self, activity): + """ We store csv data in the Journal entry for Measure; screen captures + are stored in separate Journal entries """ self.activity = activity - self.session_id = 0 - self.num_rows = 0 self.logginginterval_status = ' ' self.writer1 = None self.writer2 = None - self.making_row = False + self.new_session = True self.temp_buffer = [] - self.append_existing = False self._stopped = True - self.session_id_to_continue = 0 self.jobject = None self.user = "" self.xscale = 0 self.yscale = 0 - - if self.activity.existing: - try: - self.set_max_session_id() - self.set_number_of_rows() - except: - log.error("Couldn't get session id or rows") - - # log.debug("$$journal.py: This is the file I will work on" +\ - # self.activity._jobject.file_path) - - def __del__(self): - pass - - def on_quit(self): - pass - def set_max_session_id(self): - """Sets the existing maximum session_id if the file already exists""" - self.session_id = self.get_number_of_records() - - def set_number_of_rows(self): - """Sets the number of rows an existing file has""" - reader = csv.reader(open(self.activity._jobject.file_path, "rb")) - # for row in reader: - # self.num_rows += 1 - self.num_rows += len(reader) - - def set_session_params(self, user="", xscale=0, yscale=0): - self.user = user - self.xscale = xscale - self.yscale = yscale - def start_new_session(self, user="", xscale=0, yscale=0, \ logginginterval_status=' '): - """This needs to be called before starting any logging session""" + """ Start a new logging session by updating session parameters """ self.user = user self.xscale = xscale self.yscale = yscale self.logginginterval_status = logginginterval_status - self.session_id += 1 - self.num_rows += 1 - self.append_existing = False - self.making_row = False - # log.debug("$$journal.py: a new session has started; session_id is"\ - # + str(self.session_id)) - return self.session_id - - def continue_existing_session(self, session_id_to_continue=-1): - """Must be called before attempting to continue any logging session""" - self.append_existing = True - self.session_id_to_continue = session_id_to_continue - self.making_row = True + self.activity.session_id += 1 + self.new_session = True + return self.activity.session_id def write_value(self, value=0): - """Append the value passed to temp_buffer if a logging session is - started and continuted If a previous logging session is to be - continued upon, read the corresponding row from the file and continue - to append it and then rewrite the whole row""" - if self.append_existing: - self.apppend_session(self.session_id_to_continue) - self.append_existing = False - - if not self.making_row: - # self.write_session_params() - self.temp_buffer.append(value) - self.making_row = True - else: - self.temp_buffer.append(value) + """Append the value passed to temp_buffer """ + self.temp_buffer.append(value) self._stopped = False - # log.debug("%s %s" % ("$$Journal.py: I just wrote this value", - # str(value))) - - def get_record(self, session_id=0): - """Return list of values from logging session specified by session_id""" - reader = csv.reader(open(self.activity._jobject.file_path, "rb")) - for row in reader: - if int(row[0]) == session_id: - temp = row - for i in range(0, len(temp)): - if i != 1: - temp[i] = int(temp[i]) - return temp - - def get_number_of_records(self): - """Returns the of records""" - reader = csv.reader(open(self.activity._jobject.file_path, "rb")) - return len(reader) - # count = 0 - # for row in reader: - # count += 1 - # return count def stop_session(self): """Write the temp_buffer onto a file""" + return + """ if not self._stopped: if self.activity.existing: writer1 = csv.writer(open(self.activity._jobject.file_path, @@ -183,8 +87,7 @@ class JournalInteraction(): writer1.writerow( [ datum ] ) del writer1 self.temp_buffer = [] - self.making_row = False - self.append_existing = False + self.new_session = True try: self.jobject = datastore.create() try: @@ -204,123 +107,23 @@ class JournalInteraction(): log.debug("$$$ in outermost finally!!") self._stopped = True return False - - def get_number_of_cols(self): - """Returns the maximum number of columns amongst all the data""" - max = 0 - reader = csv.reader(open(self.activity._jobject.file_path, "rb")) - for row in reader: - if len(row) > max: - max = len(row) - return max - - def write_record(self, values=[]): - """Write a complete row specified by values - If file doesn't exist, open a new file and write to it""" - # self.write_session_params() - self.temp_buffer += values - self.stop_session() - self._stopped = False - log.debug("$$journal.py: Wrote record: " + str(values)) + """ def write_session_params(self): """Write the session parameters to temp_buffers""" - self.num_rows += 1 - self.temp_buffer.append("%s: %s" % (_('Session'), str(self.session_id))) + self.temp_buffer.append("%s: %s" % (_('Session'), + str(self.activity.session_id))) self.temp_buffer.append("%s: %s" % (_('User'), str(self.user))) self.temp_buffer.append("%s: %s" % (_('Interval'), - str(self.logginginterval_status))) - ##TODO: Probably need to add a field for timing interval - #self.temp_buffer.append(self.xscale) - #self.temp_buffer.append(self.yscale) - - def get_session_params(self, session_id=-1): - """TODO write this function""" - pass - - def find_record(self, session_id=-1): - """Returns the index 0 to N-1 of the record - session_id may be shifted around due to deletion and editing - returns -1 i it doesn't find it""" - reader = csv.reader(open(self.activity._jobject.file_path, "rb")) - found = 0 - for i in range(0, self.num_rows): - temp = reader.next() - if int(temp[0]) == session_id: - return found - else: - found += 1 - return -1 - - def append_session(self, session_id=-1): - """Deletes that record from the file and returns the existing - data in self.temp_buffer so that further recording can be done""" - reader = csv.reader(open(self.activity._jobject.file_path, "rb")) - data_new = [] - # Copy all data except row to be deleted, to a temporary buffer - for row in reader: - if int(row[0]) != session_id: - data_new.append(row) - else: - self.temp_buffer = row - # Write the temporary buffer to the file again - writer = csv.writer(open(self.activity._jobject.file_path, "wb")) - for i in range(0, len(data_new)): - writer.writerow(data_new[i]) - self.making_row = True + str(self.logginginterval_status))) - def delete_record(self, session_id=-1): - """Delete the record identified by its session_id""" - reader = csv.reader(open(self.activity._jobject.file_path, "rb")) - data_new = [] - #found = -1 - # Copy all data except row to be deleted, to a temporary buffer - for row in reader: - if int(row[0]) != session_id: - data_new.append(row) - #print row - else: - pass - #if found==-1: - # return found - #print data_new[5] - # Write the temporary buffer to the file again - self.num_rows = len(data_new) - writer1 = csv.writer(open(self.activity._jobject.file_path, "w")) - for i in range(0, len(data_new)): - writer1.writerow(data_new[i]) - - def get_existing_sessions_num(self): - """Returns the number of existing sessions already done in that - instance of the Activity""" - return self.num_rows - - def get_records_values(self): - pass - - def get_records_session_ids(self): - pass - - def get_records_users(self): - pass - - def get_all_records(self): - """Gets the 2d array of all records""" - records = [] - reader = csv.reader(open(self.activity._jobject.file_path, "rb")) - for row in reader: - temp_row = [int(x) for x in row] - records.append(temp_row) - return array(records) - def take_screenshot(self, waveform_id=1): """ Take a screenshot and save to the Journal """ - act_root = environ['SUGAR_ACTIVITY_ROOT'] - tmp_dir = join(act_root, 'data') - tmp_fd, file_path = mkstemp(dir=tmp_dir) - # TODO: This is such a crappy way to write a file to the journal - # Ideally to be implemented with write_file and read_file methods - chmod(file_path, 0777) + tmp_file_path = join(environ['SUGAR_ACTIVITY_ROOT'], 'instance', + 'screen_capture_' + str(waveform_id) + '.png') + + log.debug('saving screen capture to temp file %s' % (tmp_file_path)) + gtk.threads_enter() window = gtk.gdk.get_default_root_window() width, height = window.get_size() @@ -330,7 +133,7 @@ class JournalInteraction(): height=height) screenshot.get_from_drawable(window, window.get_colormap(), x_orig, y_orig, 0, 0, width, height) - screenshot.save(file_path, "png") + screenshot.save(tmp_file_path, "png") gtk.threads_leave() try: jobject = datastore.create() @@ -342,11 +145,12 @@ class JournalInteraction(): jobject.metadata['preview'] = '' jobject.metadata['icon-color'] = self.activity.icon_colors jobject.metadata['mime_type'] = 'image/png' - jobject.file_path = file_path + jobject.file_path = tmp_file_path datastore.write(jobject) finally: jobject.destroy() del jobject finally: - close(tmp_fd) - remove(file_path) + remove(tmp_file_path) + + log.debug('cleaning up from screen capture save') @@ -27,8 +27,9 @@ import gtk from textbox import TextBox import gobject import dbus -from os import environ, path, chmod -from tempfile import mkstemp +from os import environ, path #, chmod +# from tempfile import mkstemp +import csv from gettext import gettext as _ @@ -75,6 +76,7 @@ logging.basicConfig() XO1 = 'xo1' XO15 = 'xo1.5' UNKNOWN = 'unknown' +# UNKNOWN = 'xo1.5' def _is_xo(hw): @@ -137,12 +139,16 @@ class MeasureActivity(activity.Activity): self.connect('destroy', self.on_quit) self.hw = _get_hardware() + self.session_id = 0 + + """ if self._jobject.file_path: self.existing = True else: self._jobject.file_path = str(mkstemp(dir=tmp_dir)[1]) chmod(self._jobject.file_path, 0777) self.existing = False + """ self.ji = JournalInteraction(self) self.wave = DrawWaveform(self) @@ -265,7 +271,6 @@ class MeasureActivity(activity.Activity): def on_quit(self, data=None): """Clean up, close journal on quit""" self.audiograb.on_activity_quit() - self.ji.on_quit() return def _active_cb(self, widget, pspec): @@ -286,10 +291,22 @@ class MeasureActivity(activity.Activity): def write_file(self, file_path): """ Write data to journal on quit """ + if hasattr(self, 'ji'): + # Append new data to Journal entry + writer = csv.writer(open(file_path, 'ab')) + for datum in self.ji.temp_buffer: + print datum + writer.writerow( [ datum ] ) return def read_file(self, file_path): - """ Read data from journal on start """ + """ Read csv data from journal on start """ + reader = csv.reader(open(file_path, "rb")) + # Count the number of sessions + for r in reader: + print r + if r[0] == _('Session'): + self.session_id += 1 return def _label_cb(self, data=None): |