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
|
#!/usr/bin/python
# ZetCode PyGTK tutorial
#
# This example shows a TreeView widget
# in a list view mode
#
# author: jan bodnar
# website: zetcode.com
# last edited: February 2009
import sys, os
import gtk
import objectfetch
import urllib2
from BeautifulSoup import BeautifulSoup
from xml.dom.minidom import parse, parseString
from sugar.activity import activity
from sugar.datastore import datastore
from path import path
ACTIVITYPATH = path(activity.get_bundle_path())
global online
online = False
class PyApp(gtk.Window):
def __init__(self):
super(PyApp, self).__init__()
self.set_size_request(350, 250)
self.set_position(gtk.WIN_POS_CENTER)
self.connect("destroy", gtk.main_quit)
self.set_title("DataManager")
vbox = gtk.VBox(False, 8)
sw = gtk.ScrolledWindow()
sw.set_shadow_type(gtk.SHADOW_ETCHED_IN)
sw.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
vbox.pack_start(sw, True, True, 0)
store = self.create_model()
treeView = gtk.TreeView(store)
treeView.connect("row-activated", self.on_activated)
treeView.set_rules_hint(True)
sw.add(treeView)
self.create_columns(treeView)
self.statusbar = gtk.Statusbar()
vbox.pack_start(self.statusbar, False, False, 0)
self.add(vbox)
self.show_all()
def create_model(self):
global online
store = gtk.ListStore(str, str, str)
#let's display objects from the schoolserver
pth = ACTIVITYPATH / 'xoconfig'
configf = open(pth, "r")
config = configf.read()
configf.close()
pos = config.find('[Server]')
if pos < 0:
print 'schoolserver not found'
xoserial = None
store.append(["schoolserver not found", "",""])
else:
pos = pos + 8
print 'config', config[pos:]
pos1 = pos + config[pos:].find("= ") + 2
pos2 = pos + config[pos:].find("@")
xoserial = config[pos1:pos2]
print 'xoserial', xoserial, config[pos1:pos2+1]
#temp for testing
xoserial = 'SHC84201F3B'
if xoserial:
url = "http://schoolserver/ds-restore/" + xoserial + "/datastore-latest"
print 'url', url
try:
response = urllib2.urlopen(url)
except:
print 'schoolserver not found'
response = None
if response:
soup = BeautifulSoup(response)
online = True
else:
print 'schoolserver not found at', url
if online:
print 'soup=', soup.prettify()
entries = soup.findAll("li")
print len(entries)
for i in range(10):
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
for f in results:
obj = datastore.get(f.object_id)
f.destroy()
store.append([obj.get_file_path(), obj.metadata['title'], obj.metadata['mime_type']])
#ignore for the moment
return store
def create_columns(self, treeView):
rendererText = gtk.CellRendererText()
column = gtk.TreeViewColumn("Title", rendererText, text=1)
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()
text = model[row][0] + ", " + model[row][1] + ", " + model[row][2]
self.statusbar.push(0, text)
PyApp()
gtk.main()
|