~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to lib/lp/services/mail/commands.py

  • Committer: Julian Edwards
  • Date: 2011-07-28 20:46:18 UTC
  • mfrom: (13553 devel)
  • mto: This revision was merged to the branch mainline in revision 13555.
  • Revision ID: julian.edwards@canonical.com-20110728204618-tivj2wx2oa9s32bx
merge trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright 2009-2011 Canonical Ltd.  This software is licensed under the
2
 
# GNU Affero General Public License version 3 (see the file LICENSE).
3
 
 
4
 
__metaclass__ = type
5
 
__all__ = [
6
 
    'EditEmailCommand',
7
 
    'EmailCommand',
8
 
    'EmailCommandCollection',
9
 
    'normalize_arguments',
10
 
    'NoSuchCommand',
11
 
    ]
12
 
 
13
 
from lazr.lifecycle.event import ObjectModifiedEvent
14
 
from lazr.lifecycle.interfaces import (
15
 
    IObjectCreatedEvent,
16
 
    IObjectModifiedEvent,
17
 
    )
18
 
from lazr.lifecycle.snapshot import Snapshot
19
 
from zope.interface import providedBy
20
 
 
21
 
from lp.services.mail.helpers import get_error_message
22
 
from lp.services.mail.interfaces import EmailProcessingError
23
 
 
24
 
 
25
 
def normalize_arguments(string_args):
26
 
    """Normalizes the string arguments.
27
 
 
28
 
    The string_args argument is simply the argument string whitespace
29
 
    splitted. Sometimes arguments may be quoted, though, so that they can
30
 
    contain space characters. For example "This is a long string".
31
 
 
32
 
    This function loops through all the argument and joins the quoted strings
33
 
    into a single arguments.
34
 
 
35
 
        >>> normalize_arguments(['"This', 'is', 'a', 'long', 'string."'])
36
 
        ['This is a long string.']
37
 
 
38
 
        >>> normalize_arguments(
39
 
        ...     ['"First', 'string"', '"Second', 'string"', 'foo'])
40
 
        ['First string', 'Second string', 'foo']
41
 
    """
42
 
    result = []
43
 
    quoted_string = False
44
 
    for item in string_args:
45
 
        if item.startswith('"'):
46
 
            quoted_string = True
47
 
            result.append(item[1:])
48
 
        elif quoted_string and item.endswith('"'):
49
 
            result[-1] += ' ' + item[:-1]
50
 
            quoted_string = False
51
 
        elif quoted_string:
52
 
            result[-1] += ' ' + item
53
 
        else:
54
 
            result.append(item)
55
 
 
56
 
    return result
57
 
 
58
 
 
59
 
class EmailCommand:
60
 
    """Represents a command.
61
 
 
62
 
    Both name the values in the args list are strings.
63
 
    """
64
 
    _numberOfArguments = None
65
 
 
66
 
    def __init__(self, name, string_args):
67
 
        self.name = name
68
 
        self.string_args = normalize_arguments(string_args)
69
 
 
70
 
    def _ensureNumberOfArguments(self):
71
 
        """Check that the number of arguments is correct.
72
 
 
73
 
        Raise an EmailProcessingError
74
 
        """
75
 
        if self._numberOfArguments is not None:
76
 
            num_arguments_got = len(self.string_args)
77
 
            if self._numberOfArguments != num_arguments_got:
78
 
                raise EmailProcessingError(
79
 
                    get_error_message(
80
 
                        'num-arguments-mismatch.txt',
81
 
                        command_name=self.name,
82
 
                        num_arguments_expected=self._numberOfArguments,
83
 
                        num_arguments_got=num_arguments_got))
84
 
 
85
 
    def convertArguments(self, context):
86
 
        """Converts the string argument to Python objects.
87
 
 
88
 
        Returns a dict with names as keys, and the Python objects as
89
 
        values.
90
 
        """
91
 
        raise NotImplementedError
92
 
 
93
 
    def __str__(self):
94
 
        """See IEmailCommand."""
95
 
        return ' '.join([self.name] + self.string_args)
96
 
 
97
 
 
98
 
class EditEmailCommand(EmailCommand):
99
 
    """Helper class for commands that edits the context.
100
 
 
101
 
    It makes sure that the correct events are notified.
102
 
    """
103
 
 
104
 
    def execute(self, context, current_event):
105
 
        """See IEmailCommand."""
106
 
        self._ensureNumberOfArguments()
107
 
        args = self.convertArguments(context)
108
 
 
109
 
        edited_fields = set()
110
 
        if IObjectModifiedEvent.providedBy(current_event):
111
 
            context_snapshot = current_event.object_before_modification
112
 
            edited_fields.update(current_event.edited_fields)
113
 
        else:
114
 
            context_snapshot = Snapshot(
115
 
                context, providing=providedBy(context))
116
 
 
117
 
        edited = False
118
 
        for attr_name, attr_value in args.items():
119
 
            if getattr(context, attr_name) != attr_value:
120
 
                self.setAttributeValue(context, attr_name, attr_value)
121
 
                edited = True
122
 
        if edited and not IObjectCreatedEvent.providedBy(current_event):
123
 
            edited_fields.update(args.keys())
124
 
            current_event = ObjectModifiedEvent(
125
 
                context, context_snapshot, list(edited_fields))
126
 
 
127
 
        return context, current_event
128
 
 
129
 
    def setAttributeValue(self, context, attr_name, attr_value):
130
 
        """See IEmailCommand."""
131
 
        setattr(context, attr_name, attr_value)
132
 
 
133
 
 
134
 
class NoSuchCommand(KeyError):
135
 
    """A command with the given name couldn't be found."""
136
 
 
137
 
 
138
 
class EmailCommandCollection:
139
 
    """A collection of email commands."""
140
 
 
141
 
    @classmethod
142
 
    def names(klass):
143
 
        """Returns all the command names."""
144
 
        return klass._commands.keys()
145
 
 
146
 
    @classmethod
147
 
    def get(klass, name, string_args):
148
 
        """Returns a command object with the given name and arguments.
149
 
 
150
 
        If a command with the given name can't be found, a NoSuchCommand
151
 
        error is raised.
152
 
        """
153
 
        command_class = klass._commands.get(name)
154
 
        if command_class is None:
155
 
            raise NoSuchCommand(name)
156
 
        return command_class(name, string_args)