91 lines
3.6 KiB
Python
91 lines
3.6 KiB
Python
# -*- python -*-
|
|
# -*- coding: utf8 -*-
|
|
#
|
|
# DDPortfolio service url builder
|
|
# Copyright © 2009, 2010 Jan Dittberner <jan@dittberner.info>
|
|
#
|
|
# This file is part of DDPortfolio service.
|
|
#
|
|
# DDPortfolio service is free software: you can redistribute it and/or
|
|
# modify it under the terms of the GNU Affero General Public License
|
|
# as published by the Free Software Foundation, either version 3 of
|
|
# the License, or (at your option) any later version.
|
|
#
|
|
# DDPortfolio service is distributed in the hope that it will be
|
|
# useful, but WITHOUT ANY WARRANTY; without even the implied warranty
|
|
# of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
# Affero General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU Affero General Public
|
|
# License along with this program. If not, see
|
|
# <http://www.gnu.org/licenses/>.
|
|
#
|
|
"""
|
|
This module provides the function build_urls to build personalized
|
|
URLs using the given information and the URL patterns defined in
|
|
ddportfolio.ini.
|
|
"""
|
|
|
|
from ConfigParser import ConfigParser, InterpolationMissingOptionError
|
|
import pkg_resources
|
|
from ddportfolioservice.model import keyfinder
|
|
from urllib import quote_plus
|
|
from pylons.i18n.translation import _, N_
|
|
|
|
|
|
my_config = ConfigParser()
|
|
my_config.readfp(pkg_resources.resource_stream(__name__, 'ddportfolio.ini'))
|
|
|
|
_FIELDNAMES_MAP = {
|
|
'email' : N_('Email address'),
|
|
'name' : N_('Name'),
|
|
'gpgfp' : N_('GPG fingerprint'),
|
|
'username' : N_('Debian user name'),
|
|
'nonddemail' : N_('Non DD email address'),
|
|
'aliothusername' : N_('Alioth user name'),
|
|
}
|
|
|
|
|
|
def build_urls(fields):
|
|
"""Build personalized URLs using the developer information in
|
|
fields."""
|
|
data = []
|
|
qfields = {}
|
|
for key, value in fields.iteritems():
|
|
if value is not None:
|
|
if isinstance(value, unicode):
|
|
qfields[key] = quote_plus(value.encode('utf8'))
|
|
elif isinstance(value, str):
|
|
qfields[key] = quote_plus(value)
|
|
else:
|
|
qfields[key] = value
|
|
|
|
if 'gpgfp' not in qfields:
|
|
fpr = keyfinder.getFingerprintByEmail(fields['email'].encode('utf8'))
|
|
if fpr:
|
|
qfields['gpgfp'] = fpr[0]
|
|
qfields['firstchar'] = fields['email'][0].encode('utf8')
|
|
for section in [section.strip() for section in \
|
|
my_config.get('DEFAULT',
|
|
'urlbuilder.sections').split(',')]:
|
|
data.append(['section', section])
|
|
if my_config.has_option(section, 'urls'):
|
|
for url in my_config.get(section, 'urls').split(','):
|
|
if my_config.has_option(section, url + '.pattern'):
|
|
optional = my_config.has_option(section, url + '.optional') \
|
|
and my_config.getboolean(section, url + '.optional') or False
|
|
try:
|
|
data.append(
|
|
['url', section, url,
|
|
my_config.get(section, url + '.pattern',
|
|
False, qfields)])
|
|
except InterpolationMissingOptionError, e:
|
|
if not optional:
|
|
if e.reference in _FIELDNAMES_MAP:
|
|
data.append(['error', section, url,
|
|
_('Missing input: %s') % \
|
|
_(_FIELDNAMES_MAP[e.reference])])
|
|
else:
|
|
data.append(['error', section, url,
|
|
_('Missing input: %s') % e.reference])
|
|
return data
|