# Path: locksmith/common.py
# def enum(name, **enums):
#     def __iter__(self):
# def get_signature(params, signkey):
# def apicall(url, signkey, **params):
# def cache(seconds = 900):
#     def doCache(f):
#         def x(*args, **kwargs):
#     E = type(name or 'Enum', (), enums)
# API_OPERATING_STATUSES = (
#     (1, 'Normal'),
#     (2, 'Degraded Service'),
#     (3, 'Service Disruption'),
#     (4, 'Undergoing Maintenance')
# )
# API_STATUSES = enum('ApiStatuses',
#                     Stealth=0,
#                     Active=1,
#                     Deprecated=2,
#                     Disabled=3)
# KEY_STATUSES = (
#     ('U', 'Unactivated'),
#     ('A', 'Active'),
#     ('S', 'Suspended')
# )
# UNPUBLISHED, PUBLISHED, NEEDS_UPDATE = range(3)
# PUB_STATUSES = (
#     (UNPUBLISHED, 'Unpublished'),
#     (PUBLISHED, 'Published'),
#     (NEEDS_UPDATE, 'Needs Update'),
# )


import urllib2
from urlparse import urljoin
from locksmith.common import apicall, UNPUBLISHED, PUBLISHED, NEEDS_UPDATE
from celery.task import task
from django.conf import settings
            # Skip this API because we've queued a replicate_key task above
            print u"push_key for {k} ignoring {a} because it uses replicate_key.".format(k=key.key, a=kps.api.name)
            continue

        endpoint = urljoin(kps.api.url, endpoints[kps.status])
        try:
            apicall(endpoint, kps.api.signing_key, api=kps.api.name,
                    key=kps.key.key, email=kps.key.email, status=kps.key.status)
            print 'sent key {k} to {a}'.format(k=key.key, a=kps.api.name)
            kps.status = PUBLISHED
            kps.save()

        except Exception as e:
            ctx = {
                'a': str(kps.api.name),
                'k': str(key.key),
                'e': str(e.read()) if isinstance(e, urllib2.HTTPError) else str(e)
            }
            print 'Caught exception while pushing key {k} to {a}: {e}'.format(**ctx)
            print 'Will retry'
            retry_flag = True

    if retry_flag:
        push_key.retry()

@task(max_retries=5)
def replicate_key(key, api):
    kps = key.pub_statuses.get(api=api)
    endpoint = urljoin(kps.api.url, 'replicate_key/{k}/'.format(k=key.key))
    try:

# Path: locksmith/common.py
# def enum(name, **enums):
#     def __iter__(self):
# def get_signature(params, signkey):
# def apicall(url, signkey, **params):
# def cache(seconds = 900):
#     def doCache(f):
#         def x(*args, **kwargs):
#     E = type(name or 'Enum', (), enums)
# API_OPERATING_STATUSES = (
#     (1, 'Normal'),
#     (2, 'Degraded Service'),
#     (3, 'Service Disruption'),
#     (4, 'Undergoing Maintenance')
# )
# API_STATUSES = enum('ApiStatuses',
#                     Stealth=0,
#                     Active=1,
#                     Deprecated=2,
#                     Disabled=3)
# KEY_STATUSES = (
#     ('U', 'Unactivated'),
#     ('A', 'Active'),
#     ('S', 'Suspended')
# )
# UNPUBLISHED, PUBLISHED, NEEDS_UPDATE = range(3)
# PUB_STATUSES = (
#     (UNPUBLISHED, 'Unpublished'),
#     (PUBLISHED, 'Published'),
#     (NEEDS_UPDATE, 'Needs Update'),
# )


# Path: locksmith/hub/tasks.py
import urllib2
from urlparse import urljoin
from locksmith.common import apicall, UNPUBLISHED, PUBLISHED, NEEDS_UPDATE
from celery.task import task
from django.conf import settings

            # Skip this API because we've queued a replicate_key task above
            print u"push_key for {k} ignoring {a} because it uses replicate_key.".format(k=key.key, a=kps.api.name)
            continue

        endpoint = urljoin(kps.api.url, endpoints[kps.status])
        try:
            apicall(endpoint, kps.api.signing_key, api=kps.api.name,
                    key=kps.key.key, email=kps.key.email, status=kps.key.status)
            print 'sent key {k} to {a}'.format(k=key.key, a=kps.api.name)
            kps.status = PUBLISHED
            kps.save()

        except Exception as e:
            ctx = {
                'a': str(kps.api.name),
                'k': str(key.key),
                'e': str(e.read()) if isinstance(e, urllib2.HTTPError) else str(e)
            }
            print 'Caught exception while pushing key {k} to {a}: {e}'.format(**ctx)
            print 'Will retry'
            retry_flag = True

    if retry_flag:
        push_key.retry()

@task(max_retries=5)
def replicate_key(key, api):
    kps = key.pub_statuses.get(api=api)
    endpoint = urljoin(kps.api.url, 'replicate_key/{k}/'.format(k=key.key))
    try:

        apicall(endpoint, kps.api.signing_key,