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

Source Code for Module pytilities.delegation.delegatorfactory

  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  from .profile import Profile 
 22  from .delegator import Delegator 
 23   
24 -class DelegatorFactory(object):
25 """ 26 Allows easy creation of delegators with `Profile`s. 27 28 It also supports a form of inheritance. By specifying base 29 factories, a factory extends its bases' profiles (it takes the union of all 30 base profiles and its own). Using the inheritance you can add new profiles 31 and add attributes to existing profiles; you cannot, however, override 32 attributes. 33 34 Instance methods: 35 36 - `add_delegator_profile`: Add a `Profile` 37 - `Delegator`: Construct a `Delegator` from a stored profile 38 - `has_profile`: See if we have a particular profile 39 - `get_profile`: Get profile by name 40 41 Class invariants: 42 43 - Every profile has a unique name 44 """ 45
46 - def __init__(self, bases=()):
47 """ 48 Construct a `DelegatorFactory`, optionally with bases to inherit from. 49 50 Parameters: 51 52 `bases` :: (::DelegatorFactory...) 53 base factories for the created factory 54 """ 55 self.__bases = bases 56 self.__profiles = {}
57
58 - def add_delegator_profile(self, name, profile):
59 """ 60 Add a new delegator factory `Profile` to the factory. 61 62 Parameters: 63 64 `name` :: string 65 name of the profile 66 67 `profile` :: Profile 68 the profile to add 69 """ 70 assert name not in self.__profiles, \ 71 "Tried to add profile that's already added to this list." + \ 72 " A DelegatorFactory cannot contain the same profile twice" 73 74 self.__profiles[name] = profile
75
76 - def Delegator(self, profile_name = "default", target=None):
77 """ 78 Construct a delegator with a stored `Profile`. 79 80 Parameters: 81 82 `profile_name` :: string 83 name of the `Profile` to use to set up the delegator with 84 85 `target` = None 86 target of the newly created delegator 87 88 Returns newly created delegator :: Delegator 89 90 Raises: 91 92 - `ValueError` when no profile with name `profile_name` exists 93 """ 94 return Delegator(self.get_profile(profile_name), target)
95
96 - def has_profile(self, name):
97 """ 98 See if profile with given name exists. 99 100 Parameters: 101 102 `name` :: string 103 name of the profile to look for 104 105 Returns True if the factory can find a profile by that name, False 106 otherwise 107 """ 108 for base in self.__bases: 109 if base.has_profile(name): 110 return True 111 112 return name in self.__profiles
113
114 - def get_profile(self, name):
115 """ 116 Get profile by name. 117 118 Parameters: 119 120 `name` :: string 121 name of the profile 122 123 Returns profile by name, taking into account base factories' profiles 124 :: Profile 125 126 Raises: 127 - `ValueError` when profile isn't found 128 """ 129 profile = Profile() 130 found = False # whether we have found the profile, anywhere 131 132 if name in self.__profiles: 133 profile |= self.__profiles[name] 134 found = True 135 136 for base in self.__bases: 137 if base.has_profile(name): 138 profile |= base.get_profile(name) 139 found = True 140 141 if not found: 142 raise ValueError("Failed to find profile: %s" % name) 143 144 return profile
145