import os
from flask import Flask
from werkzeug.contrib.fixers import ProxyFix
from flask_sslify import SSLify

from services import bitbucket
from services import bitly
from services import dailymile
from services import deviantart
from services import digg
from services import disqus
from services import dropbox
from services import facebook
from services import fitbit
from services import fivehundredpx
from services import flickr
from services import foursquare
from services import getglue
from services import github
from services import goodreads
from services import google
from services import imgur
from services import instagram
from services import lastfm
from services import linkedin
from services import liveconnect
from services import meetup
from services import miso
from services import ohloh
from services import plurk
from services import readmill
from services import rdio
from services import smugmug
from services import soundcloud
from services import stackexchange
from services import tripit
from services import tumblr
from services import twitter
from services import vimeo
from services import wordpress
from services import yahoo

app = Flask(__name__)
app.secret_key = os.environ['SECRET_KEY']
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get('DATABASE_URL')
app.config['DEBUG'] = 'DEBUG' in os.environ
app.wsgi_app = ProxyFix(app.wsgi_app)
SSLify(app)


def init_services(*services):
    service_list = []

    for service in services:
        alias = service.alias.upper()
        key = os.environ.get('%s_KEY' % alias, '').decode('utf8')
        secret = os.environ.get('%s_SECRET' % alias, '').decode('utf8')

        if key and secret:  # Only initialize if all the pieces are in place
            service_list.append(service(key, secret))

    return service_list

services = init_services(bitbucket.Bitbucket,
                         bitly.Bitly,
                         dailymile.Dailymile,
                         deviantart.DeviantArt,
                         digg.Digg,
                         disqus.Disqus,
                         dropbox.Dropbox,
                         facebook.Facebook,
                         fitbit.FitBit,
                         fivehundredpx.FiveHundredPX,
                         flickr.Flickr,
                         foursquare.Foursquare,
                         getglue.GetGlue,
                         github.GitHub,
                         goodreads.Goodreads,
                         google.Google,
                         imgur.Imgur,
                         instagram.Instagram,
                         lastfm.LastFM,
                         linkedin.LinkedIn,
                         liveconnect.LiveConnect,
                         meetup.Meetup,
                         miso.Miso,
                         ohloh.Ohloh,
                         plurk.Plurk,
                         readmill.Readmill,
                         rdio.Rdio,
                         smugmug.SmugMug,
                         soundcloud.SoundCloud,
                         stackexchange.StackExchange,
                         tripit.TripIt,
                         tumblr.Tumblr,
                         twitter.Twitter,
                         vimeo.Vimeo,
                         wordpress.Wordpress,
                         yahoo.Yahoo,
)

alias_map = {}
for service in services:
    alias_map[service.alias] = service

domain_map = {}
for service in services:
    for domain in service.api_domains:
        domain_map[domain] = service

@@ -1,16 +1,28 @@
+import glob%0A
 import os%0Afrom f
@@ -126,3232 +126,1190 @@
 rom 
-services import bitbucket%0Afrom services import bitly%0Afrom services import dailymile%0Afrom services import deviantart%0Afrom services import digg%0Afrom services import disqus%0Afrom services import dropbox%0Afrom services import facebook%0Afrom services import fitbit%0Afrom services import fivehundredpx%0Afrom services import flickr%0Afrom services import foursquare%0Afrom services import getglue%0Afrom services import github%0Afrom services import goodreads%0Afrom services import google%0Afrom services import imgur%0Afrom services import instagram%0Afrom services import lastfm%0Afrom services import linkedin%0Afrom services import liveconnect%0Afrom services import meetup%0Afrom services import miso%0Afrom services import ohloh%0Afrom services import plurk%0Afrom services import readmill%0Afrom services import rdio%0Afrom services import smugmug%0Afrom services import soundcloud%0Afrom services import stackexchange%0Afrom services import tripit%0Afrom services import tumblr%0Afrom services import twitter%0Afrom services import vimeo%0Afrom services import wordpress%0Afrom services import yahoo%0A%0Aapp = Flask(__name__)%0Aapp.secret_key = os.environ%5B'SECRET_KEY'%5D%0Aapp.config%5B'SQLALCHEMY_DATABASE_URI'%5D = os.environ.get('DATABASE_URL')%0Aapp.config%5B'DEBUG'%5D = 'DEBUG' in os.environ%0Aapp.wsgi_app = ProxyFix(app.wsgi_app)%0ASSLify(app)%0A%0A%0Adef init_services(*services):%0A    service_list = %5B%5D%0A%0A    for service in services:%0A        alias = service.alias.upper()%0A        key = os.environ.get('%25s_KEY' %25 alias, '').decode('utf8')%0A        secret = os.environ.get('%25s_SECRET' %25 alias, '').decode('utf8')%0A%0A        if key and secret:  # Only initialize if all the pieces are in place%0A            service_list.append(service(key, secret))%0A%0A    return service_list%0A%0Aservices = init_services(bitbucket.Bitbucket,%0A                         bitly.Bitly,%0A                         dailymile.Dailymile,%0A                         deviantart.DeviantArt,%0A                         digg.Digg,%0A                         disqus.Disqus,%0A                         dropbox.Dropbox,%0A                         facebook.Facebook,%0A                         fitbit.FitBit,%0A                         fivehundredpx.FiveHundredPX,%0A                         flickr.Flickr,%0A                         foursquare.Foursquare,%0A                         getglue.GetGlue,%0A                         github.GitHub,%0A                         goodreads.Goodreads,%0A                         google.Google,%0A                         imgur.Imgur,%0A                         instagram.Instagram,%0A                         lastfm.LastFM,%0A                         linkedin.LinkedIn,%0A                         liveconnect.LiveConnect,%0A                         meetup.Meetup,%0A                         miso.Miso,%0A                         ohloh.Ohloh,%0A                         plurk.Plurk,%0A                         readmill.Readmill,%0A                         rdio.Rdio,%0A                         smugmug.SmugMug,%0A                         soundcloud.SoundCloud,%0A                         stackexchange.StackExchange,%0A                         tripit.TripIt,%0A                         tumblr.Tumblr,%0A                         twitter.Twitter,%0A                         vimeo.Vimeo,%0A                         wordpress.Wordpress,%0A                         yahoo.Yahoo,%0A)
+foauth.providers import OAuthMeta%0A%0Aapp = Flask(__name__)%0Aapp.secret_key = os.environ%5B'SECRET_KEY'%5D%0Aapp.config%5B'SQLALCHEMY_DATABASE_URI'%5D = os.environ.get('DATABASE_URL')%0Aapp.config%5B'DEBUG'%5D = 'DEBUG' in os.environ%0Aapp.wsgi_app = ProxyFix(app.wsgi_app)%0ASSLify(app)%0A%0A%0Adef get_service_modules():%0A    for filename in glob.glob(os.path.join('services', '*.py')):%0A        module_name = os.path.splitext(os.path.split(filename)%5B1%5D)%5B0%5D%0A        if not module_name.startswith('__'):%0A            yield module_name%0A%0A%0Adef get_oauth_providers(module_name):%0A    module = getattr(__import__('services.%25s' %25 module_name), module_name)%0A    for name, obj in module.__dict__.items():%0A        if isinstance(obj, OAuthMeta):%0A            yield obj%0A%0A%0Aservices = %5B%5D%0Afor module_name in get_service_modules():%0A    for service in get_oauth_providers(module_name):%0A        alias = service.alias.upper()%0A        key = os.environ.get('%25s_KEY' %25 alias, '').decode('utf8')%0A        secret = os.environ.get('%25s_SECRET' %25 alias, '').decode('utf8')%0A%0A        if key and secret:  # Only initialize if all the pieces are in place%0A            print '%25s: %25s' %25 (alias, key)%0A            services.append(service(key, secret))%0A
 %0A%0Aal

Simplify discovery and initialization of new providers