Package pytilities :: Package delegation :: Module profile
[hide private]
[frames] | no frames]

Source Code for Module pytilities.delegation.profile

  1  # Copyright (C) 2010 Tim Diels <limyreth@users.sourceforge.net> 
  2  #  
  3  # This file is part of pytilities. 
  4  #  
  5  # pytilities is free software: you can redistribute it and/or modify 
  6  # it under the terms of the GNU General Public License as published by 
  7  # the Free Software Foundation, either version 3 of the License, or 
  8  # (at your option) any later version. 
  9  #  
 10  # pytilities is distributed in the hope that it will be useful, 
 11  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 12  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 13  # GNU General Public License for more details. 
 14  #  
 15  # You should have received a copy of the GNU General Public License 
 16  # along with pytilities.  If not, see <http://www.gnu.org/licenses/>. 
 17  # 
 18   
 19  __docformat__ = 'reStructuredText' 
 20   
21 -class Profile(object):
22 23 """ 24 States what attributes to delegate, and to which target attributes. 25 26 Instance methods: 27 28 - `has_readable`: Check for read-access mapping of attribute 29 - `has_writable`: Check for read-access mapping of attribute 30 - `has_deletable`: Check for read-access mapping of attribute 31 - `get_readable`: Get read-access mapping for attribute 32 - `get_writable`: Get read-access mapping for attribute 33 - `get_deletable`: Get read-access mapping for attribute 34 - `remove_mappings`: Remove attribute mappings 35 - `add_mappings`: Add attribute mappings 36 37 Operators: 38 39 self |= b::Profile 40 union of this profile and the other 41 """ 42
43 - def __init__(self):
44 self.__readables = {} 45 self.__writables = {} 46 self.__deletables = {}
47
48 - def has_readable(self, attribute):
49 """ 50 Check for read-access mapping of attribute 51 52 Returns True if the mapping exists 53 """ 54 return attribute in self.__readables
55
56 - def has_writable(self, attribute):
57 """ 58 Check for write-access mapping of attribute 59 60 Returns True if the mapping exists 61 """ 62 return attribute in self.__writables
63
64 - def has_deletable(self, attribute):
65 """ 66 Check for delete-access mapping of attribute 67 68 Returns True if the mapping exists 69 """ 70 return attribute in self.__deletables
71
72 - def get_readable(self, attribute):
73 """ 74 Get the mapped value of a readable attribute 75 76 Returns ::string 77 """ 78 return self.__readables[attribute]
79
80 - def get_writable(self, attribute):
81 """ 82 Get the mapped value of a writable attribute 83 84 Returns ::string 85 """ 86 return self.__writables[attribute]
87
88 - def get_deletable(self, attribute):
89 """ 90 Get the mapped value of a deletable attribute 91 92 Returns ::string 93 """ 94 return self.__deletables[attribute]
95 96 # the |= operator
97 - def __ior__(self, other):
98 """ 99 union of this profile and the other 100 101 Parameters: 102 103 - `other` :: Profile 104 the other profile 105 """ 106 107 # check for conflicts: intersect keys, then see whether intersections 108 # map to the same values 109 # If you get an assertion failure about this, then you should check 110 # your bases to see if any of them map an attribute differently 111 # e.g. with profiles a, b and attribute x: a.x should map to the same 112 # value as b.x 113 assert reduce(lambda x, y: x and y, 114 (reduce(lambda x,y: x and y, 115 (a[key] == b[key] 116 for key 117 in set(a.iterkeys()) & set(b.iterkeys())), 118 True) 119 for a, b 120 in zip((self.__readables, self.__writables, 121 self.__deletables), 122 (other.__readables, other.__writables, 123 other.__deletables))), 124 True), \ 125 "Profiles have conflicting mappings, can't take union" 126 127 # no conflicts, go ahead and update 128 self.__readables.update(other.__readables) 129 self.__writables.update(other.__writables) 130 self.__deletables.update(other.__deletables) 131 return self
132
133 - def remove_mappings(self, modifiers, *names):
134 """ 135 Remove attribute mappings 136 137 Parameters: 138 139 `modifiers` :: string 140 the types of delegation access to the target to remove. Valid 141 modifiers are combinations of: 142 143 - `r`: read 144 - `w`: write 145 - `d`: delete 146 147 `names` :: (source_name::string...) 148 names of source_name parts of, the mappings to remove 149 """ 150 151 for name in names: 152 assert isinstance(name, basestring), \ 153 "Invalid arguments: names should be list of strings: %s" \ 154 % (names) 155 156 for name in names: 157 if "r" in modifiers: 158 self.__readables.pop(name) 159 160 if "w" in modifiers: 161 self.__writables.pop(name) 162 163 if "d" in modifiers: 164 self.__deletables.pop(name)
165 166
167 - def add_mappings(self, modifiers, *names, **mapped_names):
168 """ 169 Map source to target attribute names, for delegation. 170 171 Parameters: 172 173 `modifiers` :: string 174 the types of delegation access to the target. Valid modifiers 175 are combinations of: 176 177 - `r`: read 178 - `w`: write 179 - `d`: delete 180 181 `names` :: (source_name::string...) 182 names of the attribute names to delegate. The target attribute 183 name is the same as `source_name`. This is the same as adding a 184 {source_name : source_name} mapping, using the mapped_names 185 argument. 186 187 `mapped_names` :: {source_name::string : target_name::string ...} 188 names of source and target attributes to map and delegate. 189 190 `target_name`s will be mangled if they have a __ prefix. 191 Mangling will occur every time it is delegated to, so you can 192 change the target object any time. 193 """ 194 for name in names: 195 assert isinstance(name, basestring), ( 196 "Invalid arguments: names should be list of strings. Got: %s" % 197 names) 198 199 for key, name in mapped_names.iteritems(): 200 assert isinstance(key, basestring) and \ 201 isinstance(name, basestring), \ 202 "Invalid arguments: mapped_names should be dict of " + \ 203 "string:string. Got: %s" % mapped_names 204 205 if "r" in modifiers: 206 self.__readables.update(zip(names, names), **mapped_names) 207 208 if "w" in modifiers: 209 self.__writables.update(zip(names, names), **mapped_names) 210 211 if "d" in modifiers: 212 self.__deletables.update(zip(names, names), **mapped_names)
213