from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app

openIdProviders = (
    'Google.com/accounts/o8/id', # shorter alternative: "Gmail.com"
    'Yahoo.com',
    'AOL.com',
    # add more here
)


def set_trace():
    import pdb, sys
    debugger = pdb.Pdb(stdin=sys.__stdin__, stdout=sys.__stdout__)
    debugger.set_trace(sys._getframe().f_back)

            
class MainHandler(webapp.RequestHandler):
    def get(self):
        user = users.get_current_user()
        if user:  # signed in already
            self.response.out.write('Hello <em>%s</em>! [<a href="%s">sign out</a>]' % (
                user.nickname(), users.create_logout_url(self.request.uri)))
        else:     # let user choose authenticator
            self.response.out.write('Hello world! Sign in at: ')
            out = ''
            for p in openIdProviders:
                p_name = p.split('.')[0] # take "AOL" from "AOL.com"
                p_url = p.lower()        # "AOL.com" -> "aol.com"
                out += '[<a href="%s">%s</a>]' % (users.create_login_url(dest_url = '/', federated_identity = p_url), p_name)    
            out += '<a href="%s">%s</a>' % (users.create_login_url(dest_url = '/', federated_identity = 'www.myspace.com/dnjuls'), 'myspace')
            out += '<a href="%s">%s</a>' % (users.create_login_url(dest_url = '/', federated_identity = 'dan.julius.myopenid.com'), 'openid')
            self.response.out.write(out)

application = webapp.WSGIApplication([
    ('/', MainHandler),
], debug=True)

def main():
    run_wsgi_app(application)

if __name__ == '__main__':
    main()
