app/soc/logic/no_overwrite_sorted_dict.py
changeset 275 78fd8c2ed80a
equal deleted inserted replaced
274:56e1c1721299 275:78fd8c2ed80a
       
     1 #!/usr/bin/python2.5
       
     2 #
       
     3 # Copyright 2008 the Melange authors.
       
     4 #
       
     5 # Licensed under the Apache License, Version 2.0 (the "License");
       
     6 # you may not use this file except in compliance with the License.
       
     7 # You may obtain a copy of the License at
       
     8 #
       
     9 #   http://www.apache.org/licenses/LICENSE-2.0
       
    10 #
       
    11 # Unless required by applicable law or agreed to in writing, software
       
    12 # distributed under the License is distributed on an "AS IS" BASIS,
       
    13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
       
    14 # See the License for the specific language governing permissions and
       
    15 # limitations under the License.
       
    16 
       
    17 """Like Django SortedDict, but no repeated assignments to the same key.
       
    18 """
       
    19 
       
    20 __authors__ = [
       
    21   '"Todd Larsen" <tlarsen@google.com>',
       
    22   ]
       
    23 
       
    24 
       
    25 from django.utils import datastructures
       
    26 
       
    27 
       
    28 class NoOverwriteSortedDict(datastructures.SortedDict):
       
    29   """SortedDict where each key can be given a value only once.
       
    30   
       
    31   The purpose of this data structure is to be able to detect when
       
    32   an attempt is made to overwrite the value of an existing key
       
    33   in the SortedDict.  This is to catch, for example, cases such as
       
    34   a registry where two different callers attempt to register the
       
    35   same view, handler, etc.
       
    36  
       
    37   It is still possible to pop or del a key out of the dict and then
       
    38   add it back to the dict.
       
    39   """
       
    40   
       
    41   KEY_ALREADY_PRESENT_ERROR_FMT = \
       
    42     '%s already present, value cannot be overwritten'
       
    43   
       
    44   def __init__(self, data=None):
       
    45     if data is None:
       
    46       data = {}
       
    47 
       
    48     # call SortedDict's parent __init__()
       
    49     # (bypassing the __init__() of SortedDict itself, since it will not
       
    50     # enforce our no-overwrite requirement)
       
    51     super(datastructures.SortedDict, self).__init__(data)
       
    52     
       
    53     if isinstance(data, dict):
       
    54       self.keyOrder = data.keys()
       
    55     else:
       
    56       self.keyOrder = []
       
    57 
       
    58       for key, value in data:
       
    59         if key in self.keyOrder:
       
    60           # key has already been given a value, and that value is not
       
    61           # permitted to be overwritten, so raise an error
       
    62           raise KeyError(self.KEY_ALREADY_PRESENT_ERROR_FMT % key)
       
    63 
       
    64         self.keyOrder.append(key)
       
    65 
       
    66   def __setitem__(self, key, value):
       
    67     if key in self.keyOrder:
       
    68       # key has already been given a value, and that value is not permitted
       
    69       # to be overwritten, so raise an error
       
    70       raise KeyError(self.KEY_ALREADY_PRESENT_ERROR_FMT % key)
       
    71 
       
    72     super(NoOverwriteSortedDict, self).__setitem__(key, value)