Commit b58ea115 authored by Joshua's avatar Joshua
Browse files

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)
parent e626b7e5
......@@ -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.
......
......@@ -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
......
......@@ -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(
......
......@@ -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
......@@ -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':
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment