avoid creating up to 3 flickr_manager instances
authorThomas Schmidt <tschmidt@debian.org>
Fri, 23 Oct 2009 22:52:56 +0000 (00:52 +0200)
committerThomas Schmidt <tschmidt@debian.org>
Fri, 23 Oct 2009 22:52:56 +0000 (00:52 +0200)
canola-flickr/flickr/manager.py
canola-flickr/flickr/model.py
canola-flickr/flickr/options.py

index 0955891..5d93b4d 100644 (file)
@@ -113,11 +113,11 @@ class SmartRedirectHandler(urllib2.HTTPRedirectHandler):
         result.status = code
         return result
 
-flickr_manager = FlickrManager()
 class DownloadManager:
-    def __init__(self, verbose=False):
-        self.verbose    = verbose
-        self.opener     = urllib2.build_opener(SmartRedirectHandler())
+    def __init__(self, flickr_manager, verbose=False):
+        self.verbose        = verbose
+        self.opener         = urllib2.build_opener(SmartRedirectHandler())
+        self.flickr_manager = flickr_manager
 
     def download_image(self, url, force=False, thumbnail=False, buddyicon=False):
         '''
@@ -133,11 +133,11 @@ class DownloadManager:
             return None
 
         if thumbnail:
-            image_path = os.path.join(flickr_manager.get_image_dir(thumbnails=True), filename)
+            image_path = os.path.join(self.flickr_manager.get_image_dir(thumbnails=True), filename)
         elif buddyicon:
-            image_path = os.path.join(flickr_manager.get_image_dir(buddyicons=True), filename)
+            image_path = os.path.join(self.flickr_manager.get_image_dir(buddyicons=True), filename)
         else:
-            image_path = os.path.join(flickr_manager.get_image_dir(), filename)
+            image_path = os.path.join(self.flickr_manager.get_image_dir(), filename)
 
         # download image if the file does not exist locally
         if force or not os.path.exists(image_path):
@@ -190,7 +190,8 @@ class DownloadManager:
         return False
 
 def main():
-    dl = DownloadManager(verbose=True)
+    fm = FlickrManager()
+    dl = DownloadManager(flickr_manager=fm, verbose=True)
     image_path = dl.download_image("http://farm4.static.flickr.com/3114/2713814846_fa32aaaa19_s.jpg")
     print "image_path: %s" % image_path
 
index 36cc997..85a25c7 100644 (file)
@@ -36,7 +36,7 @@ from flickr import Photo
 mger = Manager()
 
 flickr_manager      = FlickrManager()
-download_manager    = DownloadManager()
+download_manager    = DownloadManager(flickr_manager=flickr_manager)
 
 from helper import Xlator
 
@@ -101,9 +101,10 @@ class ImageModel(ImageLocalModel):
         self.thumbnail      = None
         self.photo          = photo
         self.no_download    = no_download
+        self.flickr_manager = flickr_manager
 
         try:
-            self.owner = flickr_manager.users[self.photo.owner.nsid]
+            self.owner = self.flickr_manager.users[self.photo.owner.nsid]
         except KeyError:
             log.debug("Error, owner of %s not cached!" % self.photo)
             self.owner = self.photo.owner
@@ -121,7 +122,7 @@ class ImageModel(ImageLocalModel):
     def get_photo(self):
         # check if we already have a image-file for this
         # photo in the download-folder
-        photopath = flickr_manager.search_imagefile(self.photo)
+        photopath = self.flickr_manager.search_imagefile(self.photo)
 
         if photopath:
             path = photopath
@@ -149,7 +150,7 @@ class ImageModel(ImageLocalModel):
             if self.no_download:
                 return None
 
-            image_dir = flickr_manager.get_image_dir()
+            image_dir = self.flickr_manager.get_image_dir()
             if space_available(image_dir):
                 photo = self.photo
 
@@ -205,7 +206,7 @@ class ImageModel(ImageLocalModel):
                     old_path = path
 
                     log.debug("getting sizes-info of photo (title: %s) (photo_res: %s px, theshold_min: %s px)" % (photo.title, photo_res, threshold_min))
-                    sizes   = flickr_manager.get_photosizes(photo)
+                    sizes   = self.flickr_manager.get_photosizes(photo)
                     size    = sizes[-1]
 
                     if size.label == "Video Player":
@@ -244,12 +245,12 @@ class ImageModel(ImageLocalModel):
             return self.thumbnail
 
         filename = "%s_%s_s.jpg" % (self.photo.id, self.photo.secret)
-        thumbnail = os.path.join(flickr_manager.get_image_dir(thumbnails=True), filename)
+        thumbnail = os.path.join(self.flickr_manager.get_image_dir(thumbnails=True), filename)
         if os.path.exists(thumbnail):
             self.thumbnail = thumbnail
             return thumbnail
         else:
-            image_dir = flickr_manager.get_image_dir(thumbnails=True)
+            image_dir = self.flickr_manager.get_image_dir(thumbnails=True)
             if space_available(image_dir, bytes_required=1024*50):
                 photo = self.photo
                 url = "http://farm%s.static.flickr.com/%s/%s" % (photo.icon_farm, photo.server, filename)
@@ -335,6 +336,7 @@ class ServiceModelFolder(Task, ModelFolder):
         self.callback_search_finished = None
         self.callback_notify = None
         self.empty_msg = None
+        self.flickr_manager = flickr_manager
 
     def do_load(self):
         self.search()
@@ -368,7 +370,7 @@ class ContactsModelFolder(ServiceModelFolder):
         self.per_page   = per_page
 
     def do_search(self):
-        contactlist = flickr_manager.get_contacts(self.user, per_page=self.per_page)
+        contactlist = self.flickr_manager.get_contacts(self.user, per_page=self.per_page)
 
         if len(contactlist.contacts) > 0:
             # only add ContactsRecentPhotosModelFolder in
@@ -406,13 +408,13 @@ class ContactModelFolder(ServiceModelFolder):
         if not self.has_avatar:
             return None
 
-        self.thumbnail = flickr_manager.search_buddyicon(self.user)
+        self.thumbnail = self.flickr_manager.search_buddyicon(self.user)
         if not self.thumbnail == None:
             return self.thumbnail
         else:
-            image_dir = flickr_manager.get_image_dir(buddyicons=True)
+            image_dir = self.flickr_manager.get_image_dir(buddyicons=True)
             if space_available(image_dir, bytes_required=1024*50):
-                url = flickr_manager.get_buddyicon_url(self.user)
+                url = self.flickr_manager.get_buddyicon_url(self.user)
                 self.thumbnail = download_manager.download_image(url, buddyicon=True)
 
                 # if we did not receive an buddyicon at the first try,
@@ -456,7 +458,7 @@ class HottagModelFolder(ServiceModelFolder):
         ServiceModelFolder.__init__(self, to_utf8(title), parent)
 
     def do_search(self):
-        hottags = flickr_manager.get_hottags(period=self.period, count=self.count)
+        hottags = self.flickr_manager.get_hottags(period=self.period, count=self.count)
         for score, tag in hottags.tags:
             SearchByTagModelFolder(self, all=True, query=tag, auto_reset=False)
 
@@ -480,7 +482,7 @@ class GroupsModelFolder(ServiceModelFolder):
             return False
 
     def do_search(self):
-        grouplist = flickr_manager.get_groups(self.user)
+        grouplist = self.flickr_manager.get_groups(self.user)
         return self.add_groups(grouplist)
 
 class ImageListModelFolder(ServiceModelFolder):
@@ -537,7 +539,7 @@ class GroupModelFolder(ImageListModelFolder):
         ImageListModelFolder.__init__(self, title, parent)
 
     def do_search(self):
-        photolist = flickr_manager.get_photos_grouppool(group=self.group, per_page=self.per_page)
+        photolist = self.flickr_manager.get_photos_grouppool(group=self.group, per_page=self.per_page)
         return self.add_photos(photolist)
 
     def get_thumbnail(self):
@@ -548,13 +550,13 @@ class GroupModelFolder(ImageListModelFolder):
         if not self.has_avatar:
             return None
 
-        self.thumbnail = flickr_manager.search_buddyicon(self.group)
+        self.thumbnail = self.flickr_manager.search_buddyicon(self.group)
         if not self.thumbnail == None:
             return self.thumbnail
         else:
-            image_dir = flickr_manager.get_image_dir(buddyicons=True)
+            image_dir = self.flickr_manager.get_image_dir(buddyicons=True)
             if space_available(image_dir, bytes_required=1024*50):
-                url = flickr_manager.get_buddyicon_url(self.group, group=True)
+                url = self.flickr_manager.get_buddyicon_url(self.group, group=True)
                 self.thumbnail = download_manager.download_image(url, buddyicon=True)
 
                 # if we did not receive an buddyicon at the first try,
@@ -588,7 +590,7 @@ class FavoritesModelFolder(ImageListModelFolder):
         self.per_page   = per_page
 
     def do_search(self):
-        photolist = flickr_manager.get_favorites(user=self.user, per_page=self.per_page)
+        photolist = self.flickr_manager.get_favorites(user=self.user, per_page=self.per_page)
         return self.add_photos(photolist)
 
 class LatestImagesListModelFolder(ImageListModelFolder):
@@ -600,7 +602,7 @@ class LatestImagesListModelFolder(ImageListModelFolder):
         self.per_page   = per_page
 
     def do_search(self):
-        photolist = flickr_manager.get_photos_latest(user=self.user, per_page=self.per_page)
+        photolist = self.flickr_manager.get_photos_latest(user=self.user, per_page=self.per_page)
         return self.add_photos(photolist)
 
 class ExploreModelFolder(ServiceModelFolder):
@@ -646,7 +648,7 @@ class ExplorePhotosModelFolder(ImageListModelFolder):
             self.empty_msg  = "No interesting photos from<br>%s" % date.strftime("%A, %x")
 
     def do_search(self):
-        photolist = flickr_manager.get_photos_interesting(page=self.page, per_page=self.per_page, date=self.date)
+        photolist = self.flickr_manager.get_photos_interesting(page=self.page, per_page=self.per_page, date=self.date)
         return self.add_photos(photolist)
 
 class RecentPhotosModelFolder(ImageListModelFolder):
@@ -658,7 +660,7 @@ class RecentPhotosModelFolder(ImageListModelFolder):
         self.per_page   = per_page
 
     def do_search(self):
-        photolist = flickr_manager.get_photos_recent(page=self.page, per_page=self.per_page)
+        photolist = self.flickr_manager.get_photos_recent(page=self.page, per_page=self.per_page)
         return self.add_photos(photolist)
 
 class ContactsRecentPhotosModelFolder(ImageListModelFolder):
@@ -671,7 +673,7 @@ class ContactsRecentPhotosModelFolder(ImageListModelFolder):
         self.just_friends   = just_friends
 
     def do_search(self):
-        photolist = flickr_manager.get_photos_contacts(count=self.count, include_self=self.include_self, just_friends=self.just_friends)
+        photolist = self.flickr_manager.get_photos_contacts(count=self.count, include_self=self.include_self, just_friends=self.just_friends)
         return self.add_photos(photolist)
 
     def request_thumbnail(self, end_callback=None):
@@ -724,7 +726,7 @@ class SearchByTagModelFolder(ImageListModelFolder, SearchModelFolder):
         if self.query:
             # replace spaces in query with commas, search_tags expects a comma-separated list
             query = self.convert_query(self.query)
-            photolist = flickr_manager.search_tags(query, all=self.all, contacts=self.contacts, per_page=self.per_page)
+            photolist = self.flickr_manager.search_tags(query, all=self.all, contacts=self.contacts, per_page=self.per_page)
             if self.auto_reset:
                 self.query = None
             return self.add_photos(photolist)
@@ -741,7 +743,7 @@ class SearchGroupsModelFolder(GroupsModelFolder, SearchModelFolder):
         if self.query:
             # replace spaces in query with commas, search_tags expects a comma-separated list
             query = self.convert_query(self.query)
-            grouplist = flickr_manager.search_groups(query, per_page=self.per_page)
+            grouplist = self.flickr_manager.search_groups(query, per_page=self.per_page)
             self.query = None
             return self.add_groups(grouplist)
 
@@ -758,7 +760,7 @@ class PhotosetModelFolder(ImageListModelFolder):
         ImageListModelFolder.__init__(self, name, parent)
 
     def do_search(self):
-        photolist = flickr_manager.get_photos_photoset(photoset=self.photoset, user=self.user, per_page=self.per_page)
+        photolist = self.flickr_manager.get_photos_photoset(photoset=self.photoset, user=self.user, per_page=self.per_page)
         return self.add_photos(photolist)
 
     def get_thumbnail(self):
@@ -808,7 +810,7 @@ class PhotosetsListModelFolder(ServiceModelFolder):
         self.user       = user
 
     def do_search(self):
-        photosets = flickr_manager.get_photosets(user=self.user)
+        photosets = self.flickr_manager.get_photosets(user=self.user)
 
         if photosets:
             for photoset in photosets:
@@ -853,6 +855,10 @@ class UploadModel(ServiceModelFolder):
         return False
 
 class OptionsModel(OptionsModelFolder):
+    def __init__(self, parent, screen_controller=None):
+        OptionsModelFolder.__init__(self, parent, screen_controller)
+        self.flickr_manager = self.screen_controller.model.flickr_manager
+
     def get_image_model(self):
         model = self.screen_controller.model
         return model.children[model.current]
@@ -872,7 +878,7 @@ class ViewerOptionsModel(OptionsModel):
     title = "Photo options"
 
     def __init__(self, parent, screen_controller=None):
-        OptionsModelFolder.__init__(self, parent, screen_controller)
+        OptionsModel.__init__(self, parent, screen_controller)
 
         self.children_order = [
                     "/SlideshowMode",
@@ -898,12 +904,12 @@ class FavoriteOptionModel(OptionsModel):
         photo = parent.get_photo()
 
         # show Favorite option only in case the photo owner is not the authenticated user
-        if photo.owner.nsid == flickr_manager.user.nsid:
+        if photo.owner.nsid == parent.flickr_manager.user.nsid:
             return
 
         OptionsModel.__init__(self, parent)
         self.photo          = photo
-        self.is_favorite    = flickr_manager.is_favorite(photo)
+        self.is_favorite    = self.flickr_manager.is_favorite(photo)
         self.checked        = self.is_favorite
         self.set_title()
 
@@ -918,14 +924,14 @@ class FavoriteOptionModel(OptionsModel):
             # set new photo as the current photo
             self.photo = self.get_photo()
 
-        self.is_favorite = flickr_manager.is_favorite(self.photo)
+        self.is_favorite = self.flickr_manager.is_favorite(self.photo)
         self.set_title()
 
     def execute(self):
         if self.is_favorite:
-            return flickr_manager.remove_favorite(self.photo)
+            return self.flickr_manager.remove_favorite(self.photo)
         else:
-            return flickr_manager.add_favorite(self.photo)
+            return self.flickr_manager.add_favorite(self.photo)
 
 class CommentOptionModel(OptionsModel):
     terra_type = "Model/Options/Folder/Player/Flickr/Image/CommentsList/Item"
@@ -936,7 +942,7 @@ class CommentsListOptionModel(OptionsModel):
     comments_per_page = 25
 
     def __init__(self, parent, screen_controller=None):
-        OptionsModelFolder.__init__(self, parent, screen_controller)
+        OptionsModel.__init__(self, parent, screen_controller)
         self.is_loading         = False
         self.page               = 1
         self.comments           = None
@@ -945,10 +951,10 @@ class CommentsListOptionModel(OptionsModel):
         self.photo = self.get_photo()
 
     def get_comments(self):
-        if not self.photo.id in flickr_manager.comments:
-            self.comments = flickr_manager.get_photo_comments(self.photo)
+        if not self.photo.id in self.flickr_manager.comments:
+            self.comments = self.flickr_manager.get_photo_comments(self.photo)
         else:
-            self.comments = flickr_manager.comments[self.photo.id]
+            self.comments = self.flickr_manager.comments[self.photo.id]
         self.num_comments = len(self.comments)
 
     def get_index_start(self):
index 4f9311c..56d3992 100644 (file)
@@ -33,7 +33,7 @@ from terra.utils.encoding import to_utf8
 from manager import FlickrManager
 
 manager = Manager()
-flickr_manager = FlickrManager()
+#flickr_manager = FlickrManager()
 
 from terra.ui.modal import ModalMessage
 
@@ -92,7 +92,7 @@ class InfoOptionController(BasicPanel):
     def setup_information(self):
         photo = self.model.get_photo()
         if not photo.have_info:
-            photo = flickr_manager.get_photo_info(photo)
+            photo = self.model.flickr_manager.get_photo_info(photo)
             self.model.set_photo(photo)
 
         self._body.part_text_set("title", to_utf8(photo.title))
@@ -139,7 +139,7 @@ class ExifInfoOptionController(InfoOptionController):
     def setup_information(self):
         photo = self.model.get_photo()
         if not photo.have_exif:
-            photo = flickr_manager.get_exif_info(photo)
+            photo = self.model.flickr_manager.get_exif_info(photo)
             self.model.set_photo(photo)
 
         self._body.part_text_set("title", to_utf8(photo.title))
@@ -222,7 +222,7 @@ class UserInfoOptionController(InfoOptionController):
         user        = user_model.user
 
         if not user.have_info:
-            flickr_manager.get_userinfo(user)
+            self.model.flickr_manager.get_userinfo(user)
 
         self._body.part_text_set("username", to_utf8(user.__repr__()))
 
@@ -269,7 +269,7 @@ class GroupInfoOptionController(UserInfoOptionController):
         group       = group_model.group
 
         if not group.have_info:
-            flickr_manager.get_groupinfo(group)
+            self.model.flickr_manager.get_groupinfo(group)
 
         self._body.part_text_set("username", to_utf8(group.name))
 
@@ -393,13 +393,13 @@ class UploadStatusOptionController(InfoOptionController):
     terra_type = "Controller/Options/Folder/Player/Flickr/Upload/Status"
 
     def setup_information(self):
-        if flickr_manager.upload_status == None:
-            flickr_manager.get_uploadstatus()
+        if self.model.flickr_manager.upload_status == None:
+            self.model.flickr_manager.get_uploadstatus()
 
-        upload_status = flickr_manager.upload_status
+        upload_status = self.model.flickr_manager.upload_status
 
         self._body.part_text_set("title", "")
-        self._body.part_text_set("author", to_utf8(flickr_manager.user.__repr__()))
+        self._body.part_text_set("author", to_utf8(self.model.flickr_manager.user.__repr__()))
         self._body.part_text_set("date_taken", "")
         self._body.part_swallow("contents", self.thumbnail)