# Path: kittystore/events.py
# def subscribe(target, eventclass):
# def notify(event):
# def subscribe_to(eventclass):
#     def wrapper(f):
# def reset():
# 
# Path: kittystore/utils.py
# def get_mailman_client(settings):
#     try:
#         mm_client = mailmanclient.Client('%s/3.0' %
#                         settings.MAILMAN_REST_SERVER,
#                         settings.MAILMAN_API_USER,
#                         settings.MAILMAN_API_PASS)
#     except (HTTPError, mailmanclient.MailmanConnectionError), e:
#         raise HTTPError(e)
#     return mm_client


from urllib2 import HTTPError
from dateutil.parser import parse as date_parse
from mailman.interfaces.archiver import ArchivePolicy
from kittystore import events
from kittystore.utils import get_mailman_client
import mailmanclient


class CompatibleMList(object):
    """
    Convert the List object returned by mailmanclient to an
    IMailingList-compatible object
    """
    converters = {
        "created_at": date_parse,
        "archive_policy": lambda p: getattr(ArchivePolicy, p),
    }
    def __init__(self, mlist, props):
        for prop in props:
            try:
                value = getattr(mlist, prop)
            except AttributeError:
                value = mlist.settings[prop]
            if prop in self.converters:
                value = self.converters[prop](value)
            setattr(self, prop, value)


def update_props(store, mlist):
    l = store.get_list(mlist.fqdn_listname)
    if isinstance(mlist, mailmanclient._client._List):
        mlist = CompatibleMList(mlist, l.mailman_props)
    for propname in l.mailman_props:
        setattr(l, propname, getattr(mlist, propname))



# Path: kittystore/events.py
# def subscribe(target, eventclass):
# def notify(event):
# def subscribe_to(eventclass):
#     def wrapper(f):
# def reset():
# 
# Path: kittystore/utils.py
# def get_mailman_client(settings):
#     try:
#         mm_client = mailmanclient.Client('%s/3.0' %
#                         settings.MAILMAN_REST_SERVER,
#                         settings.MAILMAN_API_USER,
#                         settings.MAILMAN_API_PASS)
#     except (HTTPError, mailmanclient.MailmanConnectionError), e:
#         raise HTTPError(e)
#     return mm_client


# Path: kittystore/caching/mlist.py
from urllib2 import HTTPError
from dateutil.parser import parse as date_parse
from mailman.interfaces.archiver import ArchivePolicy
from kittystore import events
from kittystore.utils import get_mailman_client
import mailmanclient



class CompatibleMList(object):
    """
    Convert the List object returned by mailmanclient to an
    IMailingList-compatible object
    """
    converters = {
        "created_at": date_parse,
        "archive_policy": lambda p: getattr(ArchivePolicy, p),
    }
    def __init__(self, mlist, props):
        for prop in props:
            try:
                value = getattr(mlist, prop)
            except AttributeError:
                value = mlist.settings[prop]
            if prop in self.converters:
                value = self.converters[prop](value)
            setattr(self, prop, value)


def update_props(store, mlist):
    l = store.get_list(mlist.fqdn_listname)
    if isinstance(mlist, mailmanclient._client._List):
        mlist = CompatibleMList(mlist, l.mailman_props)
    for propname in l.mailman_props:
        setattr(l, propname, getattr(mlist, propname))



@events.subscribe_to(events.NewMessage)