Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/sources.py
blob: 0a90bb46473f14ad69937fd9f83f51a77142ffce (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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
#!/usr/bin/env python
# sources.py 
#
# Copyright (C) 2010  George Hunt
#
# 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 Frjur 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.,
# 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
"""
Notes to myself:
the problem I'm trying to address is latency during start up.
If there is database corruption, we need to start over with empty databases
(similar to the first download of the application)
What's the best I can do?
If there are very many images in the Journal, the best I can do is a directed find
of all the appropriate mime_types in Journal, then bring up the UI, and paint the
thumbnail as each is generated.

"""
from gettext import gettext as _

from sugar.datastore import datastore
import sys, os
import gtk
import shutil
import sqlite3
import time
from xml.etree.cElementTree import Element, ElementTree, SubElement

from dbphoto import *
import display
#pick up activity globals
from xophotoactivity import *
import xophotoactivity



import logging
_logger = logging.getLogger('xophoto')
_logger.setLevel(logging.DEBUG)
console_handler = logging.StreamHandler()
console_formatter = logging.Formatter('%(name)s %(levelname)s %(funcName)s: %(lineno)d||| %(message)s')
#console_handler.setFormatter(console_formatter)
#_logger.addHandler(console_handler)
"""
Notes to myself:
need a new structure which records the ds object_ids quickly
then a routine which creates at least n thumbnails, and
another routine which will create a single thumbnail and store it- called from gtk idle loop
"""

class Datastore_SQLite():
    """class for interfacing between the Journal and an SQLite database"""
    def __init__(self,database_access_object):
        """receives an open dbaccess object (defined in dbphoto) """
        #self.db = dbaccess(fn)
        self.db = database_access_object
        self.datastore_to_process = None
        self.datastore_process_index = -1
    
    def ok(self):
        if self.db.is_open():return True
        return False
    
    def scan_images(self):
        """
        returns a list of journal object ids that have mime_type equal to one
        of the entries in mimetype table of xophoto database. 
        """
        rtn = 0
        mime_list = self.db.get_mime_list()
        (results,count) = datastore.find({})
        for f in results:
            dict = f.get_metadata().get_dictionary()
            if dict["mime_type"] in mime_list:
                #record the id, file size, file date, in_ds
                self.db.create_picture_record(f.object_id, f.get_file_path())
                rtn += 1
            f.destroy()
        self.db.commit()
        _logger.debug('%s entries found in journal. Number of pictures %s'%(count,rtn,))
        return rtn

    def check_for_recent_images(self):
        """scans the journal for pictures that are not in database, records jobject_id if found in
        table groups with the journal id in category. Can be faster because we don't have to fetch file itself.
        """
        ds_list = []
        num_found = 0
        mime_list = ['image/jpg','image/png','image/jpeg','image/gif',]
        
        #build 650 doesn't seem to understand correctly the dictionary with a list right hand side
        info = xophotoactivity.sugar_version()
        if len(info)>0:
            (major,minor,micro,release) = info
            _logger.debug('sugar version major:%s minor:%s micro:%s release:%s'%info)
        else:
            _logger.debug('sugar version failure')
            minor = 70
        if minor > 80:
            (results,count) = datastore.find({'mime_type': ['image/jpeg','image/jpg', 'image/png','image/gif']})
        else:
            (results,count) = datastore.find({'mime_type': 'image/jpeg'})
            ds_list.extend(results)
            num_found += count            
            (results,count) = datastore.find({'mime_type': 'image/jpg'})
            ds_list.extend(results)
            num_found += count
            (results,count) = datastore.find({'mime_type': 'image/png'})
            ds_list.extend(results)
            num_found += count
            (results,count) = datastore.find({'mime_type': 'image/gif'})
        ds_list.extend(results)
        num_found += count
        
        _logger.debug('Journal/datastore entries found:%s'%num_found)
        added = 0
        a_row_found = False
        cursor = self.db.connection().cursor()
        journal_list = []
        for ds in ds_list:
            #at least for now assume that the newest images are returned first
            if not a_row_found:
                journal_list.append(ds.object_id)
                dict = ds.get_metadata().get_dictionary()
                if dict["mime_type"] in mime_list:
                    cursor.execute('select * from groups where category = ? and jobject_id = ?',\
                                   (display.journal_id,str(ds.object_id),))
                    rows = cursor.fetchall()
                    if len(rows) == 0:
                        #may need to add date entered into ds (create date could be confusing)
                        self.db.put_ds_into_picture(ds.object_id)
                        self.db.add_image_to_album(display.journal_id,ds.object_id)
                        added += 1
                    else: #assume that pictures are returned in last in first out order
                        #no longer true since we are getting each mime_type separately (build 650 kludge)
                        #a_row_found = True
                        pass
            ds.destroy()
        #now go through albums and remove references that are no longer in datastore
        #cursor.execute('select * from groups')
        _logger.debug('scan found %s. Added %s datastore object ids from datastore to picture'%(count,added,))
        return (num_found,added,)
    
    def make_one_thumbnail(self):
        if not self.db.is_open(): return
        if not self.datastore_to_process:
            cursor = self.db.get_connection().cursor()
            cursor.execute('select * from picture where md5_sum = null')
            self.datastore_to_process = cursor.fetchall()
            self.datastore_process_index = 0
        if self.datastore_to_process and self.datastore_process_index > -1:
            jobject_id = self.datastore_to_process[self.datastore_process_index]['jobject_id']
            fn =get_filename_from_jobject_id(jobject_id)
            if fn:
                self.db.create_picture_record(f.object_id, fn)
                self.datastore_process_index += 1
                if self.datastore_process_index > len(self.datastore_to_process):
                    self.datastore_process_index = -1
        return True #we want to continue to process in gtk_idle_loop
        
    def get_filename_from_jobject_id(self, id):
        try:
            ds_obj = datastore.get(id)
        except Exception,e:
            _logger.debug('get filename from id error: %s'%e)
            return None
        if ds_obj:
            fn = ds_obj.get_file_path()
            ds_obj.destroy()
            return(fn)
        return None

    def delete_jobject_id_from_datastore(self,jobject_id):
        try:
            datastore.delete(jobject_id)
        except Exception,e:
            _logger.debug('delete_jobject_id_from_datastore error: %s'%e)
    
    def update_metadata(self,jobject_id,**kwargs):
        try:
            ds_obj = datastore.get(jobject_id)
        except Exception,e:
            _logger.debug('update metadata error: %s'%e)
            return None
        if ds_obj:
            md = ds_obj.get_metadata()
            if md:
                for key in kwargs.keys():
                    md[key] = kwargs[key]
                try:
                    datastore.write(ds_obj)
                except Exception, e:
                    _logger.debug('datastore write exception %s'%e)
            else:
                _logger.error('no metadata recovered from journal')
        else:
            _logger.error('no jobject returned from datastore.get()')
            
class FileTree():
    def __init__(self,db,activity):
        self.db = db
        self._activity = activity
        self.dialog = None

    def get_path(self,get_dir=False):
        _logger.debug('dialog to get user path for importing into journal')
        path = '/home/olpc/Pictures'
        try:
            os.makedirs(path)
        except:
            pass
        if get_dir:
            action = gtk.FILE_CHOOSER_ACTION_SELECT_FOLDER
        else:
            action = gtk.FILE_CHOOSER_ACTION_OPEN
        dialog = gtk.FileChooserDialog("Select Folder..",
                                       None,
                                       action,
                                       (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
                                        gtk.STOCK_OPEN, gtk.RESPONSE_OK))
        dialog.set_default_response(gtk.RESPONSE_OK)
        #dialog.set_current_folder(os.path.dirname(self.last_filename))       
        dialog.set_current_folder('/home/olpc/Pictures')
        dialog.set_select_multiple(True)
        filter = gtk.FileFilter()
        filter.set_name("All files")
        filter.add_pattern("*")
        dialog.add_filter(filter)
        
        filter = gtk.FileFilter()
        filter.set_name("Pictures")
        filter.add_pattern("*.png,*.jpg,*jpeg,*.gif")
        dialog.add_filter(filter)
               
        response = dialog.run()
        if response == gtk.RESPONSE_OK:
            _logger.debug('%s selected'%dialog.get_filename() )
            if get_dir:
                fname = [dialog.get_filename(),]
            else:    
                fname = dialog.get_filenames()
            fname = fname
            self.last_filename = fname
        elif response == gtk.RESPONSE_CANCEL:
            fname = []
            _logger.debug( 'File chooseer closed, no files selected')
        dialog.hide()
        dialog.destroy()
        return fname
    
    def _response_cb(self,alert,response):
        self._activity.remove_alert(alert)
        if response == gtk.RESPONSE_CANCEL:
            self.cancel = True


    def copy_tree_to_ds(self,path):
        _logger.debug('copy tree received path:%s'%(path,))
        dirlist = os.listdir(path)
        abs_fn_list = []
        for fn in dirlist:
            abs_fn_list.append(os.path.join(path,fn))            
        self.copy_list_to_ds(abs_fn_list)
        
    def copy_list_to_ds(self,file_list):
        """receives list of absolute file names to be copied to datastore"""
        added = 0
        jobject_id_list = {}
        reserve_at_least = 50000000L  #don't fill the last 50M of the journal
        #reserve_at_least = 5000000000L  #force it to complain for testing
        self.cancel = False
        proc_start = time.clock()
        if len(file_list) == 0: return
        
        #is the requested set of images going to fit in the XO datastore?
        tot = 0.0
        acceptable_extensions = ['jpg','jpeg','png','gif','jpe','tif']
        for filename in file_list:            
            chunks = filename.split('.')
            ext = ''
            if len(chunks)>1:
                ext = chunks[-1]
            if ext in acceptable_extensions:
                file_info = os.stat(filename)
                tot += file_info.st_size                
        info = os.statvfs(file_list[0])
        free_space = info.f_bsize * info.f_bavail
        #does it fit?
        if tot > free_space - reserve_at_least:   #don't fill the last 50M of the journal
            message1 = _('Selected images total ')
            message2 = _(' Megabytes but available memory is only ')
            message = message1 + '%.2f'%(tot / 1000000) + message2 + str((free_space - reserve_at_least) // 1000000)
            title = _('Please select a smaller number of images for import.')
            alert = self._activity.util.alert(msg=message,title=title)
            self._activity.add_alert(alert)
            _logger.debug('total free space message:%s free space:%d tot:%d'%(message,free_space,tot,))
            return
        
        #is there a xml information file in the directory where these photos are stored?
        base = os.path.dirname(file_list[0])  #make assumption that list is all in a single directory
        xml_path =  os.path.join(base,'xophoto.xml')
        if os.path.isfile(xml_path):
            xml_data = self.get_xml(xml_path)
        else:
            xml_data = None
        
        #let the user know progress of the import
        num = len(file_list)
        message = _('Number of images to copy to the XO Journal: ') + str(num)
        pa_title = _('Please be patient')
        alert = display.ProgressAlert(msg=message,title=pa_title)
        self._activity.add_alert(alert)
        alert.connect('response',self._response_cb)
        
        for filename in file_list:            
            start = time.clock()
            mtype = ''
            chunks = filename.split('.')
            if len(chunks)>1:
                ext = chunks[-1]
                if ext == 'jpg' or ext == 'jpeg' :
                    mtype = 'image/jpeg'
                elif ext == 'gif':
                    mtype = 'image/gif'
                elif ext == 'png':
                    mtype = 'image/png'
            if mtype == '': continue        
            #info = os.stat(filename)
            #size = info.st_size
            
            #check if this image md5_sum is already loaded
            if xml_data:
                found = xml_data.findall(os.path.basename(filename))
                if found:
                    md5 = found[0].attrib.get('md5_sum',None)
                    md5_row = self.db.is_md5_in_picture(md5)
                    if md5  and md5_row:
                        _logger.debug('md5 match from xml to picture table')
                        jobject_id_list[filename] = md5_row['jobject_id']
                        continue
                    
            #if the md5_sum is already in ds, abort
            md5_row = self.db.is_picture_in_ds(filename)
            if md5_row:
                jobject_id_list[filename] = md5_row['jobject_id']
                continue
            
            ds = datastore.create()
            ds.metadata['filename'] = filename
            ds.metadata['title'] = os.path.basename(filename)
            ds.metadata['mime_type'] = mtype
            dest = os.path.join(os.environ['SUGAR_ACTIVITY_ROOT'],'instance',os.path.basename(filename))
            shutil.copyfile(filename,dest)
            ds.set_file_path(dest)
            datastore.write(ds,transfer_ownership=True)
            self.db.create_picture_record(ds.object_id,filename)
            jobject_id_list[filename] = ds.object_id
            ds.destroy()
            _logger.debug('writing one image to datastore took %f seconds'%(time.clock()-start))
            added += 1
            alert.set_fraction(float(added)/num)
            while gtk.events_pending():
                gtk.main_iteration()
            if self.cancel:
                break
        
        #create an album for this import
        self.create_album_for_import(file_list,xml_data,jobject_id_list)
        
        _logger.debug('writing all images to datastore took %f seconds'%(time.clock()-proc_start))
        self._activity.remove_alert(alert)
        return added
    
    def dict_dump(self,dct):
        ret = ''
        for key in dct.keys():
            ret += '%s:%s | '%(key,dct[key])
        return ret
    
    def create_album_for_import(self,file_list,xml_data,jobject_id_list):
        _logger.debug('create album for import received jobject list %s'%self.dict_dump(jobject_id_list))
        timestamp = None
        name = None
        if xml_data:
            found = xml_data.findall('album_timestamp')
            if found:
                timestamp = found[0].text
            if timestamp == display.journal_id or timestamp == display.trash_id:
                timestamp = None
        if not timestamp:
            timestamp = str(datetime.datetime.today())
        _logger.debug('timestamp:%s'%timestamp)
        for file_name in file_list:
            jobject_id = jobject_id_list.get(file_name)
            self.db.add_image_to_album(timestamp,jobject_id)
            if xml_data:
                found = xml_data.findall(os.path.basename(file_name))
                if found:
                    _logger.debug('create album xml data %s'%self.dict_dump(found[0].attrib))
                    title = found[0].attrib.get('title')
                    if title:
                        self.db.set_title_in_groups(jobject_id,title)
                    comment = found[0].attrib.get('comment')
                    if comment:
                        self.db.set_comment_in_groups(jobject_id,comment)
                    name = found[0].attrib.get('stack_name')
        if not name:
            name = _('Camera Roll')        
        self.db.create_update_album(timestamp,name)
        self._activity.game.album_collection.album_objects[timestamp] = \
                display.OneAlbum(self.db,timestamp,self._activity.game.album_collection)
        #set the image on the top of the stack to the last one in the seqence
        self._activity.game.album_collection.album_objects[timestamp].set_top_image(jobject_id)
        #save off the unique id(timestamp)as a continuing target
        self.db.set_last_album(timestamp)
        
    def get_xml(self,xml_path):
        try:
            tree = ElementTree(file=xml_path).getroot()
        except Exception,e:
            _logger.debug('get_xml parse error: %s'%e)
            return None
        return tree

if __name__ == '__main__':
    db = DbAccess('/home/olpc/.sugar/default/org.laptop.XoPhoto/data/xophoto.sqlite')
    if db.is_open():
        ds_sql = Datastore_SQLite(db)
        #count = ds_sql.scan_images()
        count,added = ds_sql.check_for_recent_images()
        exit()
        for i in imagelist:
            print('\n%s'%ds.get_filename_from_jobject_id(i))
        ft = FileTree('xophoto.sqlite')
        #new = ft.fill_ds()
        print('%s datastore records added'%new)
    else:
        print('xophoto sqlite database failed to open')