From b58ea115882f08b74a7ab0066618593c9c7ee6e8 Mon Sep 17 00:00:00 2001 From: Joshua Date: Sun, 3 Jan 2010 21:42:03 -0600 Subject: [PATCH] Added ModelFactory. This replaces the 'models' dict in the tweepy.models module. This will allow for more flexible plug 'n play for developers that need to extend Tweepy's models. To use custom models, they will extend the ModelFactory and then pass this new factory into the API constructor. Example: class MyStatus(Status): """A extended Status model""" class MyModelFactory(ModelFactory): status = MyStatus api = API(model_factory=MyModelFactory) --- CHANGELOG | 2 ++ tweepy/__init__.py | 2 +- tweepy/api.py | 6 ++++-- tweepy/models.py | 27 ++++++++++++++++----------- tweepy/parsers.py | 19 +++++++++---------- 5 files changed, 32 insertions(+), 24 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index c438efa..e666ea0 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -8,6 +8,8 @@ during upgrade will be listed here. - User model - Added lists_memberships, lists_subscriptions, and lists helpers - Added followers_ids helper + - Added ModelFactory to replace 'models' dict in tweepy.models. + Extend this factory to plugin customized models then pass into API(). + API - lists(), lists_memberships(), and lists_subscriptions() now take an "user" parameter for specifying which user to query. diff --git a/tweepy/__init__.py b/tweepy/__init__.py index 89b54d5..41da6e7 100644 --- a/tweepy/__init__.py +++ b/tweepy/__init__.py @@ -9,7 +9,7 @@ __version__ = '1.4' __author__ = 'Joshua Roesslein' __license__ = 'MIT' -from tweepy.models import Status, User, DirectMessage, Friendship, SavedSearch, SearchResult, models +from tweepy.models import Status, User, DirectMessage, Friendship, SavedSearch, SearchResult, ModelFactory from tweepy.error import TweepError from tweepy.api import API from tweepy.cache import Cache, MemoryCache, FileCache diff --git a/tweepy/api.py b/tweepy/api.py index 36304e6..4dfdafb 100644 --- a/tweepy/api.py +++ b/tweepy/api.py @@ -8,6 +8,7 @@ import mimetypes from tweepy.binder import bind_api from tweepy.error import TweepError from tweepy.parsers import * +from tweepy.models import ModelFactory class API(object): @@ -16,8 +17,8 @@ class API(object): def __init__(self, auth_handler=None, host='api.twitter.com', search_host='search.twitter.com', cache=None, secure=False, api_root='/1', search_root='', - retry_count=0, retry_delay=0, retry_errors=None): - # you may access these freely + retry_count=0, retry_delay=0, retry_errors=None, + model_factory=None): self.auth = auth_handler self.host = host self.search_host = search_host @@ -28,6 +29,7 @@ class API(object): self.retry_count = retry_count self.retry_delay = retry_delay self.retry_errors = retry_errors + self.model_factory = model_factory or ModelFactory """ statuses/public_timeline """ public_timeline = bind_api( diff --git a/tweepy/models.py b/tweepy/models.py index 1b3d648..e544558 100644 --- a/tweepy/models.py +++ b/tweepy/models.py @@ -126,15 +126,20 @@ class List(Model): def is_subscribed(self, id): return self._api.is_subscribed_list(self.user.screen_name, self.slug, id) -# link up default model implementations. -models = { - 'status': Status, - 'user': User, - 'direct_message': DirectMessage, - 'friendship': Friendship, - 'saved_search': SavedSearch, - 'search_result': SearchResult, - 'retweet': Retweet, - 'list': List, -} + +class ModelFactory(object): + """ + Used by parsers for creating instances + of models. You may subclass this factory + to add your own extended models. + """ + + status = Status + user = User + direct_message = DirectMessage + friendship = Friendship + saved_search = SavedSearch + search_result = SearchResult + retweet = Retweet + list = List diff --git a/tweepy/parsers.py b/tweepy/parsers.py index 0ea115e..6eff9e5 100644 --- a/tweepy/parsers.py +++ b/tweepy/parsers.py @@ -7,7 +7,6 @@ import re from datetime import datetime import time -from tweepy.models import models def _parse_cursor(obj): @@ -82,7 +81,7 @@ def _parse_a_href(atag): def parse_user(obj, api): - user = models['user']() + user = api.model_factory.user() user._api = api for k, v in obj.items(): if k == 'created_at': @@ -116,7 +115,7 @@ def parse_users(obj, api): def parse_status(obj, api): - status = models['status']() + status = api.model_factory.status() status._api = api for k, v in obj.items(): if k == 'user': @@ -148,7 +147,7 @@ def parse_statuses(obj, api): def parse_dm(obj, api): - dm = models['direct_message']() + dm = api.model_factory.direct_message() dm._api = api for k, v in obj.items(): if k == 'sender' or k == 'recipient': @@ -173,12 +172,12 @@ def parse_friendship(obj, api): relationship = obj['relationship'] # parse source - source = models['friendship']() + source = api.model_factory.friendship() for k, v in relationship['source'].items(): setattr(source, k, v) # parse target - target = models['friendship']() + target = api.model_factory.friendship() for k, v in relationship['target'].items(): setattr(target, k, v) @@ -194,7 +193,7 @@ def parse_ids(obj, api): def parse_saved_search(obj, api): - ss = models['saved_search']() + ss = api.model_factory.saved_search() ss._api = api for k, v in obj.items(): if k == 'created_at': @@ -207,7 +206,7 @@ def parse_saved_search(obj, api): def parse_saved_searches(obj, api): saved_searches = [] - saved_search = models['saved_search']() + saved_search = api.model_factory.saved_search() for item in obj: saved_searches.append(parse_saved_search(item, api)) return saved_searches @@ -215,7 +214,7 @@ def parse_saved_searches(obj, api): def parse_search_result(obj, api): - result = models['search_result']() + result = api.model_factory.search_result() for k, v in obj.items(): if k == 'created_at': setattr(result, k, _parse_search_datetime(v)) @@ -237,7 +236,7 @@ def parse_search_results(obj, api): def parse_list(obj, api): - lst = models['list']() + lst = api.model_factory.list() lst._api = api for k,v in obj.items(): if k == 'user': -- 2.25.1