diff options
author | Walter Bender <walter.bender@gmail.com> | 2012-09-20 19:02:13 (GMT) |
---|---|---|
committer | Walter Bender <walter.bender@gmail.com> | 2012-09-20 19:02:13 (GMT) |
commit | 2ef047694461b7ec3a9f20b3e255e89621396e6d (patch) | |
tree | 243f41d05080ae955384b691fcd1afa1bc38bdd7 | |
parent | 3b87909382235a0842dba5e493a764804fa572ce (diff) |
new sample project
-rw-r--r-- | pysamples/journal-stats.py | 137 | ||||
-rw-r--r-- | samples/media-journal-stats.ta | 53 |
2 files changed, 190 insertions, 0 deletions
diff --git a/pysamples/journal-stats.py b/pysamples/journal-stats.py new file mode 100644 index 0000000..f19b042 --- /dev/null +++ b/pysamples/journal-stats.py @@ -0,0 +1,137 @@ +# Copyright (c) 2012, Walter Bender + +# 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 + +def myblock(tw, x): # ignore second argument + ''' Load journal stats to heap (Sugar only) ''' + + import os + import glob + 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_list(self): + activity_list = [] + for i in range(len(self._activity_name)): + activity_list.append([self._activity_name[i], + self._activity_count[i]]) + return activity_list + + def get_max(self): + max_count = -1 + max_name = '' + for i in range(len(self._activity_name)): + if self._activity_count[i] > max_count: + max_count = self._activity_count[i] + max_name = self._activity_name[i] + return max_name + + def get_sorted(self): + activity_tuples = [] + for i in range(len(self._activity_name)): + activity_tuples.append((self._activity_name[i], + 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 + + + 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 + + + data = ParseJournal() + activity_list = data.get_sorted() + for a in activity_list: + tw.lc.heap.append(a[0]) + tw.lc.heap.append(a[1]) + + tw.lc.heap.append(activity_list[0][1]) + return diff --git a/samples/media-journal-stats.ta b/samples/media-journal-stats.ta new file mode 100644 index 0000000..37309c5 --- /dev/null +++ b/samples/media-journal-stats.ta @@ -0,0 +1,53 @@ +[[0, ["start", 2.0], 401, 0, [null, 1]], +[1, ["userdefined", "pysamples/journal-stats.py"], 401, 46, [0, 2, 6]], +[2, ["number", 100], 459, 46, [1, null]], +[3, "penup", 401, 296, [44, 5]], +[4, "pendown", 401, 422, [5, 11]], +[5, ["setxy2", 0], 401, 338, [3, 8, 29, 4]], +[6, ["storein", 0], 401, 88, [1, 7, 49, 51]], +[7, ["string", "scale"], 469, 88, [6, null]], +[8, "leftpos", 459, 338, [5, null]], +[9, "toppos", 513, 380, [29, null]], +[10, "forward", 67, 96, [25, 16, 17]], +[11, ["until", 0], 401, 464, [4, 12, 42, null]], +[12, "isheapempty2", 457, 480, [11, null]], +[13, "pop", 192, 96, [16, null]], +[14, "box", 192, 138, [16, 15, null]], +[15, ["string", "scale"], 247, 138, [14, null]], +[16, ["product2", 0], 138, 96, [10, 13, 14]], +[17, ["vspace", 0], 67, 138, [10, 27]], +[18, ["setxy2", 0], 67, 514, [23, 19, 20, 24]], +[19, "leftpos", 125, 514, [18, null]], +[20, "ycor", 125, 556, [18, null]], +[21, "show", 67, 430, [39, 22, 23]], +[22, "pop", 125, 430, [21, null]], +[23, "penup", 67, 472, [21, 18]], +[24, "pendown", 67, 598, [18, null]], +[25, "seth", 67, 54, [40, 26, 10]], +[26, ["number", 90], 166, 54, [25, null]], +[27, "seth", 67, 180, [17, 28, 38]], +[28, ["number", 0], 166, 180, [27, null]], +[29, ["minus2", 0], 459, 380, [5, 9, 30]], +[30, ["number", 30], 537, 422, [29, null]], +[31, ["setxy2", 20], 67, 264, [38, 35, 37, 39]], +[32, ["number", 20], 179, 306, [35, null]], +[33, ["number", -30], 179, 388, [37, null]], +[34, "xcor", 179, 264, [35, null]], +[35, ["plus2", 0], 125, 264, [31, 34, 32]], +[36, "ycor", 179, 346, [37, null]], +[37, ["plus2", 0], 125, 346, [31, 36, 33]], +[38, "penup", 67, 222, [27, 31]], +[39, "pendown", 67, 388, [31, 21]], +[40, "hat", 67, 0, [null, 41, 25]], +[41, ["string", "action"], 125, 12, [40, null]], +[42, "stack", 419, 530, [11, 43, null]], +[43, ["string", "action"], 477, 530, [42, null]], +[44, "setpensize", 401, 254, [51, 45, 3]], +[45, ["number", 20], 503, 254, [44, null]], +[46, ["division2", 0], 523, 130, [49, 47, 48]], +[47, "pop", 577, 130, [46, null]], +[48, "width", 601, 172, [46, null]], +[49, ["product2", 20], 469, 130, [6, 46, 50]], +[50, ["number", 0.15], 523, 212, [49, null]], +[51, ["vspace", 20], 401, 172, [6, 44]]] + |