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