Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/readers.py
blob: 7106b0bf9a06f08a33ebce01c5258d9b7ccd96cf (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
#!/usr/bin/env python
# -*- coding: utf-8 -*-

# readers.py by:
#    Agustin Zubiaga <aguz@sugarlabs.org>
#    Walter Bender <walter@sugarlabs.org>

# 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 3 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., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

import os
import glob
import statvfs

from gettext import gettext as _

from sugar import env
from sugar import profile


class FreeSpaceReader():
    """Reader for Free Space
    Measure free space on disk.
    """

    def __init__(self):
        """Import chart data from file."""

        space = self._get_space()
        self._reader = ((_('Free space'), space[0]),
                        (_('Used space'), space[1]))
        self.xlabel = ""
        self.ylabel = ""

    def get_chart_data(self):
        """Return data suitable for pyCHA."""

        chart_data = []

        for row in self._reader:
            print row
            label, value = row[0], row[1]

            if label == "XLabel":
                self.xlabel = value

            elif label == "YLabel":
                self.ylabel = value

            else:
                chart_data.append((label, float(value)))

        return chart_data

    def _get_space(self):
        stat = os.statvfs(env.get_profile_path())
        free_space = stat[statvfs.F_BSIZE] * stat[statvfs.F_BAVAIL]
        total_space = stat[statvfs.F_BSIZE] * stat[statvfs.F_BLOCKS]

        free_space = self._get_MBs(free_space)
        total_space = self._get_MBs(total_space)
        used_space = total_space - free_space

        return free_space, used_space, total_space

    def _get_MBs(self, space):
        space = space / (1024 * 1024)

        return space

    def _get_GBs(self, space):
        space = space / 1024

        return space

    def get_labels_name(self):
        """Return the h_label and y_label names."""

        return self.xlabel, self.ylabel


class TurtleReader():
    """Reader for Journal activity

    Import chart data from journal activity analysis
    """

    TACAT = {'clean':'forward', 'forward':'forward', 'back':'forward',
         'left':'forward', 'right':'forward', 'arc': 'arc',
         'xcor': 'coord', 'ycor': 'coord', 'heading': 'coord', 
         'setxy2': 'setxy', 'seth': 'setxy', 'penup': 'pen', 'pendown': 'pen',
         'setpensize': 'pen', 'setcolor': 'pen', 'pensize': 'pen',
         'color': 'pen', 'setshade': 'pen', 'setgray': 'pen', 'shade': 'pen',
         'gray': 'pen', 'fillscreen': 'pen', 'startfill': 'fill',
         'stopfill': 'fill', 'plus2': 'number', 'minus2': 'number',
         'product2': 'number', 'division2': 'number', 'remainder2': 'number',
         'sqrt': 'number', 'identity2': 'number', 'and2': 'boolean',
         'or2': 'boolean', 'not': 'boolean', 'greater2': 'boolean',
         'less2': 'boolean', 'equal2': 'boolean', 'random': 'random',
         'repeat': 'repeat', 'forever': 'repeat', 'if': 'ifthen',
         'ifelse': 'ifthen', 'while': 'ifthen', 'until': 'ifthen',
         'hat': 'action', 'stack': 'action', 'storein': 'box', 'box': 'box',
         'luminance': 'sensor', 'mousex': 'sensor', 'mousey': 'sensor',
         'mousebutton2': 'sensor', 'keyboard': 'sensor', 'kbinput': 'sensor',
         'readpixel': 'sensor', 'see': 'sensor', 'time': 'sensor',
         'sound': 'sensor', 'volume': 'sensor', 'pitch': 'sensor',
         'resistance': 'sensor', 'voltage': 'sensor', 'video': 'media',
         'wait': 'media', 'camera': 'media', 'journal': 'media',
         'audio': 'media', 'show': 'media', 'setscale': 'media',
         'savepix': 'media', 'savesvg': 'media', 'mediawait': 'media',
         'mediapause': 'media', 'mediastop': 'media', 'mediaplay': 'media',
         'speak': 'media', 'sinewave': 'media', 'description': 'media',
         'push':'extras', 'pop':'extras', 'printheap':'extras',
         'clearheap':'extras', 'isheapempty2':'extras', 'chr':'extras',
         'int':'extras', 'myfunction': 'python', 'userdefined': 'python',
         'loadblock': 'python', 'loadpalette': 'python'}
    TAPAL = {'forward': 'turtlep', 'arc': 'turtlep', 'coord': 'turtlep',
         'setxy': 'turtlep', 'pen': 'penp', 'fill': 'penp', 'number': 'numberp',
         'random': 'numberp', 'boolean': 'numberp', 'repeat': 'flowp',
         'ifthen': 'flowp', 'action': 'boxp', 'box': 'boxp',
         'sensor': 'sensorp', 'media': 'mediap', 'extras': 'extrasp',
         'python': 'extrasp'}
    TASCORE = {'forward': 3, 'arc': 3, 'setxy': 2.5, 'coord': 4, 'turtlep': 5,
           'pen': 2.5, 'fill': 2.5, 'penp': 5,
           'number': 2.5, 'boolean': 2.5, 'random': 2.5, 'numberp': 0,
           'repeat': 2.5, 'ifthen': 7.5, 'flowp': 10,
           'box': 7.5, 'action': 7.5, 'boxp': 0,
           'media': 5, 'mediap': 0,
           'python': 5, 'extras': 5, 'extrasp': 0,
           'sensor': 5, 'sensorp': 0}
    PALS = ['turtlep', 'penp', 'numberp', 'flowp', 'boxp', 'sensorp', 'mediap',
            'extrasp']
    PALNAMES = [_('turtle'), _('pen'), _('number'), _('flow'), _('box'),
                _('sensor'), _('media'), _('extras')]

    def hasturtleblocks(self, path):
        ''' Parse turtle block data and generate score based on rubric '''

        fd = open(path)
        blocks = []
        # block name is second token in each line
        for line in fd:
            tokens = line.split(',')
            if len(tokens) > 1:
                token = tokens[1].strip('" [')
                blocks.append(token)

        score = []
        for i in range(len(self.PALS)):
            score.append([self.PALNAMES[i], 0])
        cats = []
        pals = []

        for b in blocks:
            if b in self.TACAT:
                if not self.TACAT[b] in cats:
                    cats.append(self.TACAT[b])
        for c in cats:
            if c in self.TAPAL:
                if not self.TAPAL[c] in pals:
                    pals.append(self.TAPAL[c])

        for c in cats:
            if c in self.TASCORE:
                score[self.PALS.index(self.TAPAL[c])][1] += self.TASCORE[c]

        for p in pals:
            if p in self.TASCORE:
                score[self.PALS.index(p)][1] += self.TASCORE[p]

        return score

    def __init__(self, file):

        self._reader = self.hasturtleblocks(file)
        self.xlabel = ""
        self.ylabel = ""

    def get_chart_data(self):
        """Return data suitable for pyCHA."""

        chart_data = []

        for row in self._reader:
            label, value = row[0], row[1]

            if label == "XLabel":
                self.xlabel = value

            elif label == "YLabel":
                self.ylabel = value

            else:
                chart_data.append((label, float(value)))

        return chart_data

    def get_labels_name(self):
        """Return the h_label and y_label names."""

        return self.xlabel, self.ylabel


MAX = 19
DIROFINTEREST = 'datastore'
class ParseJournal():
    ''' Simple parser of datastore '''

    def __init__(self):
        self._dsdict = {}
        self._activity_name = []
        self._activity_count = []
        homepath = os.environ['HOME']

        for path in glob.glob(os.path.join(homepath, '.sugar', '*')):
            if isdsdir(path):
                self._dsdict[os.path.basename(path)] = []
                dsobjdirs = glob.glob(
                    os.path.join(path, DIROFINTEREST, '??'))
                for dsobjdir in dsobjdirs:
                    dsobjs = glob.glob(os.path.join(dsobjdir, '*'))
                    for dsobj in dsobjs:
                        self._dsdict[os.path.basename(path)].append({})
                        activity = isactivity(dsobj)
                        if not activity:
                            self._dsdict[os.path.basename(path)][-1][
                                'activity'] = 'media object'
                        else:
                            self._dsdict[os.path.basename(path)][-1][
                                'activity'] = activity

        for k, v in self._dsdict.iteritems():
            for a in v:
                if 'activity' in a:
                    if a['activity'] in self._activity_name:
                        i = self._activity_name.index(a['activity'])
                        self._activity_count[i] += 1
                    else:
                        self._activity_name.append(a['activity'])
                        self._activity_count.append(1)

    def get_sorted(self):
        activity_tuples = []
        for i in range(len(self._activity_name)):
            activity_tuples.append((self._activity_name[i].replace('Activity',
                                                                   ''),
                                    self._activity_count[i]))
        sorted_tuples = sorted(activity_tuples, key=lambda x: x[1])
        activity_list = []
        count = 0
        length = len(sorted_tuples)
        for i in range(length):
            if i < MAX:
                activity_list.append([sorted_tuples[length - i - 1][0],
                                      sorted_tuples[length - i - 1][1]])
            else:
                count += sorted_tuples[length - i - 1][1]
        if count > 0:
            activity_list.append([_('other'), count])
        return activity_list


class JournalReader():
    """Reader for Journal activity

    Import chart data from journal activity analysis
    """

    def __init__(self):

        self._reader = ParseJournal().get_sorted()
        self.xlabel = ""
        self.ylabel = ""

    def get_chart_data(self):
        """Return data suitable for pyCHA."""

        chart_data = []

        for row in self._reader:
            label, value = row[0], row[1]

            if label == "XLabel":
                self.xlabel = value

            elif label == "YLabel":
                self.ylabel = value

            else:
                chart_data.append((label, float(value)))

        return chart_data

    def get_labels_name(self):
        """Return the h_label and y_label names."""

        return self.xlabel, self.ylabel


def hascomponent(path, component):
    ''' Return metadata attribute, if any '''
    if not os.path.exists(os.path.join(path, 'metadata')):
        return False
    if not os.path.exists(os.path.join(path, 'metadata', component)):
        return False
    fd = open(os.path.join(path, 'metadata', component))
    data = fd.readline()
    fd.close()
    if len(data) == 0:
        return False
    return data


def isactivity(path):
    ''' Return activity name '''
    activity = hascomponent(path, 'activity')
    if not activity:
        return False
    else:
        return activity.split('.')[-1]


def isdsdir(path):
    ''' Only interested if it is a datastore directory '''
    if not os.path.isdir(path):
        return False
    if not os.path.exists(os.path.join(path, DIROFINTEREST)):
        return False
    return True