Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/atoidejouer/tools/storage.py
blob: c8db5ac70ab9bc1daa20f249d863dac2d91d1a19 (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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508

# python import
import logging, os, shutil, tarfile, time
# ..
from gettext import gettext as _

# gtk import
import gtk

# sugar import
from sugar.activity import activity
from sugar.datastore import datastore

# get application logger
logger = logging.getLogger('atoidejouer')

ACTIVITY_NAMES = {
        'paint': 'org.laptop.Oficina',
        'record': 'org.laptop.RecordActivity',
        }


def get_config_path():
    # get bundle path
    _bundle_path = activity.get_bundle_path()
    # return path
    return os.path.join(_bundle_path, 'static', 'data', 'config', 'config.ini')


def get_sequence_path(type_, sequence_name):
    return os.path.join(activity.get_activity_root(), 'data',
            'sequences', type_, '%s.seq' % sequence_name)


def get_sequence_first_graphic_name(type_, sequence_name):
    # seq file
    _f = open(get_sequence_path(type_, sequence_name))
    _name = _f.readlines()[0]
    _f.close()
    # ..
    return _name.strip()


def get_sound_path(filename, dir_='sounds'):
    # return path
    return os.path.join(activity.get_activity_root(), 'data', dir_,
            '%s.ogg' % filename)


def get_icon_path(stock_id):
    # return path
    return os.path.join(activity.get_bundle_path(), 'static', 'data', 'icons',
            '%s.png' % stock_id)


def get_image_path(filename, dir_='graphics'):
    # return path
    if filename in ['background_default', 'mask_default']\
    or dir_=='data':
        return os.path.join(activity.get_bundle_path(), 'static', 'data',
                'graphics', '%s.png' % filename)
    else:
        return os.path.join(activity.get_activity_root(), 'data', dir_,
                '%s.png' % filename)


def get_pixbuf_from_data(data, image_type=None, size=None):
    # load it
    if image_type:
        _loader = gtk.gdk.PixbufLoader(image_type=image_type)
    else:
        _loader = gtk.gdk.PixbufLoader()
    # size check
    if size is None:
        pass
    else:
        # parse size
        _w, _h = size
        # set loader size
        _loader.set_size(_w, _h)
    # load date
    _loader.write(data)
    # close loader
    _loader.close()
    # pix it
    return _loader.get_pixbuf()


def get_journal_objects(activity_name):
    # prepare query
    _query = {'activity': ACTIVITY_NAMES[activity_name]}
    # find in ds
    _results, _count = datastore.find(_query, sorting='timestamp')
    # return it
    return _results


def list_info_from_journal(activity_name):
    # get objects first
    _objs = get_journal_objects(activity_name)
    # make unique titles
    _titles = {}
    # return infos
    for _o in _objs:
        # get meta
        _m = _o.get_metadata()
        # get title
        _t = _m['title']
        # ensure description
        _d = _m['description'] if 'description' in _m else ''
        # little check
        if _t in _titles:
            # udpate reg
            _titles[_t] += 1
            # update value to show
            _t = '%s (%s)' % (_t, _titles[_t])
        # init title reg
        else:
            _titles[_t] = 1

        # DEBUG
        logger.debug('[utils] _info_from_jnl - activity_id: %s' % _m['activity_id'])
        logger.debug('[utils] _info_from_jnl - timestamp: %s' % _m['timestamp'])
        # DEBUG

        # ensure info
        yield {
                'activity_id'   : _m['activity_id'],
                'description'   : _d,
                'timestamp'     : _m['timestamp'],
                'preview'       : _m['preview'],
                'title'         : _t,
                }


def list_files_from_journal(activity_name):
    # get objects first
    _objs = get_journal_objects(activity_name)
    # return paths
    for _o in _objs:
        # TODO open the files
        yield _o.get_file_path()


def get_file_from_journal(activity_name, activity_id, timestamp):
    # prepare query
    _query = {
            'activity': ACTIVITY_NAMES[activity_name],
            'activity_id': activity_id,
            'timestamp': timestamp,
            }
    # find in ds
    _results, _count = datastore.find(_query)
    if _count == 1:
        # get path
        _path = _results[0].get_file_path()
    else:
        return None

    # ...
    _temp = os.path.join('/home', 'florent', '%s.tmp' % activity_name)

    # DEBUG
    logger.debug('[utils] _file_from_jnl - _temp: %s' % _temp)
    # DEBUG

    # ...
    os.link(_path, _temp)

    # DEBUG
    logger.debug('[utils] _file_from_jnl - _path: %s' % _path)
    # DEBUG

    _f = open(_temp)

    # DEBUG
    logger.debug('[utils] _file_from_jnl - _len: %s' % len(_f.read()))
    # DEBUG

    _f.close()

    # return it
    return _results


def __check_dir(dir_name, parent='data'):
    # get activity path
    if parent is None:
        _dir = os.path.join(activity.get_activity_root(), dir_name)
    else:
        _dir = os.path.join(activity.get_activity_root(), parent, dir_name)
    # ensure activity path
    if os.path.exists(_dir):
        pass
    else:
        os.mkdir(_dir)


def __check_file(sub_path, file_name):
    # ..
    __check_dir(sub_path)
    # file path
    _path = os.path.join(activity.get_activity_root(), 'data', sub_path,
            file_name)
    # ensure file
    if os.path.exists(_path):
        pass
    else:
        # get bundle path
        _p = os.path.join(activity.get_bundle_path(), 'static', 'ext',
                sub_path, file_name)
        # copy
        shutil.copy(_p, _path)


def __check_dir_files(sub_path):
    # get bundle path
    _path = os.path.join(activity.get_bundle_path(), 'static', 'ext', sub_path)
    # file by file
    for _f in os.listdir(_path):
        # full path
        _p = os.path.join(_path, _f)
        # little check
        if os.path.isdir(_p):
            pass
        else:
            __check_file(sub_path, _f)


def init_activity_folder():
    # check folders
    _root = activity.get_activity_root()
    # graphics
    __check_dir_files('graphics')
    # sounds
    __check_dir_files('sounds')
    # sequences
    __check_dir('sequences')
    __check_dir_files(os.path.join('sequences', 'graphics'))
    __check_dir_files(os.path.join('sequences', 'sounds'))
    # stories
    __check_dir_files('stories')


def __show_in_out_result_message(label, message):
    # ..
    label.set_markup('<span size="large" style="italic">%s</span>' % message)
    label.show()


def __merge_dir(project_name, dir_name, exist_list=None):
    # archive path
    _path_src = os.path.join(activity.get_activity_root(), 'tmp', project_name,
            dir_name)
    # little check
    if os.path.exists(_path_src):
        # project path
        _path_dst = os.path.join(activity.get_activity_root(), 'data',
                dir_name)
        # init existing list
        exist_list = list() if exist_list is None else exist_list
        for _f in os.listdir(_path_src):
            # ..
            _p_src = os.path.join(_path_src, _f)
            _p_dst = os.path.join(_path_dst, _f)
            # little check
            if os.path.isdir(_p_src):
                continue
            # do not replace
            elif os.path.exists(_p_dst):
                # update exist list
                exist_list.append(os.path.join(dir_name, _f))
            # do copy
            else:
                shutil.copy(_p_src, _path_dst)
        # OK!
        return True
    else:
        # Oops!
        return False


def __import_keys(activity_, project_name):
    # ..
    _path_data = os.path.join(activity.get_activity_root(), 'tmp',
            project_name, 'story.keys')
    # init content
    _data = None
    # little check
    if os.path.exists(_path_data):
        # read file
        _file = open(_path_data, 'r')
        try:
            _data = _file.read()
        finally:
            _file.close()
        # parse json data
        _exist_graphic_keys = activity_.graphic_keys.loads(_data, clear=False)
        _exist_sound_keys = activity_.sound_keys.loads(_data, clear=False)
        # set max
        _graphic_max = activity_.graphic_keys.get_max_frame()
        _sound_max = activity_.sound_keys.get_max_frame()
        _max = _graphic_max if _graphic_max > _sound_max else _sound_max
        # set activity new number of keys
        activity_.set_number_of_keys(_max + 1)
        # ..
        return {
                'graphics': _exist_graphic_keys,
                'sounds': _exist_sound_keys,
                }
    # ?? invalid archive
    else:
        return None


def import_project(activity_, file_path, msg_label):
    # clean tmp dir
    __remove_dir('tmp', parent=None)
    __check_dir('tmp', parent=None)
    # ..
    _tmp_root = os.path.join(activity.get_activity_root(), 'tmp')
    try:
        # copy file to tmp
        _tar_path = os.path.join(_tmp_root, '__tmp.tar.bz2')
        shutil.copy(file_path, _tar_path)
        # change dir for unzipping
        os.chdir(_tmp_root)
        # extract files in tmp dir
        _tar = tarfile.open(file_path)
        _p_name = _tar.getnames()[0]
        _tar.extractall()
        _tar.close()
    except Exception, e:
        # prepare message
        _msg = _('Project import failed!')
        _msg = _('%s\n\n[Error] Can not read archive file!' % _msg)
        # remove tmp structure
        __remove_dir('tmp', parent=None)
        # quit!
        return __show_in_out_result_message(msg_label, _msg)
    # merge dirs
    _exist_list = list()
    if __merge_dir(_p_name, 'graphics', exist_list=_exist_list)\
    and __merge_dir(_p_name, 'sounds', exist_list=_exist_list)\
    and __merge_dir(_p_name, os.path.join('sequences', 'graphics'),
            exist_list=_exist_list)\
    and __merge_dir(_p_name, os.path.join('sequences', 'sounds'),
            exist_list=_exist_list):
        # init result message
        _msg = _('Project sucessfully imported')
    else:
        # prepare message
        _msg = _('Project import failed!')
        _msg = _('%s\n\n[Error] Can not load files!' % _msg)
        # remove tmp structure
        __remove_dir('tmp', parent=None)
        # quit!
        return __show_in_out_result_message(msg_label, _msg)
    # existing files stop
    if len(_exist_list) == 0:
        pass
    else:
        # prepare message
        _msg = _('%s\n\n[Warning] Following files already exist:\n'\
                % _msg)
        for _f in _exist_list:
            _msg = '%s - %s\n' % (_msg, _f)
    # merge keys
    _existing_dict = __import_keys(activity_, _p_name)
    if _existing_dict is None:
        # prepare message
        _msg = _('Project import failed!')
        _msg = _('%s\n\n[Error] Can not load keys!' % _msg)
        # remove tmp structure
        __remove_dir('tmp', parent=None)
        # quit!
        return __show_in_out_result_message(msg_label, _msg)
    if len(_existing_dict['graphics']) == 0\
    or len(_existing_dict['sounds']) == 0:
        pass
    else:
        # prepare message
        _msg = _('%s\n\n[Warning] Following sequences already exist:\n'
                % _msg)
        for _s in _existing_dict['graphics']:
            _msg = '%s - graphics.%s\n' % (_msg, _s)
        _msg = '%s\n' % _msg
        for _s in _existing_dict['sounds']:
            _msg = '%s - sounds.%s\n' % (_msg, _s)
    # remove tmp structure
    __remove_dir('tmp', parent=None)
    # show result
    __show_in_out_result_message(msg_label, _msg)


def __remove_dir(dir_name, parent=None):
    # get activity path
    if parent is None:
        _dir = os.path.join(activity.get_activity_root(), dir_name)
        _next_parent = dir_name
    else:
        _dir = os.path.join(activity.get_activity_root(), parent, dir_name)
        _next_parent = os.path.join(parent, dir_name)
    # remove files and dir recursively
    if os.path.exists(_dir):
        for _f in os.listdir(_dir):
            _p = os.path.join(_dir, _f)
            if os.path.isdir(_p):
                __remove_dir(_f, parent=_next_parent)
            else:
                os.remove(_p)
        # and remove the dir
        if os.path.exists(_dir):
            os.removedirs(_dir)
        else:
            pass
    # nothing to do
    else:
        pass


def __get_sequence_names(sequence_path):
    _f = open(sequence_path)
    _names = _f.readlines()
    _f.close()
    # ..
    for _n in _names:
        yield _n.strip()


def __export_seq_and_res(activity_, tmp_root, type_='graphics'):
    # path updates
    _seq_src = os.path.join(activity.get_activity_root(), 'data', 'sequences',
            type_)
    _seq_dst = os.path.join(tmp_root, 'sequences', type_)
    # ..
    _res_root = os.path.join(activity.get_activity_root(), 'data', type_)
    _res_dst = os.path.join(tmp_root, type_)
    # keys factory
    _keys = activity_.graphic_keys if type_ == 'graphics'\
            else activity_.sound_keys
    # set res ext
    _ext = '.png' if type_ == 'graphics' else '.ogg'
    # copy
    for _n in _keys._names:
        _s_path = os.path.join(_seq_src, '%s.seq' % _n)
        shutil.copy(_s_path, _seq_dst)
        for _res in __get_sequence_names(_s_path):
            _res_path = os.path.join(_res_root, '%s%s' % (_res, _ext))
            shutil.copy(_res_path, _res_dst)


def export_project(activity_, msg_label, media):
    # get the toolbar
    _toolbar = activity_._toolbox.get_activity_toolbar()
    # get the projet name
    _name = _toolbar.title.get_text()
    # clean tmp dir first
    __remove_dir('tmp', parent=None)
    __check_dir('tmp', parent=None)
    # create a tmp stucture
    __check_dir(_name, parent='tmp')
    __check_dir(os.path.join(_name, 'graphics'), parent='tmp')
    __check_dir(os.path.join(_name, 'sequences'), parent='tmp')
    __check_dir(os.path.join(_name, 'sequences', 'graphics'), parent='tmp')
    __check_dir(os.path.join(_name, 'sequences', 'sounds'), parent='tmp')
    __check_dir(os.path.join(_name, 'sounds'), parent='tmp')
    # ..
    _tmp_root = os.path.join(activity.get_activity_root(), 'tmp')
    _out_root = os.path.join(_tmp_root, _name)
    # copy keys
    _keys_path = os.path.join(_out_root, 'story.keys')
    activity_.write_file(_keys_path)
    # copy sequences and resources
    __export_seq_and_res(activity_, _out_root, type_='graphics')
    __export_seq_and_res(activity_, _out_root, type_='sounds')
    # change dir for zipping
    os.chdir(_tmp_root)
    # zip all
    _tar_name = '%s.tar.bz2' % _name
    # ..
    _tar = tarfile.open(_tar_name, "w:bz2")
    _tar.add(_name)
    _tar.close()
    # try to copy
    try:
        if os.path.exists(os.path.join('/media', media, _tar_name)):
            # ..
            _msg = _('Project "%(name)s" already exported to "%(media)s"!'\
                    % {"name": _name, "media": media})
        else:
            # ..
            shutil.copy(os.path.join(_tmp_root, _tar_name),
                    os.path.join('/media', media))
            # ..
            _msg = _('Project "%(name)s" sucessfully exported to "%(media)s"'\
                    % {"name": _name, "media": media})
    except Exception, e:
        # ERROR
        logger.error('[storage] export_project - e: %s' % e)
        # ERROR
        # ..
        _msg = _('Project "%(name)s" export to "%(media)s" failed!'\
                % {"name": _name, "media": media})
    # remove tmp structure
    __remove_dir('tmp', parent=None)
    # tmp message
    __show_in_out_result_message(msg_label, _msg)