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

#    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 logging

import os
import glob
import json

from gettext import gettext as _

from sugar import env
from sugar import profile

DIROFINTEREST = 'datastore'


class ParseJournal():
    ''' Simple parser of datastore '''

    def __init__(self):
        self._dsdict = {}
        self._activity_name = []
        self._activity_launch_times = []
        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)
                        launch = launch_times(dsobj)
                        if not activity:
                            self._dsdict[os.path.basename(path)][-1][
                                'activity'] = 'media object'
                        else:
                            self._dsdict[os.path.basename(path)][-1][
                                'activity'] = activity
                        self._dsdict[os.path.basename(path)][-1][
                            'launch'] = launch

        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'])
                        if 'launch' in a:
                            self._activity_launch_times[i] += ', '
                            self._activity_launch_times[i] +=  a['launch']
                    else:
                        self._activity_name.append(a['activity'])
                        if 'launch' in a:
                            self._activity_launch_times.append(a['launch'])
                        else:
                            self._activity_launch_times.append('0')

    def get_sorted(self):
        activity_tuples = []
        for i in range(len(self._activity_name)):
            launch_times = self._activity_launch_times[i].split(', ')
            sorted_launch_times = sorted(launch_times, key=lambda x: int(x))
            activity_tuples.append((self._activity_name[i].replace('Activity',
                                                                   ''),
                                    sorted_launch_times))
        sorted_tuples = sorted(activity_tuples, key=lambda x: len(x[1]))
        activity_list = []
        count = 0
        length = len(sorted_tuples)
        for i in range(length):
            activity_list.append([sorted_tuples[length - i - 1][0],
                                  sorted_tuples[length - i - 1][1]])
        return activity_list


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 launch_times(path):
    ''' Return activity launch times '''
    launchtimes = hascomponent(path, 'launch-times')
    if not launchtimes:
        return '0'
    else:
        return launchtimes


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


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

    Import chart data from journal activity analysis
    """

    def __init__(self, mypath=None):
        if mypath is None:
            mypath = os.path.join(os.path.abspath('.'), 'journalstats')

        stats = ParseJournal().get_sorted()
        f = open(mypath, 'w')
        try:
            json.dump(stats, f)
        finally:
            f.close()


if __name__ == '__main__':
    JournalReader()