# -*- python -*- # -*- coding: utf8 -*- from paste.request import parse_formvars import pkg_resources import simplejson from ConfigParser import ConfigParser from urllib import quote_plus import logging import sys import formencode from gettext import gettext as _ from formencode import validators logging.basicConfig(level=logging.DEBUG, stream=sys.stderr) my_config = ConfigParser() my_config.readfp(pkg_resources.resource_stream(__name__, 'ddportfolio.ini')) def build_urls(fields): """Build personalized URLs using the developer information in fields.""" result = {} qfields = dict([(key, quote_plus(fields[key])) for key in fields]) for section in my_config.sections(): if my_config.has_option(section, 'urls'): for url in my_config.get(section, 'urls').split(','): if my_config.has_option(section, url + '.pattern'): try: result[section + '.' + url] = \ my_config.get(section, url + '.pattern', False, qfields) except Exception, e: logging.error(_("unable to parse %s: %s") % (my_config.get(section, url + '.pattern', True), e)) return result class DeveloperData(formencode.Schema): name = validators.String(not_empty=True) email = validators.Email(not_empty=True) username = validators.PlainText(not_empty=True) def validate_input(fields): logging.debug(fields) return DeveloperData().to_python(fields) def gather_additional_info(fields): logging.debug(fields) return fields def application(environ, start_response): """WSGI application entry point.""" if environ['REQUEST_METHOD'] == 'POST': try: fields = gather_additional_info( validate_input(parse_formvars(environ))) except formencode.Invalid, e: start_response('400 Bad Request', [('content-type', 'text/plain')]) return ["input validation failed\n", e.unpack_errors()] data = build_urls(fields) if ('mode' in fields and fields['mode'] == 'json'): start_response('200 OK', [('content-type', 'text/json')]) return [simplejson.dumps(data)] else: start_response('200 OK', [('content-type', 'text/html')]) return [''' ''', _("Debian Developer Portfolio"),''' '] else: start_response('200 OK', [('content-type', 'text/html')]) return [''' ''', _('Debian Developer Portfolio'), '''
''', _('Debian Developer Portfolio'), '''





'''] if __name__ == '__main__': from paste import httpserver httpserver.serve(application, host='127.0.0.1', port='8080')