# Path: oembed/providers.py
# class BaseProvider(object):
#     """
#     Base class for OEmbed resources.
#     """
#     regex = None # regex this provider will match
#     provides = True  # allow this provider to be accessed by third parties
#     
#     def request_resource(self, url, **kwargs):
#         """
#         Get an OEmbedResource from one of the providers configured in this 
#         provider according to the resource url.
#         
#         Args:
#             url: The url of the resource to get.
#             format: Desired response format (json or xml).
#             **kwargs: Optional parameters to pass in to the provider.
#         
#         Returns:
#             OEmbedResource object.
#             
#         If no object returned, raises OEmbedException
#         """
#         raise NotImplementedError
# 
# Path: oembed/resources.py
# class OEmbedResource(object):
#     """
#     OEmbed resource, as well as a factory for creating resource instances
#     from response json
#     """
#     _data = {}
#     content_object = None
#     
#     def __getattr__(self, name):
#         return self._data.get(name)
#     
#     def get_data(self):
#         return self._data
#         
#     def load_data(self, data):
#         self._data = data
#     
#     @property
#     def json(self):
#         return simplejson.dumps(self._data)
#     
#     @classmethod
#     def create(cls, data):
#         if not 'type' in data or not 'version' in data:
#             raise OEmbedException('Missing required fields on OEmbed response.')
# 
#         data['width'] = data.get('width') and int(data['width']) or None
#         data['height'] = data.get('height') and int(data['height']) or None
#         
#         filtered_data = dict([(k, v) for k, v in data.items() if v])
#         
#         resource = cls()
#         resource.load_data(filtered_data)
#         
#         return resource
# 
#     @classmethod
#     def create_json(cls, raw):
#         data = simplejson.loads(raw)
#         return cls.create(data)
# 
# Path: oembed/tests/settings.py
# MEDIA_ROOT = '%s/media/' % (SITE_ROOT)
# 
# MEDIA_URL = '/media/'
# 
# DEFAULT_FILE_STORAGE = 'oembed.tests.storage.DummyMemoryStorage'
# 
# Path: oembed/tests/storage.py
# class DummyMemoryStorage(storage.Storage):
#     """
#     A simple in-memory storage backend for testing image storage - copied from
#     djutils (http://github.com/coleifer/djutils/)
#     """
#     _files = {} # bampf
#     
#     def delete(self, name):
#         if name in self._files:
#             del(self._files[name])
#     
#     def exists(self, name):
#         return name in self._files
#     
#     def listdir(self, path):
#         files = []
#         if not path.endswith('/'):
#             path += '/' # make sure ends in slash for string comp below
#         for k in self._files:
#             if k.startswith(path) and '/' not in k.replace(path, ''):
#                 files.append(k.replace(path, ''))
#     
#     def size(self, name):
#         return len(self._files[name])
#     
#     def url(self, name):
#         return urlparse.urljoin(settings.MEDIA_URL, name)
#     
#     def _open(self, name, mode):
#         return StringIO(self._files.get(name, ''))
#     
#     def _save(self, name, content):
#         content.seek(0)
#         self._files[name] = content.read()
#         return name
#     
#     def get_valid_name(self, name):
#         return name
#     
#     def get_available_name(self, name):
#         if name not in self._files:
#             return name
#         
#         base_path, ext = os.path.splitext(name)
#         counter = 1
#         
#         while 1:
#             test = '%s_%s%s' % (base_path, counter, ext)
#             if test not in self._files:
#                 return test
#             counter += 1


import os
    import Image
import oembed
from urllib2 import urlparse
    from PIL import Image
    from cStringIO import StringIO
    from StringIO import StringIO
from django.conf import settings
from django.core.files import storage
from django.core.files.base import ContentFile
from django.core.urlresolvers import reverse, NoReverseMatch
from django.test import TestCase
from django.utils import simplejson
from oembed.providers import BaseProvider
from oembed.resources import OEmbedResource
from oembed.tests.settings import MEDIA_ROOT, MEDIA_URL, DEFAULT_FILE_STORAGE
from oembed.tests.storage import DummyMemoryStorage
        
        # monkeypatch default_storage
        self.orig_default_storage = storage.default_storage
        storage.default_storage = self.storage
        
        # swap media root & media url
        self.media_root, self.media_url = settings.MEDIA_ROOT, settings.MEDIA_URL
        settings.MEDIA_ROOT = MEDIA_ROOT
        settings.MEDIA_URL = MEDIA_URL

        # swap out template dirs
        self.template_dirs = settings.TEMPLATE_DIRS
        cur_dir = os.path.dirname(__file__)
        settings.TEMPLATE_DIRS = [os.path.join(os.path.dirname(cur_dir), 'templates')]
        
        # swap out file storage backend
        self.orig_file_storage = settings.DEFAULT_FILE_STORAGE
        settings.DEFAULT_FILE_STORAGE = DEFAULT_FILE_STORAGE

        # create 2 images for testing
        test_image = Image.new('CMYK', (1024, 768), (255, 255, 255, 255))
        self.test_img_buffer = StringIO()
        test_image.save(self.test_img_buffer, 'JPEG')
        
        self.test_img_file = ContentFile(self.test_img_buffer.getvalue())
        self.test_img_location = 'images/test_image1.jpg'
        storage.default_storage.save(self.test_img_location, self.test_img_file)

    def tearDown(self):
        settings.MEDIA_ROOT = self.media_root

# Path: oembed/providers.py
# class BaseProvider(object):
#     """
#     Base class for OEmbed resources.
#     """
#     regex = None # regex this provider will match
#     provides = True  # allow this provider to be accessed by third parties
#     
#     def request_resource(self, url, **kwargs):
#         """
#         Get an OEmbedResource from one of the providers configured in this 
#         provider according to the resource url.
#         
#         Args:
#             url: The url of the resource to get.
#             format: Desired response format (json or xml).
#             **kwargs: Optional parameters to pass in to the provider.
#         
#         Returns:
#             OEmbedResource object.
#             
#         If no object returned, raises OEmbedException
#         """
#         raise NotImplementedError
# 
# Path: oembed/resources.py
# class OEmbedResource(object):
#     """
#     OEmbed resource, as well as a factory for creating resource instances
#     from response json
#     """
#     _data = {}
#     content_object = None
#     
#     def __getattr__(self, name):
#         return self._data.get(name)
#     
#     def get_data(self):
#         return self._data
#         
#     def load_data(self, data):
#         self._data = data
#     
#     @property
#     def json(self):
#         return simplejson.dumps(self._data)
#     
#     @classmethod
#     def create(cls, data):
#         if not 'type' in data or not 'version' in data:
#             raise OEmbedException('Missing required fields on OEmbed response.')
# 
#         data['width'] = data.get('width') and int(data['width']) or None
#         data['height'] = data.get('height') and int(data['height']) or None
#         
#         filtered_data = dict([(k, v) for k, v in data.items() if v])
#         
#         resource = cls()
#         resource.load_data(filtered_data)
#         
#         return resource
# 
#     @classmethod
#     def create_json(cls, raw):
#         data = simplejson.loads(raw)
#         return cls.create(data)
# 
# Path: oembed/tests/settings.py
# MEDIA_ROOT = '%s/media/' % (SITE_ROOT)
# 
# MEDIA_URL = '/media/'
# 
# DEFAULT_FILE_STORAGE = 'oembed.tests.storage.DummyMemoryStorage'
# 
# Path: oembed/tests/storage.py
# class DummyMemoryStorage(storage.Storage):
#     """
#     A simple in-memory storage backend for testing image storage - copied from
#     djutils (http://github.com/coleifer/djutils/)
#     """
#     _files = {} # bampf
#     
#     def delete(self, name):
#         if name in self._files:
#             del(self._files[name])
#     
#     def exists(self, name):
#         return name in self._files
#     
#     def listdir(self, path):
#         files = []
#         if not path.endswith('/'):
#             path += '/' # make sure ends in slash for string comp below
#         for k in self._files:
#             if k.startswith(path) and '/' not in k.replace(path, ''):
#                 files.append(k.replace(path, ''))
#     
#     def size(self, name):
#         return len(self._files[name])
#     
#     def url(self, name):
#         return urlparse.urljoin(settings.MEDIA_URL, name)
#     
#     def _open(self, name, mode):
#         return StringIO(self._files.get(name, ''))
#     
#     def _save(self, name, content):
#         content.seek(0)
#         self._files[name] = content.read()
#         return name
#     
#     def get_valid_name(self, name):
#         return name
#     
#     def get_available_name(self, name):
#         if name not in self._files:
#             return name
#         
#         base_path, ext = os.path.splitext(name)
#         counter = 1
#         
#         while 1:
#             test = '%s_%s%s' % (base_path, counter, ext)
#             if test not in self._files:
#                 return test
#             counter += 1


# Path: oembed/tests/tests/base.py
import os
    import Image
import oembed
from urllib2 import urlparse
    from PIL import Image
    from cStringIO import StringIO
    from StringIO import StringIO
from django.conf import settings
from django.core.files import storage
from django.core.files.base import ContentFile
from django.core.urlresolvers import reverse, NoReverseMatch
from django.test import TestCase
from django.utils import simplejson
from oembed.providers import BaseProvider
from oembed.resources import OEmbedResource
from oembed.tests.settings import MEDIA_ROOT, MEDIA_URL, DEFAULT_FILE_STORAGE
from oembed.tests.storage import DummyMemoryStorage

        
        # monkeypatch default_storage
        self.orig_default_storage = storage.default_storage
        storage.default_storage = self.storage
        
        # swap media root & media url
        self.media_root, self.media_url = settings.MEDIA_ROOT, settings.MEDIA_URL
        settings.MEDIA_ROOT = MEDIA_ROOT
        settings.MEDIA_URL = MEDIA_URL

        # swap out template dirs
        self.template_dirs = settings.TEMPLATE_DIRS
        cur_dir = os.path.dirname(__file__)
        settings.TEMPLATE_DIRS = [os.path.join(os.path.dirname(cur_dir), 'templates')]
        
        # swap out file storage backend
        self.orig_file_storage = settings.DEFAULT_FILE_STORAGE
        settings.DEFAULT_FILE_STORAGE = DEFAULT_FILE_STORAGE

        # create 2 images for testing
        test_image = Image.new('CMYK', (1024, 768), (255, 255, 255, 255))
        self.test_img_buffer = StringIO()
        test_image.save(self.test_img_buffer, 'JPEG')
        
        self.test_img_file = ContentFile(self.test_img_buffer.getvalue())
        self.test_img_location = 'images/test_image1.jpg'
        storage.default_storage.save(self.test_img_location, self.test_img_file)

    def tearDown(self):
        settings.MEDIA_ROOT = self.media_root

        settings.MEDIA_URL = self.media_url