/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/identitymap.py

  • Committer: Adeodato Simó
  • Date: 2007-07-18 15:51:52 UTC
  • mto: (2639.1.1 jam-integration)
  • mto: This revision was merged to the branch mainline in revision 2640.
  • Revision ID: dato@net.com.org.es-20070718155152-pv6rwq41eckqyxem
New EmailMessage class, façade around email.Message and MIMEMultipart.

* bzrlib/email_message.py,
  bzrlib/tests/test_email_message.py:
  New files.

* bzrlib/tests/__init__.py:
  (test_suite): add bzrlib.tests.test_email_message.

* bzrlib/merge_directive.py:
  (MergeDirective.to_email): Use EmailMessage instead of email.Message.

* bzrlib/tests/test_merge_directive.py,
  bzrlib/tests/blackbox/test_merge_directive.py:
  (__main__): adjust EMAIL1 and EMAIL2 strings to how EmailMessage
  formats itself.

* bzrlib/smtp_connection.py:
  (SMTPConnection.get_message_addresses): do not use methods present in
  email.Message but not in EmailMessage (get_all). Use get() instead of
  __getitem__ to make explicit that None is returned if the header does
  not exist.

* bzrlib/tests/test_smtp_connection.py:
  (TestSMTPConnection.test_get_message_addresses, 
   TestSMTPConnection.test_destination_address_required): test the
   functions against EmailMessage in addition to email.Message.

* NEWS:
  Mention EmailMessage in INTERNALS.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2005 Canonical Ltd
 
2
#   Authors: Robert Collins <robert.collins@canonical.com>
 
3
#
 
4
# This program is free software; you can redistribute it and/or modify
 
5
# it under the terms of the GNU General Public License as published by
 
6
# the Free Software Foundation; either version 2 of the License, or
 
7
# (at your option) any later version.
 
8
#
 
9
# This program is distributed in the hope that it will be useful,
 
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
# GNU General Public License for more details.
 
13
#
 
14
# You should have received a copy of the GNU General Public License
 
15
# along with this program; if not, write to the Free Software
 
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
17
 
 
18
"""This module provides an IdentityMap."""
 
19
 
 
20
 
 
21
from bzrlib import (
 
22
    errors,
 
23
    osutils,
 
24
    )
 
25
 
 
26
 
 
27
class IdentityMap(object):
 
28
    """An in memory map from object id to instance.
 
29
    
 
30
    An IdentityMap maps from keys to single instances of objects in memory.
 
31
    We have explicit calls on the map for the root of each inheritance tree
 
32
    that is store in the map. Look for find_CLASS and add_CLASS methods.
 
33
    """
 
34
 
 
35
    def add_weave(self, id, weave):
 
36
        """Add weave to the map with a given id."""
 
37
        if self._weave_key(id) in self._map:
 
38
            raise errors.BzrError('weave %s already in the identity map' % id)
 
39
        self._map[self._weave_key(id)] = weave
 
40
        self._reverse_map[weave] = self._weave_key(id)
 
41
 
 
42
    def find_weave(self, id):
 
43
        """Return the weave for 'id', or None if it is not present."""
 
44
        return self._map.get(self._weave_key(id), None)
 
45
 
 
46
    def __init__(self):
 
47
        super(IdentityMap, self).__init__()
 
48
        self._map = {}
 
49
        self._reverse_map = {}
 
50
 
 
51
    def remove_object(self, an_object):
 
52
        """Remove object from map."""
 
53
        if isinstance(an_object, list):
 
54
            raise KeyError('%r not in identity map' % an_object)
 
55
        else:
 
56
            self._map.pop(self._reverse_map[an_object])
 
57
            self._reverse_map.pop(an_object)
 
58
 
 
59
    def _weave_key(self, id):
 
60
        """Return the key for a weaves id."""
 
61
        return "weave-" + id
 
62
 
 
63
        
 
64
class NullIdentityMap(object):
 
65
    """A pretend in memory map from object id to instance.
 
66
    
 
67
    A NullIdentityMap is an Identity map that does not store anything in it.
 
68
    """
 
69
 
 
70
    def add_weave(self, id, weave):
 
71
        """See IdentityMap.add_weave."""
 
72
 
 
73
    def find_weave(self, id):
 
74
        """See IdentityMap.find_weave."""
 
75
        return None