[Sugar-devel] [PATCH] Help needed adding microformat support to the activity updater

akashg1611 akashg1611 at gmail.com
Sat Jul 17 13:20:39 EDT 2010


1.Testing:  For testing purposes I replaced the call to aslo.py with my implemtation of the
  microformat, microformat.fetch_info()
2.Results: For the first time it starts a bit late and the checking stops at the first activity,
  ie. Image Viewer. On closing the software update panel now, and restarting it I get this error:
  raise RuntimeError('Multiple simultaneous requests are not supported')
  What does that mean, is it to prevent undefined behavious from the previously failed software
  updates? I am guessing here, as I just implemented the microformat parser(which seemed to work fine)
  and am now trying to modify it to sync with current backend.
3.Also if I comment out the raised exception above in the code, the software updater panel starts,
  but it hangs on the first activty ie. ImageViewer everytime.
4.As for microformat parser it fetches the activities information from the webpage and gets it into
  a dictionary with the activity_id as it's key, and each key corresponding to a two valued tuple
  (activity_version, activity_url)

I hope this provides enough information. Thanks!
---
 extensions/cpsection/updater/backends/Makefile.am  |    3 +-
 .../cpsection/updater/backends/microformat.py      |  124 ++++++++++++++++++++
 extensions/cpsection/updater/model.py              |    4 +-
 3 files changed, 128 insertions(+), 3 deletions(-)
 mode change 100644 => 100755 extensions/cpsection/updater/backends/aslo.py
 create mode 100644 extensions/cpsection/updater/backends/microformat.py
 mode change 100644 => 100755 extensions/cpsection/updater/view.py

diff --git a/extensions/cpsection/updater/backends/Makefile.am b/extensions/cpsection/updater/backends/Makefile.am
index e280a07..6ed79cb 100644
--- a/extensions/cpsection/updater/backends/Makefile.am
+++ b/extensions/cpsection/updater/backends/Makefile.am
@@ -2,4 +2,5 @@ sugardir = $(pkgdatadir)/extensions/cpsection/updater/backends
 
 sugar_PYTHON = 		\
 	aslo.py		\
-	__init__.py
+	__init__.py	\
+        microformat.py
diff --git a/extensions/cpsection/updater/backends/aslo.py b/extensions/cpsection/updater/backends/aslo.py
old mode 100644
new mode 100755
diff --git a/extensions/cpsection/updater/backends/microformat.py b/extensions/cpsection/updater/backends/microformat.py
new file mode 100644
index 0000000..edeb9f1
--- /dev/null
+++ b/extensions/cpsection/updater/backends/microformat.py
@@ -0,0 +1,124 @@
+#!/usr/bin/python
+# Copyright (C) 2009, Sugar Labs
+#
+# 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 USA
+
+'''Sugar Microformat Parser.
+
+This module implements the microformat parser for the
+activity updater.
+'''
+from HTMLParser import HTMLParser
+from urllib2 import urlopen, URLError
+
+dic = {}
+
+_fetcher = None
+
+
+class _Microformat_UpdateFetcher(object):
+
+    def __init__(self, bundle, completion_cb):
+
+        _MicroformatParser('http://wiki.paraguayeduca.org/index.php/Actividades_y_contenidos')
+        self._completion_cb = completion_cb
+        self._bundle = bundle
+        self._get_updated_info()
+
+    def _get_updated_info(self):
+        try:
+            version, link = dic[self._bundle.get_bundle_id()]
+        except KeyError:
+            print "No bundle with this id"
+	    return
+        else:
+            size = 0
+            global _fetcher
+            _fetcher = None
+            self._completion_cb(self._bundle, version, link, size, None)
+
+class _MicroformatParser(HTMLParser):
+
+	'''Microformat parser for the activity updater'''
+
+	def __init__(self,url):
+		HTMLParser.__init__(self)
+		self._activity_id = ''
+		self._activity_url = ''
+		self._activity_version = ''
+		self._inside_activity_version = False
+		self._inside_activity_id = False
+		self._inside_activity_url = False
+
+		try:
+	        	req = urlopen(url)
+		except URLError, e:
+			if hasattr(e, 'reason'):
+		        	print 'you failed to reach a server.'
+		        	print 'Reason: ', e.reason
+			elif hasattr(e, 'code'):
+			        print 'The server couldn\'t fulfill the request.'
+			        print 'Error code: ', e.code
+		else:
+			self.feed(req.read())
+
+	def handle_data(self,data):
+		if self._inside_activity_version:
+			self._activity_version = data
+			dic[self._activity_id] = (self._activity_version, self._activity_url)
+			self._inside_activity_version = False
+		elif self._inside_activity_id:
+			self._activity_id = data
+			self._inside_activity_id = False
+
+	def handle_starttag(self, tag, attrs):
+		if tag == 'span':
+			for attribute,value in attrs:
+				if value == 'olpc-activity-id':
+					self._inside_activity_id = True
+				elif value == 'olpc-activity-version':
+					self._inside_activity_version = True
+				elif value == 'olpc-activity-url':
+					self._inside_activity_url = True
+
+		elif tag == 'a':
+			if self._inside_activity_url:
+				for attribute,value in attrs:
+					if attribute == 'href':
+						self._activity_url = value
+						self._inside_activity_url = False
+
+def fetch_info(bundle, completion_cb):
+    '''Here it queries the microformat information about the activity bundle in
+       from one of the links for the listed activities,
+       ex:
+       completion_cb receives bundle, version, link, size and possibly an error
+       message:
+       
+       def completion_cb(bundle, version, link, size, error_message):
+    '''
+    global _fetcher
+
+    if _fetcher is not None:
+        raise RuntimeError('Multiple simultaneous requests are not supported')
+
+    _fetcher = _Microformat_UpdateFetcher(bundle, completion_cb)
+
+
+
+
+#Filling up the dictionary with all the activity information
+#_MicroformatParser('http://wiki.paraguayeduca.org/index.php/Actividades_y_contenidos')
+#print dic
diff --git a/extensions/cpsection/updater/model.py b/extensions/cpsection/updater/model.py
index 9845371..d64e928 100755
--- a/extensions/cpsection/updater/model.py
+++ b/extensions/cpsection/updater/model.py
@@ -37,7 +37,7 @@ from sugar.bundle.activitybundle import ActivityBundle
 from jarabe.model import bundleregistry
 
 from backends import aslo
-
+from backends import microformat
 
 class UpdateModel(gobject.GObject):
     __gtype_name__ = 'SugarUpdateModel'
@@ -76,7 +76,7 @@ class UpdateModel(gobject.GObject):
         self.emit('progress', UpdateModel.ACTION_CHECKING, bundle.get_name(),
                   current, total)
 
-        aslo.fetch_update_info(bundle, self.__check_completed_cb)
+        microformat.fetch_info(bundle, self.__check_completed_cb)
 
     def __check_completed_cb(self, bundle, version, link, size, error_message):
         if error_message is not None:
diff --git a/extensions/cpsection/updater/view.py b/extensions/cpsection/updater/view.py
old mode 100644
new mode 100755
-- 
1.7.0.4



More information about the Sugar-devel mailing list