make feedparser,rdflib,datafeed only recommands
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Fri, 06 Aug 2010 10:02:32 +0200
changeset 220 72715685d5e3
parent 219 852e7c11f252
child 221 1f8273d23dae
make feedparser,rdflib,datafeed only recommands
sobjects.py
--- a/sobjects.py	Fri Aug 06 10:01:47 2010 +0200
+++ b/sobjects.py	Fri Aug 06 10:02:32 2010 +0200
@@ -5,14 +5,25 @@
 from datetime import datetime
 
 from lxml.html import fromstring, tostring
-import feedparser
-import rdflib
+
+try:
+    import feedparser
+except ImportError:
+    feedparser = None
 
-from cubes.datafeed.sobjects import DataFeedParser
+try:
+    import rdflib
+except ImportError:
+    rdflib = None
+else:
+    RDF = rdflib.Namespace('http://www.w3.org/1999/02/22-rdf-syntax-ns#')
+    SIOC = rdflib.Namespace('http://rdfs.org/sioc/ns#')
+    DCTERMS = rdflib.Namespace('http://purl.org/dc/terms/')
 
-RDF = rdflib.Namespace('http://www.w3.org/1999/02/22-rdf-syntax-ns#')
-SIOC = rdflib.Namespace('http://rdfs.org/sioc/ns#')
-DCTERMS = rdflib.Namespace('http://purl.org/dc/terms/')
+try:
+    from cubes.datafeed.sobjects import DataFeedParser
+except ImportError:
+    DataFeedParser = None
 
 def get_subject(g, pred, obj):
     subjects = list(g.subjects(pred, obj))
@@ -135,50 +146,54 @@
         AVATAR_CACHE[screen_name] = user['profile_image_url']
     return AVATAR_CACHE[screen_name]
 
-class BlogPostParser(DataFeedParser):
-    __abstract__ = True
-    entity_type = 'BlogEntry'
+if DataFeedParser is not None:
+    class BlogPostParser(DataFeedParser):
+        __abstract__ = True
+        entity_type = 'BlogEntry'
+
+        def process(self, url):
+            for item in self.parse(url):
+                author = item.pop('author', None)
+                avatar = item.pop('avatar', None)
+                euri = self.sget_entity('ExternalUri', uri=item.pop('uri'))
+                if euri.same_as:
+                    sys.stdout.write('.')
+                    post = self.update_blogpost(euri.same_as[0], item)
+                else:
+                    sys.stdout.write('+')
+                    post = self.create_blogpost(item, euri)
+                if author:
+                    account = self.sget_entity('UserAccount', name=author)
+                    self.sget_relation(post.eid, 'has_creator', account.eid)
+                    if avatar:
+                        auri = self.sget_entity('ExternalUri', uri=avatar)
+                        self.sget_relation(account.eid, 'has_avatar', auri.eid)
+                sys.stdout.flush()
 
-    def process(self, url):
-        for item in self.parse(url):
-            author = item.pop('author', None)
-            avatar = item.pop('avatar', None)
-            euri = self.sget_entity('ExternalUri', uri=item.pop('uri'))
-            if euri.same_as:
-                sys.stdout.write('.')
-                post = self.update_blogpost(euri.same_as[0], item)
-            else:
-                sys.stdout.write('+')
-                post = self.create_blogpost(item, euri)
-            if author:
-                account = self.sget_entity('UserAccount', name=author)
-                self.sget_relation(post.eid, 'has_creator', account.eid)
-                if avatar:
-                    auri = self.sget_entity('ExternalUri', uri=avatar)
-                    self.sget_relation(account.eid, 'has_avatar', auri.eid)
-            sys.stdout.flush()
+        def create_blogpost(self, item, uri):
+            entity = self._cw.create_entity(self.entity_type, **item)
+            entity.set_relations(same_as=uri)
+            return entity
+
+        def update_blogpost(self, entity, item):
+            entity.set_attributes(**item)
+            return entity
 
-    def create_blogpost(self, item, uri):
-        entity = self._cw.create_entity(self.entity_type, **item)
-        entity.set_relations(same_as=uri)
-        return entity
-
-    def update_blogpost(self, entity, item):
-        entity.set_attributes(**item)
-        return entity
+    if rdflib is not None:
+        class BlogPostSiocParser(BlogPostParser):
+            __regid__ = 'blogpost-sioc'
+            parse = staticmethod(parse_blogpost_sioc)
 
-class BlogPostSiocParser(BlogPostParser):
-    __regid__ = 'blogpost-sioc'
-    parse = staticmethod(parse_blogpost_sioc)
+    if feedparser is not None:
+        class BlogPostRSSParser(BlogPostParser):
+            __regid__ = 'blogpost-rss'
+            parse = staticmethod(parse_blogpost_rss)
 
-class BlogPostRSSParser(BlogPostParser):
-    __regid__ = 'blogpost-rss'
-    parse = staticmethod(parse_blogpost_rss)
+        class MicroBlogPostRSSParser(BlogPostParser):
+            __regid__ = 'microblogpost-rss'
+            entity_type = 'MicroBlogEntry'
+            parse = staticmethod(parse_microblogpost_rss)
 
-class MicroBlogPostRSSParser(BlogPostParser):
-    __regid__ = 'microblogpost-rss'
-    entity_type = 'MicroBlogEntry'
-    parse = staticmethod(parse_microblogpost_rss)
 
 if __name__ == '__main__':
     import sys
@@ -189,4 +204,3 @@
 
     parser = globals()[name]
     pprint(list(parser(url)))
-