Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/journal.py
blob: 85057072f20611edc91ed5a7c6c6f601bad89c93 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
#! /usr/bin/python
#
#    Author:  Arjun Sarwal   arjun@laptop.org
#    Copyright (C) 2007, Arjun Sarwal
#    Copyright (C) 2009, Walter Bender
#    
#    	
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    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
"""

import csv
import os
import gtk
import dbus

import tempfile
import time
from os import environ
from os.path import join
from numpy import array
from gettext import gettext as _

from sugar.datastore import datastore
try:
    import gconf
except:
    from sugar import profile

class JournalInteraction():
    def __init__(self, file_path, existing):
        """_jobject is the Journal object exiting denotes if a file exists with
        that journal object (resumed from journal) or not (started afresh)"""
        print "Journal Interaction: file path: " + str(file_path)

        self.filepath= file_path
        #self.filepath = _jobject.file_path
        self.existing = existing  #Denotes if journal file already exists or not
        self.session_id = 0
        self.num_rows=0
        self.logginginterval_status = ' '
        self.writer1 = None
        self.writer2 = None
        self.making_row = False
        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.existing==True:
            try:
                self.set_max_session_id()
                self.set_number_of_rows()
            except:
                print "Couldn't get session id or rows"
                pass

        print "$$journal.py: This is the file I will work on", self.filepath
    
    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.filepath, "rb"))
        for row in reader:
            self.num_rows+=1

    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"""
        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
        print "$$journal.py: a new session has started; the session_id is", \
              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

    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==True:
            self.apppend_session(self.session_id_to_continue)
            self.append_existing=False
            
        if self.making_row==False:
          #     self.write_session_params()
                self.temp_buffer.append(value)
                self.making_row = True
        else:   
            self.temp_buffer.append(value)
        self._stopped = False
        print "$$Journal.py: I just wrote this value", value

    def get_record(self, session_id=0):
        """Return list of values from logging session specified by session_id"""
        reader = csv.reader(open(self.filepath, "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.filepath, "rb"))
        count=0
        for row in reader:
            count+=1
        return count

    def stop_session(self):
        """Write the temp_buffer onto a file"""
        if self._stopped == False:
            if self.existing==True:
                writer1 = csv.writer(open(self.filepath, "ab"))
            else:
                writer1 = csv.writer(open(self.filepath, "wb"))
                self.existing =  True
	    for datum in self.temp_buffer:
		writer1.writerow( [ datum ] )
            del writer1
            self.temp_buffer = []
            self.making_row = False
            self.append_existing = False
            #print "$$journal.py: session stopped; csv writer was", writer1
	    try:
                self.jobject = datastore.create()
                try:
                    self.jobject.metadata['title'] = \
                        'Measure Activity log, ' + \
                         str(self.logginginterval_status)
                    self.jobject.metadata['keep'] = '0'
                    self.jobject.metadata['buddies'] = ''
                    self.jobject.metadata['preview'] = ''
                    try:
                        client = gconf.client_get_default()
                        self.jobject.metadata['icon-color'] = \
                            client.get_string("/desktop/sugar/user/color")
                    except:
                        self.jobject.metadata['icon-color'] = \
                            profile.get_color().to_string()
                    self.jobject.metadata['mime_type'] = 'text/csv'
                    self.jobject.file_path = self.filepath
                    datastore.write(self.jobject)
                finally:
                    pass
            finally:
                print "$$$ in outermost finally!!"
            self._stopped = True
    
    def get_number_of_cols(self):
        """Returns the maximum number of columns amongst all the data"""
        max = 0
        reader = csv.reader(open(self.filepath, "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
        print "$$journal.py: Wrote record", values
    
    def write_session_params(self):
        """Write the session parameters to temp_buffers"""
        self.num_rows+=1
        self.temp_buffer.append(_('Session: ' + str(self.session_id)))
        self.temp_buffer.append(_('User: ' + str(self.user)))
        self.temp_buffer.append(_('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.filepath, "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.filepath, "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.filepath, "wb"))
        for i in range(0, len(data_new)):
            writer.writerow(data_new[i])
        self.making_row= True
    
    def delete_record(self, session_id=-1):
        """Delete the record identified by its session_id"""
        reader = csv.reader(open(self.filepath, "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.filepath, "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.filepath, "rb"))
        for row in reader:
            temp_row = [int(x) for x in row]
            records.append(temp_row)
        return array(records)

    def take_activity_screenshot(self):
        if self._model.get_zoom_level() != ShellModel.ZOOM_ACTIVITY:
            return
        if self.get_frame().visible:
            return

        home_model = self._model.get_home()
        activity = home_model.get_active_activity()
        if activity is not None:
            service = activity.get_service()
            if service is not None:
                try:
                    service.TakeScreenshot(timeout=2.0)
                except dbus.DBusException, e:
                    logging.debug('Error raised by TakeScreenshot(): %s', e)

    def take_screenshot(self, waveform_id = 1):
        #file_path = os.path.join(tempfile.gettempdir(), '%i' % time.time())
        act_root = environ['SUGAR_ACTIVITY_ROOT'] 
        tmp_dir = join(act_root, 'data')
        file_path = str(tempfile.mkstemp(dir=tmp_dir)[1])
        ###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
        os.chmod(file_path, 0777)   
        window = gtk.gdk.get_default_root_window()
        width, height = window.get_size()
        x_orig, y_orig = window.get_origin()
        screenshot = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, has_alpha=False, \
                                    bits_per_sample=8, width=width, \
                                    height=height)
        screenshot.get_from_drawable(window, window.get_colormap(), x_orig, \
                                     y_orig, 0, 0, width, height)
        screenshot.save(file_path, "png")
        try:
            jobject = datastore.create()
            try:
                jobject.metadata['title'] = _('Waveform ' + str(waveform_id) +\
                    ' ,from Measure Activity')
                jobject.metadata['keep'] = '0'
                jobject.metadata['buddies'] = ''
                jobject.metadata['preview'] = ''
                try:
                    client = gconf.client_get_default()
                    jobject.metadata['icon-color'] = \
                        client.get_string("/desktop/sugar/user/color")
                except:
                    jobject.metadata['icon-color'] = \
                        profile.get_color().to_string()
                jobject.metadata['mime_type'] = 'image/png'
                jobject.file_path = file_path
                datastore.write(jobject)
            finally:
                jobject.destroy()
                del jobject
        finally:
            os.remove(file_path)

"""
#FOR TESTING JOURNALINTERACTION CLASS
ji = JournalInteraction("myfile.csv", True)
print ji.get_all_records()
ji.set_session_params("guitar", 6, 6)
ji.write_record([1,1,1])
ji.write_record([2,2,2])
print ji.get_number_of_records()
ji.write_record([3,3,3])
ji.write_record([4,4,4])
print ji.get_number_of_records()
ji.write_record([5,5,5])
ji.write_record([6,6,6])
ji.write_record([7,7,7])
ji.append_session(0)
ji.write_value(2222)
ji.write_value(222222)
ji.stop_session()
ji.delete_record(3)
print type(ji.read_record(2)[0])
"""