Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/olpcfr/tools/storage.py
blob: 3e6ebbf819b46d1dd689f8d253abd78142eb7399 (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
# python import
import os

# olpcfr import
from olpcfr import BUNDLE, ROOT


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


def check_dir(path):
    if os.path.exists(path):
        pass
    else:
        os.mkdir(path)


def check_file(path):
    if os.path.exists(path):
        pass
    else:
        _f = open(path, 'wb')
        _f.write('')
        _f.close()


def get_path(path=None, bundle=True):
    # ..
    path = 'static' if path is None else path
    # ..
    if bundle is True:
        return os.path.join(BUNDLE, path)
    else:
        return os.path.join(ROOT, path)


def list_dir(path=None, bundle=True):
    # ..
    path = get_path(path=path, bundle=bundle)
    # ..
    return os.listdir(path)


def get_sound_path(filename, path=None, bundle=True, ext=None):
    # ..
    path = get_path(path=path, bundle=bundle)
    filename = filename if ext is None else '%s.%s' % (filename, ext)
    # ..
    return os.path.join(path, filename)


def get_sound_path(filename, path=None, bundle=True, ext='ogg'):
    # ..
    return get_sound_path(filename, path=path, bundle=bundle, ext=ext)


def get_image_path(filename, path=None, bundle=True, ext='png'):
    # ..
    return get_sound_path(filename, path=path, bundle=bundle, ext=ext)


def __do_query(query):
    from sugar.datastore import datastore
    # find in ds
    _results, _count = datastore.find(query, sorting='timestamp')
    for _r in _results:
        # get meta
        _m = _r.get_metadata()
        if 'activity' in query:
            yield _r
        elif _m['activity'] == '':
            yield _r
        else:
            continue


def get_journal_objects(activity_name=None, mime_type=None):
    # init
    _query = dict()
    # prepare query name
    if activity_name is None\
    and mime_type is None:
        return []
    elif mime_type is None:
        return __do_query({'activity': ACTIVITY_NAMES[activity_name]})
    else:
        return __do_query({'mime_type': mime_type})


def list_info_from_journal(activity_name=None, mime_type=None):
    # get objects first
    _objs = get_journal_objects(activity_name=activity_name, mime_type=mime_type)
    # 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 ''
        _p = _m['preview'] if 'preview' in _m else None
        # 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
        # ensure info
        yield {
                'activity_id'   : _m['activity_id'],
                'description'   : _d,
                'timestamp'     : _m['timestamp'],
                'preview'       : _p,
                'title'         : _t,
                }


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


def get_path_from_journal(timestamp, mime_type):
    from sugar.datastore import datastore
    # ..
    _query = {
            'timestamp': int(timestamp),
            'mime_type': mime_type
            }
    # find in ds
    _results, _count = datastore.find(_query)
    # ..
    if _count == 1:
        # get path
        return _results[0].get_file_path()
    else:
        return None