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
|
#!/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 subprocess
import gtk
from path import path
import urllib2
from BeautifulSoup import BeautifulSoup
ACTIVITYPATH = path(activity.get_bundle_path())
DATAPATH = path(activity.get_activity_root()) / "data"
WORKPATH = DATAPATH / 'work'
KEYPATH = ACTIVITYPATH / '.ssh'
STOREPATH = path("/library/Datastore/")
COMMONSPATH = path("/library/Commons/")
SERIALNUMBER = '/ofw/serial-number'
USAGETOOLBAR = 1
class DataManager(activity.Activity):
def __init__(self, handle):
activity.Activity.__init__(self, handle)
print 'activity initialized'
self.set_title("Datamanager")
f = open('/ofw/serial-number', 'r')
t = f.read()
self.xoserial = t[:11]
subprocess.call("mkdir -p " + WORKPATH, shell=True)
#Create the usage toolbar
self.usageTB = UsageToolbar(self.xoserial)
#Create the standard activity toolbox
toolbox = activity.ActivityToolbox(self)
toolbox.add_toolbar("Usage", self.usageTB)
toolbox.set_current_toolbar(USAGETOOLBAR)
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.usageTB, self.xoserial))
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, tb, xoserial):
global online
#object is object_id, str = title, str = mime_type or activity,
#str is color of col 1, str is source color of col 0
store = gtk.ListStore(object, str, str, str, str)
#let's display objects from the schoolserver
#temp for testing
url = "http://schoolserver/Commons/"
response = urllib2.urlopen(url)
if response:
self.online = True
self.addentries(store, response, "Blue", xoserial)
url = "http://schoolserver/Datastore/" + xoserial
response = urllib2.urlopen(url)
if response:
print 'response'
self.online = True
self.addentries(store, response, "Green", xoserial)
if self.online:
print 'online'
else:
tb.item2.remove(tb.fuelguage2)
tb.lbl2.set_text("not connected")
#objects from the local datastore
results, count = datastore.find(dict())
print 'number of datastore items', count
for f in results:
obj = datastore.get(f.object_id)
f.destroy()
if len(obj.get_file_path()) > 0:
color1 = "Red"
color2 = "Green"
else:
color1 = "White"
color2 = "Green"
itm = obj.metadata['mime_type']
if len(itm) <= 0:
itm = obj.metadata['activity']
store.append([obj, obj.metadata['title'], obj.metadata['mime_type'], color1, color2 ])
print 'return store'
return store
def addentries(self, store, response, color, xoserial):
soup = BeautifulSoup(response)
entry_candidates = soup.findAll('a')
for entry in entry_candidates:
t = str(entry)
pos = t.find('metadata')
if pos > 0:
pos2 = t.find('href=')
print color, t[pos2+6:pos+8]
obj = t[pos2+6:pos-1]
fn = t[pos2+6:pos+8]
if color == "green":
pth = COMMONSPATH / fn
else:
pth = STOREPATH / xoserial / fn
cmd = "sftp " + xoserial + "@schoolserver: " + pth
print 'get metadata:', cmd
subprocess.call(cmd, shell=True, cwd=WORKPATH)
pth = WORKPATH / fn
f = open(pth, 'r')
t = f.read()
f.close()
metadata = dict(t)
title = metadata['title']
activity = metadata['activity']
mime_type = metadata['mime_type']
store.append([obj, title, mime_type, color, color])
print 'new row', store[len(store) - 1]
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)
class UsageToolbar(gtk.Toolbar):
def __init__(self, xoserial):
gtk.Toolbar.__init__(self)
self.lbl1 = gtk.Label("XO")
self.lbl1.show()
self.item0 = gtk.ToolItem()
self.item0.add(self.lbl1)
self.insert(self.item0, -1)
self.fuelguage1 = gtk.ProgressBar(adjustment=None)
style = self.fuelguage1.get_style().copy()
style.bg[gtk.STATE_NORMAL] = gtk.gdk.color_parse('Blue')
style.bg[gtk.STATE_PRELIGHT] = gtk.gdk.color_parse('Red')
self.fuelguage1.set_style(style)
cmd = 'df -h /home/olpc/.sugar/default/datastore'
#need way to return stdout to get rslt
#subprocess.call(cmd, shell=True)
rslt = 0.69
self.fuelguage1.set_fraction(rslt)
self.fuelguage1.set_orientation(gtk.PROGRESS_LEFT_TO_RIGHT)
self.fuelguage1.show()
self.item1 = gtk.ToolItem()
self.item1.add(self.fuelguage1)
self.insert(self.item1, -1)
self.spacer = gtk.ToolItem()
self.spacer.set_expand(True)
self.insert(self.spacer, -1)
self.lbl2 = gtk.Label("schoolserver")
self.lbl2.show()
self.item3 = gtk.ToolItem()
self.item3.add(self.lbl2)
self.insert(self.item3, -1)
self.fuelguage2 = gtk.ProgressBar(adjustment=None)
style = self.fuelguage2.get_style().copy()
style.bg[gtk.STATE_NORMAL] = gtk.gdk.color_parse('Green')
style.bg[gtk.STATE_PRELIGHT] = gtk.gdk.color_parse('Red')
self.fuelguage2.set_style(style)
#need a way to know if schoolserver not online
#need a way to return rslt from stdout
cmd = "ssh -i " + KEYPATH + xoserial + "@schoolserver "
cmd = cmd + "du -h /library/datastore/" + xoserial
#subprocess.call(cmd, shell=True)
rslt = 0.01
self.fuelguage2.set_fraction(rslt)
self.fuelguage2.set_orientation(gtk.PROGRESS_LEFT_TO_RIGHT)
self.item2 = gtk.ToolItem()
self.item2.add(self.fuelguage2)
self.insert(self.item2, -1)
|