Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/datamanager.py
blob: 212b6a979b5e5a109480fed0ce48fb6188851c58 (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
#!/usr/bin/python
# -*- mode:python; tab-width:4; indent-tabs-mode:t; -*-
# datamanager.py
#
# provides utility to manage datastore locally and on schoolserver
#
# 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 2 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

from sugar.activity import activity
from sugar.datastore import datastore
from sugar.graphics.toolbutton import ToolButton
from sugar.graphics.menuitem import MenuItem

import os, sys
import gtk
from path import path

import urllib2
from BeautifulSoup import BeautifulSoup

ACTIVITYPATH = path(activity.get_bundle_path())

SERIALNUMBER = '/ofw/serial-number'

class DataManager(activity.Activity):

    def __init__(self, handle):
        activity.Activity.__init__(self, handle)
        print 'activity initialized'
        self.set_title("Datamanager")
        #Create the standard activity toolbox
        toolbox = activity.ActivityToolbox(self)
        self.set_toolbox(toolbox)
        toolbox.show()
        self.viewer = Listview()
        self.child.pack_start(self.viewer, True, True, 0)
        treeView = self.viewer.get_treeView()
        treeView.set_model(self.viewer.create_model())
        self.show_all()

# based on PyGTK tutorial by jan bodnar, zetcode.com, February 2009
class Listview(gtk.VBox):
    def __init__(self):
        print 'Listview init'
        gtk.VBox.__init__(self)
        self.connect("destroy", gtk.main_quit)
        self.online = False
        sw = gtk.ScrolledWindow()
        sw.set_shadow_type(gtk.SHADOW_ETCHED_IN)
        sw.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
        self.treeView = gtk.TreeView()
        self.treeView.connect("row-activated", self.on_activated)
        self.treeView.set_rules_hint(True)
        sw.add(self.treeView)

        print 'create_columns'
        self.create_columns(self.treeView)
        print 'pack sw'
        self.pack_start(sw, True, True, 0)

        print 'set up metadata display'
        self.label = gtk.Label("")
        self.label.show()
        self.frame = gtk.Frame(label='metadata')
        self.frame.add(self.label)
        self.frame.show()
        self.pack_start(self.frame, False, False, 0)
        print 'show all'
        self.show_all()

    def get_treeView(self):
        return self.treeView

    def create_model(self):
        global online
        store = gtk.ListStore(object, str, str, str)
        #let's display objects from the schoolserver
        src = '/ofw/serial-number'
        print 'open configf', src
        configf = open(src, "r")
        xoserial = configf.read()
        configf.close()
        print 'xoserial:', xoserial[:11]
        #temp for testing
        if len(xoserial)>0:
            url = "http://schoolserver/ds-restore/" + xoserial[:11] + "/datastore-latest"
            print 'url', url
            try:
                response = urllib2.urlopen(url)
            except:
                print 'schoolserver not found'
                response = None
            if response:
                soup = BeautifulSoup(response)
                self.online = True
            else:
                print 'schoolserver not found at', url
        else:
            print 'no xoserial'
        if self.online:
            print 'soup=', soup.prettify()
            entries = soup.findAll("li")
            print len(entries)
            rnge = len(entries)
            print 'entries:', rnge
            if rnge > 10:
                rnge = 10
            for i in range(rnge):
               print len(entries[i].contents), entries[i].contents
               colx = str(entries[i].contents[0])
               pos1 = colx.find('<')
               pos2 = colx.find('>')
               col0 = colx[pos1:pos2]
               col1 = 'col1' + colx[pos2+1:]
               temp = entries[i].contents[1].split(' ')
               col2 = 'col2:' + temp[0]
               col3 = 'col3:' + temp[1]
               store.append([col1, col2, col3])
        #print 'sample', entries[0].toprettyxml()
        #objects from the local datastore
        results, count = datastore.find(dict())
        print 'number of datastore items', count
        color = 0
        firsttime = True
        colors = ["red", "yellow", "blue", "green"]
        for f in results:
            obj = datastore.get(f.object_id)
            if firsttime:
               firsttime = False
               print 'type obj', type(obj)
            store.append([obj, obj.metadata['title'], obj.metadata['mime_type'], colors[color] ])
            f.destroy()
            color += 1
            if color > 3:
                color = 0
        #ignore for the moment
        print 'return store'
        return store

    def create_columns(self, treeView):

        rendererText = gtk.CellRendererText()
        column = gtk.TreeViewColumn("Title", rendererText, text=1)
        column.add_attribute(rendererText, "cell-background", 3)
        column.set_sort_column_id(0)
        treeView.append_column(column)

        rendererText = gtk.CellRendererText()
        column = gtk.TreeViewColumn("Mime_type", rendererText, text=2)
        column.set_sort_column_id(1)
        treeView.append_column(column)

    def on_activated(self, widget, row, col):

        model = widget.get_model()
        model[row][3] = "cyan"
        obj = model[row][0]
        pth = obj.get_file_path()
        print 'pth', pth
        if len(pth) > 0:
            f = open(pth,'r')
            text = f.read()
            f.close()
        tpllist = []
        metakeys = obj.metadata.keys()
        for metakey in metakeys:
            metavalue = obj.metadata[metakey]
            tpllist.append((metakey, metavalue))
        metadata = dict(tpllist)

        tstr = ""
        for k, v in metadata.iteritems():
            try:
                if len(str(v)) > 0:
                    tstr = tstr + k + ':' + v + '\n'
            except:
                tstr = tstr + k + ':' + "" + '\n'
        self.label.set_text(tstr)