# Path: feedplatform/addins.py
# class base(object):
#     class __metaclass__(type):
#         def __new__(cls, name, bases, attrs):
#     def __new__(cls, *args, **kwargs):
#     def setup(self):
#     def log(self):
# def get_addins():
# def _make_addin(addin):
# def reinstall(addins=None):
#         def resolve_dependencies(addin, index):
# def install(*args, **kwargs):
# _ADDINS = None
#     _ADDINS = to_be_setup
# 
# Path: feedplatform/db.py
# class MultipleObjectsReturned(Exception):
# class DatabaseProxy(object):
# class ModelsProxy(types.ModuleType):
# def get_one(result):
# def cap_model_name(name):
#     def repl(m):
# def uncap_model_name(name):
#     def __init__(self, create_func):
#     def _connect(self):
#     def __getattr__(self, name):
#     def __setattr__(self, name, value):
#     def __init__(self):
#     def __getattr__(self, name):
#     def __iter__(self):
#     def iternames(self):
#     def iteritems(self):
#     def _construct_models(self):
# def reconfigure():
# BASE_MODELS = {
#     'feed': {
#         'id': (Int, (), {'primary': True}),
#         'url': (Unicode, (), {}),
#         'items': (ReferenceSet, ('Feed.id', 'Item.feed_id',), {}),
#     },
#     'item': {
#         'id': (Int, (), {'primary': True}),
#         'feed_id': (Int, (), {}),
#         'feed': (Reference, ('feed_id', 'Feed.id',), {}),
#         'guid': (Unicode, (), {}),
#     },
# }
# 
# Path: feedplatform/util.py
# def datetime_to_struct(datetime):
#     """Convert a datetime object to an UTC-based 9-tuple.
# 
#     The reverse of ``struct_to_datetime``.
#     """
#     if not datetime:
#         return datetime
#     return datetime.utctimetuple()
# 
# def struct_to_datetime(structt):
#     """Convert timestructs/9-tuples, as returned by feedparser, to
#     naive, UTC-based datetime objects.
# 
#     See also:
#         http://www.deadlybloodyserious.com/2007/09/feedparser-v-django/
#         http://www.intertwingly.net/blog/2007/09/02/Dealing-With-Dates
#     """
#     if not structt:
#         return structt
#     return datetime.datetime.utcfromtimestamp(calendar.timegm(structt))
# 
# def to_unicode(s):
#     """Convert ``s`` to a unicode object if not None.
# 
#     This is helpful in scenarios where you want to assign to a nullable
#     Storm ``Unicode`` column (Storm enforces a unicode string type),
#     without checking the source for a ``None`` value manually.
#     """
# 
#     if s is None or isinstance(s, unicode):
#         return s
#     else:
#         return unicode(s)


from storm.locals import DateTime, Unicode
from feedplatform import addins
from feedplatform import db
from feedplatform.util import \
    datetime_to_struct, struct_to_datetime, to_unicode

    # TODO: add a mode for merging items
    """

    def __init__(self, delete=None, force=None, ignore=None):
        if delete:
            if not delete in ['self', 'other']:
                raise ValueError('"%s" is not a valid value for "delete". '
                    'Need "self" or "other".' % delete)

        if not any([delete, force, ignore]):
            raise ValueError('One of the parameters delete, force, '
                'ignore is required')

        if len([x for x in (delete, force, ignore) if x]) > 1:
            raise ValueError('Only one of the parameters delete, force, '
                'ignore is allowed')

        if delete == 'self':
            self._conflict_strategy = self._delete_self
        elif delete == 'other':
            self._conflict_strategy = self._delete_other
        elif force:
            self._conflict_strategy = self._force
        elif ignore:
            self._conflict_strategy = self._ignore

    def on_after_parse(self, feed, data_dict):
        if data_dict.status == 301:
            new_url = data_dict.href
            self.log.info('Feed #%d: permanent redirect to %s' % (feed.id, new_url))

# Path: feedplatform/addins.py
# class base(object):
#     class __metaclass__(type):
#         def __new__(cls, name, bases, attrs):
#     def __new__(cls, *args, **kwargs):
#     def setup(self):
#     def log(self):
# def get_addins():
# def _make_addin(addin):
# def reinstall(addins=None):
#         def resolve_dependencies(addin, index):
# def install(*args, **kwargs):
# _ADDINS = None
#     _ADDINS = to_be_setup
# 
# Path: feedplatform/db.py
# class MultipleObjectsReturned(Exception):
# class DatabaseProxy(object):
# class ModelsProxy(types.ModuleType):
# def get_one(result):
# def cap_model_name(name):
#     def repl(m):
# def uncap_model_name(name):
#     def __init__(self, create_func):
#     def _connect(self):
#     def __getattr__(self, name):
#     def __setattr__(self, name, value):
#     def __init__(self):
#     def __getattr__(self, name):
#     def __iter__(self):
#     def iternames(self):
#     def iteritems(self):
#     def _construct_models(self):
# def reconfigure():
# BASE_MODELS = {
#     'feed': {
#         'id': (Int, (), {'primary': True}),
#         'url': (Unicode, (), {}),
#         'items': (ReferenceSet, ('Feed.id', 'Item.feed_id',), {}),
#     },
#     'item': {
#         'id': (Int, (), {'primary': True}),
#         'feed_id': (Int, (), {}),
#         'feed': (Reference, ('feed_id', 'Feed.id',), {}),
#         'guid': (Unicode, (), {}),
#     },
# }
# 
# Path: feedplatform/util.py
# def datetime_to_struct(datetime):
#     """Convert a datetime object to an UTC-based 9-tuple.
# 
#     The reverse of ``struct_to_datetime``.
#     """
#     if not datetime:
#         return datetime
#     return datetime.utctimetuple()
# 
# def struct_to_datetime(structt):
#     """Convert timestructs/9-tuples, as returned by feedparser, to
#     naive, UTC-based datetime objects.
# 
#     See also:
#         http://www.deadlybloodyserious.com/2007/09/feedparser-v-django/
#         http://www.intertwingly.net/blog/2007/09/02/Dealing-With-Dates
#     """
#     if not structt:
#         return structt
#     return datetime.datetime.utcfromtimestamp(calendar.timegm(structt))
# 
# def to_unicode(s):
#     """Convert ``s`` to a unicode object if not None.
# 
#     This is helpful in scenarios where you want to assign to a nullable
#     Storm ``Unicode`` column (Storm enforces a unicode string type),
#     without checking the source for a ``None`` value manually.
#     """
# 
#     if s is None or isinstance(s, unicode):
#         return s
#     else:
#         return unicode(s)


# Path: feedplatform/lib/addins/feeds/http.py
from storm.locals import DateTime, Unicode
from feedplatform import addins
from feedplatform import db
from feedplatform.util import \
    datetime_to_struct, struct_to_datetime, to_unicode

    # TODO: add a mode for merging items
    """

    def __init__(self, delete=None, force=None, ignore=None):
        if delete:
            if not delete in ['self', 'other']:
                raise ValueError('"%s" is not a valid value for "delete". '
                    'Need "self" or "other".' % delete)

        if not any([delete, force, ignore]):
            raise ValueError('One of the parameters delete, force, '
                'ignore is required')

        if len([x for x in (delete, force, ignore) if x]) > 1:
            raise ValueError('Only one of the parameters delete, force, '
                'ignore is allowed')

        if delete == 'self':
            self._conflict_strategy = self._delete_self
        elif delete == 'other':
            self._conflict_strategy = self._delete_other
        elif force:
            self._conflict_strategy = self._force
        elif ignore:
            self._conflict_strategy = self._ignore

    def on_after_parse(self, feed, data_dict):
        if data_dict.status == 301:
            new_url = data_dict.href
            self.log.info('Feed #%d: permanent redirect to %s' % (feed.id, new_url))

            dup_feeds = db.store.find(db.models.Feed, db.models.Feed.url == new_url)
