~launchpad-pqm/launchpad/devel

8687.15.17 by Karl Fogel
Add the copyright header block to the rest of the files under lib/lp/.
1
# Copyright 2009 Canonical Ltd.  This software is licensed under the
2
# GNU Affero General Public License version 3 (see the file LICENSE).
4284.5.6 by Tom Haddon
Code refactoring following review comments
3
4
"""List all team members: name, preferred email address."""
5
6
__metaclass__ = type
7
__all__ = ['process_team']
4284.5.2 by Tom Haddon
Optionally give it -e or -f options for email only or full details
8
10631.1.2 by Curtis Hovey
Strip sshkeys of line breaks when using listteammembers. Quiet lint's many
9
import re
10
4284.5.2 by Tom Haddon
Optionally give it -e or -f options for email only or full details
11
from zope.component import getUtility
12
7675.110.3 by Curtis Hovey
Ran the migration script to move registry code to lp.registry.
13
from lp.registry.interfaces.person import IPersonSet
8137.17.24 by Barry Warsaw
thread merge
14
from lp.registry.interfaces.ssh import SSHKeyType
7675.110.5 by Curtis Hovey
Removed unused imports reported by lint.
15
16
4284.5.7 by Tom Haddon
Better variable names, trimming some whitespace and more elegantly handling argument parsing by setting default "format" variable
17
OUTPUT_TEMPLATES = {
10631.1.2 by Curtis Hovey
Strip sshkeys of line breaks when using listteammembers. Quiet lint's many
18
    'simple': '%(name)s, %(email)s',
19
    'email': '%(email)s',
20
    'full': '%(teamname)s|%(id)s|%(name)s|%(email)s|'
21
            '%(displayname)s|%(ubuntite)s',
22
    'sshkeys': '%(name)s: %(sshkey)s',
23
    }
4284.5.6 by Tom Haddon
Code refactoring following review comments
24
25
4785.3.7 by Jeroen Vermeulen
Removed whitespace at ends of lines
26
class NoSuchTeamError(Exception):
4284.5.6 by Tom Haddon
Code refactoring following review comments
27
    """Used if non-existent team name is specified."""
4284.5.7 by Tom Haddon
Better variable names, trimming some whitespace and more elegantly handling argument parsing by setting default "format" variable
28
4284.5.6 by Tom Haddon
Code refactoring following review comments
29
10631.1.3 by Curtis Hovey
Fixed pattern name. Tested that the sshkey comment is sanitised.
30
bad_ssh_pattern = re.compile('[\r\n\f]')
10631.1.2 by Curtis Hovey
Strip sshkeys of line breaks when using listteammembers. Quiet lint's many
31
32
33
def make_sshkey_params(member, type_name, key):
34
    sshkey = "%s %s %s" % (
35
        type_name,
10631.1.3 by Curtis Hovey
Fixed pattern name. Tested that the sshkey comment is sanitised.
36
        bad_ssh_pattern.sub('', key.keytext),
37
        bad_ssh_pattern.sub('', key.comment).strip())
10631.1.2 by Curtis Hovey
Strip sshkeys of line breaks when using listteammembers. Quiet lint's many
38
    return dict(name=member.name, sshkey=sshkey)
39
40
4284.5.6 by Tom Haddon
Code refactoring following review comments
41
def process_team(teamname, display_option='simple'):
4284.5.2 by Tom Haddon
Optionally give it -e or -f options for email only or full details
42
    output = []
43
    people = getUtility(IPersonSet)
44
    memberset = people.getByName(teamname)
45
    if memberset == None:
46
        raise NoSuchTeamError
47
4284.5.7 by Tom Haddon
Better variable names, trimming some whitespace and more elegantly handling argument parsing by setting default "format" variable
48
    template = OUTPUT_TEMPLATES[display_option]
4284.5.6 by Tom Haddon
Code refactoring following review comments
49
    for member in memberset.allmembers:
50
        # Email
51
        if member.preferredemail is not None:
52
            email = member.preferredemail.email
53
        else:
54
            email = '--none--'
4794.1.1 by Tom Haddon
Show validated emails if we have specified the email display_option
55
        if display_option == 'email':
4595.2.1 by Tom Haddon
For the email only option, we want all validated emails
56
            for validatedemail in member.validatedemails:
57
                params = dict(
58
                    email=validatedemail.email,
59
                    )
60
                output.append(template % params)
8137.17.24 by Barry Warsaw
thread merge
61
        # SSH Keys
62
        sshkey = '--none--'
63
        if display_option == 'sshkeys':
64
            for key in member.sshkeys:
65
                if key.keytype == SSHKeyType.DSA:
66
                    type_name = 'ssh-dss'
67
                elif key.keytype == SSHKeyType.RSA:
68
                    type_name = 'ssh-rsa'
69
                else:
70
                    type_name = 'Unknown key type'
10631.1.2 by Curtis Hovey
Strip sshkeys of line breaks when using listteammembers. Quiet lint's many
71
                params = make_sshkey_params(member, type_name, key)
8137.17.24 by Barry Warsaw
thread merge
72
                output.append(template % params)
4284.5.6 by Tom Haddon
Code refactoring following review comments
73
        # Ubuntite
4284.5.7 by Tom Haddon
Better variable names, trimming some whitespace and more elegantly handling argument parsing by setting default "format" variable
74
        ubuntite = "no"
4284.5.6 by Tom Haddon
Code refactoring following review comments
75
        if member.signedcocs:
76
            for i in member.signedcocs:
77
                if i.active:
78
                    ubuntite = "yes"
79
                    break
80
        params = dict(
4785.3.7 by Jeroen Vermeulen
Removed whitespace at ends of lines
81
            email=email,
4284.5.6 by Tom Haddon
Code refactoring following review comments
82
            name=member.name,
83
            teamname=teamname,
84
            id=member.id,
4813.1.1 by Tom Haddon
Handling non-ascii characters correctly in listteammembers.py
85
            displayname=member.displayname.encode("ascii", "replace"),
8137.17.24 by Barry Warsaw
thread merge
86
            ubuntite=ubuntite,
10631.1.2 by Curtis Hovey
Strip sshkeys of line breaks when using listteammembers. Quiet lint's many
87
            sshkey=sshkey,
4284.5.6 by Tom Haddon
Code refactoring following review comments
88
            )
4284.5.7 by Tom Haddon
Better variable names, trimming some whitespace and more elegantly handling argument parsing by setting default "format" variable
89
        output.append(template % params)
4284.5.6 by Tom Haddon
Code refactoring following review comments
90
    # If we're only looking at email, remove --none-- entries
91
    # as we're only interested in emails
92
    if display_option == 'email':
4284.5.7 by Tom Haddon
Better variable names, trimming some whitespace and more elegantly handling argument parsing by setting default "format" variable
93
        output = [line for line in output if line != '--none--']
8137.17.24 by Barry Warsaw
thread merge
94
    # If we're only looking at sshkeys, remove --none-- entries
95
    # as we're only interested in sshkeys
96
    if display_option == 'sshkeys':
97
        output = [line for line in output if line[-8:] != '--none--']
4284.5.6 by Tom Haddon
Code refactoring following review comments
98
    return sorted(output)